1
0
mirror of synced 2025-12-16 20:28:11 +08:00
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 WxCpMessageHandler {
*
* @param wxMessage
* @param context 上下文如果handler或interceptor之间有信息要传递可以用这个
* @param wxCpService
* @return xml格式的消息如果在异步规则里处理的话可以返回null
*/
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context);
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService);
}

View File

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

View File

@@ -45,12 +45,18 @@ public class WxCpMessageRouter {
private final ExecutorService es = Executors.newCachedThreadPool();
private final WxCpService wxCpService;
public WxCpMessageRouter(WxCpService wxCpService) {
this.wxCpService = wxCpService;
}
/**
* 开始一个新的Route规则
* @return
*/
public Rule rule() {
return new Rule(this);
return new Rule(this, wxCpService);
}
/**
@@ -101,6 +107,8 @@ public class WxCpMessageRouter {
private final WxCpMessageRouter routerBuilder;
private final WxCpService wxCpService;
private boolean async = true;
private String msgType;
@@ -121,8 +129,9 @@ public class WxCpMessageRouter {
private List<WxCpMessageInterceptor> interceptors = new ArrayList<WxCpMessageInterceptor>();
protected Rule(WxCpMessageRouter routerBuilder) {
protected Rule(WxCpMessageRouter routerBuilder, WxCpService wxCpService) {
this.routerBuilder = routerBuilder;
this.wxCpService = wxCpService;
}
/**
@@ -288,7 +297,7 @@ public class WxCpMessageRouter {
Map<String, Object> context = new HashMap<String, Object>();
// 如果拦截器不通过
for (WxCpMessageInterceptor interceptor : this.interceptors) {
if (!interceptor.intercept(wxMessage, context)) {
if (!interceptor.intercept(wxMessage, context, wxCpService)) {
return null;
}
}
@@ -297,7 +306,7 @@ public class WxCpMessageRouter {
WxCpXmlOutMessage res = null;
for (WxCpMessageHandler handler : this.handlers) {
// 返回最后handler的结果
res = handler.handle(wxMessage, context);
res = handler.handle(wxMessage, context, wxCpService);
}
return res;
}

View File

@@ -1,25 +1,27 @@
package me.chanjar.weixin.cp.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import me.chanjar.weixin.common.bean.WxAccessToken;
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.util.json.GsonHelper;
import me.chanjar.weixin.cp.bean.*;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
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.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -33,20 +35,13 @@ import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import com.google.gson.JsonElement;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
public class WxCpServiceImpl implements WxCpService {

View File

@@ -8,17 +8,12 @@
*/
package me.chanjar.weixin.cp.util.json;
import java.lang.reflect.Type;
import com.google.gson.*;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Type;
/**
*

View File

@@ -1,6 +1,6 @@
package me.chanjar.weixin.cp.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 WxCpMessageRouterTest {
@Test(dataProvider="messages-1")
public void testSync(WxCpXmlMessage message, String expected) {
StringBuffer sb = new StringBuffer();
WxCpMessageRouter router = new WxCpMessageRouter();
WxCpMessageRouter router = new WxCpMessageRouter(null);
prepare(false, sb, router);
router.route(message);
Assert.assertEquals(sb.toString(), expected);
@@ -56,7 +56,7 @@ public class WxCpMessageRouterTest {
@Test(dataProvider="messages-1")
public void testAsync(WxCpXmlMessage message, String expected) throws InterruptedException {
StringBuffer sb = new StringBuffer();
WxCpMessageRouter router = new WxCpMessageRouter();
WxCpMessageRouter router = new WxCpMessageRouter(null);
prepare(true, sb, router);
router.route(message);
Thread.sleep(500l);
@@ -64,10 +64,10 @@ public class WxCpMessageRouterTest {
}
public void testConcurrency() throws InterruptedException {
final WxCpMessageRouter router = new WxCpMessageRouter();
final WxCpMessageRouter router = new WxCpMessageRouter(null);
router.rule().handler(new WxCpMessageHandler() {
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context) {
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService) {
return null;
}
}).end();
@@ -149,7 +149,7 @@ public class WxCpMessageRouterTest {
}
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context) {
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService) {
sb.append(this.echoStr).append(',');
return null;
}

View File

@@ -1,11 +1,11 @@
package me.chanjar.weixin.cp.demo;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.cp.api.*;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -21,8 +21,8 @@ import java.util.Map;
*/
public class WxCpDemoServlet extends HttpServlet {
protected WxCpService wxCpService;
protected WxCpConfigStorage wxCpConfigStorage;
protected WxCpService wxCpService;
protected WxCpMessageRouter wxCpMessageRouter;
@Override public void init() throws ServletException {
@@ -37,7 +37,7 @@ public class WxCpDemoServlet extends HttpServlet {
wxCpService.setWxCpConfigStorage(config);
WxCpMessageHandler handler = new WxCpMessageHandler() {
@Override public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context) {
@Override public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService) {
WxCpXmlOutTextMessage m = WxCpXmlOutMessage
.TEXT()
.content("测试加密消息")
@@ -48,7 +48,7 @@ public class WxCpDemoServlet extends HttpServlet {
}
};
wxCpMessageRouter = new WxCpMessageRouter();
wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
wxCpMessageRouter
.rule()
.async(false)
@@ -64,13 +64,14 @@ public class WxCpDemoServlet 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 msgSignature = request.getParameter("msg_signature");
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
String echostr = request.getParameter("echostr");
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
if (StringUtils.isNotBlank(echostr)) {
if (!wxCpService.checkSignature(msgSignature, timestamp, nonce, echostr)) {
// 消息签名不正确,说明不是公众平台发过来的消息
@@ -84,11 +85,8 @@ public class WxCpDemoServlet extends HttpServlet {
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));
}