weixin4j-server:新增SingleResponseEncoder类

This commit is contained in:
jinyu 2015-08-02 11:25:09 +08:00
parent 8ed7ba3f49
commit fb7593684d
7 changed files with 58 additions and 9 deletions

View File

@ -24,7 +24,7 @@ public class ChatItem implements Serializable {
private static final long serialVersionUID = -5921235260175596270L; private static final long serialVersionUID = -5921235260175596270L;
public final String LIST_SEPARATOR = "|"; public final String LIST_SEPARATOR = "\\|";
/** /**
* 操作成员UserID * 操作成员UserID

View File

@ -0,0 +1,41 @@
package com.foxinmy.weixin4j.socket;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.List;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.response.SingleResponse;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
/**
* 微信回复编码类
*
* @className WeixinResponseEncoder
* @author jy
* @date 2014年11月13日
* @since JDK 1.7
* @see <a
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
* @see com.foxinmy.weixin4j.response.WeixinResponse
*/
public class SingleResponseEncoder extends
MessageToMessageEncoder<SingleResponse> {
private final InternalLogger logger = InternalLoggerFactory
.getInstance(getClass());
@Override
protected void encode(ChannelHandlerContext ctx, SingleResponse response,
List<Object> out) throws WeixinException {
String content = response.toContent();
ctx.writeAndFlush(HttpUtil.createHttpResponse(content, OK,
Consts.CONTENTTYPE$APPLICATION_XML));
logger.info("encode single response:{}", content);
}
}

View File

@ -8,13 +8,13 @@ import com.foxinmy.weixin4j.util.AesToken;
/** /**
* 消息传递 * 消息传递
* *
* @className MessageTransfer * @className WeixinMessageTransfer
* @author jy * @author jy
* @date 2015年6月23日 * @date 2015年6月23日
* @since JDK 1.7 * @since JDK 1.7
* @see * @see
*/ */
public class MessageTransfer implements Serializable { public class WeixinMessageTransfer implements Serializable {
private static final long serialVersionUID = 7779948135156353261L; private static final long serialVersionUID = 7779948135156353261L;
@ -36,7 +36,7 @@ public class MessageTransfer implements Serializable {
*/ */
private String fromUserName; private String fromUserName;
public MessageTransfer(AesToken aesToken, EncryptType encryptType, public WeixinMessageTransfer(AesToken aesToken, EncryptType encryptType,
String toUserName, String fromUserName) { String toUserName, String fromUserName) {
this.aesToken = aesToken; this.aesToken = aesToken;
this.encryptType = encryptType; this.encryptType = encryptType;
@ -59,4 +59,11 @@ public class MessageTransfer implements Serializable {
public String getFromUserName() { public String getFromUserName() {
return fromUserName; return fromUserName;
} }
@Override
public String toString() {
return "WeixinMessageTransfer [aesToken=" + aesToken + ", encryptType="
+ encryptType + ", toUserName=" + toUserName
+ ", fromUserName=" + fromUserName + "]";
}
} }

View File

@ -98,7 +98,7 @@ public class WeixinRequestHandler extends
} }
CruxMessageHandler cruxMessage = CruxMessageHandler.parser(request CruxMessageHandler cruxMessage = CruxMessageHandler.parser(request
.getOriginalContent()); .getOriginalContent());
MessageTransfer messageTransfer = new MessageTransfer(aesToken, WeixinMessageTransfer messageTransfer = new WeixinMessageTransfer(aesToken,
request.getEncryptType(), cruxMessage.getToUserName(), request.getEncryptType(), cruxMessage.getToUserName(),
cruxMessage.getFromUserName()); cruxMessage.getFromUserName());
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer); ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);

View File

@ -38,7 +38,7 @@ public class WeixinResponseEncoder extends
@Override @Override
protected void encode(ChannelHandlerContext ctx, WeixinResponse response, protected void encode(ChannelHandlerContext ctx, WeixinResponse response,
List<Object> out) throws WeixinException { List<Object> out) throws WeixinException {
MessageTransfer messageTransfer = ctx.channel() WeixinMessageTransfer messageTransfer = ctx.channel()
.attr(Consts.MESSAGE_TRANSFER_KEY).get(); .attr(Consts.MESSAGE_TRANSFER_KEY).get();
AesToken aesToken = messageTransfer.getAesToken(); AesToken aesToken = messageTransfer.getAesToken();
EncryptType encryptType = messageTransfer.getEncryptType(); EncryptType encryptType = messageTransfer.getEncryptType();
@ -85,6 +85,6 @@ public class WeixinResponseEncoder extends
} }
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK, ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK,
Consts.CONTENTTYPE$APPLICATION_XML)); Consts.CONTENTTYPE$APPLICATION_XML));
logger.info("{} encode response:{}", encryptType, content); logger.info("{} encode weixin response:{}", encryptType, content);
} }
} }

View File

@ -41,6 +41,7 @@ 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 WeixinRequestHandler(messageDispatcher)); pipeline.addLast(new WeixinRequestHandler(messageDispatcher));
} }

View File

@ -4,7 +4,7 @@ import io.netty.util.AttributeKey;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import com.foxinmy.weixin4j.socket.MessageTransfer; import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
/** /**
* 常量类 * 常量类
@ -35,6 +35,6 @@ public final class Consts {
public static final String CONTENTTYPE$APPLICATION_XML = "application/xml"; public static final String CONTENTTYPE$APPLICATION_XML = "application/xml";
public static final String CONTENTTYPE$TEXT_PLAIN = "text/plain"; public static final String CONTENTTYPE$TEXT_PLAIN = "text/plain";
public static final AttributeKey<MessageTransfer> MESSAGE_TRANSFER_KEY = AttributeKey public static final AttributeKey<WeixinMessageTransfer> MESSAGE_TRANSFER_KEY = AttributeKey
.valueOf("$_MESSAGETRANSFER"); .valueOf("$_MESSAGETRANSFER");
} }