WeixinServerBootstrap类 新增 openAlwaysResponse方法

This commit is contained in:
jinyu 2015-08-03 18:23:33 +08:00
parent 64dc0bdc66
commit b857ec6b52
2 changed files with 21 additions and 26 deletions

View File

@ -30,7 +30,7 @@ import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor; import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
import com.foxinmy.weixin4j.request.WeixinMessage; import com.foxinmy.weixin4j.request.WeixinMessage;
import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse; import com.foxinmy.weixin4j.response.BlankResponse;
import com.foxinmy.weixin4j.response.WeixinResponse; import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.type.AccountType; import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.util.ClassUtil; import com.foxinmy.weixin4j.util.ClassUtil;
@ -90,11 +90,10 @@ public class WeixinMessageDispatcher {
* 消息转换 * 消息转换
*/ */
private Map<Class<? extends WeixinMessage>, Unmarshaller> messageUnmarshaller; private Map<Class<? extends WeixinMessage>, Unmarshaller> messageUnmarshaller;
/** /**
* 开启debug:未匹配到MessageHanlder输出消息信 * 是否总是响应请求,如未匹配到MessageHandler时回复空白消
*/ */
private boolean isDebug; private boolean alwaysResponse;
public WeixinMessageDispatcher() { public WeixinMessageDispatcher() {
this(new DefaultMessageMatcher()); this(new DefaultMessageMatcher());
@ -119,8 +118,9 @@ public class WeixinMessageDispatcher {
public void doDispatch(final ChannelHandlerContext context, public void doDispatch(final ChannelHandlerContext context,
final WeixinRequest request, final CruxMessageHandler cruxMessage) final WeixinRequest request, final CruxMessageHandler cruxMessage)
throws WeixinException { throws WeixinException {
WeixinMessageKey messageKey = defineMessageKey(cruxMessage.getMsgType(), WeixinMessageKey messageKey = defineMessageKey(
cruxMessage.getEventType(), cruxMessage.getAccountType()); cruxMessage.getMsgType(), cruxMessage.getEventType(),
cruxMessage.getAccountType());
Class<? extends WeixinMessage> targetClass = messageMatcher Class<? extends WeixinMessage> targetClass = messageMatcher
.match(messageKey); .match(messageKey);
Object message = messageRead(request.getOriginalContent(), targetClass); Object message = messageRead(request.getOriginalContent(), targetClass);
@ -159,8 +159,8 @@ public class WeixinMessageDispatcher {
* 账号类型 * 账号类型
* @return * @return
*/ */
protected WeixinMessageKey defineMessageKey(String messageType, String eventType, protected WeixinMessageKey defineMessageKey(String messageType,
AccountType accountType) { String eventType, AccountType accountType) {
return new WeixinMessageKey(messageType, eventType, accountType); return new WeixinMessageKey(messageType, eventType, accountType);
} }
@ -176,17 +176,8 @@ public class WeixinMessageDispatcher {
*/ */
protected void noHandlerFound(ChannelHandlerContext context, protected void noHandlerFound(ChannelHandlerContext context,
WeixinRequest request, Object message) { WeixinRequest request, Object message) {
if (isDebug) { if (alwaysResponse) {
if (message == null) { context.write(BlankResponse.global);
context.writeAndFlush(
new TextResponse(request.getOriginalContent()
.replaceAll("\\!\\[CDATA\\[", "")
.replaceAll("\\]\\]", ""))).addListener(
ChannelFutureListener.CLOSE);
} else {
context.writeAndFlush(new TextResponse(message.toString()))
.addListener(ChannelFutureListener.CLOSE);
}
} else { } else {
context.writeAndFlush( context.writeAndFlush(
HttpUtil.createHttpResponse(null, NOT_FOUND, null)) HttpUtil.createHttpResponse(null, NOT_FOUND, null))
@ -456,7 +447,10 @@ public class WeixinMessageDispatcher {
return this.messageMatcher; return this.messageMatcher;
} }
public void openDebugMode() { /**
isDebug = true; * 打开总是响应开关,如未匹配到MessageHandler时回复空白消息
*/
public void openAlwaysResponse() {
this.alwaysResponse = true;
} }
} }

View File

@ -18,8 +18,8 @@ import java.util.Map;
import com.foxinmy.weixin4j.dispatcher.BeanFactory; import com.foxinmy.weixin4j.dispatcher.BeanFactory;
import com.foxinmy.weixin4j.dispatcher.DefaultMessageMatcher; import com.foxinmy.weixin4j.dispatcher.DefaultMessageMatcher;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageKey;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher; import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageKey;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageMatcher; import com.foxinmy.weixin4j.dispatcher.WeixinMessageMatcher;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler; import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
@ -310,17 +310,18 @@ public final class WeixinServerBootstrap {
* 消息类 * 消息类
* @return * @return
*/ */
public WeixinServerBootstrap registMessageClass(WeixinMessageKey messageKey, public WeixinServerBootstrap registMessageClass(
WeixinMessageKey messageKey,
Class<? extends WeixinMessage> messageClass) { Class<? extends WeixinMessage> messageClass) {
messageDispatcher.registMessageClass(messageKey, messageClass); messageDispatcher.registMessageClass(messageKey, messageClass);
return this; return this;
} }
/** /**
* 开启debug:未匹配到MessageHanlder输出消息信 * 打开总是响应开关,如未匹配到MessageHandler时回复空白消
*/ */
public WeixinServerBootstrap openDebugMode() { public WeixinServerBootstrap openAlwaysResponse() {
messageDispatcher.openDebugMode(); messageDispatcher.openAlwaysResponse();
return this; return this;
} }
} }