148 lines
4.9 KiB
Java
148 lines
4.9 KiB
Java
package io.company.localhost.utils;
|
|
|
|
import io.company.localhost.common.webEnum.WebEnum;
|
|
import io.company.localhost.common.wrapper.RequestMappingWrapper;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.method.HandlerMethod;
|
|
import org.springframework.web.servlet.HandlerMapping;
|
|
|
|
import java.lang.annotation.Annotation;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
|
|
|
|
//web 요청에 대한 정보얻을 때 사용
|
|
@Slf4j
|
|
public class WebUtil {
|
|
|
|
private WebUtil() {
|
|
//
|
|
}
|
|
|
|
|
|
/**
|
|
* HttpServletRequest를 가져옴
|
|
*
|
|
* @return HttpServletRequest
|
|
*/
|
|
public static HttpServletRequest getRequest() {
|
|
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
|
}
|
|
|
|
/**
|
|
* HttpServletResponse를 가져옴
|
|
*
|
|
* @return HttpServletResponse
|
|
*/
|
|
public static HttpServletResponse getResponse() {
|
|
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
|
|
}
|
|
|
|
/**
|
|
* ajax 호출 여부
|
|
*
|
|
* @param HttpServletRequest
|
|
* @return boolean - true : ajax호출, false : 일반(?) 호출
|
|
*/
|
|
public static boolean isAjax(HttpServletRequest request) {
|
|
// String xRequestedWith = request.getHeader("X-REQUESTED-WITH");
|
|
// String xRequestedWith = request.getHeader("x-requested-with");
|
|
// String xRequestedWith = request.getHeader(HttpHeaders.CONTENT_TYPE);
|
|
if (Objects.isNull(request)) {
|
|
return false;
|
|
}
|
|
|
|
String xRequestedWith = request.getHeader("X-Requested-With");
|
|
return WebEnum.AJAX_IDENTIFIER.value().equals(xRequestedWith);
|
|
}
|
|
|
|
/**
|
|
* @param HttpServletRequest
|
|
* @return boolean - true : json type, false : not json type
|
|
*/
|
|
public static boolean isJson(HttpServletRequest request) {
|
|
// String contentType = request.getHeader("Content-Type");
|
|
// return MediaType.APPLICATION_JSON_VALUE.equals(contentType);
|
|
if (Objects.isNull(request)) {
|
|
return false;
|
|
}
|
|
|
|
String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
|
|
if (contentType != null && contentType.startsWith(MediaType.APPLICATION_JSON_VALUE)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
/**
|
|
* controller method annotation 가져오는 메소드
|
|
*
|
|
* @param HttpServletRequest
|
|
* @param Class<? extends Annotation>
|
|
* @return Annotation
|
|
*/
|
|
public static <T> T getAnnotation(HttpServletRequest request, Class<? extends Annotation> annotationType) {
|
|
RequestMappingWrapper wrapper = ContextUtil.getBean(RequestMappingWrapper.class);
|
|
|
|
assert wrapper != null;
|
|
return wrapper.getAnnotation(request, annotationType);
|
|
}
|
|
|
|
public static <T> T getAnnotation(HandlerMethod handlerMethod, Class<? extends Annotation> annotationType) {
|
|
RequestMappingWrapper wrapper = ContextUtil.getBean(RequestMappingWrapper.class);
|
|
|
|
assert wrapper != null;
|
|
return wrapper.getAnnotation(handlerMethod, annotationType);
|
|
}
|
|
|
|
/**
|
|
* controller method annotation 있는지 여부
|
|
*
|
|
* @param HttpServletRequest
|
|
* @param Class<? extends Annotation>
|
|
* @return boolean
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public static <A extends Annotation> boolean hasAnyAnnotation(HttpServletRequest request,
|
|
Class<A>... annotationTypes) {
|
|
RequestMappingWrapper wrapper = ContextUtil.getBean(RequestMappingWrapper.class);
|
|
|
|
assert wrapper != null;
|
|
return wrapper.hasAnyAnnotation(request, annotationTypes);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public static <A extends Annotation> boolean hasAnyAnnotation(HandlerMethod handlerMethod,
|
|
Class<A>... annotationTypes) {
|
|
RequestMappingWrapper wrapper = ContextUtil.getBean(RequestMappingWrapper.class);
|
|
|
|
assert wrapper != null;
|
|
return wrapper.hasAnyAnnotation(handlerMethod, annotationTypes);
|
|
}
|
|
|
|
/**
|
|
* PathVariable
|
|
*
|
|
* @param HttpServletRequest
|
|
* @param String
|
|
* @return String
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public static String getPathVariable(HttpServletRequest request, String pathVariableName) {
|
|
final Map<String, String> pathVariables = (Map<String, String>) request
|
|
.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
|
|
|
if (pathVariables == null) {
|
|
return null;
|
|
}
|
|
return pathVariables.get(pathVariableName);
|
|
}
|
|
}
|