Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
#20 warn 포함 컨벤션 위반시 빌드 실패 및 포매팅 (#21)
Browse files Browse the repository at this point in the history
* style: 컨벤션에 맞게 코드 정리

* chore: indents를 tab -> space로 설정 변경

* docs: readme.md
  • Loading branch information
soongjamm authored May 26, 2021
1 parent 8beb2c2 commit 88e1e3f
Show file tree
Hide file tree
Showing 28 changed files with 616 additions and 547 deletions.
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repositories {

checkstyle {
toolVersion = '8.42'
ignoreFailures = true // 분석결과 예외가 발생하면 build 실패하는데, 이것을 무시함
ignoreFailures = false // 분석결과 error가 발생하면 build 실패
configFile = file("config/checkstyle/checkstyle.xml") // 작성한 checkstyle.xml 적용 (네이버)
reportsDir = file("${buildDir}/checkstyle-output") // report 파일 위치 지정
}
Expand All @@ -38,3 +38,18 @@ dependencies {
test {
useJUnitPlatform()
}

/**
* https://github.com/gradle/gradle/issues/881
* IgnoreFailures = false 가 Warn에 적용되도록 수정
*/
tasks.withType(Checkstyle).each { checkstyleTask ->
checkstyleTask.doLast {
reports.all { report ->
def outputFile = report.destination
if (outputFile.exists() && outputFile.text.contains("<error ")) {
throw new GradleException("There were checkstyle warnings! For more info check $outputFile")
}
}
}
}
12 changes: 6 additions & 6 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ The following rules in the Naver coding convention cannot be checked by this con
</module>

<!-- [no-trailing-spaces] -->
<module name="RegexpSingleline">
<property name="format" value="^(?!\s+\* $).*?\s+$"/>
<property name="message" value="[no-trailing-spaces] Line has trailing spaces."/>
</module>
<!-- <module name="RegexpSingleline">-->
<!-- <property name="format" value="^(?!\s+\* $).*?\s+$"/>-->
<!-- <property name="message" value="[no-trailing-spaces] Line has trailing spaces."/>-->
<!-- </module>-->

<!-- [line-length-120] -->
<property name="tabWidth" value="4"/>
Expand Down Expand Up @@ -168,8 +168,8 @@ The following rules in the Naver coding convention cannot be checked by this con

<!-- [indentation-tab] -->
<module name="RegexpSinglelineJava">
<property name="format" value="^\t* "/>
<property name="message" value="[indentation-tab] Indent must use tab characters"/>
<property name="format" value="^\t+"/>
<property name="message" value="[indentation-space] Indent must use space characters"/>
<property name="ignoreComments" value="true"/>
</module>

Expand Down
11 changes: 4 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
---

## 기능정의
**가게**
[위키참조](https://github.com/f-lab-edu/blue-delivery/wiki/%EA%B8%B0%EB%8A%A5%EC%A0%95%EC%9D%98:-%EA%B0%80%EA%B2%8C")

**고객**

Expand All @@ -15,13 +17,6 @@
- 결제하기
- 리뷰작성

**가게**

- 가게 정보 관리
- 메뉴 등록
- 배달기사 호출
- 리뷰답글

**라이더**

- 배달 요청 수신하기
Expand Down Expand Up @@ -76,6 +71,8 @@ Github Flow는 main 브랜치를 가 곧 product가 되는 전략입니다.

- 테스트 수행
- Linting (checkstyle 플러그인 + <a href="https://naver.github.io/hackday-conventions-java">네이버 캠퍼스 핵데이 Java 코딩 컨벤션</a>)
- [indentation-tab] indent 를 tab -> space로 변경
- [no-trailing-spaces] 적용 안함

`push`

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/delivery/DeliveryApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

@SpringBootApplication
public class DeliveryApplication {

public static void main(String[] args) {
SpringApplication.run(DeliveryApplication.class, args);


}
}
30 changes: 16 additions & 14 deletions src/main/java/com/delivery/aop/AuthenticationAop.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package com.delivery.aop;

import com.delivery.exception.InvalidAuthenticationException;
import com.delivery.user.AuthenticationHolder;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.delivery.exception.InvalidAuthenticationException;
import com.delivery.user.AuthenticationHolder;


@Aspect
@Component
public class AuthenticationAop {

@Pointcut("@annotation(com.delivery.aop.AuthenticationRequired)")
public void pointcut() {
}

@Before("pointcut()")
public void authenticate() {
if (!AuthenticationHolder.hasAuthentication()) {
throw new InvalidAuthenticationException();
}
}

@Pointcut("@annotation(com.delivery.aop.AuthenticationRequired)")
public void pointcut() {
}
@Before("pointcut()")
public void authenticate() {
if (!AuthenticationHolder.hasAuthentication()) {
throw new InvalidAuthenticationException();
}
}
}
51 changes: 30 additions & 21 deletions src/main/java/com/delivery/config/AuthenticationHolderFilter.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
package com.delivery.config;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;

import com.delivery.exception.InvalidAuthenticationException;
import com.delivery.user.Authentication;
import com.delivery.user.AuthenticationHolder;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@Component
public class AuthenticationHolderFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
try {
Authentication auth = (Authentication) session.getAttribute("auth");
AuthenticationHolder.setAuthentication(auth);
} catch (ClassCastException ex) {
throw new InvalidAuthenticationException();
}
chain.doFilter(request, response);
}

@Override
public void destroy() {
AuthenticationHolder.reset();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
try {
Authentication auth = (Authentication) session.getAttribute("auth");
AuthenticationHolder.setAuthentication(auth);
} catch (ClassCastException ex) {
throw new InvalidAuthenticationException();
}
chain.doFilter(request, response);
}

@Override
public void destroy() {
AuthenticationHolder.reset();
}
}
69 changes: 35 additions & 34 deletions src/main/java/com/delivery/config/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
package com.delivery.config;

import com.delivery.exception.InvalidAuthenticationException;
import com.delivery.exception.PasswordAuthenticationException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.LinkedHashMap;
import java.util.Map;
import com.delivery.exception.InvalidAuthenticationException;
import com.delivery.exception.PasswordAuthenticationException;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
Map<String, String> errors = new LinkedHashMap<>();
errors.put("errorLength", String.valueOf(ex.getFieldErrors().size()));
for (FieldError error : ex.getFieldErrors()) {
errors.put(error.getField(), error.getDefaultMessage());
}
return ResponseEntity.badRequest().body(errors.toString());
}

