|
|
|
@ -1,38 +1,35 @@
|
|
|
|
|
package xyz.wbsite.dbtool.web.action;
|
|
|
|
|
|
|
|
|
|
import org.apache.tomcat.util.http.fileupload.FileUtils;
|
|
|
|
|
import xyz.wbsite.dbtool.web.config.ActionConfig;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.base.*;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.auth.LocalData;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.utils.FileUtil;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.utils.MapperUtil;
|
|
|
|
|
import com.fasterxml.jackson.core.TreeNode;
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.web.servlet.DispatcherServlet;
|
|
|
|
|
import org.springframework.web.servlet.LocaleResolver;
|
|
|
|
|
import org.springframework.web.servlet.View;
|
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
|
|
|
|
import xyz.wbsite.dbtool.web.config.ActionConfig;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.auth.LocalData;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.base.BaseResponse;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.base.ErrorType;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.base.Screen;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.base.Token;
|
|
|
|
|
import xyz.wbsite.dbtool.web.frame.utils.MapperUtil;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.lang.reflect.Parameter;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
@ -41,9 +38,8 @@ import java.util.regex.Pattern;
|
|
|
|
|
/**
|
|
|
|
|
* 全局请求Controller,如果无特殊请求,则不需再增加其他Controller
|
|
|
|
|
* 全局htm后缀入口{@link GlobalController#action(Model, HttpServletRequest, HttpServletResponse)}
|
|
|
|
|
* 全局ajax入口{@link GlobalController#ajax(String, String, String, HttpServletRequest, HttpServletResponse, String, MultipartFile)}
|
|
|
|
|
* 全局异常捕捉{@link GlobalController#exceptionHandler(HttpServletRequest, HttpServletResponse, Model, Exception)}
|
|
|
|
|
* 全局上传接口{@link GlobalController#upload(HttpServletRequest)}
|
|
|
|
|
* 全局下载接口{@link GlobalController#download(String)}
|
|
|
|
|
* 全局消息订阅{@link GlobalController#sse(String)}
|
|
|
|
|
* <p>
|
|
|
|
|
* 说明Request命名规则,驼峰式命名
|
|
|
|
@ -223,89 +219,73 @@ public class GlobalController implements ErrorController {
|
|
|
|
|
return layout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/upload")
|
|
|
|
|
@RequestMapping("/ajax/{module}/{target}/{method}")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public BaseResponse upload(HttpServletRequest request) {
|
|
|
|
|
FileUploadResponse fileUploadResponse = new FileUploadResponse();
|
|
|
|
|
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
|
|
|
|
|
MultipartFile target = multipartHttpServletRequest.getFile("file");
|
|
|
|
|
|
|
|
|
|
String fileName = target.getOriginalFilename();
|
|
|
|
|
public Object ajax(
|
|
|
|
|
@PathVariable String module,
|
|
|
|
|
@PathVariable String target,
|
|
|
|
|
@PathVariable String method,
|
|
|
|
|
HttpServletRequest request,
|
|
|
|
|
HttpServletResponse response,
|
|
|
|
|
@RequestBody(required = false) String data,
|
|
|
|
|
@RequestParam(name = "file", required = false) MultipartFile file) {
|
|
|
|
|
try {
|
|
|
|
|
String beanClassName = (ActionConfig.AJAX_PREFIX + module + "/" + target).toLowerCase();
|
|
|
|
|
Object ajax = LocalData.getApplicationContext().getBean(beanClassName);
|
|
|
|
|
|
|
|
|
|
//========
|
|
|
|
|
//处理文件
|
|
|
|
|
//========
|
|
|
|
|
fileUploadResponse.setId(1L);
|
|
|
|
|
fileUploadResponse.setUrl("example.com\\img\\1.jpg");
|
|
|
|
|
fileUploadResponse.setDownloadUrl("example.com\\img\\1.jpg");
|
|
|
|
|
Class ajaxClass = ajax.getClass();
|
|
|
|
|
|
|
|
|
|
Method[] methods = ajaxClass.getDeclaredMethods();
|
|
|
|
|
|
|
|
|
|
if (target != null) {
|
|
|
|
|
fileUploadResponse.addError(ErrorType.BUSINESS_ERROR, "文件上传成功,但未处理文件[" + fileName + "]!");
|
|
|
|
|
} else {
|
|
|
|
|
fileUploadResponse.addError(ErrorType.BUSINESS_ERROR, "文件上传失败!");
|
|
|
|
|
Method methodC = null;
|
|
|
|
|
for (Method meth : methods) {
|
|
|
|
|
if (meth.getName().equals(method)) {
|
|
|
|
|
methodC = meth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileUploadResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (methodC == null) {
|
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "未找到对应的方法!");
|
|
|
|
|
return baseResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/ajax/{module}/{target}/{method}")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public BaseResponse ajax(@PathVariable String module, @PathVariable String target, @PathVariable String method, @RequestBody String param) {
|
|
|
|
|
BaseResponse baseResponse = null;
|
|
|
|
|
Parameter[] parameters = methodC.getParameters();
|
|
|
|
|
Object[] arg = new Object[parameters.length];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
String beanClassName = (ActionConfig.AJAX_PREFIX + module + "/" + target).toLowerCase();
|
|
|
|
|
Object ajax = LocalData.getApplicationContext().getBean(beanClassName);
|
|
|
|
|
|
|
|
|
|
Class ajaxClass = ajax.getClass();
|
|
|
|
|
Method methodC = ajaxClass.getMethod(method, String.class);
|
|
|
|
|
Object invoke = methodC.invoke(ajax, param);
|
|
|
|
|
if (invoke instanceof BaseResponse) {
|
|
|
|
|
baseResponse = (BaseResponse) invoke;
|
|
|
|
|
} else {
|
|
|
|
|
baseResponse = new BaseResponse();
|
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "方法返回值错误!");
|
|
|
|
|
for (int i = 0; i < parameters.length; i++) {
|
|
|
|
|
Parameter parameter = parameters[i];
|
|
|
|
|
if (parameter.getType() == HttpServletRequest.class) {
|
|
|
|
|
arg[i] = request;
|
|
|
|
|
} else if (parameter.getType() == HttpServletResponse.class) {
|
|
|
|
|
arg[i] = response;
|
|
|
|
|
} else if (parameter.getType() == TreeNode.class) {
|
|
|
|
|
arg[i] = MapperUtil.toTree(data);
|
|
|
|
|
} else if (parameter.getType() == String.class) {
|
|
|
|
|
arg[i] = data;
|
|
|
|
|
} else if (parameter.getType() == MultipartFile.class) {
|
|
|
|
|
arg[i] = file;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return methodC.invoke(ajax, arg);
|
|
|
|
|
} catch (BeansException e) {
|
|
|
|
|
baseResponse = new BaseResponse();
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "未找到对应的目标!");
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
baseResponse = new BaseResponse();
|
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "未找到对应的方法!");
|
|
|
|
|
return baseResponse;
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
baseResponse = new BaseResponse();
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "方法执必须公开!");
|
|
|
|
|
return baseResponse;
|
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
|
baseResponse = new BaseResponse();
|
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "方法执行错误!");
|
|
|
|
|
}
|
|
|
|
|
e.getTargetException().printStackTrace();
|
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "方法执行错误[" + e.getTargetException().getMessage() + "]");
|
|
|
|
|
return baseResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/download")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public ResponseEntity<byte[]> download(@RequestParam(value = "file", required = false) String file) throws IOException {
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
|
|
|
|
|
|
|
//========
|
|
|
|
|
//下载DEMO
|
|
|
|
|
//========
|
|
|
|
|
if (file == null) {
|
|
|
|
|
file = "test.txt";
|
|
|
|
|
headers.setContentDispositionFormData("attachment", new String(file.getBytes("UTF-8"), "iso-8859-1"));
|
|
|
|
|
return new ResponseEntity<byte[]>("test".getBytes(),
|
|
|
|
|
headers, HttpStatus.CREATED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ResponseEntity<byte[]>(FileUtil.readFileToByteArray(new File(file)),
|
|
|
|
|
headers, HttpStatus.CREATED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ConcurrentHashMap<String, SseEmitter> sseMap = new ConcurrentHashMap();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -338,3 +318,4 @@ public class GlobalController implements ErrorController {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|