You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
5.5 KiB
144 lines
5.5 KiB
2 years ago
|
package com.ynxbd.common.config.interceptor;
|
||
|
|
||
|
import com.opensymphony.xwork2.ActionContext;
|
||
|
import com.opensymphony.xwork2.ActionInvocation;
|
||
|
import com.ynxbd.common.helper.common.ErrorHelper;
|
||
|
import com.ynxbd.common.helper.common.JsonHelper;
|
||
|
import com.ynxbd.common.result.Result;
|
||
|
import org.apache.commons.lang3.ObjectUtils;
|
||
|
import org.apache.struts2.interceptor.ServletConfigInterceptor;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.IOException;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.lang.reflect.Method;
|
||
|
import java.lang.reflect.Parameter;
|
||
|
import java.math.BigDecimal;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* 方法拦截器
|
||
|
*/
|
||
|
public class MethodInterceptor extends ServletConfigInterceptor {
|
||
|
|
||
|
@Override
|
||
|
public String intercept(ActionInvocation invocation) throws Exception {
|
||
|
Object action = invocation.getAction();
|
||
|
ActionContext context = invocation.getInvocationContext();
|
||
|
HttpServletRequest request = context.getServletRequest();
|
||
|
if (action instanceof org.apache.struts2.action.ServletRequestAware) {
|
||
|
((org.apache.struts2.action.ServletRequestAware) action).withServletRequest(request);
|
||
|
}
|
||
|
|
||
|
String methodName = invocation.getProxy().getMethod();
|
||
|
String initials;
|
||
|
for (Method method : action.getClass().getDeclaredMethods()) {
|
||
|
if (method.getName().equals(methodName)) {
|
||
|
List<Object> params = new ArrayList<>();
|
||
|
String simpleName;
|
||
|
String value;
|
||
|
for (Parameter parameter : method.getParameters()) {
|
||
|
simpleName = parameter.getType().getSimpleName();
|
||
|
value = request.getParameter(parameter.getName());
|
||
|
|
||
|
if ("String".equals(simpleName)) {
|
||
|
params.add(ObjectUtils.isEmpty(value) ? null : value);
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
initials = simpleName.substring(0, 1);
|
||
|
if (initials.equals(initials.toUpperCase()) && ObjectUtils.isEmpty(value)) { // 大写
|
||
|
params.add(null);
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (simpleName.equals("int")) {
|
||
|
simpleName = "integer";
|
||
|
}
|
||
|
|
||
|
simpleName = simpleName.toLowerCase();
|
||
|
try {
|
||
|
switch (simpleName) {
|
||
|
case "boolean":
|
||
|
params.add(Boolean.valueOf(value));
|
||
|
|
||
|
break;
|
||
|
case "bigdecimal":
|
||
|
params.add(new BigDecimal(value));
|
||
|
|
||
|
break;
|
||
|
case "integer":
|
||
|
params.add(Integer.valueOf(value));
|
||
|
|
||
|
break;
|
||
|
case "long":
|
||
|
params.add(Long.valueOf(value));
|
||
|
|
||
|
break;
|
||
|
case "double":
|
||
|
params.add(Double.valueOf(value));
|
||
|
|
||
|
break;
|
||
|
default:
|
||
|
params.add(null);
|
||
|
break;
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
params.add(null);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
Object invoke = method.invoke(action, params.toArray());
|
||
|
if (invoke == null) { // 避免返回值为空
|
||
|
return "SUCCESS";
|
||
|
}
|
||
|
|
||
|
if (Result.class == invoke.getClass()) {
|
||
|
respJson(context.getServletResponse(), action, invoke);
|
||
|
}
|
||
|
return "SUCCESS";
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
if (e.getMessage() == null) {
|
||
|
Throwable cause = e.getCause();
|
||
|
if (cause != null) {
|
||
|
ErrorHelper.println((Exception) cause);
|
||
|
}
|
||
|
} else {
|
||
|
ErrorHelper.println(e);
|
||
|
}
|
||
|
return "SUCCESS";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return "SUCCESS";
|
||
|
}
|
||
|
|
||
|
|
||
|
public void respJson(HttpServletResponse response, Object action, Object data) {
|
||
|
if (action instanceof org.apache.struts2.action.ServletResponseAware) {
|
||
|
((org.apache.struts2.action.ServletResponseAware) action).withServletResponse(response);
|
||
|
}
|
||
|
response.setContentType("application/json;charset=utf-8");
|
||
|
/* 允许跨域的主机地址 */
|
||
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
||
|
/* 允许跨域的请求方法GET, POST, HEAD 等 */
|
||
|
response.setHeader("Access-Control-Allow-Methods", "*");
|
||
|
/* 重新预检验跨域的缓存时间 (s) */
|
||
|
response.setHeader("Access-Control-Max-Age", "3600");
|
||
|
/* 允许跨域的请求头 */
|
||
|
response.setHeader("Access-Control-Allow-Headers", "*");
|
||
|
/* 是否携带cookie */
|
||
|
response.setHeader("Access-Control-Allow-Credentials", "true");
|
||
|
try (PrintWriter pw = response.getWriter()) {
|
||
|
pw.write(JsonHelper.toJsonString(data));
|
||
|
pw.flush();
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|