fixed一些bug

This commit is contained in:
jinyu
2015-06-26 00:18:08 +08:00
parent f6b1f76210
commit 663cba9677
10 changed files with 94 additions and 162 deletions
@@ -29,6 +29,7 @@ import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.TextResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.util.ClassUtil;
@@ -89,6 +90,11 @@ public class WeixinMessageDispatcher {
*/
private Map<Class<?>, Unmarshaller> messageUnmarshaller;
/**
* 开启debug:未匹配到MessageHanlder输出消息信息
*/
private boolean isDebug;
public WeixinMessageDispatcher() {
this(new DefaultMessageMatcher());
}
@@ -105,8 +111,8 @@ public class WeixinMessageDispatcher {
* 上下文环境
* @param request
* 微信请求
* @param messageKey
* 消息的key
* @param cruxMessage
* 微信的关键消息
* @throws WeixinException
*/
public void doDispatch(final ChannelHandlerContext context,
@@ -171,9 +177,15 @@ public class WeixinMessageDispatcher {
*/
protected void noHandlerFound(ChannelHandlerContext context,
WeixinRequest request, Object message) {
context.writeAndFlush(
HttpUtil.createHttpResponse(null, NOT_FOUND, null))
.addListener(ChannelFutureListener.CLOSE);
if (isDebug) {
context.writeAndFlush(
new TextResponse(request.getOriginalContent()))
.addListener(ChannelFutureListener.CLOSE);
} else {
context.writeAndFlush(
HttpUtil.createHttpResponse(null, NOT_FOUND, null))
.addListener(ChannelFutureListener.CLOSE);
}
}
/**
@@ -242,8 +254,8 @@ public class WeixinMessageDispatcher {
}
if (beanFactory != null) {
for (Class<?> clazz : messageHandlerClass) {
messageHandlerList
.add((WeixinMessageHandler) beanFactory
messageHandlerList.add(0,
(WeixinMessageHandler) beanFactory
.getBean(clazz));
}
} else {
@@ -256,8 +268,9 @@ public class WeixinMessageDispatcher {
Constructor<?> ctor = clazz
.getDeclaredConstructor();
ReflectionUtil.makeAccessible(ctor);
messageHandlerList.add((WeixinMessageHandler) ctor
.newInstance((Object[]) null));
messageHandlerList.add(0,
(WeixinMessageHandler) ctor
.newInstance((Object[]) null));
} catch (Exception ex) {
throw new WeixinException(clazz.getName()
+ " instantiate fail", ex);
@@ -293,8 +306,8 @@ public class WeixinMessageDispatcher {
}
if (beanFactory != null) {
for (Class<?> clazz : messageInterceptorClass) {
messageInterceptorList
.add((WeixinMessageInterceptor) beanFactory
messageInterceptorList.add(0,
(WeixinMessageInterceptor) beanFactory
.getBean(clazz));
}
} else {
@@ -307,8 +320,8 @@ public class WeixinMessageDispatcher {
Constructor<?> ctor = clazz
.getDeclaredConstructor();
ReflectionUtil.makeAccessible(ctor);
messageInterceptorList
.add((WeixinMessageInterceptor) ctor
messageInterceptorList.add(0,
(WeixinMessageInterceptor) ctor
.newInstance((Object[]) null));
} catch (Exception ex) {
throw new WeixinException(clazz.getName()
@@ -432,4 +445,8 @@ public class WeixinMessageDispatcher {
public WeixinMessageMatcher getMessageMatcher() {
return this.messageMatcher;
}
public void openDebugMode() {
isDebug = true;
}
}
@@ -1,6 +1,8 @@
package com.foxinmy.weixin4j.request;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken;
@@ -65,11 +67,15 @@ public class WeixinRequest implements Serializable, Cloneable {
* aes & token
*/
private AesToken aesToken;
/**
* url parameter
*/
private Map<String, List<String>> parameters;
public WeixinRequest(String method, EncryptType encryptType,
String echoStr, String timeStamp, String nonce, String signature,
String msgSignature, String originalContent, String encryptContent,
AesToken aesToken) {
AesToken aesToken, Map<String, List<String>> parameters) {
this.method = method;
this.encryptType = encryptType;
this.echoStr = echoStr;
@@ -80,6 +86,7 @@ public class WeixinRequest implements Serializable, Cloneable {
this.originalContent = originalContent;
this.encryptContent = encryptContent;
this.aesToken = aesToken;
this.parameters = parameters;
}
public String getMethod() {
@@ -122,6 +129,10 @@ public class WeixinRequest implements Serializable, Cloneable {
return aesToken;
}
public Map<String, List<String>> getParameters() {
return parameters;
}
@Override
public String toString() {
return "WeixinRequest [encryptContent=" + encryptContent
@@ -129,6 +140,6 @@ public class WeixinRequest implements Serializable, Cloneable {
+ ", timeStamp=" + timeStamp + ", nonce=" + nonce
+ ", signature=" + signature + ", originalContent="
+ originalContent + ", method=" + method + ", aesToken="
+ aesToken + "]";
+ aesToken + ", parameters=" + parameters + "]";
}
}
@@ -67,6 +67,11 @@ public class WeixinMessageDecoder extends
String weixinId = parameters.containsKey("weixin_id") ? parameters.get(
"weixin_id").get(0) : null;
AesToken aesToken = aesTokenMap.get(weixinId);
if (aesToken == null) { //
AesToken _aesToken = aesTokenMap.get(null);
aesToken = new AesToken(weixinId, _aesToken.getToken(),
_aesToken.getAesKey());
}
String originalContent = content;
String encryptContent = null;
if (!content.isEmpty() && encryptType == EncryptType.AES) {
@@ -81,6 +86,6 @@ public class WeixinMessageDecoder extends
}
out.add(new WeixinRequest(methodName, encryptType, echoStr, timeStamp,
nonce, signature, msgSignature, originalContent,
encryptContent, aesToken));
encryptContent, aesToken, parameters));
}
}
@@ -43,10 +43,6 @@ public class WeixinResponseEncoder extends
.attr(Consts.MESSAGE_TRANSFER_KEY).get();
AesToken aesToken = messageTransfer.getAesToken();
EncryptType encryptType = messageTransfer.getEncryptType();
String weixinId = aesToken.getWeixinId();
if (StringUtil.isBlank(weixinId)) {
weixinId = messageTransfer.getToUserName();
}
StringBuilder content = new StringBuilder();
if (response instanceof BlankResponse) {
content.append(response.toContent());
@@ -56,7 +52,9 @@ public class WeixinResponseEncoder extends
"<ToUserName><![CDATA[%s]]></ToUserName>",
messageTransfer.getFromUserName()));
content.append(String.format(
"<FromUserName><![CDATA[%s]]></FromUserName>", weixinId));
"<FromUserName><![CDATA[%s]]></FromUserName>",
StringUtil.isBlank(aesToken.getWeixinId()) ? messageTransfer
.getToUserName() : aesToken.getWeixinId()));
content.append(String.format(
"<CreateTime><![CDATA[%d]]></CreateTime>",
System.currentTimeMillis() / 1000l));
@@ -68,8 +66,9 @@ public class WeixinResponseEncoder extends
String nonce = RandomUtil.generateString(32);
String timestamp = String
.valueOf(System.currentTimeMillis() / 1000l);
String encrtypt = MessageUtil.aesEncrypt(weixinId,
aesToken.getAesKey(), content.toString());
String encrtypt = MessageUtil.aesEncrypt(
aesToken.getWeixinId(), aesToken.getAesKey(),
content.toString());
String msgSignature = MessageUtil.signature(
aesToken.getToken(), nonce, timestamp, encrtypt);
content.delete(0, content.length());
@@ -61,7 +61,6 @@ public final class WeixinServerBootstrap {
* 消息分发器
*/
private WeixinMessageDispatcher messageDispatcher;
/**
* 消息处理器
*/
@@ -289,4 +288,12 @@ public final class WeixinServerBootstrap {
messageDispatcher.registMessageClass(messageKey, messageClass);
return this;
}
/**
* 开启debug:未匹配到MessageHanlder输出消息信息
*/
public WeixinServerBootstrap openDebugMode() {
messageDispatcher.openDebugMode();
return this;
}
}
@@ -11,7 +11,7 @@ import java.io.Serializable;
* @since JDK 1.7
* @see
*/
public class AesToken implements Serializable {
public class AesToken implements Serializable, Cloneable {
private static final long serialVersionUID = -6001008896414323534L;
@@ -145,8 +145,7 @@ public final class MessageUtil {
fromAppId = StringUtil.newStringUtf8(Arrays.copyOfRange(bytes,
20 + xmlLength, bytes.length));
} catch (Exception e) {
throw new WeixinException("-40008", "公众平台发送的xml不合法"
+ e.getMessage());
throw new WeixinException("-40008", "xml内容不合法" + e.getMessage());
}
// 校验appId是否一致
if (!fromAppId.trim().equals(appId)) {