forked from softeerbootcamp-7th/be-was
-
Notifications
You must be signed in to change notification settings - Fork 0
[Web-view] 동적 뷰 개발 #52
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1800b51
Merge pull request #50 from codingbaraGo/feat/authentication/#22
codingbaraGo 280dc64
feat(web-view): DynamicViewResponse 개발
codingbaraGo 44f875b
enhance(auth): AuthenticationInfo에 Attribute 추가
codingbaraGo f8cddbe
feat(web-view): View template engine 개발
codingbaraGo 04c24e6
feat(web-view): 동적 뷰 렌더러 개발
codingbaraGo 1388cf6
feat(app-handler): 기본 페이지 핸들러 개발
codingbaraGo b5b90dc
feat(view): 기본 페이지 수정
codingbaraGo 76c85c6
chore: 코드 정리
codingbaraGo 428f758
chore(test): 기본 페이지 이동으로 인한 변경사항 반영
codingbaraGo e4ec9a7
fix: 코드리뷰 반영
codingbaraGo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,20 @@ | ||
| package app.handler; | ||
|
|
||
| import http.HttpMethod; | ||
| import http.HttpStatus; | ||
| import http.request.HttpRequest; | ||
| import web.handler.SingleArgHandler; | ||
| import web.response.DynamicViewResponse; | ||
| import web.response.HandlerResponse; | ||
|
|
||
| public class HomeHandler extends SingleArgHandler<HttpRequest> { | ||
|
|
||
| public HomeHandler() { | ||
| super(HttpMethod.GET, "/"); | ||
| } | ||
|
|
||
| @Override | ||
| public HandlerResponse handle(HttpRequest request) { | ||
| return DynamicViewResponse.of(HttpStatus.OK, "/index.html"); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4,5 +4,7 @@ | |
|
|
||
| public interface AuthenticationInfo { | ||
| Optional<Long> getUserId(); | ||
| Optional<Object> getAttribute(String key); | ||
|
Comment on lines
4
to
+7
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.
개선 방안: |
||
| void addAttribute(String key, Object value); | ||
| UserRole getRole(); | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
src/main/java/web/filter/authentication/UnanimousAuthentication.java
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
src/main/java/web/filter/authentication/UserAuthentication.java
This file contains hidden or 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 hidden or 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,35 @@ | ||
| package web.renderer; | ||
|
|
||
| import http.response.HttpResponse; | ||
| import web.response.DynamicViewResponse; | ||
| import web.response.HandlerResponse; | ||
| import web.renderer.view.TemplateEngine; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| public class DynamicViewRenderer implements HttpResponseRenderer { | ||
| private final TemplateEngine templateEngine; | ||
|
|
||
| public DynamicViewRenderer(TemplateEngine templateEngine) { | ||
| this.templateEngine = templateEngine; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supports(HandlerResponse response) { | ||
| return response instanceof DynamicViewResponse; | ||
| } | ||
|
|
||
| @Override | ||
| public HttpResponse handle(HttpResponse httpResponse, HandlerResponse handlerResponse) { | ||
| DynamicViewResponse dynamicViewResponse = (DynamicViewResponse) handlerResponse; | ||
| String path = dynamicViewResponse.getPath(); | ||
| templateEngine.clearCache(); | ||
| String render = templateEngine.render(path, dynamicViewResponse.getModel()); | ||
|
|
||
| httpResponse.setStatus(handlerResponse.getStatus()); | ||
| httpResponse.setBody(render.getBytes(StandardCharsets.UTF_8)); | ||
| httpResponse.addHeader("Content-Type", "text/html; charset=utf-8"); | ||
| handlerResponse.getCookies().forEach(cookie->httpResponse.addHeader("Set-Cookie", cookie)); | ||
| return httpResponse; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,42 @@ | ||
| package web.renderer.view; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.Map; | ||
|
|
||
| public class ExpressionResolver { | ||
|
|
||
| public Object resolve(String expr, Map<String, Object> model) { | ||
| if (expr == null || expr.isBlank()) return null; | ||
|
|
||
| String[] parts = expr.trim().split("\\."); | ||
| Object cur = model.get(parts[0]); | ||
|
|
||
| for (int i = 1; i < parts.length; i++) { | ||
| if (cur == null) return null; | ||
| cur = getProperty(cur, parts[i]); | ||
| } | ||
| return cur; | ||
| } | ||
|
|
||
| private Object getProperty(Object target, String name) { | ||
| if (target instanceof Map<?, ?> m) { | ||
| return m.get(name); | ||
| } | ||
|
|
||
| Class<?> c = target.getClass(); | ||
|
|
||
| // getXxx() | ||
| String getter = "get" + capitalize(name); | ||
| try { | ||
| Method method = c.getMethod(getter); | ||
| return method.invoke(target); | ||
| } catch (Exception ignore) {} | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private String capitalize(String s) { | ||
| if (s == null || s.isEmpty()) return s; | ||
| return Character.toUpperCase(s.charAt(0)) + s.substring(1); | ||
| } | ||
| } |
This file contains hidden or 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,14 @@ | ||
| package web.renderer.view; | ||
|
|
||
| public final class HtmlEscaper { | ||
| private HtmlEscaper() {} | ||
|
|
||
| public static String escape(String s) { | ||
| if (s == null || s.isEmpty()) return ""; | ||
| return s.replace("&", "&") | ||
| .replace("<", "<") | ||
| .replace(">", ">") | ||
| .replace("\"",""") | ||
| .replace("'", "'"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
postHandling()을 모든 응답에 적용하는 것은 좋은 설계이지만, 응답 타입마다 필요한 처리가 다를 수 있습니다. 현재는DynamicViewResponse만 구현했지만, 다른 응답 타입에서 필요하지 않은 작업이 있을 경우 성능이나 로직 복잡도가 증가할 수 있습니다. 필요시 선택적 적용 인터페이스를 고려하세요."