Skip to content

Commit

Permalink
Merge pull request #97 from mju-likelion/develop
Browse files Browse the repository at this point in the history
로깅, HttpMediaTypeNotAcceptableException 핸들러 추가
  • Loading branch information
Dh3356 authored Mar 1, 2024
2 parents cedb916 + 09f06e3 commit 3677cd9
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM openjdk:17-jdk
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENV JAVA_OPTS="-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses=true"
ENTRYPOINT ["java", "-Dspring.profiles.active=docker", "-Duser.timezone=Asia/Seoul", "-jar", "app.jar"]
15 changes: 15 additions & 0 deletions src/main/java/org/mjulikelion/bagel/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mjulikelion.bagel.config;

import org.mjulikelion.bagel.interceptor.CustomLoggingInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CustomLoggingInterceptor());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.mjulikelion.bagel.controller;

import static org.mjulikelion.bagel.errorcode.ErrorCode.HTTP_MEDIA_TYPE_NOT_ACCEPTABLE_ERROR;
import static org.mjulikelion.bagel.errorcode.ErrorCode.HTTP_MEDIA_TYPE_NOT_SUPPORTED_ERROR;

import lombok.extern.slf4j.Slf4j;
import org.mjulikelion.bagel.dto.response.ResponseDto;
import org.mjulikelion.bagel.errorcode.ErrorCode;
Expand All @@ -13,6 +16,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand Down Expand Up @@ -157,4 +162,28 @@ public ResponseEntity<ResponseDto<Void>> handleJpaException(JpaException jpaExce
log.error("SqlException: {}", message);
return new ResponseEntity<>(ResponseDto.res(code, message), HttpStatus.INTERNAL_SERVER_ERROR);
}

//HttpMediaTypeNotAcceptableException
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public ResponseEntity<ResponseDto<Void>> handleHttpMediaTypeNotAcceptableException(
HttpMediaTypeNotAcceptableException httpMediaTypeNotAcceptableException) {
ErrorCode errorCode = HTTP_MEDIA_TYPE_NOT_ACCEPTABLE_ERROR;
String code = errorCode.getCode();
String message = errorCode.getMessage() + " : " + httpMediaTypeNotAcceptableException.getMessage();
log.error("HttpMediaTypeNotAcceptableException: {}", message);
return new ResponseEntity<>(ResponseDto.res(code, message), HttpStatus.NOT_ACCEPTABLE);
}

//HttpMediaTypeNotSupportedException
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseEntity<ResponseDto<Void>> handleHttpMediaTypeNotSupportedException(
org.springframework.web.HttpMediaTypeNotSupportedException httpMediaTypeNotSupportedException) {
ErrorCode errorCode = HTTP_MEDIA_TYPE_NOT_SUPPORTED_ERROR;
String code = errorCode.getCode();
String message = errorCode.getMessage() + " : " + httpMediaTypeNotSupportedException.getMessage();
log.error("HttpMediaTypeNotSupportedException: {}", message);
return new ResponseEntity<>(ResponseDto.res(code, message), HttpStatus.UNSUPPORTED_MEDIA_TYPE);
}
}
5 changes: 4 additions & 1 deletion src/main/java/org/mjulikelion/bagel/errorcode/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public enum ErrorCode {
INVALID_AGREEMENT_ERROR("4004", "유효하지 않은 동의 항목 입니다."),//유효하지 않은 동의 항목
INVALID_INTRODUCE_MISSING_ERROR("4005", "자기소개 항목이 누락되었습니다."),//자기소개 항목이 누락됨
INVALID_AGREEMENT_MISSING_ERROR("4006", "동의 항목이 누락되었습니다."),//동의 항목이 누락됨
INVALID_INTRODUCE_LENGTH_ERROR("4007", "자기소개 항목의 길이가 유효하지 않습니다.");//자기소개 항목의 길이가 유효하지 않음
INVALID_INTRODUCE_LENGTH_ERROR("4007", "자기소개 항목의 길이가 유효하지 않습니다."),//자기소개 항목의 길이가 유효하지 않음
//미디어 타입 오류들
HTTP_MEDIA_TYPE_NOT_ACCEPTABLE_ERROR("4060", "지원하지 않는 미디어 타입입니다."),//지원하지 않는 미디어 타입
HTTP_MEDIA_TYPE_NOT_SUPPORTED_ERROR("4150", "수락할 수 없는 미디어 타입입니다.");//지원하지 않는 미디어 타입


private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.mjulikelion.bagel.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class CustomLoggingInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String ipAddress = getClientIP(request);
String method = request.getMethod();
String uri = request.getRequestURI();

log.info("----- {} {} {} -----", ipAddress, method, uri);

// 추가적인 로깅
logUserAgent(request);
logQueryString(request);
logRequestHeaders(request);

log.info("----- Request end -----");

return true;
}

private String getClientIP(HttpServletRequest request) throws UnknownHostException {
String ip = request.getHeader("X-Forwarded-For");

if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-RealIP");
}
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("REMOTE_ADDR");
}
if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
log.info(String.valueOf(Inet4Address.getByName(ip)));

return ip;
}

private void logUserAgent(HttpServletRequest request) {
String userAgent = request.getHeader("User-Agent");
log.info("User-Agent: {}", userAgent);
}

private void logQueryString(HttpServletRequest request) {
String queryString = request.getQueryString();
log.info("Query String: {}", queryString);
}

private void logRequestHeaders(HttpServletRequest request) {
String contentType = request.getHeader("Content-Type");
String userAgent = request.getHeader("User-Agent");
log.info("Content-Type: {}", contentType);
log.info("User-Agent: {}", userAgent);
}
}

0 comments on commit 3677cd9

Please sign in to comment.