@ExceptionHandler(DuplicateKeyException.class)
public ResponseEntity<String> duplicateKeyExceptionHandler(DuplicateKeyException ex) {
return ResponseEntity.badRequest().body("email already exists - " + ex.getMessage());
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> illegalArgumentExceptionHandler(IllegalArgumentException ex) {
return ResponseEntity.badRequest().body(ex.getMessage());
}

@ExceptionHandler(InvalidAuthenticationException.class)
public ResponseEntity<String> invalidAuthenticationExceptionHandler(InvalidAuthenticationException ex) {
return ResponseEntity.badRequest().build();
}

@ExceptionHandler(PasswordAuthenticationException.class)
public ResponseEntity<String> PasswordAuthenticationExceptionHandler(PasswordAuthenticationException ex) {
return ResponseEntity.badRequest().body("password not match - " + ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
Map<String, String> errors = new LinkedHashMap<>();
errors.put("errorLength", String.valueOf(ex.getFieldErrors().size()));
for (FieldError error : ex.getFieldErrors()) {
errors.put(error.getField(), error.getDefaultMessage());
}
return ResponseEntity.badRequest().body(errors.toString());
}
@ExceptionHandler(DuplicateKeyException.class)
public ResponseEntity<String> duplicateKeyExceptionHandler(DuplicateKeyException ex) {
return ResponseEntity.badRequest().body("email already exists - " + ex.getMessage());
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> illegalArgumentExceptionHandler(IllegalArgumentException ex) {
return ResponseEntity.badRequest().body(ex.getMessage());
}
@ExceptionHandler(InvalidAuthenticationException.class)
public ResponseEntity<String> invalidAuthenticationExceptionHandler(InvalidAuthenticationException ex) {
return ResponseEntity.badRequest().build();
}
@ExceptionHandler(PasswordAuthenticationException.class)
public ResponseEntity<String> passwordAuthenticationExceptionHandler(PasswordAuthenticationException ex) {
return ResponseEntity.badRequest().body("password not match - " + ex.getMessage());
}
}
17 changes: 9 additions & 8 deletions src/main/java/com/delivery/config/RepositoryConfigDev.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.delivery.config;

import com.delivery.user.UserRepository;
import com.delivery.user.UserRepositoryHashMap;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.delivery.user.UserRepository;
import com.delivery.user.UserRepositoryHashMap;

@Configuration
@Profile("dev")
public class RepositoryConfigDev {

@Bean
public UserRepository userRepository() {
return new UserRepositoryHashMap();
}
}
@Bean
public UserRepository userRepository() {
return new UserRepositoryHashMap();
}
}
18 changes: 9 additions & 9 deletions src/main/java/com/delivery/user/Authentication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.delivery.user;

public class Authentication {
private String email;
private String nickname;
private String phone;

public Authentication(String email, String nickname, String phone) {
this.email = email;
this.nickname = nickname;
this.phone = phone;
}
private String email;
private String nickname;
private String phone;
public Authentication(String email, String nickname, String phone) {
this.email = email;
this.nickname = nickname;
this.phone = phone;
}
}
34 changes: 17 additions & 17 deletions src/main/java/com/delivery/user/AuthenticationHolder.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.delivery.user;

public class AuthenticationHolder {
private static ThreadLocal<Authentication> local = new ThreadLocal<>();

public static boolean hasAuthentication() {
return local.get() != null;
}

public static Authentication getAuthentication() {
return local.get();
}

public static void setAuthentication(Authentication auth) {
local.set(auth);
}

public static void reset() {
local.remove();
}
private static ThreadLocal<Authentication> local = new ThreadLocal<>();
public static boolean hasAuthentication() {
return local.get() != null;
}
public static Authentication getAuthentication() {
return local.get();
}
public static void setAuthentication(Authentication auth) {
local.set(auth);
}
public static void reset() {
local.remove();
}
}
Loading

0 comments on commit 88e1e3f

Please sign in to comment.