删除SingleContentResponse类
This commit is contained in:
parent
432575988b
commit
f6961e1c8e
@ -4,8 +4,6 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.util.internal.logging.InternalLogger;
|
||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
|
||||
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
|
||||
@ -38,19 +36,16 @@ public class MessageHandlerExecutor {
|
||||
/**
|
||||
* 节点名称集合
|
||||
*/
|
||||
private final Set<String> nodeNames;
|
||||
|
||||
private final ChannelHandlerContext context;
|
||||
|
||||
private int interceptorIndex = -1;
|
||||
|
||||
public MessageHandlerExecutor(ChannelHandlerContext context,
|
||||
WeixinMessageHandler messageHandler,
|
||||
WeixinMessageInterceptor[] messageInterceptors,
|
||||
Set<String> nodeNames) {
|
||||
WeixinMessageInterceptor[] messageInterceptors) {
|
||||
this.context = context;
|
||||
this.messageHandler = messageHandler;
|
||||
this.messageInterceptors = messageInterceptors;
|
||||
this.nodeNames = nodeNames;
|
||||
}
|
||||
|
||||
public WeixinMessageHandler getMessageHandler() {
|
||||
@ -73,8 +68,8 @@ public class MessageHandlerExecutor {
|
||||
for (int i = 0; i < messageInterceptors.length; i++) {
|
||||
WeixinMessageInterceptor interceptor = messageInterceptors[i];
|
||||
if (!interceptor.preHandle(context, request, message,
|
||||
nodeNames, messageHandler)) {
|
||||
triggerAfterCompletion(request, message, null);
|
||||
messageHandler)) {
|
||||
triggerAfterCompletion(request, null, message, null);
|
||||
return false;
|
||||
}
|
||||
this.interceptorIndex = i;
|
||||
@ -102,7 +97,7 @@ public class MessageHandlerExecutor {
|
||||
for (int i = messageInterceptors.length - 1; i >= 0; i--) {
|
||||
WeixinMessageInterceptor interceptor = messageInterceptors[i];
|
||||
interceptor.postHandle(context, request, response, message,
|
||||
nodeNames, messageHandler);
|
||||
messageHandler);
|
||||
}
|
||||
}
|
||||
|
||||
@ -111,22 +106,25 @@ public class MessageHandlerExecutor {
|
||||
*
|
||||
* @param request
|
||||
* 微信请求
|
||||
* @param response
|
||||
* 微信响应 可能为空
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @param exception
|
||||
* 处理时可能的异常
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void triggerAfterCompletion(WeixinRequest request, Object message,
|
||||
WeixinException exception) throws WeixinException {
|
||||
public void triggerAfterCompletion(WeixinRequest request,
|
||||
WeixinResponse response, Object message, Exception exception)
|
||||
throws WeixinException {
|
||||
if (messageInterceptors == null) {
|
||||
return;
|
||||
}
|
||||
for (int i = this.interceptorIndex; i >= 0; i--) {
|
||||
WeixinMessageInterceptor interceptor = messageInterceptors[i];
|
||||
try {
|
||||
interceptor.afterCompletion(context, request, message,
|
||||
nodeNames, messageHandler, exception);
|
||||
interceptor.afterCompletion(context, request, response,
|
||||
message, messageHandler, exception);
|
||||
} catch (WeixinException e) {
|
||||
logger.error(
|
||||
"MessageInterceptor.afterCompletion threw exception", e);
|
||||
|
||||
@ -31,7 +31,6 @@ import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
|
||||
import com.foxinmy.weixin4j.request.WeixinMessage;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.BlankResponse;
|
||||
import com.foxinmy.weixin4j.response.SingleResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
import com.foxinmy.weixin4j.util.ClassUtil;
|
||||
@ -136,20 +135,18 @@ public class WeixinMessageDispatcher {
|
||||
if (!handlerExecutor.applyPreHandle(request, message)) {
|
||||
return;
|
||||
}
|
||||
WeixinException dispatchException = null;
|
||||
Exception exception = null;
|
||||
WeixinResponse response = null;
|
||||
try {
|
||||
SingleResponse response = handlerExecutor.getMessageHandler()
|
||||
.doHandle(request, message, cruxMessage.getNodeNames());
|
||||
if (response instanceof WeixinResponse) {
|
||||
handlerExecutor.applyPostHandle(request,
|
||||
(WeixinResponse) response, message);
|
||||
}
|
||||
response = handlerExecutor.getMessageHandler().doHandle(request,
|
||||
message, cruxMessage.getNodeNames());
|
||||
handlerExecutor.applyPostHandle(request, response, message);
|
||||
context.write(response);
|
||||
} catch (WeixinException e) {
|
||||
dispatchException = e;
|
||||
} catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
handlerExecutor.triggerAfterCompletion(request, message,
|
||||
dispatchException);
|
||||
handlerExecutor.triggerAfterCompletion(request, response, message,
|
||||
exception);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -202,7 +199,7 @@ public class WeixinMessageDispatcher {
|
||||
* @param nodeNames
|
||||
* 节点名称集合
|
||||
* @return MessageHandlerExecutor
|
||||
* @see com.foxinmy.weixin4j.dispatcher.MessageHandlerExecutor
|
||||
* @see MessageHandlerExecutor
|
||||
* @throws WeixinException
|
||||
*/
|
||||
protected MessageHandlerExecutor getHandlerExecutor(
|
||||
@ -234,7 +231,7 @@ public class WeixinMessageDispatcher {
|
||||
}
|
||||
}
|
||||
return new MessageHandlerExecutor(context, messageHandler,
|
||||
getMessageInterceptors(), nodeNames);
|
||||
getMessageInterceptors());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -350,16 +347,16 @@ public class WeixinMessageDispatcher {
|
||||
* @return 消息对象
|
||||
* @throws WeixinException
|
||||
*/
|
||||
protected Object messageRead(String message,
|
||||
Class<? extends WeixinMessage> clazz) throws WeixinException {
|
||||
protected <M extends WeixinMessage> M messageRead(String message,
|
||||
Class<M> clazz) throws WeixinException {
|
||||
if (clazz == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Source source = new StreamSource(new ByteArrayInputStream(
|
||||
message.getBytes(Consts.UTF_8)));
|
||||
JAXBElement<? extends WeixinMessage> jaxbElement = getUnmarshaller(
|
||||
clazz).unmarshal(source, clazz);
|
||||
JAXBElement<M> jaxbElement = getUnmarshaller(clazz).unmarshal(
|
||||
source, clazz);
|
||||
return jaxbElement.getValue();
|
||||
} catch (JAXBException e) {
|
||||
throw new WeixinException(e);
|
||||
|
||||
@ -4,7 +4,7 @@ import java.util.Set;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
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 回复内容
|
||||
*/
|
||||
public SingleResponse doHandle(WeixinRequest request, Object message,
|
||||
public WeixinResponse doHandle(WeixinRequest request, Object message,
|
||||
Set<String> nodeNames) throws WeixinException;
|
||||
}
|
||||
|
||||
@ -2,8 +2,6 @@ package com.foxinmy.weixin4j.interceptor;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
@ -23,22 +21,21 @@ public abstract class MessageInterceptorAdapter implements
|
||||
|
||||
@Override
|
||||
public boolean preHandle(ChannelHandlerContext context,
|
||||
WeixinRequest request, Object message, Set<String> nodeNames,
|
||||
WeixinMessageHandler handler) throws WeixinException {
|
||||
WeixinRequest request, Object message, WeixinMessageHandler handler)
|
||||
throws WeixinException {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(ChannelHandlerContext context,
|
||||
WeixinRequest request, WeixinResponse response, Object message,
|
||||
Set<String> nodeNames, WeixinMessageHandler handler)
|
||||
throws WeixinException {
|
||||
WeixinMessageHandler handler) throws WeixinException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(ChannelHandlerContext context,
|
||||
WeixinRequest request, Object message, Set<String> nodeNames,
|
||||
WeixinMessageHandler handler, WeixinException exception)
|
||||
WeixinRequest request, WeixinResponse response, Object message,
|
||||
WeixinMessageHandler handler, Exception exception)
|
||||
throws WeixinException {
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,8 +2,6 @@ package com.foxinmy.weixin4j.interceptor;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
@ -29,15 +27,13 @@ public interface WeixinMessageInterceptor {
|
||||
* 微信请求
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @param nodeNames
|
||||
* 节点名称集合
|
||||
* @param handler
|
||||
* 消息处理器
|
||||
* @return 返回true执行下一个拦截器
|
||||
* @throws WeixinException
|
||||
*/
|
||||
boolean preHandle(ChannelHandlerContext context, WeixinRequest request,
|
||||
Object message, Set<String> nodeNames, WeixinMessageHandler handler)
|
||||
Object message, WeixinMessageHandler handler)
|
||||
throws WeixinException;
|
||||
|
||||
/**
|
||||
@ -51,14 +47,12 @@ public interface WeixinMessageInterceptor {
|
||||
* 微信响应
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @param nodeNames
|
||||
* 节点名称集合
|
||||
* @param handler
|
||||
* 消息处理器
|
||||
* @throws WeixinException
|
||||
*/
|
||||
void postHandle(ChannelHandlerContext context, WeixinRequest request,
|
||||
WeixinResponse response, Object message, Set<String> nodeNames,
|
||||
WeixinResponse response, Object message,
|
||||
WeixinMessageHandler handler) throws WeixinException;
|
||||
|
||||
/**
|
||||
@ -70,8 +64,6 @@ public interface WeixinMessageInterceptor {
|
||||
* 微信请求
|
||||
* @param message
|
||||
* 微信消息
|
||||
* @param nodeNames
|
||||
* 节点名称集合
|
||||
* @param handler
|
||||
* 消息处理器
|
||||
* @param exception
|
||||
@ -79,7 +71,7 @@ public interface WeixinMessageInterceptor {
|
||||
* @throws WeixinException
|
||||
*/
|
||||
void afterCompletion(ChannelHandlerContext context, WeixinRequest request,
|
||||
Object message, Set<String> nodeNames,
|
||||
WeixinMessageHandler handler, WeixinException exception)
|
||||
WeixinResponse response, Object message,
|
||||
WeixinMessageHandler handler, Exception exception)
|
||||
throws WeixinException;
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ package com.foxinmy.weixin4j.response;
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class BlankResponse extends SingleContentResponse {
|
||||
public class BlankResponse extends SingleResponse {
|
||||
|
||||
public static final BlankResponse global = new BlankResponse();
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -1,19 +1,34 @@
|
||||
package com.foxinmy.weixin4j.response;
|
||||
|
||||
/**
|
||||
* 单一的字符串回复,如回复SUCCESS
|
||||
* 单一内容回复
|
||||
*
|
||||
* @className SingleResponse
|
||||
* @author jy
|
||||
* @date 2015年6月23日
|
||||
* @date 2015年8月3日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public interface SingleResponse {
|
||||
/**
|
||||
* 回复内容
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toContent();
|
||||
public class SingleResponse implements WeixinResponse {
|
||||
|
||||
private final String content;
|
||||
|
||||
public SingleResponse(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsgType() {
|
||||
return "single";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SingleResponse [content=" + content + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package com.foxinmy.weixin4j.response;
|
||||
|
||||
|
||||
/**
|
||||
* 微信被动消息回复
|
||||
*
|
||||
@ -15,17 +14,25 @@ package com.foxinmy.weixin4j.response;
|
||||
* @see VideoResponse
|
||||
* @see NewsResponse
|
||||
* @see TransferCustomerResponse
|
||||
* @see SingleResponse
|
||||
* @see BlankResponse
|
||||
* @see <a href=
|
||||
* "http://mp.weixin.qq.com/wiki/9/2c15b20a16019ae613d413e30cac8ea1.html">订阅号、服务号的被动响应消息</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>
|
||||
*/
|
||||
public interface WeixinResponse extends SingleResponse {
|
||||
public interface WeixinResponse {
|
||||
/**
|
||||
* 回复的消息类型
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getMsgType();
|
||||
|
||||
/**
|
||||
* 回复的消息内容
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toContent();
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||
import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
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.util.AesToken;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
@ -59,7 +59,7 @@ public class WeixinRequestHandler extends
|
||||
if (MessageUtil.signature(aesToken.getToken(),
|
||||
request.getTimeStamp(), request.getNonce()).equals(
|
||||
request.getSignature())) {
|
||||
ctx.write(new SingleContentResponse(request.getEchoStr()));
|
||||
ctx.write(new SingleResponse(request.getEchoStr()));
|
||||
return;
|
||||
}
|
||||
ctx.writeAndFlush(
|
||||
|
||||
@ -41,8 +41,8 @@ public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
|
||||
pipeline.addLast(new HttpServerCodec());
|
||||
pipeline.addLast(new HttpObjectAggregator(65536));
|
||||
pipeline.addLast(new WeixinMessageDecoder(aesTokenMap));
|
||||
pipeline.addLast(new SingleResponseEncoder());
|
||||
pipeline.addLast(new WeixinResponseEncoder());
|
||||
pipeline.addLast(new SingleResponseEncoder());
|
||||
pipeline.addLast(new WeixinRequestHandler(messageDispatcher));
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,8 +2,6 @@ package com.foxinmy.weixin4j.server.test;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
|
||||
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
|
||||
@ -89,8 +87,7 @@ public class MessageServerStartup {
|
||||
@Override
|
||||
public boolean preHandle(ChannelHandlerContext context,
|
||||
WeixinRequest request, Object message,
|
||||
Set<String> nodeNames, WeixinMessageHandler handler)
|
||||
throws WeixinException {
|
||||
WeixinMessageHandler handler) throws WeixinException {
|
||||
context.writeAndFlush(new TextResponse("所有消息被拦截了!"));
|
||||
return false;
|
||||
}
|
||||
@ -98,16 +95,16 @@ public class MessageServerStartup {
|
||||
@Override
|
||||
public void postHandle(ChannelHandlerContext context,
|
||||
WeixinRequest request, WeixinResponse response,
|
||||
Object message, Set<String> nodeNames,
|
||||
WeixinMessageHandler handler) throws WeixinException {
|
||||
Object message, WeixinMessageHandler handler)
|
||||
throws WeixinException {
|
||||
System.err.println("preHandle返回为true,执行handler后");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(ChannelHandlerContext context,
|
||||
WeixinRequest request, Object message,
|
||||
Set<String> nodeNames, WeixinMessageHandler handler,
|
||||
WeixinException exception) throws WeixinException {
|
||||
WeixinRequest request, WeixinResponse response,
|
||||
Object message, WeixinMessageHandler handler,
|
||||
Exception exception) throws WeixinException {
|
||||
System.err.println("请求处理完毕");
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user