-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 컨트롤러 매핑 RequestMapping으로 분리, TargetPath 객체 분리
- Loading branch information
Showing
10 changed files
with
226 additions
and
81 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
tomcat/src/main/java/nextstep/jwp/handler/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nextstep.jwp.handler; | ||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.TargetPath; | ||
import org.apache.coyote.http11.controller.Controller; | ||
import org.apache.coyote.http11.header.ContentType; | ||
|
||
public class HelloController implements Controller { | ||
|
||
private static final TargetPath SUPPORTED_PATH = new TargetPath("/"); | ||
|
||
@Override | ||
public boolean supports(HttpRequest httpRequest) { | ||
return httpRequest.getTarget().getPath().equals(SUPPORTED_PATH) && httpRequest.getMethod().isGet(); | ||
} | ||
|
||
@Override | ||
public void handle(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
httpResponse.setContentType(new ContentType("text/html")); | ||
httpResponse.setBody("Hello world!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
tomcat/src/main/java/org/apache/coyote/http11/TargetPath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.util.Objects; | ||
|
||
public class TargetPath { | ||
|
||
private static final String DEFAULT_PAGE = "index"; | ||
private static final String DEFAULT_EXTENSION = ".html"; | ||
private static final String STATIC_RESOURCES_PATH = "static"; | ||
|
||
private final String path; | ||
|
||
public TargetPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
private boolean endsWithRoot(String path) { | ||
return path.endsWith("/"); | ||
} | ||
|
||
private boolean hasExtensionIn(String path) { | ||
int lastSlash = path.lastIndexOf('/'); | ||
String lastSubPath = path.substring(lastSlash + 1); | ||
if (lastSubPath.contains(".")) { | ||
return lastSubPath.lastIndexOf('.') > 0; | ||
} | ||
return false; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public TargetPath autoComplete() { | ||
if (endsWithRoot(path)) { | ||
return new TargetPath(path + DEFAULT_PAGE + DEFAULT_EXTENSION); | ||
} | ||
if (!hasExtensionIn(path)) { | ||
return new TargetPath(path + DEFAULT_EXTENSION); | ||
} | ||
return new TargetPath(path); | ||
} | ||
|
||
public String getExtension() { | ||
int lastSlash = autoComplete().path.lastIndexOf('/'); | ||
String lastSubPath = autoComplete().path.substring(lastSlash + 1); | ||
int extensionStart = lastSubPath.lastIndexOf('.'); | ||
return lastSubPath.substring(extensionStart + 1); | ||
} | ||
|
||
public File asStaticFile() { | ||
URL resource = getClass().getClassLoader().getResource(STATIC_RESOURCES_PATH + autoComplete().path); | ||
if (resource == null) { | ||
return new File("/"); | ||
} | ||
return new File(resource.getPath()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
TargetPath that = (TargetPath) o; | ||
|
||
return Objects.equals(path, that.path); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return path != null ? path.hashCode() : 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 19 additions & 53 deletions
72
tomcat/src/main/java/org/apache/coyote/http11/controller/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,52 @@ | ||
package org.apache.coyote.http11.controller; | ||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.TargetPath; | ||
import org.apache.coyote.http11.header.ContentType; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.header.ContentType; | ||
|
||
public class FileController implements Controller { | ||
|
||
private static final ClassLoader CLASS_LOADER = FileController.class.getClassLoader(); | ||
|
||
private static final String STATIC_RESOURCES_PATH = "static"; | ||
private static final String DEFAULT_INDEX = "index"; | ||
private static final String DEFAULT_EXTENSION = ".html"; | ||
|
||
private static final Map<String, ContentType> CONTENT_TYPES_BY_EXTENSION = Map.of( | ||
"html", new ContentType("text/html"), | ||
"css", new ContentType("text/css") | ||
); | ||
|
||
@Override | ||
public boolean supports(final HttpRequest httpRequest) { | ||
TargetPath targetPath = httpRequest.getTarget().getPath().autoComplete(); | ||
return targetPath.asStaticFile().exists(); | ||
} | ||
|
||
@Override | ||
public void handle(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
try { | ||
String targetPath = fillExtensionIfDoesNotExist(httpRequest.getTarget().getPath()); | ||
TargetPath targetPath = httpRequest.getTarget().getPath().autoComplete(); | ||
File staticFile = targetPath.asStaticFile(); | ||
validatePresenceOf(staticFile); | ||
|
||
File resource = getResourceFileFrom(targetPath); | ||
String body = Files.readString(resource.toPath()); | ||
httpResponse.setContentType(getContentTypeFrom(targetPath)); | ||
httpResponse.setBody(body); | ||
httpResponse.setContentType(getContentTypeBy(targetPath.getExtension())); | ||
httpResponse.setBody(Files.readString(staticFile.toPath())); | ||
} catch (IOException exception) { | ||
throw new IllegalArgumentException("파일 읽기에 실패했습니다"); | ||
} | ||
} | ||
|
||
private String fillExtensionIfDoesNotExist(String target) { | ||
if (Objects.isNull(getExtensionOf(target))) { | ||
return target + DEFAULT_EXTENSION; | ||
} | ||
return target; | ||
} | ||
|
||
private ContentType getContentTypeFrom(final String target) { | ||
String extension = getExtensionOf(target); | ||
private ContentType getContentTypeBy(final String extension) { | ||
if (CONTENT_TYPES_BY_EXTENSION.containsKey(extension)) { | ||
return CONTENT_TYPES_BY_EXTENSION.get(extension); | ||
} | ||
return new ContentType("text/html"); | ||
} | ||
|
||
private File getResourceFileFrom(final String target) { | ||
String relativePath = getRelativePathFrom(target); | ||
URL resource = CLASS_LOADER.getResource(relativePath); | ||
validatePresenceOf(resource); | ||
|
||
String absolutePath = resource.getPath(); | ||
return new File(absolutePath); | ||
} | ||
|
||
private String getRelativePathFrom(final String target) { | ||
if (target.endsWith("/")) { | ||
return STATIC_RESOURCES_PATH + target + DEFAULT_INDEX; | ||
} | ||
return STATIC_RESOURCES_PATH + target; | ||
} | ||
|
||
private void validatePresenceOf(final URL resource) { | ||
if (Objects.isNull(resource)) { | ||
private void validatePresenceOf(final File file) { | ||
if (!file.exists()) { | ||
throw new IllegalArgumentException("리소스를 찾지 못했습니다."); | ||
} | ||
} | ||
|
||
private String getExtensionOf(String path) { | ||
int lastSlash = path.lastIndexOf('/'); | ||
String lastSubPath = path.substring(lastSlash + 1); | ||
if (lastSubPath.contains(".")) { | ||
int extensionStart = lastSubPath.lastIndexOf('.'); | ||
return lastSubPath.substring(extensionStart + 1); | ||
} | ||
return null; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tomcat/src/main/java/org/apache/coyote/http11/controller/RequestMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.apache.coyote.http11.controller; | ||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
|
||
import java.util.List; | ||
|
||
public class RequestMapping { | ||
|
||
private final List<Controller> controllers; | ||
|
||
public RequestMapping(List<Controller> controllers) { | ||
this.controllers = controllers; | ||
} | ||
|
||
public void handle(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
getControllerFor(httpRequest).handle(httpRequest, httpResponse); | ||
} | ||
|
||
private Controller getControllerFor(final HttpRequest httpRequest) { | ||
return controllers.stream() | ||
.filter(it -> it.supports(httpRequest)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("적절한 컨트롤러를 찾지 못했습니다")); | ||
} | ||
} |
Oops, something went wrong.