删除BlankMessageHandler类,新增SingleContentResponse类
This commit is contained in:
parent
5bf612c9c5
commit
3644b73608
@ -70,4 +70,6 @@
|
||||
|
||||
* 2015-08-03
|
||||
|
||||
新增base64解编码类(来自apache)
|
||||
+ 新增base64解编码类(来自apache)
|
||||
|
||||
+ 删除`BlankMessageHandler`类,新增`SingleContentResponse`类
|
||||
@ -1,38 +0,0 @@
|
||||
package com.foxinmy.weixin4j.handler;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.BlankResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
|
||||
/**
|
||||
* 空白响应消息处理器
|
||||
*
|
||||
* @className BlankMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年5月17日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class BlankMessageHandler implements WeixinMessageHandler {
|
||||
|
||||
public final static BlankMessageHandler global = new BlankMessageHandler();
|
||||
|
||||
private BlankMessageHandler() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canHandle(WeixinRequest request, Object message,
|
||||
Set<String> nodeNames) throws WeixinException {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WeixinResponse doHandle(WeixinRequest request, Object message,
|
||||
Set<String> nodeNames) throws WeixinException {
|
||||
return BlankResponse.global;
|
||||
}
|
||||
}
|
||||
@ -33,6 +33,9 @@ public class DebugMessageHandler implements WeixinMessageHandler {
|
||||
@Override
|
||||
public WeixinResponse doHandle(WeixinRequest request, Object message,
|
||||
Set<String> nodeNames) throws WeixinException {
|
||||
return new TextResponse(message.toString());
|
||||
String content = message == null ? content = request
|
||||
.getOriginalContent().replaceAll("\\!\\[CDATA\\[", "")
|
||||
.replaceAll("\\]\\]", "") : message.toString();
|
||||
return new TextResponse(content);
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,21 +9,11 @@ package com.foxinmy.weixin4j.response;
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class BlankResponse implements WeixinResponse {
|
||||
public class BlankResponse extends SingleContentResponse {
|
||||
|
||||
public static final BlankResponse global = new BlankResponse();
|
||||
|
||||
private BlankResponse() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsgType() {
|
||||
return "blank";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toContent() {
|
||||
return "success";
|
||||
super("success");
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -33,7 +33,7 @@ public class SingleResponseEncoder extends
|
||||
List<Object> out) throws WeixinException {
|
||||
String content = response.toContent();
|
||||
ctx.writeAndFlush(HttpUtil.createHttpResponse(content, OK,
|
||||
Consts.CONTENTTYPE$APPLICATION_XML));
|
||||
Consts.CONTENTTYPE$TEXT_PLAIN));
|
||||
logger.info("encode single response:{}", content);
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,6 @@ package com.foxinmy.weixin4j.socket;
|
||||
|
||||
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
|
||||
import static io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;
|
||||
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
@ -13,6 +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.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.util.AesToken;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
@ -59,10 +59,7 @@ public class WeixinRequestHandler extends
|
||||
if (MessageUtil.signature(aesToken.getToken(),
|
||||
request.getTimeStamp(), request.getNonce()).equals(
|
||||
request.getSignature())) {
|
||||
ctx.writeAndFlush(
|
||||
HttpUtil.createHttpResponse(request.getEchoStr(), OK,
|
||||
Consts.CONTENTTYPE$TEXT_PLAIN)).addListener(
|
||||
ChannelFutureListener.CLOSE);
|
||||
ctx.write(new SingleContentResponse(request.getEchoStr()));
|
||||
return;
|
||||
}
|
||||
ctx.writeAndFlush(
|
||||
@ -98,9 +95,9 @@ public class WeixinRequestHandler extends
|
||||
}
|
||||
CruxMessageHandler cruxMessage = CruxMessageHandler.parser(request
|
||||
.getOriginalContent());
|
||||
WeixinMessageTransfer messageTransfer = new WeixinMessageTransfer(aesToken,
|
||||
request.getEncryptType(), cruxMessage.getToUserName(),
|
||||
cruxMessage.getFromUserName());
|
||||
WeixinMessageTransfer messageTransfer = new WeixinMessageTransfer(
|
||||
aesToken, request.getEncryptType(),
|
||||
cruxMessage.getToUserName(), cruxMessage.getFromUserName());
|
||||
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
|
||||
messageDispatcher.doDispatch(ctx, request, cruxMessage);
|
||||
}
|
||||
|
||||
@ -9,7 +9,6 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||
import java.util.List;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.response.BlankResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.util.AesToken;
|
||||
@ -43,18 +42,13 @@ public class WeixinResponseEncoder extends
|
||||
AesToken aesToken = messageTransfer.getAesToken();
|
||||
EncryptType encryptType = messageTransfer.getEncryptType();
|
||||
StringBuilder content = new StringBuilder();
|
||||
if (response instanceof BlankResponse) {
|
||||
content.append(response.toContent());
|
||||
} else {
|
||||
content.append("<xml>");
|
||||
content.append(String.format(
|
||||
"<ToUserName><![CDATA[%s]]></ToUserName>",
|
||||
content.append(String.format("<ToUserName><![CDATA[%s]]></ToUserName>",
|
||||
messageTransfer.getFromUserName()));
|
||||
content.append(String.format(
|
||||
"<FromUserName><![CDATA[%s]]></FromUserName>",
|
||||
messageTransfer.getToUserName()));
|
||||
content.append(String.format(
|
||||
"<CreateTime><![CDATA[%d]]></CreateTime>",
|
||||
content.append(String.format("<CreateTime><![CDATA[%d]]></CreateTime>",
|
||||
System.currentTimeMillis() / 1000l));
|
||||
content.append(String.format("<MsgType><![CDATA[%s]]></MsgType>",
|
||||
response.getMsgType()));
|
||||
@ -64,25 +58,23 @@ public class WeixinResponseEncoder extends
|
||||
String nonce = RandomUtil.generateString(32);
|
||||
String timestamp = Long
|
||||
.toString(System.currentTimeMillis() / 1000l);
|
||||
String encrtypt = MessageUtil.aesEncrypt(
|
||||
aesToken.getWeixinId(), aesToken.getAesKey(),
|
||||
content.toString());
|
||||
String msgSignature = MessageUtil.signature(
|
||||
aesToken.getToken(), nonce, timestamp, encrtypt);
|
||||
String encrtypt = MessageUtil.aesEncrypt(aesToken.getWeixinId(),
|
||||
aesToken.getAesKey(), content.toString());
|
||||
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("<Nonce><![CDATA[%s]]></Nonce>", nonce));
|
||||
content.append(String.format(
|
||||
"<TimeStamp><![CDATA[%s]]></TimeStamp>", timestamp));
|
||||
content.append(String.format(
|
||||
"<MsgSignature><![CDATA[%s]]></MsgSignature>",
|
||||
content.append(String
|
||||
.format("<MsgSignature><![CDATA[%s]]></MsgSignature>",
|
||||
msgSignature));
|
||||
content.append(String.format(
|
||||
"<Encrypt><![CDATA[%s]]></Encrypt>", encrtypt));
|
||||
content.append(String.format("<Encrypt><![CDATA[%s]]></Encrypt>",
|
||||
encrtypt));
|
||||
content.append("</xml>");
|
||||
}
|
||||
}
|
||||
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK,
|
||||
Consts.CONTENTTYPE$APPLICATION_XML));
|
||||
logger.info("{} encode weixin response:{}", encryptType, content);
|
||||
|
||||
@ -324,4 +324,6 @@ public final class WeixinServerBootstrap {
|
||||
messageDispatcher.openAlwaysResponse();
|
||||
return this;
|
||||
}
|
||||
|
||||
public final static String VERSION = "1.0.3";
|
||||
}
|
||||
@ -4,7 +4,7 @@ import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.DATE;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.SERVER;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.USER_AGENT;
|
||||
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
|
||||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
||||
import io.netty.buffer.Unpooled;
|
||||
@ -16,6 +16,8 @@ import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
||||
|
||||
/**
|
||||
* HTTP工具类
|
||||
*
|
||||
@ -27,6 +29,9 @@ import java.util.Date;
|
||||
*/
|
||||
public class HttpUtil {
|
||||
|
||||
private static String SERVER = "netty4";
|
||||
private static String WEIXIN4J = "weixin4j-server";
|
||||
|
||||
public static HttpResponse createHttpResponse(String content,
|
||||
HttpResponseStatus status, String contentType) {
|
||||
if (content == null) {
|
||||
@ -48,7 +53,11 @@ public class HttpUtil {
|
||||
content.getBytes(Consts.UTF_8).length);
|
||||
httpResponse.headers().set(CONNECTION, Values.KEEP_ALIVE);
|
||||
httpResponse.headers().set(DATE, new Date());
|
||||
httpResponse.headers().set(SERVER, "netty4");
|
||||
httpResponse.headers().set(SERVER, SERVER);
|
||||
httpResponse.headers()
|
||||
.set(USER_AGENT,
|
||||
String.format("%s/%s", WEIXIN4J,
|
||||
WeixinServerBootstrap.VERSION));
|
||||
return httpResponse;
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import java.util.Set;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.BlankMessageHandler;
|
||||
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
|
||||
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
|
||||
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
|
||||
@ -113,7 +112,7 @@ public class MessageServerStartup {
|
||||
}
|
||||
};
|
||||
new WeixinServerBootstrap(openid, token).addInterceptor(interceptor)
|
||||
.addHandler(BlankMessageHandler.global).startup();
|
||||
.openAlwaysResponse().startup();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user