Skip to content

Commit

Permalink
refactor : 세션을 null 로도 반환할 수도 있도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kpeel5839 committed Sep 11, 2023
1 parent a2bb988 commit 5284db5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public void doGet(
HttpResponse httpResponse
) {
HttpSession httpSession = httpRequest.getHttpSession();
User user = (User) httpSession.get("user");
User user = null;

if (!Objects.isNull(httpSession)) {
user = (User) httpSession.get("user");
}

if (!Objects.isNull(user)) {
httpResponse.setStatus(HttpStatus.FOUND);
Expand Down
35 changes: 33 additions & 2 deletions tomcat/src/main/java/org/apache/coyote/http11/Cookie.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
package org.apache.coyote.http11;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Cookie {

private final Map<String, String> values;

public Cookie() {
values = new HashMap<>();
public Cookie(Map<String, String> values) {
this.values = values;
}

public static Cookie from(String cookies) {
if (cookies == null) {
return new Cookie(Collections.emptyMap());
}

Map<String, String> parseResultOfCookies = new HashMap<>();

for (String cookie : cookies.split(";")) {
putParseResultOfComponent(parseResultOfCookies, cookie);
}

return new Cookie(parseResultOfCookies);
}

private static void putParseResultOfComponent(
Map<String, String> parseResultOfRequestBody,
String component
) {
if (component.length() < 2) {
return;
}

String[] keyAndValue = component.split("=");
parseResultOfRequestBody.put(keyAndValue[0].trim(), keyAndValue[1].trim());
}

public String get(String key) {
return values.getOrDefault(key, "");
}

public void put(String key, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public HttpSession getHttpSession() {

public HttpSession getHttpSession(boolean create) {
String jsessionId = httpRequestHeader.getCookies()
.getOrDefault("JSESSIONID", "");
.get("JSESSIONID");

HttpSession session = null;

Expand All @@ -49,7 +49,7 @@ public HttpSession getHttpSession(boolean create) {
// ignored
}

if (Objects.isNull(session)) {
if (create && Objects.isNull(session)) {
String uuid = UUID.randomUUID()
.toString();
session = new HttpSession(uuid);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.apache.coyote.http11;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -10,20 +9,20 @@ public class HttpRequestHeader {
private final String path;
private final Map<String, String> extraContents;
private final Map<String, String> queryStrings;
private final Map<String, String> cookies;
private final Cookie cookie;

private HttpRequestHeader(
HttpMethod method,
String path,
Map<String, String> extraContents,
Map<String, String> queryStrings,
Map<String, String> cookies
Cookie cookie
) {
this.method = method;
this.path = path;
this.extraContents = extraContents;
this.queryStrings = queryStrings;
this.cookies = cookies;
this.cookie = cookie;
}

public static HttpRequestHeader of(String method, String path, String[] extraContents) {
Expand Down Expand Up @@ -88,26 +87,16 @@ private static Map<String, String> parseQueryStrings(String path) {
return parseResultOfQueryStrings;
}

private static Map<String, String> parseCookies(String cookies) {
if (cookies == null) {
return Collections.emptyMap();
}

Map<String, String> parseResultOfCookies = new HashMap<>();

for (String cookie : cookies.split(";")) {
putParseResultOfComponent(parseResultOfCookies, cookie, "=");
}

return parseResultOfCookies;
private static Cookie parseCookies(String cookies) {
return Cookie.from(cookies);
}

public String get(String key) {
return extraContents.getOrDefault(key, "");
}

public Map<String, String> getCookies() {
return cookies;
public Cookie getCookies() {
return cookie;
}

public String getQueryString(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.apache.coyote.http11.HttpHeader.LOCATION;
import static org.apache.coyote.http11.HttpHeader.SET_COOKIE;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringJoiner;
Expand All @@ -12,13 +13,16 @@ public class HttpResponse {

private static final String PROTOCOL = "HTTP/1.1";
private final Map<String, String> attribute = new LinkedHashMap<>();
private final Cookie cookie;
private HttpStatus status;
private String responseBody;
private Cookie cookie;

public HttpResponse() {
this.cookie = new Cookie(new HashMap<>());
}

public void setStatus(HttpStatus status) {
this.status = status;
cookie = new Cookie();
}

public void setResponseBody(String responseBody) {
Expand Down

0 comments on commit 5284db5

Please sign in to comment.