优化MchPayRequest & ServerToolkits
This commit is contained in:
parent
1fc5a34369
commit
66bd25bb58
@ -130,13 +130,14 @@ public class Pay3Api {
|
|||||||
PrePay prePay = createPrePay(payPackage);
|
PrePay prePay = createPrePay(payPackage);
|
||||||
String tradeType = payPackage.getTradeType();
|
String tradeType = payPackage.getTradeType();
|
||||||
if (TradeType.APP.name().equalsIgnoreCase(tradeType)) {
|
if (TradeType.APP.name().equalsIgnoreCase(tradeType)) {
|
||||||
return new APPPayRequest(prePay, weixinAccount);
|
return new APPPayRequest(prePay.getPrepayId(), weixinAccount);
|
||||||
} else if (TradeType.JSAPI.name().equalsIgnoreCase(tradeType)) {
|
} else if (TradeType.JSAPI.name().equalsIgnoreCase(tradeType)) {
|
||||||
return new JSAPIPayRequest(prePay, weixinAccount);
|
return new JSAPIPayRequest(prePay.getPrepayId(), weixinAccount);
|
||||||
} else if (TradeType.NATIVE.name().equalsIgnoreCase(tradeType)) {
|
} else if (TradeType.NATIVE.name().equalsIgnoreCase(tradeType)) {
|
||||||
return new NATIVEPayRequest(prePay, weixinAccount);
|
return new NATIVEPayRequest(prePay.getPrepayId(),
|
||||||
|
prePay.getCodeUrl(), weixinAccount);
|
||||||
} else if (TradeType.WAP.name().equalsIgnoreCase(tradeType)) {
|
} else if (TradeType.WAP.name().equalsIgnoreCase(tradeType)) {
|
||||||
return new WAPPayRequest(prePay, weixinAccount);
|
return new WAPPayRequest(prePay.getPrepayId(), weixinAccount);
|
||||||
} else if (TradeType.MICROPAY.name().equalsIgnoreCase(tradeType)) {
|
} else if (TradeType.MICROPAY.name().equalsIgnoreCase(tradeType)) {
|
||||||
throw new WeixinPayException("maybe use createMicroPay method?");
|
throw new WeixinPayException("maybe use createMicroPay method?");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -19,8 +19,8 @@ import com.foxinmy.weixin4j.util.MapUtil;
|
|||||||
*/
|
*/
|
||||||
public class APPPayRequest extends AbstractPayRequest {
|
public class APPPayRequest extends AbstractPayRequest {
|
||||||
|
|
||||||
public APPPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
|
public APPPayRequest(String prePayId, WeixinPayAccount payAccount) {
|
||||||
super(prePay, payAccount);
|
super(prePayId, payAccount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,7 +30,7 @@ public class APPPayRequest extends AbstractPayRequest {
|
|||||||
public PayRequest toRequestObject() {
|
public PayRequest toRequestObject() {
|
||||||
PayRequest payRequest = new PayRequest(getPayAccount().getId(),
|
PayRequest payRequest = new PayRequest(getPayAccount().getId(),
|
||||||
"Sign=WXPay");
|
"Sign=WXPay");
|
||||||
payRequest.setPrepayId(getPrePay().getPrepayId());
|
payRequest.setPrepayId(getPrePayId());
|
||||||
payRequest.setPartnerId(getPayAccount().getPartnerId());
|
payRequest.setPartnerId(getPayAccount().getPartnerId());
|
||||||
return payRequest;
|
return payRequest;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,30 +1,24 @@
|
|||||||
package com.foxinmy.weixin4j.payment.mch;
|
package com.foxinmy.weixin4j.payment.mch;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.foxinmy.weixin4j.model.WeixinPayAccount;
|
import com.foxinmy.weixin4j.model.WeixinPayAccount;
|
||||||
|
|
||||||
public abstract class AbstractPayRequest implements MchPayRequest {
|
public abstract class AbstractPayRequest implements MchPayRequest {
|
||||||
|
|
||||||
private final PrePay prePay;
|
private final String prePayId;
|
||||||
private final WeixinPayAccount payAccount;
|
private final WeixinPayAccount payAccount;
|
||||||
|
|
||||||
public AbstractPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
|
public AbstractPayRequest(String prePayId, WeixinPayAccount payAccount) {
|
||||||
this.prePay = prePay;
|
this.prePayId = prePayId;
|
||||||
this.payAccount = payAccount;
|
this.payAccount = payAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PrePay getPrePay() {
|
public String getPrePayId() {
|
||||||
return this.prePay;
|
return this.prePayId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WeixinPayAccount getPayAccount() {
|
public WeixinPayAccount getPayAccount() {
|
||||||
return this.payAccount;
|
return this.payAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toRequestString() {
|
|
||||||
return JSON.toJSONString(toRequestObject());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package com.foxinmy.weixin4j.payment.mch;
|
package com.foxinmy.weixin4j.payment.mch;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.foxinmy.weixin4j.model.WeixinPayAccount;
|
import com.foxinmy.weixin4j.model.WeixinPayAccount;
|
||||||
import com.foxinmy.weixin4j.payment.PayRequest;
|
import com.foxinmy.weixin4j.payment.PayRequest;
|
||||||
import com.foxinmy.weixin4j.type.SignType;
|
import com.foxinmy.weixin4j.type.SignType;
|
||||||
@ -24,17 +25,22 @@ import com.foxinmy.weixin4j.util.DigestUtil;
|
|||||||
*/
|
*/
|
||||||
public class JSAPIPayRequest extends AbstractPayRequest {
|
public class JSAPIPayRequest extends AbstractPayRequest {
|
||||||
|
|
||||||
public JSAPIPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
|
public JSAPIPayRequest(String prePayId, WeixinPayAccount payAccount) {
|
||||||
super(prePay, payAccount);
|
super(prePayId, payAccount);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PayRequest toRequestObject() {
|
public PayRequest toRequestObject() {
|
||||||
PayRequest payRequest = new PayRequest(getPayAccount().getId(),
|
PayRequest payRequest = new PayRequest(getPayAccount().getId(),
|
||||||
"prepay_id=" + getPrePay().getPrepayId());
|
"prepay_id=" + getPrePayId());
|
||||||
payRequest.setSignType(SignType.MD5);
|
payRequest.setSignType(SignType.MD5);
|
||||||
payRequest.setPaySign(DigestUtil.paysignMd5(payRequest, getPayAccount()
|
payRequest.setPaySign(DigestUtil.paysignMd5(payRequest, getPayAccount()
|
||||||
.getPaySignKey()));
|
.getPaySignKey()));
|
||||||
return payRequest;
|
return payRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toRequestString() {
|
||||||
|
return JSON.toJSONString(toRequestObject());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,11 +17,11 @@ import com.foxinmy.weixin4j.payment.PayRequest;
|
|||||||
*/
|
*/
|
||||||
public interface MchPayRequest {
|
public interface MchPayRequest {
|
||||||
/**
|
/**
|
||||||
* 预支付对象
|
* 预支付交易ID
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public PrePay getPrePay();
|
public String getPrePayId();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 商户信息
|
* 商户信息
|
||||||
|
|||||||
@ -17,8 +17,12 @@ import com.foxinmy.weixin4j.payment.PayRequest;
|
|||||||
*/
|
*/
|
||||||
public class NATIVEPayRequest extends AbstractPayRequest {
|
public class NATIVEPayRequest extends AbstractPayRequest {
|
||||||
|
|
||||||
public NATIVEPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
|
private final String codeUrl;
|
||||||
super(prePay, payAccount);
|
|
||||||
|
public NATIVEPayRequest(String prePayId, String codeUrl,
|
||||||
|
WeixinPayAccount payAccount) {
|
||||||
|
super(prePayId, payAccount);
|
||||||
|
this.codeUrl = codeUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,12 +30,11 @@ public class NATIVEPayRequest extends AbstractPayRequest {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public PayRequest toRequestObject() {
|
public PayRequest toRequestObject() {
|
||||||
return new PayRequest(getPayAccount().getId(), "code_url="
|
return new PayRequest(getPayAccount().getId(), "code_url=" + codeUrl);
|
||||||
+ getPrePay().getCodeUrl());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toRequestString() {
|
public String toRequestString() {
|
||||||
return getPrePay().getCodeUrl();
|
return this.codeUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,8 +22,8 @@ import com.foxinmy.weixin4j.util.URLEncodingUtil;
|
|||||||
*/
|
*/
|
||||||
public class WAPPayRequest extends AbstractPayRequest {
|
public class WAPPayRequest extends AbstractPayRequest {
|
||||||
|
|
||||||
public WAPPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
|
public WAPPayRequest(String prePayId, WeixinPayAccount payAccount) {
|
||||||
super(prePay, payAccount);
|
super(prePayId, payAccount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,7 +32,7 @@ public class WAPPayRequest extends AbstractPayRequest {
|
|||||||
@Override
|
@Override
|
||||||
public PayRequest toRequestObject() {
|
public PayRequest toRequestObject() {
|
||||||
PayRequest payRequest = new PayRequest(getPayAccount().getId(), "WAP");
|
PayRequest payRequest = new PayRequest(getPayAccount().getId(), "WAP");
|
||||||
payRequest.setPrepayId(getPrePay().getPrepayId());
|
payRequest.setPrepayId(getPrePayId());
|
||||||
return payRequest;
|
return payRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,7 +33,6 @@ import com.foxinmy.weixin4j.response.BlankResponse;
|
|||||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||||
import com.foxinmy.weixin4j.util.ClassUtil;
|
import com.foxinmy.weixin4j.util.ClassUtil;
|
||||||
import com.foxinmy.weixin4j.util.Consts;
|
|
||||||
import com.foxinmy.weixin4j.util.HttpUtil;
|
import com.foxinmy.weixin4j.util.HttpUtil;
|
||||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||||
|
|
||||||
@ -352,7 +351,7 @@ public class WeixinMessageDispatcher {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Source source = new StreamSource(new ByteArrayInputStream(
|
Source source = new StreamSource(new ByteArrayInputStream(
|
||||||
message.getBytes(Consts.UTF_8)));
|
ServerToolkits.getBytesUtf8(message)));
|
||||||
JAXBElement<M> jaxbElement = getUnmarshaller(clazz).unmarshal(
|
JAXBElement<M> jaxbElement = getUnmarshaller(clazz).unmarshal(
|
||||||
source, clazz);
|
source, clazz);
|
||||||
return jaxbElement.getValue();
|
return jaxbElement.getValue();
|
||||||
|
|||||||
@ -10,8 +10,8 @@ import java.util.List;
|
|||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.response.SingleResponse;
|
import com.foxinmy.weixin4j.response.SingleResponse;
|
||||||
import com.foxinmy.weixin4j.util.Consts;
|
|
||||||
import com.foxinmy.weixin4j.util.HttpUtil;
|
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 {
|
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$TEXT_PLAIN));
|
ServerToolkits.CONTENTTYPE$TEXT_PLAIN));
|
||||||
logger.info("encode single response:{}", content);
|
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.request.WeixinRequest;
|
||||||
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.MessageUtil;
|
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||||
import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
|
import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
|
||||||
@ -44,7 +43,7 @@ public class WeixinMessageDecoder extends
|
|||||||
@Override
|
@Override
|
||||||
protected void decode(ChannelHandlerContext ctx, FullHttpRequest req,
|
protected void decode(ChannelHandlerContext ctx, FullHttpRequest req,
|
||||||
List<Object> out) throws WeixinException {
|
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(),
|
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri(),
|
||||||
true);
|
true);
|
||||||
String methodName = req.getMethod().name();
|
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.response.SingleResponse;
|
||||||
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.HttpUtil;
|
import com.foxinmy.weixin4j.util.HttpUtil;
|
||||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||||
@ -112,7 +111,7 @@ public class WeixinRequestHandler extends
|
|||||||
}
|
}
|
||||||
WeixinMessageTransfer messageTransfer = MessageTransferHandler
|
WeixinMessageTransfer messageTransfer = MessageTransferHandler
|
||||||
.parser(request);
|
.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);
|
messageDispatcher.doDispatch(ctx, request, messageTransfer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,6 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
|||||||
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;
|
||||||
import com.foxinmy.weixin4j.util.Consts;
|
|
||||||
import com.foxinmy.weixin4j.util.HttpUtil;
|
import com.foxinmy.weixin4j.util.HttpUtil;
|
||||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||||
@ -38,7 +37,7 @@ public class WeixinResponseEncoder extends
|
|||||||
protected void encode(ChannelHandlerContext ctx, WeixinResponse response,
|
protected void encode(ChannelHandlerContext ctx, WeixinResponse response,
|
||||||
List<Object> out) throws WeixinException {
|
List<Object> out) throws WeixinException {
|
||||||
WeixinMessageTransfer messageTransfer = ctx.channel()
|
WeixinMessageTransfer messageTransfer = ctx.channel()
|
||||||
.attr(Consts.MESSAGE_TRANSFER_KEY).get();
|
.attr(ServerToolkits.MESSAGE_TRANSFER_KEY).get();
|
||||||
EncryptType encryptType = messageTransfer.getEncryptType();
|
EncryptType encryptType = messageTransfer.getEncryptType();
|
||||||
StringBuilder content = new StringBuilder();
|
StringBuilder content = new StringBuilder();
|
||||||
content.append("<xml>");
|
content.append("<xml>");
|
||||||
@ -76,7 +75,7 @@ public class WeixinResponseEncoder extends
|
|||||||
content.append("</xml>");
|
content.append("</xml>");
|
||||||
}
|
}
|
||||||
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK,
|
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK,
|
||||||
Consts.CONTENTTYPE$APPLICATION_XML));
|
ServerToolkits.CONTENTTYPE$APPLICATION_XML));
|
||||||
logger.info("{} encode weixin response:{}", encryptType, content);
|
logger.info("{} encode weixin response:{}", encryptType, content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,10 +40,10 @@ public final class ClassUtil {
|
|||||||
String packageFileName = packageName.replace(".", File.separator);
|
String packageFileName = packageName.replace(".", File.separator);
|
||||||
URL fullPath = getDefaultClassLoader().getResource(packageFileName);
|
URL fullPath = getDefaultClassLoader().getResource(packageFileName);
|
||||||
String protocol = fullPath.getProtocol();
|
String protocol = fullPath.getProtocol();
|
||||||
if (protocol.equals(Consts.PROTOCOL_FILE)) {
|
if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) {
|
||||||
File dir = new File(fullPath.getPath());
|
File dir = new File(fullPath.getPath());
|
||||||
return findClassesByFile(dir, packageName);
|
return findClassesByFile(dir, packageName);
|
||||||
} else if (protocol.equals(Consts.PROTOCOL_JAR)) {
|
} else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) {
|
||||||
try {
|
try {
|
||||||
return findClassesByJar(
|
return findClassesByJar(
|
||||||
((JarURLConnection) fullPath.openConnection())
|
((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;
|
FullHttpResponse httpResponse = null;
|
||||||
|
|
||||||
httpResponse = new DefaultFullHttpResponse(HTTP_1_1, status,
|
httpResponse = new DefaultFullHttpResponse(HTTP_1_1, status,
|
||||||
Unpooled.copiedBuffer(content, Consts.UTF_8));
|
Unpooled.copiedBuffer(content, ServerToolkits.UTF_8));
|
||||||
httpResponse.headers().set(
|
httpResponse.headers().set(
|
||||||
CONTENT_TYPE,
|
CONTENT_TYPE,
|
||||||
String.format("%s;encoding=%s", contentType,
|
String.format("%s;encoding=%s", contentType,
|
||||||
Consts.UTF_8.displayName()));
|
ServerToolkits.UTF_8.displayName()));
|
||||||
httpResponse.headers().set(CONTENT_LENGTH,
|
httpResponse.headers().set(CONTENT_LENGTH,
|
||||||
content.getBytes(Consts.UTF_8).length);
|
content.getBytes(ServerToolkits.UTF_8).length);
|
||||||
createHeaders(httpResponse);
|
createHeaders(httpResponse);
|
||||||
return httpResponse;
|
return httpResponse;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -92,7 +92,7 @@ public final class MessageUtil {
|
|||||||
byte[] aesKey = NettyBase64.decodeBase64(encodingAesKey + "=");
|
byte[] aesKey = NettyBase64.decodeBase64(encodingAesKey + "=");
|
||||||
// 设置加密模式为AES的CBC模式
|
// 设置加密模式为AES的CBC模式
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
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);
|
IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
|
||||||
// 加密
|
// 加密
|
||||||
@ -124,7 +124,7 @@ public final class MessageUtil {
|
|||||||
try {
|
try {
|
||||||
// 设置解密模式为AES的CBC模式
|
// 设置解密模式为AES的CBC模式
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
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,
|
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey,
|
||||||
0, 16));
|
0, 16));
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
|
cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
|
||||||
|
|||||||
@ -50,7 +50,7 @@ public class PKCS7Encoder {
|
|||||||
for (int index = 0; index < amountToPad; index++) {
|
for (int index = 0; index < amountToPad; index++) {
|
||||||
tmp.append(padChr);
|
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;
|
package com.foxinmy.weixin4j.util;
|
||||||
|
|
||||||
|
import io.netty.util.AttributeKey;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -7,6 +9,8 @@ import java.security.MessageDigest;
|
|||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工具包
|
* 工具包
|
||||||
*
|
*
|
||||||
@ -18,6 +22,16 @@ import java.util.Random;
|
|||||||
*/
|
*/
|
||||||
public final class ServerToolkits {
|
public final class ServerToolkits {
|
||||||
private static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
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) {
|
public static String digestSHA1(String content) {
|
||||||
byte[] data = ServerToolkits.getBytesUtf8(content);
|
byte[] data = ServerToolkits.getBytesUtf8(content);
|
||||||
try {
|
try {
|
||||||
return HexUtil.encodeHexString(MessageDigest.getInstance(
|
return HexUtil.encodeHexString(MessageDigest.getInstance(SHA1)
|
||||||
Consts.SHA1).digest(data));
|
.digest(data));
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -69,11 +83,11 @@ public final class ServerToolkits {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] getBytesUtf8(final String content) {
|
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) {
|
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.DefaultHandler;
|
||||||
import org.xml.sax.helpers.XMLReaderFactory;
|
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 xmlReader = XMLReaderFactory.createXMLReader();
|
||||||
xmlReader.setContentHandler(global);
|
xmlReader.setContentHandler(global);
|
||||||
xmlReader.parse(new InputSource(new ByteArrayInputStream(xmlContent
|
xmlReader.parse(new InputSource(new ByteArrayInputStream(xmlContent
|
||||||
.getBytes(Consts.UTF_8))));
|
.getBytes(ServerToolkits.UTF_8))));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
|
|||||||
@ -14,7 +14,6 @@ import org.xml.sax.helpers.XMLReaderFactory;
|
|||||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||||
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
|
||||||
import com.foxinmy.weixin4j.type.AccountType;
|
import com.foxinmy.weixin4j.type.AccountType;
|
||||||
import com.foxinmy.weixin4j.util.Consts;
|
|
||||||
import com.foxinmy.weixin4j.util.ServerToolkits;
|
import com.foxinmy.weixin4j.util.ServerToolkits;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -90,7 +89,7 @@ public class MessageTransferHandler extends DefaultHandler {
|
|||||||
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
||||||
xmlReader.setContentHandler(global);
|
xmlReader.setContentHandler(global);
|
||||||
xmlReader.parse(new InputSource(new ByteArrayInputStream(request
|
xmlReader.parse(new InputSource(new ByteArrayInputStream(request
|
||||||
.getOriginalContent().getBytes(Consts.UTF_8))));
|
.getOriginalContent().getBytes(ServerToolkits.UTF_8))));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package com.foxinmy.weixin4j.server.test;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
|
import org.apache.http.Consts;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
import org.apache.http.StatusLine;
|
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.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.util.EntityUtils;
|
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");
|
throw new IOException(Integer.toString(status) + "uri moved");
|
||||||
}
|
}
|
||||||
return EntityUtils.toString(httpResponse.getEntity(),
|
return EntityUtils.toString(httpResponse.getEntity(),
|
||||||
Consts.UTF_8);
|
ServerToolkits.UTF_8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user