删除BlankMessageHandler类,新增SingleContentResponse类
This commit is contained in:
parent
5bf612c9c5
commit
3644b73608
@ -70,4 +70,6 @@
|
|||||||
|
|
||||||
* 2015-08-03
|
* 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
|
@Override
|
||||||
public WeixinResponse doHandle(WeixinRequest request, Object message,
|
public WeixinResponse doHandle(WeixinRequest request, Object message,
|
||||||
Set<String> nodeNames) throws WeixinException {
|
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
|
* @since JDK 1.7
|
||||||
* @see
|
* @see
|
||||||
*/
|
*/
|
||||||
public class BlankResponse implements WeixinResponse {
|
public class BlankResponse extends SingleContentResponse {
|
||||||
|
|
||||||
public static final BlankResponse global = new BlankResponse();
|
public static final BlankResponse global = new BlankResponse();
|
||||||
|
|
||||||
private BlankResponse(){
|
private BlankResponse() {
|
||||||
|
super("success");
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMsgType() {
|
|
||||||
return "blank";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toContent() {
|
|
||||||
return "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 {
|
List<Object> out) throws WeixinException {
|
||||||
String content = response.toContent();
|
String content = response.toContent();
|
||||||
ctx.writeAndFlush(HttpUtil.createHttpResponse(content, OK,
|
ctx.writeAndFlush(HttpUtil.createHttpResponse(content, OK,
|
||||||
Consts.CONTENTTYPE$APPLICATION_XML));
|
Consts.CONTENTTYPE$TEXT_PLAIN));
|
||||||
logger.info("encode single response:{}", content);
|
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.FORBIDDEN;
|
||||||
import static io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;
|
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.ChannelFutureListener;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
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.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.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,10 +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.writeAndFlush(
|
ctx.write(new SingleContentResponse(request.getEchoStr()));
|
||||||
HttpUtil.createHttpResponse(request.getEchoStr(), OK,
|
|
||||||
Consts.CONTENTTYPE$TEXT_PLAIN)).addListener(
|
|
||||||
ChannelFutureListener.CLOSE);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ctx.writeAndFlush(
|
ctx.writeAndFlush(
|
||||||
@ -98,9 +95,9 @@ public class WeixinRequestHandler extends
|
|||||||
}
|
}
|
||||||
CruxMessageHandler cruxMessage = CruxMessageHandler.parser(request
|
CruxMessageHandler cruxMessage = CruxMessageHandler.parser(request
|
||||||
.getOriginalContent());
|
.getOriginalContent());
|
||||||
WeixinMessageTransfer messageTransfer = new WeixinMessageTransfer(aesToken,
|
WeixinMessageTransfer messageTransfer = new WeixinMessageTransfer(
|
||||||
request.getEncryptType(), cruxMessage.getToUserName(),
|
aesToken, request.getEncryptType(),
|
||||||
cruxMessage.getFromUserName());
|
cruxMessage.getToUserName(), cruxMessage.getFromUserName());
|
||||||
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
|
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
|
||||||
messageDispatcher.doDispatch(ctx, request, cruxMessage);
|
messageDispatcher.doDispatch(ctx, request, cruxMessage);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.response.BlankResponse;
|
|
||||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.type.EncryptType;
|
import com.foxinmy.weixin4j.type.EncryptType;
|
||||||
import com.foxinmy.weixin4j.util.AesToken;
|
import com.foxinmy.weixin4j.util.AesToken;
|
||||||
@ -43,45 +42,38 @@ public class WeixinResponseEncoder extends
|
|||||||
AesToken aesToken = messageTransfer.getAesToken();
|
AesToken aesToken = messageTransfer.getAesToken();
|
||||||
EncryptType encryptType = messageTransfer.getEncryptType();
|
EncryptType encryptType = messageTransfer.getEncryptType();
|
||||||
StringBuilder content = new StringBuilder();
|
StringBuilder content = new StringBuilder();
|
||||||
if (response instanceof BlankResponse) {
|
content.append("<xml>");
|
||||||
content.append(response.toContent());
|
content.append(String.format("<ToUserName><![CDATA[%s]]></ToUserName>",
|
||||||
} else {
|
messageTransfer.getFromUserName()));
|
||||||
|
content.append(String.format(
|
||||||
|
"<FromUserName><![CDATA[%s]]></FromUserName>",
|
||||||
|
messageTransfer.getToUserName()));
|
||||||
|
content.append(String.format("<CreateTime><![CDATA[%d]]></CreateTime>",
|
||||||
|
System.currentTimeMillis() / 1000l));
|
||||||
|
content.append(String.format("<MsgType><![CDATA[%s]]></MsgType>",
|
||||||
|
response.getMsgType()));
|
||||||
|
content.append(response.toContent());
|
||||||
|
content.append("</xml>");
|
||||||
|
if (encryptType == EncryptType.AES) {
|
||||||
|
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);
|
||||||
|
content.delete(0, content.length());
|
||||||
content.append("<xml>");
|
content.append("<xml>");
|
||||||
|
content.append(String
|
||||||
|
.format("<Nonce><![CDATA[%s]]></Nonce>", nonce));
|
||||||
content.append(String.format(
|
content.append(String.format(
|
||||||
"<ToUserName><![CDATA[%s]]></ToUserName>",
|
"<TimeStamp><![CDATA[%s]]></TimeStamp>", timestamp));
|
||||||
messageTransfer.getFromUserName()));
|
content.append(String
|
||||||
content.append(String.format(
|
.format("<MsgSignature><![CDATA[%s]]></MsgSignature>",
|
||||||
"<FromUserName><![CDATA[%s]]></FromUserName>",
|
msgSignature));
|
||||||
messageTransfer.getToUserName()));
|
content.append(String.format("<Encrypt><![CDATA[%s]]></Encrypt>",
|
||||||
content.append(String.format(
|
encrtypt));
|
||||||
"<CreateTime><![CDATA[%d]]></CreateTime>",
|
|
||||||
System.currentTimeMillis() / 1000l));
|
|
||||||
content.append(String.format("<MsgType><![CDATA[%s]]></MsgType>",
|
|
||||||
response.getMsgType()));
|
|
||||||
content.append(response.toContent());
|
|
||||||
content.append("</xml>");
|
content.append("</xml>");
|
||||||
if (encryptType == EncryptType.AES) {
|
|
||||||
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);
|
|
||||||
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>");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK,
|
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK,
|
||||||
Consts.CONTENTTYPE$APPLICATION_XML));
|
Consts.CONTENTTYPE$APPLICATION_XML));
|
||||||
|
|||||||
@ -324,4 +324,6 @@ public final class WeixinServerBootstrap {
|
|||||||
messageDispatcher.openAlwaysResponse();
|
messageDispatcher.openAlwaysResponse();
|
||||||
return this;
|
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_LENGTH;
|
||||||
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
|
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.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.HttpResponseStatus.OK;
|
||||||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
@ -16,6 +16,8 @@ import io.netty.handler.codec.http.HttpResponseStatus;
|
|||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP工具类
|
* HTTP工具类
|
||||||
*
|
*
|
||||||
@ -27,6 +29,9 @@ import java.util.Date;
|
|||||||
*/
|
*/
|
||||||
public class HttpUtil {
|
public class HttpUtil {
|
||||||
|
|
||||||
|
private static String SERVER = "netty4";
|
||||||
|
private static String WEIXIN4J = "weixin4j-server";
|
||||||
|
|
||||||
public static HttpResponse createHttpResponse(String content,
|
public static HttpResponse createHttpResponse(String content,
|
||||||
HttpResponseStatus status, String contentType) {
|
HttpResponseStatus status, String contentType) {
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
@ -48,7 +53,11 @@ public class HttpUtil {
|
|||||||
content.getBytes(Consts.UTF_8).length);
|
content.getBytes(Consts.UTF_8).length);
|
||||||
httpResponse.headers().set(CONNECTION, Values.KEEP_ALIVE);
|
httpResponse.headers().set(CONNECTION, Values.KEEP_ALIVE);
|
||||||
httpResponse.headers().set(DATE, new Date());
|
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;
|
return httpResponse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@ import io.netty.channel.ChannelHandlerContext;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.handler.BlankMessageHandler;
|
|
||||||
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
|
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
|
||||||
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
|
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
|
||||||
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
|
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
|
||||||
@ -113,7 +112,7 @@ public class MessageServerStartup {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
new WeixinServerBootstrap(openid, token).addInterceptor(interceptor)
|
new WeixinServerBootstrap(openid, token).addInterceptor(interceptor)
|
||||||
.addHandler(BlankMessageHandler.global).startup();
|
.openAlwaysResponse().startup();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user