-
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.
[톰캣 구현하기 - 3, 4단계] 푸우(백승준) 미션 제출합니다. (#446)
- Loading branch information
Showing
25 changed files
with
710 additions
and
277 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
tomcat/src/main/java/org/apache/coyote/http11/HttpResponseConverter.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,52 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.StringJoiner; | ||
import java.util.stream.Collectors; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.apache.coyote.http11.response.HttpResponseHeaders; | ||
import org.apache.coyote.http11.response.HttpStatusCode; | ||
import org.apache.coyote.http11.response.ResponseBody; | ||
import org.apache.coyote.http11.response.StatusLine; | ||
|
||
public class HttpResponseConverter { | ||
|
||
private static final String HTTP_VERSION = "HTTP/1.1"; | ||
|
||
private HttpResponseConverter() { | ||
} | ||
|
||
public static String convert(HttpResponse httpResponse) { | ||
StringJoiner responseJoiner = new StringJoiner(System.lineSeparator()); | ||
responseJoiner.add(extractStatusLine(httpResponse.getStatusLine())); | ||
responseJoiner.add(extractHeaders(httpResponse.getHeaders())); | ||
responseJoiner.add(System.lineSeparator()); | ||
addBody(responseJoiner, httpResponse.getBody()); | ||
return responseJoiner.toString(); | ||
} | ||
|
||
private static String extractStatusLine(StatusLine statusLine) { | ||
HttpStatusCode httpStatusCode = statusLine.getHttpStatusCode(); | ||
return HTTP_VERSION + " " + httpStatusCode.getStatusCode() + " " + httpStatusCode.name(); | ||
} | ||
|
||
private static String extractHeaders(HttpResponseHeaders headers) { | ||
Map<String, String> headerValues = headers.getHeaders(); | ||
return headerValues.keySet() | ||
.stream() | ||
.map(headerName -> makeHeader(headerName, headerValues.get(headerName))) | ||
.collect(Collectors.joining(System.lineSeparator())); | ||
} | ||
|
||
private static String makeHeader(String headerName, String value) { | ||
return headerName + ": " + value; | ||
} | ||
|
||
private static void addBody(StringJoiner responseJoiner, ResponseBody body) { | ||
Optional<String> responseBody = body.getValue(); | ||
if (responseBody.isPresent()) { | ||
responseJoiner.add(responseBody.get()); | ||
} | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
tomcat/src/main/java/org/apache/coyote/http11/controller/AbstractController.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,48 @@ | ||
package org.apache.coyote.http11.controller; | ||
|
||
import org.apache.coyote.http11.request.HttpMethod; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.apache.coyote.http11.response.ResponseEntity; | ||
|
||
public abstract class AbstractController implements Controller { | ||
|
||
|
||
@Override | ||
public void service(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
if (httpRequest.isRequestOf(HttpMethod.GET)) { | ||
doGet(httpRequest, httpResponse); | ||
return; | ||
} | ||
if (httpRequest.isRequestOf(HttpMethod.POST)) { | ||
doPost(httpRequest, httpResponse); | ||
return; | ||
} | ||
if (httpRequest.isRequestOf(HttpMethod.PUT)) { | ||
doPut(httpRequest, httpResponse); | ||
return; | ||
} | ||
if (httpRequest.isRequestOf(HttpMethod.DELETE)) { | ||
doDelete(httpRequest, httpResponse); | ||
return; | ||
} | ||
throw new UnsupportedOperationException( | ||
"해당 HttpMethod 는 아직 지원하지 않습니다." + httpRequest.getRequestLine().getMethod()); | ||
} | ||
|
||
protected void doGet(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
httpResponse.responseFrom(ResponseEntity.notAllowed()); | ||
} | ||
|
||
protected void doPost(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
httpResponse.responseFrom(ResponseEntity.notAllowed()); | ||
} | ||
|
||
protected void doPut(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
httpResponse.responseFrom(ResponseEntity.notAllowed()); | ||
} | ||
|
||
protected void doDelete(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
httpResponse.responseFrom(ResponseEntity.notAllowed()); | ||
} | ||
} |
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
Oops, something went wrong.