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.

100 lines
4.2 KiB

10 months ago
package com.dsideal.gw.Handler;
import cn.hutool.json.JSONObject;
10 months ago
import com.dsideal.gw.GwApplication;
10 months ago
import com.jfinal.handler.Handler;
10 months ago
import okhttp3.*;
10 months ago
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RouterHandler extends Handler {
10 months ago
/**
* JSON
10 months ago
*
10 months ago
* @param res
* @param jo
*/
public void renderJson(HttpServletResponse res, JSONObject jo) {
10 months ago
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Cache-Control", "no-cache");
res.setCharacterEncoding("UTF-8");
res.setContentType("application/json");
try {
res.getWriter().println(jo);
res.getWriter().flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
10 months ago
}
10 months ago
10 months ago
public void renderJson(HttpServletResponse res, String msg) {
renderJson(res, new JSONObject().put("msg", msg));
}
10 months ago
//OkHttp的实例单例模式
private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient();
//OkHttp的回调函数同步执行
private void executeRequest(Request request, HttpServletResponse res) throws IOException {
Response response = OK_HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful()) {
String responseBody = response.body().string();
renderJson(res, responseBody);
} else {
renderJson(res, "请求失败!");
}
}
10 months ago
@Override
public void handle(String target, HttpServletRequest req, HttpServletResponse res, boolean[] isHandled) {
//可以正确获取到URL的完整路径
String servletPath = req.getServletPath();
10 months ago
String queryString = req.getQueryString();
//路由到哪个微服务
int firstSlashIndex = servletPath.indexOf('/'); // 找到第一个 '/' 的索引
int secondSlashIndex = servletPath.indexOf('/', firstSlashIndex + 1); // 从第一个 '/' 之后开始找第二个 '/' 的索引
10 months ago
10 months ago
if (firstSlashIndex != -1 && secondSlashIndex != -1) {
String prefix = servletPath.substring(firstSlashIndex + 1, secondSlashIndex); // 截取两个 '/' 之间的内容
if (!GwApplication.routeDict.containsKey(prefix)) {
renderJson(res, prefix + "前缀没有找到合适的路由表,请检查是否请求正确!");
} else {
//路由到哪个微服务
String FORWARD_URL = GwApplication.routeDict.get(prefix);
//处理GET请求
if (req.getMethod().equals("GET")) {
//参数:queryString
Request request;
if (queryString != null) {
request = new Request.Builder().url(FORWARD_URL + "?" + queryString).get().build();
} else {
request = new Request.Builder().url(FORWARD_URL).get().build();
}
try {
executeRequest(request, res);
} catch (IOException e) {
renderJson(res, "请求失败!"+ e);
}
}//处理POST请求
else if (req.getMethod().equals("POST")) {
// RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json; charset=utf-8"));
// Request request = new Request.Builder().url(FORWARD_URL).post(body).build();
} else {
// MinIO - 服务端签名直传(前端 + 后端 + 效果演示)
//https://blog.csdn.net/CYK_byte/article/details/140254412
renderJson(res, "系统只支持GET,POST其它的OPTIONS不支持文件上传请使用S3协议进行直传不通过JAVA处理JAVA只处理授权");
}
}
10 months ago
} else {
10 months ago
renderJson(res, "输入的字符串格式不正确,没有找到两个 '/'。");
10 months ago
}
//停止filter
10 months ago
isHandled[0] = true;
}
}