com.javajy.api.component.FrComponent Maven / Gradle / Ivy
The newest version!
package com.javajy.api.component;
import com.alibaba.fastjson.JSON;
import com.javajy.api.e.b;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
@Component
@Aspect
public class FrComponent implements HandlerInterceptor{
private static Map sysCache = new HashMap();
private static Map sysCacheExp = new HashMap();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
//System.out.println("------000"+request.getRequestURI() + getCacheKey(request));
if(loadRequest(request,response)) return false;
if(b.a(request,response)) return HandlerInterceptor.super.preHandle(request, response, handler);
return false;
}
private String getCacheKey(HttpServletRequest request) {
String[] uris = request.getRequestURI().split("/");
if(uris.length == 3) {
return uris[1] + "controller." + uris[2] + "-" + request.getQueryString();
}
return null;
}
// @Before("within(com.javajy.api.controller..*)")
// public void init(JoinPoint joinPoint) {
// String key = joinPoint.getSignature()+param;
// sysCache.put(key, returnObject);
// sysCacheExp.put(key, new Date().getTime());
// }
public boolean loadRequest(HttpServletRequest request, HttpServletResponse response) {
String method = request.getMethod();
try {
//Object obj = sysCache.get(Integer.parseInt(request.getParameter(request.getParameterMap().keySet().toArray()[0].toString())));
String key = getCacheKey(request);
Object obj = sysCache.get(key);
if(obj != null) response.getOutputStream().write(JSON.toJSONString(obj,true).getBytes());
if(obj != null && method.equalsIgnoreCase("get")) {
cacheValid(request);
return builResponse(response,200);
}
print(1,"loadRequest..");
if(canCache(key)) {
sysCache.put(key, null);
}
}catch(Exception e) {
print(e);
}
return false;
}
private void cacheValid(HttpServletRequest request) {
String key = getCacheKey(request);//Integer.parseInt(request.getParameter(request.getParameterMap().keySet().toArray()[0].toString()));
long exp = sysCacheExp.get(key);
if(new Date().getTime() - exp >= 100000) {
sysCache.remove(key);
sysCacheExp.remove(key);
}
}
private boolean canCache(String key) {
return key == null || key.indexOf("queryby") == 0 ? true : false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Bean
public CorsFilter crosFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedHeader("*");
config.addAllowedOrigin("*");
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
private boolean builResponse(HttpServletResponse response, int status) {
response.setHeader("Content-Type", "application/json");
response.setStatus(status);
return true;
}
@AfterReturning(pointcut="within(com.javajy.api.controller..*)", returning = "returnObject")
public void afterCompletion(JoinPoint joinPoint, Object returnObject) {
try {
//String key = joinPoint.getSignature().toString().toLowerCase().replace("list com.javajy.api.controller.", "");
//key = key.substring(0, key.indexOf("(string"))+"-id="+joinPoint.getArgs()[0];
String currentKey = joinPoint.getSignature().toString().toLowerCase();
print(2,currentKey);
Set keySet = sysCache.keySet();
List candidateKey = new ArrayList();
String key = null;
for(String k: keySet) {
if(k == null) continue;
String tk = k.substring(0, k.indexOf("-"));
print(3,tk);
if(currentKey.contains(tk) && sysCache.get(k) == null) {
candidateKey.add(k);
}
}
//Object[] arg = joinPoint.getArgs();
print(4, candidateKey.size()+"");
if(candidateKey.size() == 1) {
key = candidateKey.get(0);
sysCache.put(key, returnObject);
sysCacheExp.put(key, new Date().getTime());
}else {
for(String dirtyKey: candidateKey) {
sysCache.remove(dirtyKey);
sysCacheExp.remove(dirtyKey);
}
}
}catch(Exception e) {
print(e);
}
finally {
}
}
private void print(int pos, String log) {
//System.out.println(pos+":"+log);
}
private void print(Exception e) {
e.printStackTrace();
}
}