-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[레베카] 3단계 - HTTP 웹 서버 리팩토링 미션 제출합니다. #179
base: chws
Are you sure you want to change the base?
Changes from all commits
207f21b
640d67f
5705e93
7b20f65
125f7d9
79590c3
0d9abe6
c074b42
c6a6548
27c5c08
df96a20
22759b5
6866721
2a6a8bb
c127113
76acaaa
87c5856
6b138b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package http.controller; | ||
|
||
import http.request.HttpRequest; | ||
import http.session.HttpSession; | ||
import http.session.HttpSessionStorage; | ||
|
||
public abstract class AuthController extends Controller { | ||
protected HttpSession retrieveHttpSession(HttpRequest httpRequest) { | ||
if (httpRequest.hasCookie("SESSIONID")) { | ||
HttpSession httpSession = HttpSessionStorage.getSession(httpRequest.getSessionId()); | ||
if (httpSession == null) { | ||
httpSession = HttpSessionStorage.create(); | ||
} | ||
return httpSession; | ||
} else { | ||
return HttpSessionStorage.create(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package http.controller; | ||
|
||
import com.github.jknack.handlebars.Handlebars; | ||
import com.github.jknack.handlebars.Template; | ||
import com.github.jknack.handlebars.io.ClassPathTemplateLoader; | ||
import com.github.jknack.handlebars.io.TemplateLoader; | ||
import db.DataBase; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
import utils.HttpResponseHeaderParser; | ||
|
||
import java.io.IOException; | ||
|
||
public class UserListController extends Controller { | ||
@Override | ||
public HttpResponse get(HttpRequest httpRequest) { | ||
try { | ||
TemplateLoader loader = new ClassPathTemplateLoader(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. template 부분과 비즈니스로직을 분리해보면 어떨까요? |
||
loader.setPrefix("/templates"); | ||
loader.setSuffix(".html"); | ||
Handlebars handlebars = new Handlebars(loader); | ||
|
||
Template template = handlebars.compile("user/list"); | ||
|
||
byte[] userListPage = template.apply(DataBase.findAll()).getBytes(); | ||
String header = HttpResponseHeaderParser.ok("text/html", userListPage.length); | ||
return new HttpResponse(header, userListPage); | ||
} catch (IOException e) { | ||
return new HttpResponse(HttpResponseHeaderParser.internalServerError(), null); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package http.controller; | ||
|
||
import db.DataBase; | ||
import http.request.Cookie; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
import http.session.HttpSession; | ||
import model.User; | ||
import service.UserService; | ||
import utils.HttpResponseHeaderParser; | ||
|
||
public class UserLoginController extends AuthController { | ||
|
||
@Override | ||
public HttpResponse post(HttpRequest httpRequest) { | ||
UserService userService = UserService.getInstance(); | ||
boolean auth = userService.authenticateUser(httpRequest); | ||
String header; | ||
HttpSession httpSession; | ||
Cookie cookie = new Cookie(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cookie 객체 생성을 내부로 감추면 어떨까요? |
||
if (auth) { | ||
User user = DataBase.findUserById(httpRequest.getBodyValue("userId")); | ||
httpSession = retrieveHttpSession(httpRequest); | ||
httpSession.setAttribute("email", user.getEmail()); | ||
cookie.setCookie("logined", "true"); | ||
cookie.setCookie("SESSIONID", httpSession.getId()); | ||
header = HttpResponseHeaderParser.found("/", cookie); | ||
} else { | ||
cookie.setCookie("logined", "false"); | ||
header = HttpResponseHeaderParser.found("/user/login_failed.html", cookie); | ||
} | ||
return new HttpResponse(header); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package http.controller; | ||
|
||
import http.request.Cookie; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
import http.session.HttpSessionStorage; | ||
import utils.HttpResponseHeaderParser; | ||
|
||
public class UserLogoutController extends AuthController { | ||
@Override | ||
public HttpResponse get(HttpRequest httpRequest) { | ||
String header; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 선언과 대입을 분리한 이유가 있나요? |
||
Cookie cookie = new Cookie(); | ||
cookie.setCookie("logined", "false"); | ||
HttpSessionStorage.deleteSession(httpRequest.getSessionId()); | ||
header = HttpResponseHeaderParser.found("/", cookie); | ||
return new HttpResponse(header); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package http.request; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class Cookie { | ||
private Map<String, String> cookies = new HashMap<>(); | ||
|
||
public Cookie() { | ||
} | ||
|
||
public Cookie(String rawCookie) { | ||
if (rawCookie.length() != 0) { | ||
String[] tokens = rawCookie.trim().split("; "); | ||
for (String token : tokens) { | ||
String[] tmp = token | ||
.replace(";", "") | ||
.split("="); | ||
this.cookies.put(tmp[0], tmp[1]); | ||
} | ||
} | ||
} | ||
|
||
public boolean hasCookie(String key) { | ||
return this.cookies.containsKey(key); | ||
} | ||
|
||
public String getCookie(String key) { | ||
return this.cookies.get(key); | ||
} | ||
|
||
public Set<Map.Entry<String, String>> getAllCookie() { | ||
return this.cookies.entrySet(); | ||
} | ||
|
||
public void setCookie(String key, String value) { | ||
this.cookies.put(key, value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package http; | ||
package http.request; | ||
|
||
public enum HeaderParam { | ||
HOST("host"), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package http; | ||
package http.request; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package http; | ||
package http.response; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package http.session; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class HttpSession { | ||
|
||
private final String sessionId; | ||
private final Map<String, Object> attributes; | ||
|
||
public HttpSession() { | ||
this.sessionId = UUID.randomUUID().toString(); | ||
this.attributes = new HashMap<>(); | ||
} | ||
|
||
public String getId() { | ||
return this.sessionId; | ||
} | ||
|
||
public void setAttribute(String name, Object value) { | ||
this.attributes.put(name, value); | ||
} | ||
|
||
public Object getAttribute(String name) { | ||
return this.attributes.get(name); | ||
} | ||
|
||
public void removeAttribute(String name) { | ||
this.attributes.remove(name); | ||
} | ||
|
||
public void invalidate() { | ||
this.attributes.clear(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getSession을 했을 때 존재하지 않으면 내부에서 만들어주면 어떨까요?
HttpSessionStorage를 controller까지 오픈할 필요는 없을 것 같아요.