| 33764 |
ranu |
1 |
package com.spice.profitmandi.web.interceptor;
|
|
|
2 |
|
|
|
3 |
import javax.servlet.http.HttpServletRequest;
|
|
|
4 |
|
|
|
5 |
import javax.servlet.http.HttpServletResponse;
|
|
|
6 |
|
|
|
7 |
import org.apache.logging.log4j.Logger;
|
|
|
8 |
import org.apache.logging.log4j.LogManager;
|
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
10 |
import org.springframework.cache.Cache;
|
|
|
11 |
import org.springframework.cache.CacheManager;
|
|
|
12 |
import org.springframework.stereotype.Component;
|
|
|
13 |
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
14 |
import org.springframework.web.servlet.ModelAndView;
|
|
|
15 |
|
|
|
16 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
17 |
import com.spice.profitmandi.common.model.Response;
|
|
|
18 |
import com.spice.profitmandi.common.model.UserInfo;
|
|
|
19 |
import com.spice.profitmandi.common.util.JWTUtil;
|
|
|
20 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
|
|
21 |
|
|
|
22 |
@Component
|
|
|
23 |
public class PostInterceptor implements HandlerInterceptor {
|
|
|
24 |
|
|
|
25 |
private static final Logger LOGGER = LogManager.getLogger(PostInterceptor.class);
|
|
|
26 |
|
|
|
27 |
@Autowired
|
|
|
28 |
private ResponseSender<?> responseSender;
|
|
|
29 |
|
|
|
30 |
@Autowired
|
|
|
31 |
private CacheManager timeoutCacheManager;
|
|
|
32 |
|
|
|
33 |
@Override
|
|
|
34 |
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
|
|
|
35 |
throws Exception {
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
@Override
|
|
|
39 |
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
|
|
|
40 |
throws Exception {
|
|
|
41 |
LOGGER.info("request is received after : "+request.getRequestURL().toString());
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
@Override
|
|
|
45 |
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
|
|
|
46 |
//LOGGER.info("request is received in post interceptor before [{}] with method [{}]", request.getRequestURL().toString(), request.getMethod());
|
|
|
47 |
if ("POST".equalsIgnoreCase(request.getMethod())) {
|
|
|
48 |
String idempotencyKey = request.getHeader("IdempotencyKey");
|
|
|
49 |
if (idempotencyKey == null || idempotencyKey.isEmpty()) {
|
|
|
50 |
// response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing idempotency token in header");
|
|
|
51 |
return true;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
Cache cache = timeoutCacheManager.getCache("IdempotencyKey");
|
|
|
55 |
if (cache != null) {
|
|
|
56 |
if (cache.get(idempotencyKey) != null) {
|
|
|
57 |
response.setStatus(HttpServletResponse.SC_OK);
|
|
|
58 |
response.getWriter().write("Duplicate request. Idempotency Key already processed.");
|
|
|
59 |
return false;
|
|
|
60 |
} else {
|
|
|
61 |
cache.put(idempotencyKey, "PROCESSED");
|
|
|
62 |
return true;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
return true;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
private UserInfo getUserInfo(String token) throws ProfitMandiBusinessException{
|
|
|
70 |
return JWTUtil.getUserInfo(token);
|
|
|
71 |
}
|
|
|
72 |
}
|