Search in sources :

Example 1 with WxCpXmlOutMessage

use of me.chanjar.weixin.cp.bean.WxCpXmlOutMessage in project weixin-java-tools by chanjarster.

the class WxCpMessageRouter method route.

/**
   * 处理微信消息
   * @param wxMessage
   */
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
    if (isDuplicateMessage(wxMessage)) {
        // 如果是重复消息,那么就不做处理
        return null;
    }
    final List<WxCpMessageRouterRule> matchRules = new ArrayList<WxCpMessageRouterRule>();
    // 收集匹配的规则
    for (final WxCpMessageRouterRule rule : rules) {
        if (rule.test(wxMessage)) {
            matchRules.add(rule);
            if (!rule.isReEnter()) {
                break;
            }
        }
    }
    if (matchRules.size() == 0) {
        return null;
    }
    WxCpXmlOutMessage res = null;
    final List<Future> futures = new ArrayList<Future>();
    for (final WxCpMessageRouterRule rule : matchRules) {
        // 返回最后一个非异步的rule的执行结果
        if (rule.isAsync()) {
            futures.add(executorService.submit(new Runnable() {

                public void run() {
                    rule.service(wxMessage, wxCpService, sessionManager, exceptionHandler);
                }
            }));
        } else {
            res = rule.service(wxMessage, wxCpService, 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;
}
Also used : WxCpXmlOutMessage(me.chanjar.weixin.cp.bean.WxCpXmlOutMessage) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with WxCpXmlOutMessage

use of me.chanjar.weixin.cp.bean.WxCpXmlOutMessage in project weixin-java-tools by chanjarster.

the class WxCpMessageRouterRule method service.

/**
   * 处理微信推送过来的消息
   *
   * @param wxMessage
   * @return true 代表继续执行别的router,false 代表停止执行别的router
   */
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage, WxCpService wxCpService, WxSessionManager sessionManager, WxErrorExceptionHandler exceptionHandler) {
    try {
        Map<String, Object> context = new HashMap<String, Object>();
        // 如果拦截器不通过
        for (WxCpMessageInterceptor interceptor : this.interceptors) {
            if (!interceptor.intercept(wxMessage, context, wxCpService, sessionManager)) {
                return null;
            }
        }
        // 交给handler处理
        WxCpXmlOutMessage res = null;
        for (WxCpMessageHandler handler : this.handlers) {
            // 返回最后handler的结果
            res = handler.handle(wxMessage, context, wxCpService, sessionManager);
        }
        return res;
    } catch (WxErrorException e) {
        exceptionHandler.handle(e);
    }
    return null;
}
Also used : HashMap(java.util.HashMap) WxCpXmlOutMessage(me.chanjar.weixin.cp.bean.WxCpXmlOutMessage) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 3 with WxCpXmlOutMessage

use of me.chanjar.weixin.cp.bean.WxCpXmlOutMessage in project weixin-java-tools by chanjarster.

the class WxCpEndpointServlet 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 msgSignature = request.getParameter("msg_signature");
    String nonce = request.getParameter("nonce");
    String timestamp = request.getParameter("timestamp");
    String echostr = request.getParameter("echostr");
    if (StringUtils.isNotBlank(echostr)) {
        if (!wxCpService.checkSignature(msgSignature, timestamp, nonce, echostr)) {
            // 消息签名不正确,说明不是公众平台发过来的消息
            response.getWriter().println("非法请求");
            return;
        }
        WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
        String plainText = cryptUtil.decrypt(echostr);
        // 说明是一个仅仅用来验证的请求,回显echostr
        response.getWriter().println(plainText);
        return;
    }
    WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(request.getInputStream(), wxCpConfigStorage, timestamp, nonce, msgSignature);
    WxCpXmlOutMessage outMessage = wxCpMessageRouter.route(inMessage);
    if (outMessage != null) {
        response.getWriter().write(outMessage.toEncryptedXml(wxCpConfigStorage));
    }
    return;
}
Also used : WxCpCryptUtil(me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil) WxCpXmlMessage(me.chanjar.weixin.cp.bean.WxCpXmlMessage) WxCpXmlOutMessage(me.chanjar.weixin.cp.bean.WxCpXmlOutMessage)

Aggregations

WxCpXmlOutMessage (me.chanjar.weixin.cp.bean.WxCpXmlOutMessage)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)1 WxCpXmlMessage (me.chanjar.weixin.cp.bean.WxCpXmlMessage)1 WxCpCryptUtil (me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil)1