use of me.chanjar.weixin.mp.bean.WxMpXmlOutMessage in project weixin-java-tools by chanjarster.
the class WxMpEndpointServlet method service.
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
String signature = request.getParameter("signature");
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
// 消息签名不正确,说明不是公众平台发过来的消息
response.getWriter().println("非法请求");
return;
}
String echostr = request.getParameter("echostr");
if (StringUtils.isNotBlank(echostr)) {
// 说明是一个仅仅用来验证的请求,回显echostr
response.getWriter().println(echostr);
return;
}
String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw" : request.getParameter("encrypt_type");
if ("raw".equals(encryptType)) {
// 明文传输的消息
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
if (outMessage != null) {
response.getWriter().write(outMessage.toXml());
}
return;
}
if ("aes".equals(encryptType)) {
// 是aes加密的消息
String msgSignature = request.getParameter("msg_signature");
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), wxMpConfigStorage, timestamp, nonce, msgSignature);
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
response.getWriter().write(outMessage.toEncryptedXml(wxMpConfigStorage));
return;
}
response.getWriter().println("不可识别的加密类型");
return;
}
use of me.chanjar.weixin.mp.bean.WxMpXmlOutMessage in project weixin-java-tools by chanjarster.
the class WxMpMessageRouterRule method service.
/**
* 处理微信推送过来的消息
*
* @param wxMessage
* @return true 代表继续执行别的router,false 代表停止执行别的router
*/
protected WxMpXmlOutMessage service(WxMpXmlMessage wxMessage, WxMpService wxMpService, WxSessionManager sessionManager, WxErrorExceptionHandler exceptionHandler) {
try {
Map<String, Object> context = new HashMap<String, Object>();
// 如果拦截器不通过
for (WxMpMessageInterceptor interceptor : this.interceptors) {
if (!interceptor.intercept(wxMessage, context, wxMpService, sessionManager)) {
return null;
}
}
// 交给handler处理
WxMpXmlOutMessage res = null;
for (WxMpMessageHandler handler : this.handlers) {
// 返回最后handler的结果
res = handler.handle(wxMessage, context, wxMpService, sessionManager);
}
return res;
} catch (WxErrorException e) {
exceptionHandler.handle(e);
}
return null;
}
use of me.chanjar.weixin.mp.bean.WxMpXmlOutMessage in project weixin-java-tools by chanjarster.
the class WxMpMessageRouter method route.
/**
* 处理微信消息
* @param wxMessage
*/
public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage) {
if (isDuplicateMessage(wxMessage)) {
// 如果是重复消息,那么就不做处理
return null;
}
final List<WxMpMessageRouterRule> matchRules = new ArrayList<WxMpMessageRouterRule>();
// 收集匹配的规则
for (final WxMpMessageRouterRule rule : rules) {
if (rule.test(wxMessage)) {
matchRules.add(rule);
if (!rule.isReEnter()) {
break;
}
}
}
if (matchRules.size() == 0) {
return null;
}
WxMpXmlOutMessage res = null;
final List<Future> futures = new ArrayList<Future>();
for (final WxMpMessageRouterRule rule : matchRules) {
// 返回最后一个非异步的rule的执行结果
if (rule.isAsync()) {
futures.add(executorService.submit(new Runnable() {
public void run() {
rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler);
}
}));
} else {
res = rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler);
// 在同步操作结束,session访问结束
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
sessionEndAccess(wxMessage);
}
}
if (futures.size() > 0) {
executorService.submit(new Runnable() {
@Override
public void run() {
for (Future future : futures) {
try {
future.get();
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
// 异步操作结束,session访问结束
sessionEndAccess(wxMessage);
} catch (InterruptedException e) {
log.error("Error happened when wait task finish", e);
} catch (ExecutionException e) {
log.error("Error happened when wait task finish", e);
}
}
}
});
}
return res;
}
Aggregations