fixed bugs

This commit is contained in:
jinyu
2015-06-29 17:03:29 +08:00
parent 735f897989
commit b706f80cee
14 changed files with 59 additions and 101 deletions
@@ -178,9 +178,16 @@ public class WeixinMessageDispatcher {
protected void noHandlerFound(ChannelHandlerContext context,
WeixinRequest request, Object message) {
if (isDebug) {
context.writeAndFlush(
new TextResponse(request.getOriginalContent()))
.addListener(ChannelFutureListener.CLOSE);
if (message instanceof String) {
context.writeAndFlush(
new TextResponse(request.getOriginalContent()
.replaceAll("\\!\\[CDATA\\[", "")
.replaceAll("\\]\\]", ""))).addListener(
ChannelFutureListener.CLOSE);
} else {
context.writeAndFlush(new TextResponse(message.toString()))
.addListener(ChannelFutureListener.CLOSE);
}
} else {
context.writeAndFlush(
HttpUtil.createHttpResponse(null, NOT_FOUND, null))
@@ -17,7 +17,6 @@ import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.MessageUtil;
import com.foxinmy.weixin4j.util.RandomUtil;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 微信回复编码类
@@ -53,8 +52,7 @@ public class WeixinResponseEncoder extends
messageTransfer.getFromUserName()));
content.append(String.format(
"<FromUserName><![CDATA[%s]]></FromUserName>",
StringUtil.isBlank(aesToken.getWeixinId()) ? messageTransfer
.getToUserName() : aesToken.getWeixinId()));
messageTransfer.getToUserName()));
content.append(String.format(
"<CreateTime><![CDATA[%d]]></CreateTime>",
System.currentTimeMillis() / 1000l));
@@ -64,8 +62,8 @@ public class WeixinResponseEncoder extends
content.append("</xml>");
if (encryptType == EncryptType.AES) {
String nonce = RandomUtil.generateString(32);
String timestamp = String
.valueOf(System.currentTimeMillis() / 1000l);
String timestamp = Long
.toString(System.currentTimeMillis() / 1000l);
String encrtypt = MessageUtil.aesEncrypt(
aesToken.getWeixinId(), aesToken.getAesKey(),
content.toString());
@@ -77,23 +77,24 @@ public final class WeixinServerBootstrap {
private final Map<String, AesToken> aesTokenMap;
/**
*
* 明文模式
*
* @param openid
* 微信号(原始ID)
* @param weixinid
* 微信号(原始ID)或者appid
* @param token
* 开发者token
*
*/
public WeixinServerBootstrap(String openid, String token) {
this(openid, token, null);
public WeixinServerBootstrap(String weixinid, String token) {
this(weixinid, token, null);
}
/**
* 兼容模式 & 密文模式
*
* @param appid
* 公众号的唯一ID
* 公众号的appid
* @param token
* 开发者填写的token
* @param aesKey
@@ -67,4 +67,10 @@ public class AesToken implements Serializable, Cloneable {
public String getAesKey() {
return aesKey;
}
@Override
public String toString() {
return "AesToken [weixinId=" + weixinId + ", token=" + token
+ ", aesKey=" + aesKey + "]";
}
}
@@ -39,6 +39,6 @@ public final class Base64 {
public static String encodeBase64String(final byte[] bytes) {
byte[] data = encodeBase64(bytes);
return HexUtil.encodeHexString(data);
return StringUtil.newStringUtf8(data).trim();
}
}
@@ -33,7 +33,7 @@ public final class MessageUtil {
*/
public static String signature(String... para) {
Arrays.sort(para);
StringBuilder sb = new StringBuilder();
StringBuffer sb = new StringBuffer();
for (String str : para) {
sb.append(str);
}
@@ -55,7 +55,7 @@ public final class MessageUtil {
public static String aesEncrypt(String appId, String encodingAesKey,
String xmlContent) throws WeixinException {
byte[] randomBytes = StringUtil.getBytesUtf8(RandomUtil
.generateString(16));
.generateString(32));
byte[] xmlBytes = StringUtil.getBytesUtf8(xmlContent);
int xmlLength = xmlBytes.length;
byte[] orderBytes = new byte[4];
@@ -64,6 +64,7 @@ public final class MessageUtil {
orderBytes[1] = (byte) (xmlLength >> 16 & 0xFF);
orderBytes[0] = (byte) (xmlLength >> 24 & 0xFF);
byte[] appidBytes = StringUtil.getBytesUtf8(appId);
int byteLength = randomBytes.length + xmlLength + orderBytes.length
+ appidBytes.length;
// ... + pad: 使用自定义的填充方式对明文进行补位填充