新增MultipleResponse多个回复类

This commit is contained in:
jinyu 2016-04-27 21:13:38 +08:00
parent f15f808a18
commit 20c3432091
4 changed files with 144 additions and 20 deletions

View File

@ -0,0 +1,85 @@
package com.foxinmy.weixin4j.response;
import java.util.Arrays;
import java.util.List;
/**
* 多个消息回复
*
* @className MultipleResponse
* @author jy
* @date 2016年4月27日
* @since JDK 1.6
* @see
*/
public class MultipleResponse implements WeixinResponse {
private List<WeixinResponse> responses;
public MultipleResponse(WeixinResponse... responses) {
if (responses == null) {
throw new IllegalArgumentException("responses not be empty");
}
this.responses = Arrays.asList(responses);
}
@Override
public String getMsgType() {
return "multiple";
}
/**
* 插入首条回复
*
* @param response
*/
public void addFirstResponse(WeixinResponse response) {
responses.add(0, response);
}
/**
* 移除首条回复
*
* @return 移除的回复
*/
public WeixinResponse removeFirstResponse() {
return responses.remove(0);
}
/**
* 插入末条回复
*
* @param response
*/
public void addLastResponse(WeixinResponse response) {
responses.add(response);
}
/**
* 移除末条回复
*
* @return 移除的回复
*/
public WeixinResponse removeLastResponse() {
return responses.remove(responses.size() - 1);
}
/**
* 获取所有的回复
*
* @return
*/
public List<WeixinResponse> getResponses() {
return this.responses;
}
@Override
public String toContent() {
return null;
}
@Override
public String toString() {
return "MultipleResponse [responses=" + responses + "]";
}
}

View File

@ -0,0 +1,32 @@
package com.foxinmy.weixin4j.socket;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import java.util.List;
import com.foxinmy.weixin4j.response.MultipleResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
/**
* 微信多个回复编码类
*
* @className WeixinResponseEncoder
* @author jy
* @date 2016年4月27日
* @since JDK 1.6
* @see <a
* href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引</a>
* @see com.foxinmy.weixin4j.response.MultipleResponse
*/
public class MultipleResponseEncoder extends
MessageToMessageEncoder<MultipleResponse> {
@Override
protected void encode(ChannelHandlerContext ctx, MultipleResponse response,
List<Object> out) throws Exception {
for (WeixinResponse r : response.getResponses()) {
ctx.writeAndFlush(r);
}
}
}

View File

@ -29,9 +29,22 @@ import com.foxinmy.weixin4j.util.ServerToolkits;
public class WeixinResponseEncoder extends
MessageToMessageEncoder<WeixinResponse> {
private final InternalLogger logger = InternalLoggerFactory
protected final InternalLogger logger = InternalLoggerFactory
.getInstance(getClass());
private final String XML_START = "<xml>";
// ---------------明文节点
private final String ELEMENT_TOUSERNAME = "<ToUserName><![CDATA[%s]]></ToUserName>";
private final String ELEMENT_FROMUSERNAME = "<FromUserName><![CDATA[%s]]></FromUserName>";
private final String ELEMENT_CREATETIME = "<CreateTime><![CDATA[%d]]></CreateTime>";
private final String ELEMENT_MSGTYPE = "<MsgType><![CDATA[%s]]></MsgType>";
// ---------------密文节点
private final String ELEMENT_MSGSIGNATURE = "<MsgSignature><![CDATA[%s]]></MsgSignature>";
private final String ELEMENT_ENCRYPT = "<Encrypt><![CDATA[%s]]></Encrypt>";
private final String ELEMENT_TIMESTAMP = "<TimeStamp><![CDATA[%s]]></TimeStamp>";
private final String ELEMENT_NONCE = "<Nonce><![CDATA[%s]]></Nonce>";
private final String XML_END = "</xml>";
@Override
protected void encode(ChannelHandlerContext ctx, WeixinResponse response,
List<Object> out) throws WeixinException {
@ -39,18 +52,16 @@ public class WeixinResponseEncoder extends
.attr(ServerToolkits.MESSAGE_TRANSFER_KEY).get();
EncryptType encryptType = messageTransfer.getEncryptType();
StringBuilder content = new StringBuilder();
content.append("<xml>");
content.append(String.format("<ToUserName><![CDATA[%s]]></ToUserName>",
content.append(XML_START);
content.append(String.format(ELEMENT_TOUSERNAME,
messageTransfer.getFromUserName()));
content.append(String.format(
"<FromUserName><![CDATA[%s]]></FromUserName>",
content.append(String.format(ELEMENT_FROMUSERNAME,
messageTransfer.getToUserName()));
content.append(String.format("<CreateTime><![CDATA[%d]]></CreateTime>",
content.append(String.format(ELEMENT_CREATETIME,
System.currentTimeMillis() / 1000l));
content.append(String.format("<MsgType><![CDATA[%s]]></MsgType>",
response.getMsgType()));
content.append(String.format(ELEMENT_MSGTYPE, response.getMsgType()));
content.append(response.toContent());
content.append("</xml>");
content.append(XML_END);
if (encryptType == EncryptType.AES) {
AesToken aesToken = messageTransfer.getAesToken();
String nonce = ServerToolkits.generateRandomString(32);
@ -61,17 +72,12 @@ public class WeixinResponseEncoder extends
String msgSignature = MessageUtil.signature(aesToken.getToken(),
nonce, timestamp, encrtypt);
content.delete(0, content.length());
content.append("<xml>");
content.append(String
.format("<Nonce><![CDATA[%s]]></Nonce>", nonce));
content.append(String.format(
"<TimeStamp><![CDATA[%s]]></TimeStamp>", timestamp));
content.append(String
.format("<MsgSignature><![CDATA[%s]]></MsgSignature>",
msgSignature));
content.append(String.format("<Encrypt><![CDATA[%s]]></Encrypt>",
encrtypt));
content.append("</xml>");
content.append(XML_START);
content.append(String.format(ELEMENT_NONCE, nonce));
content.append(String.format(ELEMENT_TIMESTAMP, timestamp));
content.append(String.format(ELEMENT_MSGSIGNATURE, msgSignature));
content.append(String.format(ELEMENT_ENCRYPT, encrtypt));
content.append(XML_END);
}
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(),
ServerToolkits.CONTENTTYPE$APPLICATION_XML));

View File

@ -39,6 +39,7 @@ public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
pipeline.addLast(new WeixinMessageDecoder(aesTokenMap));
pipeline.addLast(new WeixinResponseEncoder());
pipeline.addLast(new SingleResponseEncoder());
pipeline.addLast(new MultipleResponseEncoder());
pipeline.addLast(new WeixinRequestHandler(messageDispatcher));
}
}