-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] 블로그 조회수 기능 추가 및 중복 증가 방지 / #99 #100
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
Open
JayongLee
wants to merge
11
commits into
dev
Choose a base branch
from
feat/#99
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
023ef28
[feat] 블로그 조회수 기능 추가 및 중복 증가 방지
JayongLee 8433278
[feat] PR Review : 쿠키 오버플로우 방지 및 update 검증 로직 수정
JayongLee a058885
[feat] PR Review : 홈화면 블로그 조회 쿼리 수정
JayongLee a66fb90
[refactor] OOP 준수를 위한 쿠키 관련 분리
JayongLee af07ea5
[refactor] PR Review 반영
JayongLee 28a8c6a
[chore] SonarQube : 미사용 import 제거
JayongLee 8a04bf5
[chore] SonarQube : Private Constructor
JayongLee a9459a0
[refactor] PR Review : 조회수 증가 로직 예외 처리
JayongLee 567afb4
[refactor] Revert : 조회수 증가 로직 예외 처리
JayongLee ec045ec
[refactor] PR Review 불필요한 쿼리 제거
JayongLee bfccd07
Merge branch 'dev' into feat/#99
JayongLee 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
Some comments aren't visible on the classic Files Changed page.
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
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
64 changes: 64 additions & 0 deletions
64
src/main/java/dev/woori/woorilog/global/util/CookieUtils.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package dev.woori.woorilog.global.util; | ||
|
|
||
| import jakarta.servlet.http.Cookie; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
|
|
||
| import java.net.URLDecoder; | ||
| import java.net.URLEncoder; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.Optional; | ||
|
|
||
| public class CookieUtils { | ||
|
|
||
| private static final int COOKIE_MAX_AGE = 24 * 60 * 60; // 24시간 | ||
| private static final int MAX_COOKIE_SIZE = 3000; | ||
| private static final String NAME_PREFIX = "["; | ||
| private static final String NAME_POSTFIX = "]"; | ||
|
|
||
| public static final String VIEW_COOKIE_NAME = "postView"; | ||
|
|
||
| public static Cookie createViewCookie(String name, String value) { | ||
| String cookieValue = getCookieValue(value); | ||
| Cookie cookie = new Cookie(name, URLEncoder.encode(cookieValue, StandardCharsets.UTF_8)); | ||
| cookie.setMaxAge(COOKIE_MAX_AGE); | ||
| cookie.setPath("/"); | ||
| cookie.setHttpOnly(true); | ||
| return cookie; | ||
| } | ||
|
|
||
| public static String getCookieValue(String value) { | ||
| return NAME_PREFIX + value + NAME_POSTFIX; | ||
| } | ||
|
|
||
| public static String getDecodedCookieValue(Cookie cookie) { | ||
| return URLDecoder.decode(cookie.getValue(), StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| public static Cookie updateCookie(Cookie oldCookie, String newValue) { | ||
| String decodedValue = getDecodedCookieValue(oldCookie); | ||
| String tempValue = decodedValue + "_" + newValue; | ||
| // 쿠키 오버플로우 방지 | ||
| String updateValue = tempValue.length() <= MAX_COOKIE_SIZE ? | ||
| URLEncoder.encode(tempValue, StandardCharsets.UTF_8) : URLEncoder.encode(newValue, StandardCharsets.UTF_8); | ||
|
|
||
| oldCookie.setValue(updateValue); | ||
| oldCookie.setMaxAge(COOKIE_MAX_AGE); | ||
| return oldCookie; | ||
| } | ||
|
|
||
| public static boolean isContainedValue(String originalValue, String newValue) { | ||
| return Arrays.stream(originalValue.split("_")).noneMatch(newValue::equals); | ||
JayongLee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // 쿠키 조회 | ||
| public static Optional<Cookie> getCookie(HttpServletRequest request, String cookieName) { | ||
| Cookie[] cookies = request.getCookies(); | ||
| if (cookies == null) { | ||
| return Optional.empty(); | ||
| } | ||
| return Arrays.stream(cookies) | ||
| .filter(cookie -> cookieName.equals(cookie.getName())) | ||
| .findFirst(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.