|
|
|
@ -32,6 +32,7 @@ public class RouterHandler extends Handler {
|
|
|
|
|
static final String HEADER_COOKIE = "Cookie";
|
|
|
|
|
static final String HEADER_ACCEPT = "Accept";
|
|
|
|
|
static final String HEADER_CONTENT_TYPE = "Content-Type";
|
|
|
|
|
static final String ERROR_MESSAGE = "服务繁忙,请稍后重试";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 静态资源扩展名
|
|
|
|
@ -81,7 +82,9 @@ public class RouterHandler extends Handler {
|
|
|
|
|
// 获取转发URL
|
|
|
|
|
String forwardUrl = getForwardUrl(servletPath);
|
|
|
|
|
if (forwardUrl == null) {
|
|
|
|
|
handleError(res, isHandled, "无效的请求路径", null);
|
|
|
|
|
logger.error("无效的请求路径: {}", servletPath);
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -101,11 +104,14 @@ public class RouterHandler extends Handler {
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
handleError(res, isHandled, "不支持的请求方法: " + req.getMethod(), null);
|
|
|
|
|
logger.warn("不支持的请求方法: {}", req.getMethod());
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求处理异常", e);
|
|
|
|
|
handleError(res, isHandled, "系统异常", e);
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -147,7 +153,9 @@ public class RouterHandler extends Handler {
|
|
|
|
|
executeRequest(request, res);
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
handleError(res, isHandled, "GET请求处理失败", e);
|
|
|
|
|
logger.error("GET请求处理失败: {}", forwardUrl, e);
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -159,7 +167,9 @@ public class RouterHandler extends Handler {
|
|
|
|
|
executeRequest(request, res);
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
handleError(res, isHandled, "POST请求处理失败", e);
|
|
|
|
|
logger.error("POST请求处理失败: {}", forwardUrl, e);
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -171,7 +181,9 @@ public class RouterHandler extends Handler {
|
|
|
|
|
List<UploadFile> files = mp.getFiles();
|
|
|
|
|
|
|
|
|
|
if (files.isEmpty()) {
|
|
|
|
|
handleError(res, isHandled, "没有上传文件", null);
|
|
|
|
|
logger.error("文件上传失败: 未找到上传文件");
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -179,7 +191,9 @@ public class RouterHandler extends Handler {
|
|
|
|
|
executeRequest(request, res);
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
handleError(res, isHandled, "文件上传失败", e);
|
|
|
|
|
logger.error("文件上传异常: {}", forwardUrl, e);
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -267,6 +281,7 @@ public class RouterHandler extends Handler {
|
|
|
|
|
|
|
|
|
|
// 执行请求
|
|
|
|
|
private void executeRequest(Request request, HttpServletResponse res) throws IOException {
|
|
|
|
|
try {
|
|
|
|
|
Response response = OK_HTTP_CLIENT.newCall(request).execute();
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
|
ResponseBody body = response.body();
|
|
|
|
@ -278,10 +293,16 @@ public class RouterHandler extends Handler {
|
|
|
|
|
handleTextResponse(res, body);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, "响应体为空").toString());
|
|
|
|
|
logger.error("响应体为空: {}", request.url());
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, "请求失败!").toString());
|
|
|
|
|
logger.error("请求失败: {} - {}", request.url(), response.code());
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("请求执行异常: {}", request.url(), e);
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -315,17 +336,11 @@ public class RouterHandler extends Handler {
|
|
|
|
|
res.getWriter().println(body);
|
|
|
|
|
res.getWriter().flush();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error("响应写入异常", e);
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理错误
|
|
|
|
|
private void handleError(HttpServletResponse res, boolean[] isHandled, String message, Exception e) {
|
|
|
|
|
String errorMessage = e != null ? message + ": " + e.getMessage() : message;
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, errorMessage).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查认证
|
|
|
|
|
private boolean isAuthenticated(HttpServletRequest req, HttpServletResponse res, boolean[] isHandled) {
|
|
|
|
|
String servletPath = req.getServletPath();
|
|
|
|
@ -348,7 +363,9 @@ public class RouterHandler extends Handler {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleError(res, isHandled, "登录已过期,请重新登录!", null);
|
|
|
|
|
logger.warn("认证失败: {}", servletPath);
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -394,7 +411,9 @@ public class RouterHandler extends Handler {
|
|
|
|
|
// 检查是否为内部调用
|
|
|
|
|
private boolean isInternalCall(String servletPath, HttpServletResponse res, boolean[] isHandled) {
|
|
|
|
|
if (servletPath.endsWith("/internal/")) {
|
|
|
|
|
handleError(res, isHandled, "微服务间内部接口调用,是不需要走网关的!", null);
|
|
|
|
|
logger.warn("内部接口调用: {}", servletPath);
|
|
|
|
|
renderJson(res, new RetBean(RetBean.ERROR, Constants.ERROR_MESSAGE).toString());
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
@ -413,6 +432,6 @@ public class RouterHandler extends Handler {
|
|
|
|
|
|
|
|
|
|
// 记录请求日志
|
|
|
|
|
private void logRequest(HttpServletRequest req, String forwardUrl) {
|
|
|
|
|
logger.info("Request: {} {} -> {}", req.getMethod(), req.getRequestURI(), forwardUrl);
|
|
|
|
|
logger.info("请求转发: {} {} -> {}", req.getMethod(), req.getRequestURI(), forwardUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|