优化MchPayRequest & ServerToolkits
This commit is contained in:
+1
-2
@@ -33,7 +33,6 @@ import com.foxinmy.weixin4j.response.BlankResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||
import com.foxinmy.weixin4j.util.ClassUtil;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.HttpUtil;
|
||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
|
||||
@@ -352,7 +351,7 @@ public class WeixinMessageDispatcher {
|
||||
}
|
||||
try {
|
||||
Source source = new StreamSource(new ByteArrayInputStream(
|
||||
message.getBytes(Consts.UTF_8)));
|
||||
ServerToolkits.getBytesUtf8(message)));
|
||||
JAXBElement<M> jaxbElement = getUnmarshaller(clazz).unmarshal(
|
||||
source, clazz);
|
||||
return jaxbElement.getValue();
|
||||
|
||||
+2
-2
@@ -10,8 +10,8 @@ import java.util.List;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.response.SingleResponse;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.HttpUtil;
|
||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
|
||||
/**
|
||||
* 单一回复编码类
|
||||
@@ -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$TEXT_PLAIN));
|
||||
ServerToolkits.CONTENTTYPE$TEXT_PLAIN));
|
||||
logger.info("encode single response:{}", content);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.util.AesToken;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
|
||||
@@ -44,7 +43,7 @@ public class WeixinMessageDecoder extends
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, FullHttpRequest req,
|
||||
List<Object> out) throws WeixinException {
|
||||
String messageContent = req.content().toString(Consts.UTF_8);
|
||||
String messageContent = req.content().toString(ServerToolkits.UTF_8);
|
||||
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri(),
|
||||
true);
|
||||
String methodName = req.getMethod().name();
|
||||
|
||||
@@ -16,7 +16,6 @@ import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.SingleResponse;
|
||||
import com.foxinmy.weixin4j.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.util.AesToken;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.HttpUtil;
|
||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
@@ -112,7 +111,7 @@ public class WeixinRequestHandler extends
|
||||
}
|
||||
WeixinMessageTransfer messageTransfer = MessageTransferHandler
|
||||
.parser(request);
|
||||
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
|
||||
ctx.channel().attr(ServerToolkits.MESSAGE_TRANSFER_KEY).set(messageTransfer);
|
||||
messageDispatcher.doDispatch(ctx, request, messageTransfer);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -12,7 +12,6 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.util.AesToken;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.HttpUtil;
|
||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
@@ -38,7 +37,7 @@ public class WeixinResponseEncoder extends
|
||||
protected void encode(ChannelHandlerContext ctx, WeixinResponse response,
|
||||
List<Object> out) throws WeixinException {
|
||||
WeixinMessageTransfer messageTransfer = ctx.channel()
|
||||
.attr(Consts.MESSAGE_TRANSFER_KEY).get();
|
||||
.attr(ServerToolkits.MESSAGE_TRANSFER_KEY).get();
|
||||
EncryptType encryptType = messageTransfer.getEncryptType();
|
||||
StringBuilder content = new StringBuilder();
|
||||
content.append("<xml>");
|
||||
@@ -76,7 +75,7 @@ public class WeixinResponseEncoder extends
|
||||
content.append("</xml>");
|
||||
}
|
||||
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK,
|
||||
Consts.CONTENTTYPE$APPLICATION_XML));
|
||||
ServerToolkits.CONTENTTYPE$APPLICATION_XML));
|
||||
logger.info("{} encode weixin response:{}", encryptType, content);
|
||||
}
|
||||
}
|
||||
@@ -40,10 +40,10 @@ public final class ClassUtil {
|
||||
String packageFileName = packageName.replace(".", File.separator);
|
||||
URL fullPath = getDefaultClassLoader().getResource(packageFileName);
|
||||
String protocol = fullPath.getProtocol();
|
||||
if (protocol.equals(Consts.PROTOCOL_FILE)) {
|
||||
if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) {
|
||||
File dir = new File(fullPath.getPath());
|
||||
return findClassesByFile(dir, packageName);
|
||||
} else if (protocol.equals(Consts.PROTOCOL_JAR)) {
|
||||
} else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) {
|
||||
try {
|
||||
return findClassesByJar(
|
||||
((JarURLConnection) fullPath.openConnection())
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.foxinmy.weixin4j.util;
|
||||
|
||||
import io.netty.util.AttributeKey;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||
|
||||
/**
|
||||
* 常量类
|
||||
*
|
||||
* @className Consts
|
||||
* @author jy
|
||||
* @date 2015年4月19日
|
||||
* @since JDK 1.6
|
||||
* @see
|
||||
*/
|
||||
public final class Consts {
|
||||
|
||||
public static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
public static final Charset GBK = Charset.forName("GBK");
|
||||
public static final String SUCCESS = "SUCCESS";
|
||||
public static final String FAIL = "FAIL";
|
||||
public static final String SunX509 = "SunX509";
|
||||
public static final String JKS = "JKS";
|
||||
public static final String PKCS12 = "PKCS12";
|
||||
public static final String TLS = "TLS";
|
||||
public static final String X509 = "X.509";
|
||||
public static final String AES = "AES";
|
||||
public static final String MD5 = "MD5";
|
||||
public static final String SHA = "SHA";
|
||||
public static final String SHA1 = "SHA-1";
|
||||
public static final String PROTOCOL_FILE = "file";
|
||||
public static final String PROTOCOL_JAR = "jar";
|
||||
public static final String CONTENTTYPE$APPLICATION_XML = "application/xml";
|
||||
public static final String CONTENTTYPE$TEXT_PLAIN = "text/plain";
|
||||
|
||||
public static final AttributeKey<WeixinMessageTransfer> MESSAGE_TRANSFER_KEY = AttributeKey
|
||||
.valueOf("$_MESSAGETRANSFER");
|
||||
}
|
||||
@@ -62,13 +62,13 @@ public class HttpUtil {
|
||||
FullHttpResponse httpResponse = null;
|
||||
|
||||
httpResponse = new DefaultFullHttpResponse(HTTP_1_1, status,
|
||||
Unpooled.copiedBuffer(content, Consts.UTF_8));
|
||||
Unpooled.copiedBuffer(content, ServerToolkits.UTF_8));
|
||||
httpResponse.headers().set(
|
||||
CONTENT_TYPE,
|
||||
String.format("%s;encoding=%s", contentType,
|
||||
Consts.UTF_8.displayName()));
|
||||
ServerToolkits.UTF_8.displayName()));
|
||||
httpResponse.headers().set(CONTENT_LENGTH,
|
||||
content.getBytes(Consts.UTF_8).length);
|
||||
content.getBytes(ServerToolkits.UTF_8).length);
|
||||
createHeaders(httpResponse);
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ public final class MessageUtil {
|
||||
byte[] aesKey = NettyBase64.decodeBase64(encodingAesKey + "=");
|
||||
// 设置加密模式为AES的CBC模式
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
||||
SecretKeySpec keySpec = new SecretKeySpec(aesKey, Consts.AES);
|
||||
SecretKeySpec keySpec = new SecretKeySpec(aesKey, ServerToolkits.AES);
|
||||
IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
|
||||
// 加密
|
||||
@@ -124,7 +124,7 @@ public final class MessageUtil {
|
||||
try {
|
||||
// 设置解密模式为AES的CBC模式
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
||||
SecretKeySpec key_spec = new SecretKeySpec(aesKey, Consts.AES);
|
||||
SecretKeySpec key_spec = new SecretKeySpec(aesKey, ServerToolkits.AES);
|
||||
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey,
|
||||
0, 16));
|
||||
cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
|
||||
|
||||
@@ -50,7 +50,7 @@ public class PKCS7Encoder {
|
||||
for (int index = 0; index < amountToPad; index++) {
|
||||
tmp.append(padChr);
|
||||
}
|
||||
return tmp.toString().getBytes(Consts.UTF_8);
|
||||
return tmp.toString().getBytes(ServerToolkits.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.foxinmy.weixin4j.util;
|
||||
|
||||
import io.netty.util.AttributeKey;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -7,6 +9,8 @@ import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Random;
|
||||
|
||||
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||
|
||||
/**
|
||||
* 工具包
|
||||
*
|
||||
@@ -18,6 +22,16 @@ import java.util.Random;
|
||||
*/
|
||||
public final class ServerToolkits {
|
||||
private static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
public static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
public static final String AES = "AES";
|
||||
public static final String SHA1 = "SHA-1";
|
||||
public static final String PROTOCOL_FILE = "file";
|
||||
public static final String PROTOCOL_JAR = "jar";
|
||||
public static final String CONTENTTYPE$APPLICATION_XML = "application/xml";
|
||||
public static final String CONTENTTYPE$TEXT_PLAIN = "text/plain";
|
||||
|
||||
public static final AttributeKey<WeixinMessageTransfer> MESSAGE_TRANSFER_KEY = AttributeKey
|
||||
.valueOf("$_MESSAGETRANSFER");
|
||||
|
||||
/**
|
||||
* 返回一个定长的随机字符串(包含数字和大小写字母)
|
||||
@@ -57,8 +71,8 @@ public final class ServerToolkits {
|
||||
public static String digestSHA1(String content) {
|
||||
byte[] data = ServerToolkits.getBytesUtf8(content);
|
||||
try {
|
||||
return HexUtil.encodeHexString(MessageDigest.getInstance(
|
||||
Consts.SHA1).digest(data));
|
||||
return HexUtil.encodeHexString(MessageDigest.getInstance(SHA1)
|
||||
.digest(data));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
return null;
|
||||
}
|
||||
@@ -69,11 +83,11 @@ public final class ServerToolkits {
|
||||
}
|
||||
|
||||
public static byte[] getBytesUtf8(final String content) {
|
||||
return content != null ? content.getBytes(Consts.UTF_8) : null;
|
||||
return content != null ? content.getBytes(UTF_8) : null;
|
||||
}
|
||||
|
||||
public static String newStringUtf8(final byte[] bytes) {
|
||||
return newString(bytes, Consts.UTF_8);
|
||||
return newString(bytes, UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,7 +10,7 @@ import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
|
||||
/**
|
||||
* 获取加密的密文内容
|
||||
@@ -71,7 +71,7 @@ public class EncryptMessageHandler extends DefaultHandler {
|
||||
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
||||
xmlReader.setContentHandler(global);
|
||||
xmlReader.parse(new InputSource(new ByteArrayInputStream(xmlContent
|
||||
.getBytes(Consts.UTF_8))));
|
||||
.getBytes(ServerToolkits.UTF_8))));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (SAXException e) {
|
||||
|
||||
@@ -14,7 +14,6 @@ import org.xml.sax.helpers.XMLReaderFactory;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
|
||||
/**
|
||||
@@ -90,7 +89,7 @@ public class MessageTransferHandler extends DefaultHandler {
|
||||
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
||||
xmlReader.setContentHandler(global);
|
||||
xmlReader.parse(new InputSource(new ByteArrayInputStream(request
|
||||
.getOriginalContent().getBytes(Consts.UTF_8))));
|
||||
.getOriginalContent().getBytes(ServerToolkits.UTF_8))));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (SAXException e) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.foxinmy.weixin4j.server.test;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.StatusLine;
|
||||
@@ -13,7 +14,7 @@ import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import com.foxinmy.weixin4j.util.Consts;
|
||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||
|
||||
/**
|
||||
* 发送消息请求到服务器
|
||||
@@ -69,6 +70,6 @@ public class MessagePush {
|
||||
throw new IOException(Integer.toString(status) + "uri moved");
|
||||
}
|
||||
return EntityUtils.toString(httpResponse.getEntity(),
|
||||
Consts.UTF_8);
|
||||
ServerToolkits.UTF_8);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user