删除SingleContentResponse类

This commit is contained in:
jinyu 2015-08-05 22:59:15 +08:00
parent 432575988b
commit f6961e1c8e
12 changed files with 81 additions and 102 deletions

View File

@ -4,8 +4,6 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler; import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor; import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
@ -38,19 +36,16 @@ public class MessageHandlerExecutor {
/** /**
* 节点名称集合 * 节点名称集合
*/ */
private final Set<String> nodeNames;
private final ChannelHandlerContext context; private final ChannelHandlerContext context;
private int interceptorIndex = -1; private int interceptorIndex = -1;
public MessageHandlerExecutor(ChannelHandlerContext context, public MessageHandlerExecutor(ChannelHandlerContext context,
WeixinMessageHandler messageHandler, WeixinMessageHandler messageHandler,
WeixinMessageInterceptor[] messageInterceptors, WeixinMessageInterceptor[] messageInterceptors) {
Set<String> nodeNames) {
this.context = context; this.context = context;
this.messageHandler = messageHandler; this.messageHandler = messageHandler;
this.messageInterceptors = messageInterceptors; this.messageInterceptors = messageInterceptors;
this.nodeNames = nodeNames;
} }
public WeixinMessageHandler getMessageHandler() { public WeixinMessageHandler getMessageHandler() {
@ -73,8 +68,8 @@ public class MessageHandlerExecutor {
for (int i = 0; i < messageInterceptors.length; i++) { for (int i = 0; i < messageInterceptors.length; i++) {
WeixinMessageInterceptor interceptor = messageInterceptors[i]; WeixinMessageInterceptor interceptor = messageInterceptors[i];
if (!interceptor.preHandle(context, request, message, if (!interceptor.preHandle(context, request, message,
nodeNames, messageHandler)) { messageHandler)) {
triggerAfterCompletion(request, message, null); triggerAfterCompletion(request, null, message, null);
return false; return false;
} }
this.interceptorIndex = i; this.interceptorIndex = i;
@ -102,7 +97,7 @@ public class MessageHandlerExecutor {
for (int i = messageInterceptors.length - 1; i >= 0; i--) { for (int i = messageInterceptors.length - 1; i >= 0; i--) {
WeixinMessageInterceptor interceptor = messageInterceptors[i]; WeixinMessageInterceptor interceptor = messageInterceptors[i];
interceptor.postHandle(context, request, response, message, interceptor.postHandle(context, request, response, message,
nodeNames, messageHandler); messageHandler);
} }
} }
@ -111,22 +106,25 @@ public class MessageHandlerExecutor {
* *
* @param request * @param request
* 微信请求 * 微信请求
* @param response
* 微信响应 可能为空
* @param message * @param message
* 微信消息 * 微信消息
* @param exception * @param exception
* 处理时可能的异常 * 处理时可能的异常
* @throws WeixinException * @throws WeixinException
*/ */
public void triggerAfterCompletion(WeixinRequest request, Object message, public void triggerAfterCompletion(WeixinRequest request,
WeixinException exception) throws WeixinException { WeixinResponse response, Object message, Exception exception)
throws WeixinException {
if (messageInterceptors == null) { if (messageInterceptors == null) {
return; return;
} }
for (int i = this.interceptorIndex; i >= 0; i--) { for (int i = this.interceptorIndex; i >= 0; i--) {
WeixinMessageInterceptor interceptor = messageInterceptors[i]; WeixinMessageInterceptor interceptor = messageInterceptors[i];
try { try {
interceptor.afterCompletion(context, request, message, interceptor.afterCompletion(context, request, response,
nodeNames, messageHandler, exception); message, messageHandler, exception);
} catch (WeixinException e) { } catch (WeixinException e) {
logger.error( logger.error(
"MessageInterceptor.afterCompletion threw exception", e); "MessageInterceptor.afterCompletion threw exception", e);

View File

@ -31,7 +31,6 @@ 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.BlankResponse; import com.foxinmy.weixin4j.response.BlankResponse;
import com.foxinmy.weixin4j.response.SingleResponse;
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;
@ -136,20 +135,18 @@ public class WeixinMessageDispatcher {
if (!handlerExecutor.applyPreHandle(request, message)) { if (!handlerExecutor.applyPreHandle(request, message)) {
return; return;
} }
WeixinException dispatchException = null; Exception exception = null;
WeixinResponse response = null;
try { try {
SingleResponse response = handlerExecutor.getMessageHandler() response = handlerExecutor.getMessageHandler().doHandle(request,
.doHandle(request, message, cruxMessage.getNodeNames()); message, cruxMessage.getNodeNames());
if (response instanceof WeixinResponse) { handlerExecutor.applyPostHandle(request, response, message);
handlerExecutor.applyPostHandle(request,
(WeixinResponse) response, message);
}
context.write(response); context.write(response);
} catch (WeixinException e) { } catch (Exception e) {
dispatchException = e; exception = e;
} }
handlerExecutor.triggerAfterCompletion(request, message, handlerExecutor.triggerAfterCompletion(request, response, message,
dispatchException); exception);
} }
/** /**
@ -202,7 +199,7 @@ public class WeixinMessageDispatcher {
* @param nodeNames * @param nodeNames
* 节点名称集合 * 节点名称集合
* @return MessageHandlerExecutor * @return MessageHandlerExecutor
* @see com.foxinmy.weixin4j.dispatcher.MessageHandlerExecutor * @see MessageHandlerExecutor
* @throws WeixinException * @throws WeixinException
*/ */
protected MessageHandlerExecutor getHandlerExecutor( protected MessageHandlerExecutor getHandlerExecutor(
@ -234,7 +231,7 @@ public class WeixinMessageDispatcher {
} }
} }
return new MessageHandlerExecutor(context, messageHandler, return new MessageHandlerExecutor(context, messageHandler,
getMessageInterceptors(), nodeNames); getMessageInterceptors());
} }
/** /**
@ -350,16 +347,16 @@ public class WeixinMessageDispatcher {
* @return 消息对象 * @return 消息对象
* @throws WeixinException * @throws WeixinException
*/ */
protected Object messageRead(String message, protected <M extends WeixinMessage> M messageRead(String message,
Class<? extends WeixinMessage> clazz) throws WeixinException { Class<M> clazz) throws WeixinException {
if (clazz == null) { if (clazz == null) {
return null; return null;
} }
try { try {
Source source = new StreamSource(new ByteArrayInputStream( Source source = new StreamSource(new ByteArrayInputStream(
message.getBytes(Consts.UTF_8))); message.getBytes(Consts.UTF_8)));
JAXBElement<? extends WeixinMessage> jaxbElement = getUnmarshaller( JAXBElement<M> jaxbElement = getUnmarshaller(clazz).unmarshal(
clazz).unmarshal(source, clazz); source, clazz);
return jaxbElement.getValue(); return jaxbElement.getValue();
} catch (JAXBException e) { } catch (JAXBException e) {
throw new WeixinException(e); throw new WeixinException(e);

View File

@ -4,7 +4,7 @@ import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.SingleResponse; import com.foxinmy.weixin4j.response.WeixinResponse;
/** /**
* 微信消息处理器 * 微信消息处理器
@ -42,6 +42,6 @@ public interface WeixinMessageHandler {
* 节点名称集合 * 节点名称集合
* @return 回复内容 * @return 回复内容
*/ */
public SingleResponse doHandle(WeixinRequest request, Object message, public WeixinResponse doHandle(WeixinRequest request, Object message,
Set<String> nodeNames) throws WeixinException; Set<String> nodeNames) throws WeixinException;
} }

View File

@ -2,8 +2,6 @@ package com.foxinmy.weixin4j.interceptor;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler; import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.request.WeixinRequest;
@ -23,22 +21,21 @@ public abstract class MessageInterceptorAdapter implements
@Override @Override
public boolean preHandle(ChannelHandlerContext context, public boolean preHandle(ChannelHandlerContext context,
WeixinRequest request, Object message, Set<String> nodeNames, WeixinRequest request, Object message, WeixinMessageHandler handler)
WeixinMessageHandler handler) throws WeixinException { throws WeixinException {
return true; return true;
} }
@Override @Override
public void postHandle(ChannelHandlerContext context, public void postHandle(ChannelHandlerContext context,
WeixinRequest request, WeixinResponse response, Object message, WeixinRequest request, WeixinResponse response, Object message,
Set<String> nodeNames, WeixinMessageHandler handler) WeixinMessageHandler handler) throws WeixinException {
throws WeixinException {
} }
@Override @Override
public void afterCompletion(ChannelHandlerContext context, public void afterCompletion(ChannelHandlerContext context,
WeixinRequest request, Object message, Set<String> nodeNames, WeixinRequest request, WeixinResponse response, Object message,
WeixinMessageHandler handler, WeixinException exception) WeixinMessageHandler handler, Exception exception)
throws WeixinException { throws WeixinException {
} }
} }

View File

@ -2,8 +2,6 @@ package com.foxinmy.weixin4j.interceptor;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler; import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.request.WeixinRequest;
@ -29,15 +27,13 @@ public interface WeixinMessageInterceptor {
* 微信请求 * 微信请求
* @param message * @param message
* 微信消息 * 微信消息
* @param nodeNames
* 节点名称集合
* @param handler * @param handler
* 消息处理器 * 消息处理器
* @return 返回true执行下一个拦截器 * @return 返回true执行下一个拦截器
* @throws WeixinException * @throws WeixinException
*/ */
boolean preHandle(ChannelHandlerContext context, WeixinRequest request, boolean preHandle(ChannelHandlerContext context, WeixinRequest request,
Object message, Set<String> nodeNames, WeixinMessageHandler handler) Object message, WeixinMessageHandler handler)
throws WeixinException; throws WeixinException;
/** /**
@ -51,14 +47,12 @@ public interface WeixinMessageInterceptor {
* 微信响应 * 微信响应
* @param message * @param message
* 微信消息 * 微信消息
* @param nodeNames
* 节点名称集合
* @param handler * @param handler
* 消息处理器 * 消息处理器
* @throws WeixinException * @throws WeixinException
*/ */
void postHandle(ChannelHandlerContext context, WeixinRequest request, void postHandle(ChannelHandlerContext context, WeixinRequest request,
WeixinResponse response, Object message, Set<String> nodeNames, WeixinResponse response, Object message,
WeixinMessageHandler handler) throws WeixinException; WeixinMessageHandler handler) throws WeixinException;
/** /**
@ -70,8 +64,6 @@ public interface WeixinMessageInterceptor {
* 微信请求 * 微信请求
* @param message * @param message
* 微信消息 * 微信消息
* @param nodeNames
* 节点名称集合
* @param handler * @param handler
* 消息处理器 * 消息处理器
* @param exception * @param exception
@ -79,7 +71,7 @@ public interface WeixinMessageInterceptor {
* @throws WeixinException * @throws WeixinException
*/ */
void afterCompletion(ChannelHandlerContext context, WeixinRequest request, void afterCompletion(ChannelHandlerContext context, WeixinRequest request,
Object message, Set<String> nodeNames, WeixinResponse response, Object message,
WeixinMessageHandler handler, WeixinException exception) WeixinMessageHandler handler, Exception exception)
throws WeixinException; throws WeixinException;
} }

View File

@ -9,7 +9,7 @@ package com.foxinmy.weixin4j.response;
* @since JDK 1.7 * @since JDK 1.7
* @see * @see
*/ */
public class BlankResponse extends SingleContentResponse { public class BlankResponse extends SingleResponse {
public static final BlankResponse global = new BlankResponse(); public static final BlankResponse global = new BlankResponse();

View File

@ -1,24 +0,0 @@
package com.foxinmy.weixin4j.response;
/**
* 单一内容回复
*
* @className SingleContentResponse
* @author jy
* @date 2015年8月3日
* @since JDK 1.7
* @see
*/
public class SingleContentResponse implements SingleResponse {
private final String content;
public SingleContentResponse(String content) {
this.content = content;
}
@Override
public String toContent() {
return content;
}
}

View File

@ -1,19 +1,34 @@
package com.foxinmy.weixin4j.response; package com.foxinmy.weixin4j.response;
/** /**
* 单一的字符串回复,如回复SUCCESS * 单一内容回复
* *
* @className SingleResponse * @className SingleResponse
* @author jy * @author jy
* @date 2015年6月23日 * @date 2015年8月3日
* @since JDK 1.7 * @since JDK 1.7
* @see * @see
*/ */
public interface SingleResponse { public class SingleResponse implements WeixinResponse {
/**
* 回复内容 private final String content;
*
* @return public SingleResponse(String content) {
*/ this.content = content;
public String toContent(); }
@Override
public String toContent() {
return content;
}
@Override
public String getMsgType() {
return "single";
}
@Override
public String toString() {
return "SingleResponse [content=" + content + "]";
}
} }

View File

@ -1,6 +1,5 @@
package com.foxinmy.weixin4j.response; package com.foxinmy.weixin4j.response;
/** /**
* 微信被动消息回复 * 微信被动消息回复
* *
@ -15,17 +14,25 @@ package com.foxinmy.weixin4j.response;
* @see VideoResponse * @see VideoResponse
* @see NewsResponse * @see NewsResponse
* @see TransferCustomerResponse * @see TransferCustomerResponse
* @see SingleResponse
* @see BlankResponse * @see BlankResponse
* @see <a href= * @see <a href=
* "http://mp.weixin.qq.com/wiki/9/2c15b20a16019ae613d413e30cac8ea1.html">订阅号服务号的被动响应消息</a> * "http://mp.weixin.qq.com/wiki/9/2c15b20a16019ae613d413e30cac8ea1.html">订阅号服务号的被动响应消息</a>
* @see <a * @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%A2%AB%E5%8A%A8%E5%93%8D%E5%BA%94%E6%B6%88%E6%81%AF">企业号的被动响应消息</a> * href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%A2%AB%E5%8A%A8%E5%93%8D%E5%BA%94%E6%B6%88%E6%81%AF">企业号的被动响应消息</a>
*/ */
public interface WeixinResponse extends SingleResponse { public interface WeixinResponse {
/** /**
* 回复的消息类型 * 回复的消息类型
* *
* @return * @return
*/ */
public String getMsgType(); public String getMsgType();
/**
* 回复的消息内容
*
* @return
*/
public String toContent();
} }

View File

@ -12,7 +12,7 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher; import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.SingleContentResponse; import com.foxinmy.weixin4j.response.SingleResponse;
import com.foxinmy.weixin4j.type.EncryptType; import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken; import com.foxinmy.weixin4j.util.AesToken;
import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.Consts;
@ -59,7 +59,7 @@ public class WeixinRequestHandler extends
if (MessageUtil.signature(aesToken.getToken(), if (MessageUtil.signature(aesToken.getToken(),
request.getTimeStamp(), request.getNonce()).equals( request.getTimeStamp(), request.getNonce()).equals(
request.getSignature())) { request.getSignature())) {
ctx.write(new SingleContentResponse(request.getEchoStr())); ctx.write(new SingleResponse(request.getEchoStr()));
return; return;
} }
ctx.writeAndFlush( ctx.writeAndFlush(

View File

@ -41,8 +41,8 @@ public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new WeixinMessageDecoder(aesTokenMap)); pipeline.addLast(new WeixinMessageDecoder(aesTokenMap));
pipeline.addLast(new SingleResponseEncoder());
pipeline.addLast(new WeixinResponseEncoder()); pipeline.addLast(new WeixinResponseEncoder());
pipeline.addLast(new SingleResponseEncoder());
pipeline.addLast(new WeixinRequestHandler(messageDispatcher)); pipeline.addLast(new WeixinRequestHandler(messageDispatcher));
} }
} }

View File

@ -2,8 +2,6 @@ package com.foxinmy.weixin4j.server.test;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.util.Set;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.DebugMessageHandler; import com.foxinmy.weixin4j.handler.DebugMessageHandler;
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter; import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
@ -89,8 +87,7 @@ public class MessageServerStartup {
@Override @Override
public boolean preHandle(ChannelHandlerContext context, public boolean preHandle(ChannelHandlerContext context,
WeixinRequest request, Object message, WeixinRequest request, Object message,
Set<String> nodeNames, WeixinMessageHandler handler) WeixinMessageHandler handler) throws WeixinException {
throws WeixinException {
context.writeAndFlush(new TextResponse("所有消息被拦截了!")); context.writeAndFlush(new TextResponse("所有消息被拦截了!"));
return false; return false;
} }
@ -98,16 +95,16 @@ public class MessageServerStartup {
@Override @Override
public void postHandle(ChannelHandlerContext context, public void postHandle(ChannelHandlerContext context,
WeixinRequest request, WeixinResponse response, WeixinRequest request, WeixinResponse response,
Object message, Set<String> nodeNames, Object message, WeixinMessageHandler handler)
WeixinMessageHandler handler) throws WeixinException { throws WeixinException {
System.err.println("preHandle返回为true,执行handler后"); System.err.println("preHandle返回为true,执行handler后");
} }
@Override @Override
public void afterCompletion(ChannelHandlerContext context, public void afterCompletion(ChannelHandlerContext context,
WeixinRequest request, Object message, WeixinRequest request, WeixinResponse response,
Set<String> nodeNames, WeixinMessageHandler handler, Object message, WeixinMessageHandler handler,
WeixinException exception) throws WeixinException { Exception exception) throws WeixinException {
System.err.println("请求处理完毕"); System.err.println("请求处理完毕");
} }
}; };