refactor
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -15,8 +15,9 @@ public interface WxCpMessageInterceptor {
|
||||
* 拦截微信消息
|
||||
* @param wxMessage
|
||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||
* @param wxCpService
|
||||
* @return true代表OK,false代表不OK
|
||||
*/
|
||||
public boolean intercept(WxCpXmlMessage wxMessage, Map<String, Object> context);
|
||||
public boolean intercept(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService);
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user