This commit is contained in:
Daniel Qian
2014-11-06 13:47:25 +08:00
parent d4c25ceb38
commit 89aefa45d0
18 changed files with 202 additions and 108 deletions

View File

@ -16,8 +16,9 @@ public interface WxMpMessageHandler {
*
* @param wxMessage
* @param context 上下文如果handler或interceptor之间有信息要传递可以用这个
* @param wxMpService
* @return xml格式的消息如果在异步规则里处理的话可以返回null
*/
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context);
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService);
}

View File

@ -15,8 +15,9 @@ public interface WxMpMessageInterceptor {
* 拦截微信消息
* @param wxMessage
* @param context 上下文如果handler或interceptor之间有信息要传递可以用这个
* @param wxMpService
* @return true代表OKfalse代表不OK
*/
public boolean intercept(WxMpXmlMessage wxMessage, Map<String, Object> context);
public boolean intercept(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService);
}

View File

@ -43,14 +43,20 @@ public class WxMpMessageRouter {
private final List<Rule> rules = new ArrayList<Rule>();
private final ExecutorService es = Executors.newCachedThreadPool();
private final ExecutorService executorService = Executors.newCachedThreadPool();
private final WxMpService wxMpService;
public WxMpMessageRouter(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
/**
* 开始一个新的Route规则
* @return
*/
public Rule rule() {
return new Rule(this);
return new Rule(this, wxMpService);
}
/**
@ -73,7 +79,7 @@ public class WxMpMessageRouter {
if (matchRules.get(0).async) {
// 只要第一个是异步的,那就异步执行
// 在另一个线程里执行
es.submit(new Runnable() {
executorService.submit(new Runnable() {
public void run() {
for (final Rule rule : matchRules) {
rule.service(wxMessage);
@ -101,6 +107,8 @@ public class WxMpMessageRouter {
private final WxMpMessageRouter routerBuilder;
private final WxMpService wxMpService;
private boolean async = true;
private String msgType;
@ -119,8 +127,9 @@ public class WxMpMessageRouter {
private List<WxMpMessageInterceptor> interceptors = new ArrayList<WxMpMessageInterceptor>();
protected Rule(WxMpMessageRouter routerBuilder) {
protected Rule(WxMpMessageRouter routerBuilder, WxMpService wxMpService) {
this.routerBuilder = routerBuilder;
this.wxMpService = wxMpService;
}
/**
@ -274,7 +283,7 @@ public class WxMpMessageRouter {
Map<String, Object> context = new HashMap<String, Object>();
// 如果拦截器不通过
for (WxMpMessageInterceptor interceptor : this.interceptors) {
if (!interceptor.intercept(wxMessage, context)) {
if (!interceptor.intercept(wxMessage, context, wxMpService)) {
return null;
}
}
@ -283,7 +292,7 @@ public class WxMpMessageRouter {
WxMpXmlOutMessage res = null;
for (WxMpMessageHandler handler : this.handlers) {
// 返回最后handler的结果
res = handler.handle(wxMessage, context);
res = handler.handle(wxMessage, context, wxMpService);
}
return res;
}

View File

@ -10,6 +10,7 @@ import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.*;
@ -18,7 +19,6 @@ import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
@ -27,7 +27,6 @@ import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
@ -38,7 +37,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

View File

@ -1,6 +1,6 @@
package me.chanjar.weixin.mp.api;
import org.apache.commons.lang3.StringUtils;
import me.chanjar.weixin.common.util.StringUtils;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

View File

@ -47,7 +47,7 @@ public class WxMpMessageRouterTest {
@Test(dataProvider="messages-1")
public void testSync(WxMpXmlMessage message, String expected) {
StringBuffer sb = new StringBuffer();
WxMpMessageRouter router = new WxMpMessageRouter();
WxMpMessageRouter router = new WxMpMessageRouter(null);
prepare(false, sb, router);
router.route(message);
Assert.assertEquals(sb.toString(), expected);
@ -56,7 +56,7 @@ public class WxMpMessageRouterTest {
@Test(dataProvider="messages-1")
public void testAsync(WxMpXmlMessage message, String expected) throws InterruptedException {
StringBuffer sb = new StringBuffer();
WxMpMessageRouter router = new WxMpMessageRouter();
WxMpMessageRouter router = new WxMpMessageRouter(null);
prepare(true, sb, router);
router.route(message);
Thread.sleep(500l);
@ -64,10 +64,10 @@ public class WxMpMessageRouterTest {
}
public void testConcurrency() throws InterruptedException {
final WxMpMessageRouter router = new WxMpMessageRouter();
final WxMpMessageRouter router = new WxMpMessageRouter(null);
router.rule().handler(new WxMpMessageHandler() {
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context) {
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService) {
return null;
}
}).end();
@ -149,7 +149,7 @@ public class WxMpMessageRouterTest {
}
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context) {
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService) {
sb.append(this.echoStr).append(',');
return null;
}

View File

@ -1,10 +1,10 @@
package me.chanjar.weixin.mp.demo;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@ -20,8 +20,8 @@ import java.util.Map;
*/
public class WxMpDemoServlet extends HttpServlet {
protected WxMpService wxMpService;
protected WxMpConfigStorage wxMpConfigStorage;
protected WxMpService wxMpService;
protected WxMpMessageRouter wxMpMessageRouter;
@Override public void init() throws ServletException {
@ -36,7 +36,7 @@ public class WxMpDemoServlet extends HttpServlet {
wxMpService.setWxMpConfigStorage(config);
WxMpMessageHandler handler = new WxMpMessageHandler() {
@Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context) {
@Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService) {
WxMpXmlOutTextMessage m
= WxMpXmlOutMessage.TEXT().content("测试加密消息").fromUser(wxMessage.getToUserName())
.toUser(wxMessage.getFromUserName()).build();
@ -44,7 +44,7 @@ public class WxMpDemoServlet extends HttpServlet {
}
};
wxMpMessageRouter = new WxMpMessageRouter();
wxMpMessageRouter = new WxMpMessageRouter(wxMpService);
wxMpMessageRouter
.rule()
.async(false)
@ -60,13 +60,13 @@ public class WxMpDemoServlet extends HttpServlet {
@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");
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
// 消息签名不正确,说明不是公众平台发过来的消息
response.getWriter().println("非法请求");
@ -84,31 +84,25 @@ public class WxMpDemoServlet extends HttpServlet {
"raw" :
request.getParameter("encrypt_type");
WxMpXmlMessage inMessage = null;
if ("raw".equals(encryptType)) {
// 明文传输的消息
inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
} else if ("aes".equals(encryptType)) {
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
response.getWriter().write(outMessage.toXml());
return;
}
if ("aes".equals(encryptType)) {
// 是aes加密的消息
String msgSignature = request.getParameter("msg_signature");
inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), wxMpConfigStorage, timestamp, nonce, msgSignature);
} else {
response.getWriter().println("不可识别的加密类型");
return;
}
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
if (outMessage != null) {
if ("raw".equals(encryptType)) {
response.getWriter().write(outMessage.toXml());
} else if ("aes".equals(encryptType)) {
response.getWriter().write(outMessage.toEncryptedXml(wxMpConfigStorage));
}
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;
}
}