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.

65 lines
2.1 KiB

package com.fr.plugin.xx.zyjn.handler.action;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.general.IOUtils;
import com.fr.json.JSONObject;
import com.fr.plugin.xx.zyjn.utils.LogUtils;
import com.fr.stable.StringUtils;
import com.fr.third.org.apache.commons.codec.digest.DigestUtils;
import com.fr.web.utils.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author xx
* @Date 2022/10/27
* @Description
**/
public abstract class BaseHandler extends BaseHttpHandler {
private final static String PREFIX = "jynsh";
@Override
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception {
String tokenId = req.getHeader("tokenId");
String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
if (!StringUtils.equals(DigestUtils.md5Hex((PREFIX + format).getBytes(StandardCharsets.UTF_8)).toLowerCase(), tokenId)) {
setError(res, "报文认证不通过");
return;
}
String body = IOUtils.inputStream2String(req.getInputStream());
if (StringUtils.isBlank(body)) {
setError(res, "请求内容不能为空");
return;
}
JSONObject object = null;
try {
object = new JSONObject(body);
} catch (Exception e) {
setError(res, "报文格式非json");
}
if (!object.has("sn")) {
setError(res, "请求sn不能为空");
return;
}
LogUtils.debug4plugin("sync url is {} body is {}", req.getRequestURI(), body);
sync(res, object);
}
abstract void sync(HttpServletResponse res, JSONObject entity) throws Exception;
protected void setError(HttpServletResponse res, String mess) {
try {
JSONObject error = JSONObject.create().put("code", 1).put("msg", mess);
WebUtils.printAsJSON(res, error);
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}
}
}