Skip to content

Commit 7610644

Browse files
authored
Fix version to 0.9.1 (#103)
2 parents 81e90c3 + df6df58 commit 7610644

7 files changed

Lines changed: 363 additions & 11 deletions

File tree

api-payload-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.namul</groupId>
88
<artifactId>beginner</artifactId>
9-
<version>0.9.0</version>
9+
<version>0.9.1</version>
1010
</parent>
1111

1212
<artifactId>api-payload-core</artifactId>

api-payload-webmvc/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.namul</groupId>
8+
<artifactId>beginner</artifactId>
9+
<version>0.9.1</version>
10+
</parent>
11+
12+
<artifactId>api-payload-webmvc</artifactId>
13+
<name>api-payload-webmvc</name>
14+
<description>This module offers api-payload classes which can run in Spring MVC environment. It doesn't contain api-payload-core module. So, You must add api-payload-core module with same version in your project.</description>
15+
<url>https://github.com/projectnamul/beginner</url>
16+
17+
<licenses>
18+
<license>
19+
<name>Apache License, Version 2.0</name>
20+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
21+
<distribution>repo</distribution>
22+
</license>
23+
</licenses>
24+
25+
<scm>
26+
<url>https://github.com/projectnamul</url>
27+
<connection>scm:git:git://github.com/projectnamul/beginner.git</connection>
28+
<developerConnection>scm:git:ssh://github.com/projectnamul/beginner.git</developerConnection>
29+
<tag>HEAD</tag>
30+
</scm>
31+
32+
<developers>
33+
<developer>
34+
<id>Properks</id>
35+
<name>Jeongmo Seo</name>
36+
<email>wjdah1402@gmail.com</email>
37+
<url>https://github.com/Properks</url>
38+
</developer>
39+
</developers>
40+
41+
<properties>
42+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43+
</properties>
44+
45+
46+
<dependencies>
47+
<dependency>
48+
<groupId>jakarta.servlet</groupId>
49+
<artifactId>jakarta.servlet-api</artifactId>
50+
<scope>provided</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework</groupId>
54+
<artifactId>spring-webmvc</artifactId>
55+
<scope>provided</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.namul</groupId>
59+
<artifactId>api-payload-core</artifactId>
60+
<scope>provided</scope>
61+
</dependency>
62+
</dependencies>
63+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.namul.api.payload.webmvc.error;
2+
3+
import jakarta.servlet.http.HttpServletRequest;
4+
import jakarta.servlet.http.HttpServletResponse;
5+
import org.namul.api.payload.core.code.BaseErrorCode;
6+
import org.namul.api.payload.core.error.ErrorCodeExceptionHandler;
7+
import org.namul.api.payload.core.response.BaseResponse;
8+
import org.namul.api.payload.webmvc.web.HttpServletWebRequestWrapper;
9+
import org.namul.api.payload.webmvc.web.HttpServletWebResponseWrapper;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.web.bind.annotation.ExceptionHandler;
12+
import org.springframework.web.bind.annotation.RestControllerAdvice;
13+
14+
15+
/**
16+
* The RestController Advice class which uses ErrorCodeExceptionHandler
17+
* @param <T> The BaseErrorCode which you use
18+
*/
19+
@RestControllerAdvice
20+
public class ExceptionRestControllerAdvice<T extends BaseErrorCode> {
21+
22+
private final ErrorCodeExceptionHandler<T> errorCodeExceptionHandler;
23+
24+
/**
25+
* The Content-type of response. The default value is "application/json"
26+
*/
27+
private final String contentType;
28+
29+
public ExceptionRestControllerAdvice(ErrorCodeExceptionHandler<T> errorCodeExceptionHandler) {
30+
this.errorCodeExceptionHandler = errorCodeExceptionHandler;
31+
this.contentType = MediaType.APPLICATION_JSON_VALUE;
32+
}
33+
34+
public ExceptionRestControllerAdvice(ErrorCodeExceptionHandler<T> errorCodeExceptionHandler, String contentType) {
35+
this.errorCodeExceptionHandler = errorCodeExceptionHandler;
36+
this.contentType = contentType;
37+
}
38+
39+
40+
@ExceptionHandler(Exception.class)
41+
public BaseResponse handleException(HttpServletRequest request, HttpServletResponse response, Exception ex) {
42+
T code = errorCodeExceptionHandler.getCode(ex);
43+
response.setStatus(code.getHttpStatus().value());
44+
response.setContentType(contentType);
45+
46+
return errorCodeExceptionHandler.handle(
47+
HttpServletWebRequestWrapper.wrap(request),
48+
HttpServletWebResponseWrapper.wrap(response),
49+
ex
50+
);
51+
}
52+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package org.namul.api.payload.webmvc.error;
2+
3+
import org.namul.api.payload.core.code.BaseErrorCode;
4+
import org.namul.api.payload.core.error.AdditionalExceptionHandler;
5+
import org.namul.api.payload.core.error.ErrorCodeExceptionHandlerConfigurer;
6+
import org.namul.api.payload.core.writer.FailureResponseWriter;
7+
import org.springframework.beans.TypeMismatchException;
8+
import org.springframework.http.converter.HttpMessageNotReadableException;
9+
import org.springframework.web.HttpRequestMethodNotSupportedException;
10+
import org.springframework.web.bind.MethodArgumentNotValidException;
11+
import org.springframework.web.bind.MissingPathVariableException;
12+
import org.springframework.web.bind.MissingServletRequestParameterException;
13+
import org.springframework.web.servlet.resource.NoResourceFoundException;
14+
15+
import java.util.List;
16+
import java.util.concurrent.Executor;
17+
18+
public class HttpServletErrorCodeExceptionHandlerConfigurer<T extends BaseErrorCode> extends ErrorCodeExceptionHandlerConfigurer<T> {
19+
20+
public HttpServletErrorCodeExceptionHandlerConfigurer(FailureResponseWriter<T> failureResponseWriter) {
21+
super(failureResponseWriter);
22+
}
23+
24+
/**
25+
* The method for Default configuration it contains MethodArgumentNotValid, HttpMessageNotReadable, HttpRequestMethodNotSupported, MissingPathVariable, MissingServletRequestParameter, NoResourceFound, TypeMismatch, ServerApplication and Exception
26+
* it set Exception to have internalServerErrorCode and other Exceptions to have badRequestError
27+
* @param badRequestError The BaseErrorCode that can return ErrorReasonDTO for bad request error
28+
* @param internalServerError The BaseErrorCode that can return ErrorReasonDTO for internal server error
29+
*/
30+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> withDefault(T badRequestError,
31+
T internalServerError) {
32+
return this
33+
.addMethodArgumentNotValid(badRequestError)
34+
.addHttpMessageNotReadable(badRequestError)
35+
.addHttpRequestMethodNotSupported(badRequestError)
36+
.addMissingPathVariable(badRequestError)
37+
.addMissingServletRequestParameter(badRequestError)
38+
.addNoResourceFound(badRequestError)
39+
.addTypeMismatch(badRequestError)
40+
.addServerApplication(badRequestError)
41+
.addGlobalException(internalServerError);
42+
}
43+
44+
/**
45+
* The method that add ErrorReasonDTO with default handler about MethodArgumentNotValidException
46+
* @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response
47+
*/
48+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addMethodArgumentNotValid(T code) {
49+
return this.addAdvice(MethodArgumentNotValidException.class, code);
50+
}
51+
52+
/**
53+
* The method that add ErrorReasonDTO with default handler about HttpMessageNotReadableException
54+
* @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response
55+
*/
56+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addHttpMessageNotReadable(T code) {
57+
return this.addAdvice(HttpMessageNotReadableException.class, code);
58+
}
59+
60+
/**
61+
* The method that add ErrorReasonDTO with default handler about HttpRequestMethodNotSupported
62+
* @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response
63+
*/
64+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addHttpRequestMethodNotSupported(T code) {
65+
return this.addAdvice(HttpRequestMethodNotSupportedException.class, code);
66+
}
67+
68+
/**
69+
* The method that add ErrorReasonDTO with default handler about MissingPathVariableException
70+
* @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response
71+
*/
72+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addMissingPathVariable(T code) {
73+
return this.addAdvice(MissingPathVariableException.class, code);
74+
}
75+
76+
/**
77+
* The method that add ErrorReasonDTO with default handler about MissingServletRequestParameterException
78+
* @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response
79+
*/
80+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addMissingServletRequestParameter(T code) {
81+
return this.addAdvice(MissingServletRequestParameterException.class, code);
82+
}
83+
84+
/**
85+
* The method that add ErrorReasonDTO with default handler about NoResourceFoundException
86+
* @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response
87+
*/
88+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addNoResourceFound(T code) {
89+
return this.addAdvice(NoResourceFoundException.class, code);
90+
}
91+
92+
/**
93+
* The method that add ErrorReasonDTO with default handler about TypeMismatchException
94+
* @param code The BaseErrorCode which return ErrorReasonDTO that will be written in response
95+
*/
96+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addTypeMismatch(T code) {
97+
return this.addAdvice(TypeMismatchException.class, code);
98+
}
99+
100+
@Override
101+
public <E extends Throwable> HttpServletErrorCodeExceptionHandlerConfigurer<T> addAdvice(Class<E> cls, T code) {
102+
super.addAdvice(cls, code);
103+
return this;
104+
}
105+
106+
@Override
107+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addAdditionalExceptionHandler(AdditionalExceptionHandler<T> additionalExceptionHandler) {
108+
super.addAdditionalExceptionHandler(additionalExceptionHandler);
109+
return this;
110+
}
111+
112+
@Override
113+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addAdditionalExceptionHandlers(List<AdditionalExceptionHandler<T>> additionalExceptionHandlers) {
114+
super.addAdditionalExceptionHandlers(additionalExceptionHandlers);
115+
return this;
116+
}
117+
118+
@Override
119+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addGlobalException(T code) {
120+
super.addGlobalException(code);
121+
return this;
122+
}
123+
124+
@Override
125+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> addServerApplication(T code) {
126+
super.addServerApplication(code);
127+
return this;
128+
}
129+
130+
@Override
131+
public <E extends Throwable> HttpServletErrorCodeExceptionHandlerConfigurer<T> deleteAdvice(Class<E> cls) {
132+
super.deleteAdvice(cls);
133+
return this;
134+
}
135+
136+
@Override
137+
public HttpServletErrorCodeExceptionHandlerConfigurer<T> setAdditionalExceptionHandlersExecutor(Executor executor) {
138+
super.setAdditionalExceptionHandlersExecutor(executor);
139+
return this;
140+
}
141+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.namul.api.payload.webmvc.web;
2+
3+
import jakarta.servlet.http.Cookie;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import org.namul.api.payload.core.web.WebRequestWrapper;
6+
7+
import java.util.Optional;
8+
9+
public class HttpServletWebRequestWrapper implements WebRequestWrapper {
10+
11+
private final HttpServletRequest httpServletRequest;
12+
13+
public HttpServletWebRequestWrapper(HttpServletRequest httpServletRequest) {
14+
this.httpServletRequest = httpServletRequest;
15+
}
16+
17+
public static HttpServletWebRequestWrapper wrap(HttpServletRequest httpServletRequest) {
18+
return new HttpServletWebRequestWrapper(httpServletRequest);
19+
}
20+
21+
@Override
22+
public String getHeader(String header) {
23+
return httpServletRequest.getHeader(header);
24+
}
25+
26+
@Override
27+
public String getMethod() {
28+
return httpServletRequest.getMethod();
29+
}
30+
31+
@Override
32+
public String getRequestURI() {
33+
return httpServletRequest.getRequestURI();
34+
}
35+
36+
@Override
37+
public String getParameter(String parameter) {
38+
return httpServletRequest.getParameter(parameter);
39+
}
40+
41+
@Override
42+
public String getCookie(String name) {
43+
Cookie[] cookies = httpServletRequest.getCookies();
44+
if (cookies != null && cookies.length != 0) {
45+
for (int i = 0; i < cookies.length; i++) {
46+
Cookie cookie = cookies[i];
47+
if (cookie.getName().equals(name)) {
48+
return cookie.getValue();
49+
}
50+
}
51+
}
52+
return null;
53+
}
54+
55+
@Override
56+
public Optional<String> getRemoteAddress() {
57+
return Optional.ofNullable(httpServletRequest.getRemoteAddr());
58+
}
59+
60+
@Override
61+
public Optional<String> getUserAgent() {
62+
return Optional.ofNullable(httpServletRequest.getHeader("User-Agent"));
63+
}
64+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.namul.api.payload.webmvc.web;
2+
3+
import jakarta.servlet.http.HttpServletResponse;
4+
import org.namul.api.payload.core.web.WebResponseWrapper;
5+
6+
public class HttpServletWebResponseWrapper implements WebResponseWrapper {
7+
8+
private final HttpServletResponse httpServletResponse;
9+
10+
public HttpServletWebResponseWrapper(HttpServletResponse httpServletResponse) {
11+
this.httpServletResponse = httpServletResponse;
12+
}
13+
14+
public static HttpServletWebResponseWrapper wrap(HttpServletResponse httpServletResponse) {
15+
return new HttpServletWebResponseWrapper(httpServletResponse);
16+
}
17+
18+
@Override
19+
public String getContentType() {
20+
return httpServletResponse.getContentType();
21+
}
22+
23+
@Override
24+
public int getStatus() {
25+
return httpServletResponse.getStatus();
26+
}
27+
28+
@Override
29+
public String getHeader(String name) {
30+
return httpServletResponse.getHeader(name);
31+
}
32+
33+
34+
}

0 commit comments

Comments
 (0)