0 Star 24 Fork 10

夏哥 / logserver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
LajaxHandler.java 6.52 KB
一键复制 编辑 原始数据 按行查看 历史
xlongwei 提交于 2021-11-09 19:17 . lajax
package com.xlongwei.logserver;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpRequest;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.networknt.body.BodyHandler;
import com.networknt.config.Config;
import com.networknt.handler.LightHttpHandler;
import com.networknt.utility.StringUtils;
import org.apache.commons.codec.CharEncoding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HeaderMap;
import io.undertow.util.HttpString;
import io.undertow.util.Methods;
public class LajaxHandler implements LightHttpHandler {
public static final HttpString ORIGIN = new HttpString("Origin");
public static final HttpString ACCESS_CONTROL_REQUEST_METHOD = new HttpString("Access-Control-Request-Method");
public static final HttpString ACCESS_CONTROL_REQUEST_HEADERS = new HttpString("Access-Control-Request-Headers");
public static final HttpString ACCESS_CONTROL_ALLOW_ORIGIN = new HttpString("Access-Control-Allow-Origin");
public static final HttpString ACCESS_CONTROL_ALLOW_METHODS = new HttpString("Access-Control-Allow-Methods");
public static final HttpString ACCESS_CONTROL_ALLOW_HEADERS = new HttpString("Access-Control-Allow-Headers");
private static final Logger log = LoggerFactory.getLogger("lajax");
public static final String token = System.getProperty("lajax.token");
public static final String apiUrl = System.getProperty("apiUrl", "https://api.xlongwei.com");
private static final String LAJAX_LOG_FORMAT = "{} {} {} {}: {}, url={}, agent={}";
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void handleRequest(HttpServerExchange exchange) throws Exception {
HeaderMap requestHeaders = exchange.getRequestHeaders();
if(isCoreRequest(requestHeaders)) {
HeaderMap responseHeaders = exchange.getResponseHeaders();
responseHeaders.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
responseHeaders.add(ACCESS_CONTROL_ALLOW_METHODS, "POST");
responseHeaders.add(ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type");
responseHeaders.add(ACCESS_CONTROL_ALLOW_HEADERS, "X-Request-Token");
if(Methods.OPTIONS.equals(exchange.getRequestMethod())) {
exchange.setStatusCode(HttpURLConnection.HTTP_OK);
return;
}
}
Object body = exchange.getAttachment(BodyHandler.REQUEST_BODY);
if (body == null || body instanceof Map) {
if (body != null) {
Map map = (Map) body;
body = map.get("commonAnnotations");
if (body != null && body instanceof Map) {
log.info(Config.getInstance().getMapper()
.writeValueAsString(exchange.getAttachment(BodyHandler.REQUEST_BODY)));
map = (Map) body;
String description = map.getOrDefault("description", "").toString();
if (StringUtils.isNotBlank(description)) {
log.warn(description);
try {
String email = PageHandler.getParam(exchange, "email");
String openid = PageHandler.getParam(exchange, "openid");
HttpRequest request = null;
map = new HashMap<>();
if (StringUtils.isBlank(email) && StringUtils.isBlank(openid)) {
request = new HttpRequest(apiUrl + "/service/weixin/notify");
map.put("openid", "oNlp_wiCfSmCEnpYqHoroD8t07t4");
map.put("text", description);
String bodyString = Config.getInstance().getMapper().writeValueAsString(map);
request.setSysMethod(MethodType.POST);
request.setHttpContent(bodyString.getBytes(CharEncoding.UTF_8), CharEncoding.UTF_8,
FormatType.JSON);
HttpResponse response = PageHandler.httpClient.syncInvoke(request);
log.info(response.getHttpContentString());
} else {
if (StringUtils.isNotBlank(email)) {
request = new HttpRequest(apiUrl + "/service/checkcode/email");
map.put("toEmail", email);
map.put("showapi_userName", "prometheus");
map.put("title", map.getOrDefault("summary", "").toString());
map.put("checkcode", description);
String bodyString = Config.getInstance().getMapper().writeValueAsString(map);
request.setSysMethod(MethodType.POST);
request.setHttpContent(bodyString.getBytes(CharEncoding.UTF_8), CharEncoding.UTF_8,
FormatType.JSON);
HttpResponse response = PageHandler.httpClient.syncInvoke(request);
log.info(response.getHttpContentString());
}
if (StringUtils.isNotBlank(openid)) {
request = new HttpRequest(apiUrl + "/service/weixin/notify");
map.put("openid", openid);
map.put("text", description);
String bodyString = Config.getInstance().getMapper().writeValueAsString(map);
request.setSysMethod(MethodType.POST);
request.setHttpContent(bodyString.getBytes(CharEncoding.UTF_8), CharEncoding.UTF_8,
FormatType.JSON);
HttpResponse response = PageHandler.httpClient.syncInvoke(request);
log.info(response.getHttpContentString());
}
}
} catch (Exception e) {
log.warn(e.getMessage());
}
}
}
}
} else {
//[{time,level,messages:["{reqId}",arg1,...args],url,agent}]
try {
String token = exchange.getRequestHeaders().getFirst("X-Request-Token");
final String contextName = StringUtils.isBlank(token) ? "lajax" : token;
((List)body).forEach(item -> {
Map map = (Map)item;
String level = ((String)(map).get("level")).toUpperCase();
List list = (List)map.get("messages");
String reqId = list.get(0).toString();
String message = list.get(1).toString();
reqId = reqId.substring(1, reqId.length()-1);
Object time = map.get("time");
Object url = map.get("url");
Object agent = map.get("agent");
if("info".equals(level)) {
log.info(LAJAX_LOG_FORMAT, contextName, time, level, reqId, message, url, agent);
}else if("warn".equals(level)) {
log.warn(LAJAX_LOG_FORMAT, contextName, time, level, reqId, message, url, agent);
}else {
log.error(LAJAX_LOG_FORMAT, contextName, time, level, reqId, message, url, agent);
}
});
}catch(Exception e) {
log.debug("lajax fail={}, msg={}, body={}", e.getClass().getSimpleName(), e.getMessage(), body);
}
}
exchange.setStatusCode(HttpURLConnection.HTTP_OK);
}
public static boolean isCoreRequest(HeaderMap headers) {
return headers.contains(ORIGIN)
|| headers.contains(ACCESS_CONTROL_REQUEST_HEADERS)
|| headers.contains(ACCESS_CONTROL_REQUEST_METHOD);
}
}
Java
1
https://gitee.com/xlongwei/logserver.git
git@gitee.com:xlongwei/logserver.git
xlongwei
logserver
logserver
master

搜索帮助