Search in sources :

Example 1 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.WxMpXmlMessage in project weixin-java-tools by chanjarster.

the class WxMpMessageRouterTest method testSessionClean3.

@Test(dataProvider = "standardSessionManager")
public void testSessionClean3(StandardSessionManager ism) throws InterruptedException {
    // 2个异步请求,看是否处理完毕后会被清理掉
    final WxMpMessageRouter router = new WxMpMessageRouter(null);
    router.setSessionManager(ism);
    router.rule().async(true).handler(new WxSessionMessageHandler()).next().rule().async(true).handler(new WxSessionMessageHandler()).end();
    WxMpXmlMessage msg = new WxMpXmlMessage();
    msg.setFromUserName("abc");
    router.route(msg);
    Thread.sleep(2000l);
    Assert.assertEquals(ism.getActiveSessions(), 0);
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.WxMpXmlMessage) Test(org.testng.annotations.Test)

Example 2 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.WxMpXmlMessage in project weixin-java-tools by chanjarster.

the class WxMpMessageRouterTest method testConcurrency.

public void testConcurrency() throws InterruptedException {
    final WxMpMessageRouter router = new WxMpMessageRouter(null);
    router.rule().handler(new WxMpMessageHandler() {

        @Override
        public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
            return null;
        }
    }).end();
    final WxMpXmlMessage m = new WxMpXmlMessage();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            router.route(m);
            try {
                Thread.sleep(1000l);
            } catch (InterruptedException e) {
            }
        }
    };
    for (int i = 0; i < 10; i++) {
        new Thread(r).start();
    }
    Thread.sleep(1000l * 2);
}
Also used : WxSessionManager(me.chanjar.weixin.common.session.WxSessionManager) WxMpXmlMessage(me.chanjar.weixin.mp.bean.WxMpXmlMessage) Map(java.util.Map)

Example 3 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.WxMpXmlMessage in project weixin-java-tools by chanjarster.

the class WxMpMessageRouterTest method messages2.

@DataProvider(name = "messages-1")
public Object[][] messages2() {
    WxMpXmlMessage message1 = new WxMpXmlMessage();
    message1.setMsgType(WxConsts.XML_MSG_TEXT);
    WxMpXmlMessage message2 = new WxMpXmlMessage();
    message2.setEvent(WxConsts.EVT_CLICK);
    WxMpXmlMessage message3 = new WxMpXmlMessage();
    message3.setEventKey("KEY_1");
    WxMpXmlMessage message4 = new WxMpXmlMessage();
    message4.setContent("CONTENT_1");
    WxMpXmlMessage message5 = new WxMpXmlMessage();
    message5.setContent("BLA");
    WxMpXmlMessage message6 = new WxMpXmlMessage();
    message6.setContent("abcd");
    WxMpXmlMessage message7 = new WxMpXmlMessage();
    message7.setFormat("strangeformat");
    WxMpXmlMessage c2 = new WxMpXmlMessage();
    c2.setMsgType(WxConsts.XML_MSG_TEXT);
    c2.setEvent(WxConsts.EVT_CLICK);
    WxMpXmlMessage c3 = new WxMpXmlMessage();
    c3.setMsgType(WxConsts.XML_MSG_TEXT);
    c3.setEvent(WxConsts.EVT_CLICK);
    c3.setEventKey("KEY_1");
    WxMpXmlMessage c4 = new WxMpXmlMessage();
    c4.setMsgType(WxConsts.XML_MSG_TEXT);
    c4.setEvent(WxConsts.EVT_CLICK);
    c4.setEventKey("KEY_1");
    c4.setContent("CONTENT_1");
    return new Object[][] { new Object[] { message1, WxConsts.XML_MSG_TEXT + "," }, new Object[] { message2, WxConsts.EVT_CLICK + "," }, new Object[] { message3, "KEY_1," }, new Object[] { message4, "CONTENT_1," }, new Object[] { message5, "ALL," }, new Object[] { message6, "abcd," }, new Object[] { message7, "matcher," }, new Object[] { c2, "COMBINE_2," }, new Object[] { c3, "COMBINE_3," }, new Object[] { c4, "COMBINE_4," } };
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.WxMpXmlMessage) DataProvider(org.testng.annotations.DataProvider)

Example 4 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.WxMpXmlMessage 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;
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.WxMpXmlMessage) WxMpXmlOutMessage(me.chanjar.weixin.mp.bean.WxMpXmlOutMessage)

Example 5 with WxMpXmlMessage

use of me.chanjar.weixin.mp.bean.WxMpXmlMessage in project weixin-java-tools by chanjarster.

the class WxMpMessageRouterTest method testSessionClean1.

@Test(dataProvider = "standardSessionManager")
public void testSessionClean1(StandardSessionManager ism) throws InterruptedException {
    // 两个同步请求,看是否处理完毕后会被清理掉
    final WxMpMessageRouter router = new WxMpMessageRouter(null);
    router.setSessionManager(ism);
    router.rule().async(false).handler(new WxSessionMessageHandler()).next().rule().async(false).handler(new WxSessionMessageHandler()).end();
    WxMpXmlMessage msg = new WxMpXmlMessage();
    msg.setFromUserName("abc");
    router.route(msg);
    Thread.sleep(2000l);
    Assert.assertEquals(ism.getActiveSessions(), 0);
}
Also used : WxMpXmlMessage(me.chanjar.weixin.mp.bean.WxMpXmlMessage) Test(org.testng.annotations.Test)

Aggregations

WxMpXmlMessage (me.chanjar.weixin.mp.bean.WxMpXmlMessage)5 Test (org.testng.annotations.Test)2 Map (java.util.Map)1 WxSessionManager (me.chanjar.weixin.common.session.WxSessionManager)1 WxMpXmlOutMessage (me.chanjar.weixin.mp.bean.WxMpXmlOutMessage)1 DataProvider (org.testng.annotations.DataProvider)1