Rev 34819 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.logging.log4j.Logger;import org.apache.logging.log4j.LogManager;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.Cache;import org.springframework.cache.CacheManager;import org.springframework.stereotype.Component;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.Response;import com.spice.profitmandi.common.model.UserInfo;import com.spice.profitmandi.common.util.JWTUtil;import com.spice.profitmandi.common.web.util.ResponseSender;@Componentpublic class PostInterceptor implements HandlerInterceptor {private static final Logger LOGGER = LogManager.getLogger(PostInterceptor.class);@Autowiredprivate ResponseSender<?> responseSender;@Autowiredprivate CacheManager timeoutCacheManager;@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)throws Exception {}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)throws Exception {LOGGER.info("request is received after : "+request.getRequestURL().toString());}@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {LOGGER.info("request is received in post interceptor before [{}] with method [{}]", request.getRequestURL().toString(), request.getMethod());if ("POST".equalsIgnoreCase(request.getMethod())) {String idempotencyKey = request.getHeader("IdempotencyKey");if (idempotencyKey == null || idempotencyKey.isEmpty()) {// response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing idempotency token in header");return true;}Cache cache = timeoutCacheManager.getCache("IdempotencyKey");if (cache != null) {if (cache.get(idempotencyKey) != null) {response.setStatus(HttpServletResponse.SC_OK);response.getWriter().write("Duplicate request. Idempotency Key already processed.");return false;} else {cache.put(idempotencyKey, "PROCESSED");return true;}}}return true;}private UserInfo getUserInfo(String token) throws ProfitMandiBusinessException{return JWTUtil.getUserInfo(token);}}