1 package cn.home1.oss.lib.webmvc.internal;
2
3 import static lombok.AccessLevel.PACKAGE;
4 import static org.apache.commons.lang3.StringUtils.isNotBlank;
5 import static org.springframework.http.HttpHeaders.ACCEPT;
6 import static org.springframework.http.HttpMethod.DELETE;
7 import static org.springframework.http.HttpMethod.POST;
8 import static org.springframework.http.HttpMethod.PUT;
9 import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
10
11 import cn.home1.oss.lib.webmvc.api.RequestResolver;
12
13 import lombok.Getter;
14 import lombok.Setter;
15 import lombok.extern.slf4j.Slf4j;
16
17 import org.springframework.http.HttpMethod;
18
19 import javax.servlet.http.HttpServletRequest;
20
21
22
23
24 @Getter
25 @Setter(PACKAGE)
26 @Slf4j
27 public class DefaultRequestResolver implements RequestResolver {
28
29 private String loginEndpoint;
30
31 public DefaultRequestResolver(final String loginEndpoint) {
32 this.loginEndpoint = loginEndpoint;
33 }
34
35 @Override
36 public Boolean isLoginRequest(final HttpServletRequest request) {
37 final String reqContextPath = request.getContextPath();
38 final String contextPath = reqContextPath.endsWith("/") ? reqContextPath : reqContextPath + "/";
39 final boolean isLoginRequest = request.getRequestURI().equals(contextPath + this.loginEndpoint);
40
41 if (log.isTraceEnabled()) {
42 log.trace("contextPath: {}, requestUri: {}, isLoginRequest: {}",
43 request.getContextPath(), request.getRequestURI(), isLoginRequest);
44 }
45 return isLoginRequest;
46 }
47
48
49
50
51 @Override
52 public Boolean isAjaxRequest(final HttpServletRequest request) {
53 final HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());
54
55
56 final String acceptHeader = request.getHeader(ACCEPT);
57 final String accept = isNotBlank(acceptHeader) ? acceptHeader : "";
58 final String underscore = request.getParameter("_");
59 final boolean result = POST == httpMethod || PUT == httpMethod || DELETE == httpMethod
60 || accept.contains(APPLICATION_JSON_VALUE)
61 || isNotBlank(underscore);
62 return result;
63 }
64 }