219 lines
7.1 KiB
Java
219 lines
7.1 KiB
Java
package com.tenwa.collectaudit.cache;
|
||
import java.io.IOException;
|
||
|
||
import org.apache.commons.httpclient.HttpClient;
|
||
import org.apache.commons.httpclient.HttpException;
|
||
import org.apache.commons.httpclient.HttpStatus;
|
||
import org.apache.commons.httpclient.methods.PostMethod;
|
||
import org.apache.commons.httpclient.params.HttpMethodParams;
|
||
import org.apache.commons.logging.Log;
|
||
import org.apache.commons.logging.LogFactory;
|
||
import org.apache.commons.httpclient.protocol.Protocol;
|
||
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
|
||
import com.amarsoft.app.awe.config.InitCollectConfig;
|
||
import com.gnete.security.crypt.Crypt;
|
||
import com.gnete.security.crypt.CryptException;
|
||
|
||
|
||
/**
|
||
* 新签名包测试用例(依赖新签名包gnete-bc-139.jar与gnete-security.jar)
|
||
*/
|
||
public class CollectAuditProcess {
|
||
|
||
private static Log log = LogFactory.getLog(CollectAuditProcess.class);
|
||
|
||
/**
|
||
*代收付代收、代付交易测试URL
|
||
*/
|
||
//private static final String SERVER_URL = "http://59.41.103.98:333/gzdsf/ProcessServlet";
|
||
|
||
//代收付签约、银行卡验证交易测试URL
|
||
//private static final String SERVER_URL = "http://59.41.103.98:9110/gzdsf/ProcessServlet";
|
||
|
||
|
||
/* public static void main(String[] args) throws CryptException {
|
||
CollectAuditProcess tp = new CollectAuditProcess( );
|
||
tp.CollectAuditPayReq();
|
||
}
|
||
*/
|
||
/**
|
||
* 验证签名信息
|
||
* @throws CryptException
|
||
*/
|
||
private boolean verifySign(String strXML) throws CryptException {
|
||
//签名(代收付系统JDK环境使用GBK编码,商户使用签名包时需传送GBK编码集)
|
||
Crypt crypt = new Crypt("gbk" );
|
||
String pathCer = InitCollectConfig.PUBLICKEYURL;
|
||
//"D:\\BaiduNetdisk\\Download\\代收付系统接口文档和JAVA版测试demo及测试相关参数\\JAVA版测试DEMO及测试参数_20170825\\测试商户证书\\gzdsf.cer";
|
||
int iStart = strXML.indexOf("<SIGNED_MSG>");
|
||
if (iStart != -1) {
|
||
int end = strXML.indexOf("</SIGNED_MSG>");
|
||
String signedMsg = strXML.substring(iStart+12, end);
|
||
log.info(signedMsg);
|
||
String strMsg = strXML.substring(0, iStart) + strXML.substring(end+13);
|
||
//调用签名包验签接口(原文,签名,公钥路径)
|
||
if (crypt.verify(strMsg,signedMsg, pathCer)){
|
||
log.info("verify ok");
|
||
return true;
|
||
}
|
||
else {
|
||
log.error("verify error");
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 数据签名
|
||
* comment here
|
||
* @param strData
|
||
* @return
|
||
* @throws CryptException
|
||
* @since gnete-ora 0.0.0.1
|
||
*/
|
||
private String signMsg(String strData) throws CryptException {
|
||
String strRnt = "";
|
||
//签名(代收付系统JDK环境使用GBK编码,商户使用签名包时需传送GBK编码集)
|
||
Crypt crypt = new Crypt("gbk");
|
||
String pathPfx = InitCollectConfig.PRIVATEKEYURL;
|
||
//"D:\\BaiduNetdisk\\Download\\代收付系统接口文档和JAVA版测试demo及测试相关参数\\JAVA版测试DEMO及测试参数_20170825\\测试商户证书\\cps@00019140020764901.p12";
|
||
//"D:\\BaiduNetdisk\\Download\\代收付系统接口文档和JAVA版测试demo及测试相关参数\\JAVA版测试DEMO及测试参数_20170825\\测试商户证书\\ORA@TEST1.p12";
|
||
String strMsg = strData.replaceAll("<SIGNED_MSG></SIGNED_MSG>", "");
|
||
//调用签名包签名接口(原文,私钥路径,密码)
|
||
String signedMsg = crypt.sign(strMsg, pathPfx, "123456");
|
||
return strData.replaceAll("<SIGNED_MSG></SIGNED_MSG>", "<SIGNED_MSG>"+signedMsg+"</SIGNED_MSG>");
|
||
}
|
||
|
||
/**
|
||
* 测试请求
|
||
* comment here
|
||
* @throws CryptException
|
||
* @since gnete-ora 0.0.0.1
|
||
*/
|
||
protected String CollectAuditPayReq(StringBuffer sendXml,String fileSavePath,String fileName) throws CryptException {
|
||
String strSendData = sendXml.toString();
|
||
HttpClient httpClient = new HttpClient( );
|
||
PostMethod postMethod =
|
||
new PostMethod(InitCollectConfig.C_SERVERURL);
|
||
//设置编码
|
||
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GBK");
|
||
|
||
//新加入代码
|
||
ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
|
||
//加入相关的https请求方式
|
||
Protocol.registerProtocol("https", new Protocol("https", fcty, 443));
|
||
|
||
strSendData = this.signMsg(strSendData);
|
||
log.info(strSendData);
|
||
System.out.println("发送报文:"+strSendData);
|
||
InterFaceFileCreate.sendDiscFileCreate(strSendData, fileSavePath, fileName);
|
||
postMethod.setRequestBody(strSendData);
|
||
try {
|
||
long start = System.currentTimeMillis();
|
||
//执行getMethod
|
||
int statusCode = httpClient.executeMethod(postMethod);
|
||
System.out.println("cost:"+(System.currentTimeMillis()-start));
|
||
//失败
|
||
if (statusCode != HttpStatus.SC_OK) {
|
||
log.error(
|
||
"Method failed: " + postMethod.getStatusLine());
|
||
//读取内容
|
||
byte[] responseBody = postMethod.getResponseBody();
|
||
//处理内容
|
||
String strResp = new String(responseBody, "GBK");
|
||
log.error(strResp);
|
||
return strResp;
|
||
}
|
||
else {
|
||
//读取内容
|
||
byte[] responseBody = postMethod.getResponseBody();
|
||
String strResp = new String(responseBody, "GBK");
|
||
log.info("服务器返回:" + strResp);
|
||
InterFaceFileCreate.replyDiscFileCreate(strResp, fileSavePath, fileName);
|
||
System.out.println("服务器返回:" + strResp);
|
||
//验签
|
||
if (this.verifySign(strResp)) {
|
||
log.info("验签正确,处理服务器返回的报文");
|
||
}
|
||
return strResp;
|
||
}
|
||
} catch (HttpException e) {
|
||
//发生致命的异常,可能是协议不对或者返回的内容有问题
|
||
log.error("Please check your provided http address!", e);
|
||
e.printStackTrace( );
|
||
} catch (IOException e) {
|
||
//发生网络异常
|
||
log.error(e);
|
||
} catch (Exception e) {
|
||
log.error(null, e);
|
||
} finally {
|
||
//释放连接
|
||
postMethod.releaseConnection();
|
||
}
|
||
return "";
|
||
}
|
||
/**
|
||
* 测试验证银行卡二、三、四要素请求
|
||
* comment here
|
||
* @throws CryptException
|
||
* @since gnete-ora 0.0.0.1
|
||
*/
|
||
protected String CollectAuditVerifyReq(StringBuffer sendXml) throws CryptException {
|
||
String strSendData = sendXml.toString();
|
||
HttpClient httpClient = new HttpClient();
|
||
PostMethod postMethod =
|
||
new PostMethod(InitCollectConfig.B_SERVERURL);
|
||
//设置编码
|
||
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GBK");
|
||
strSendData = this.signMsg(strSendData);
|
||
log.info(strSendData);
|
||
System.out.println("发送报文:"+strSendData);
|
||
postMethod.setRequestBody(strSendData);
|
||
try {
|
||
long start = System.currentTimeMillis();
|
||
//执行getMethod
|
||
int statusCode = httpClient.executeMethod(postMethod);
|
||
System.out.println("cost:"+(System.currentTimeMillis()-start));
|
||
//失败
|
||
if (statusCode != HttpStatus.SC_OK) {
|
||
log.error(
|
||
"Method failed: " + postMethod.getStatusLine());
|
||
//读取内容
|
||
byte[] responseBody = postMethod.getResponseBody();
|
||
//处理内容
|
||
String strResp = new String(responseBody, "GBK");
|
||
log.error(strResp);
|
||
return strResp;
|
||
}
|
||
else {
|
||
//读取内容
|
||
byte[] responseBody = postMethod.getResponseBody();
|
||
String strResp = new String(responseBody, "GBK");
|
||
log.info("服务器返回:" + strResp);
|
||
System.out.println("服务器返回:" + strResp);
|
||
//验签
|
||
if (this.verifySign(strResp)) {
|
||
log.info("验签正确,处理服务器返回的报文");
|
||
}
|
||
return strResp;
|
||
}
|
||
} catch (HttpException e) {
|
||
//发生致命的异常,可能是协议不对或者返回的内容有问题
|
||
log.error("Please check your provided http address!", e);
|
||
e.printStackTrace( );
|
||
} catch (IOException e) {
|
||
//发生网络异常
|
||
log.error(e);
|
||
} catch (Exception e) {
|
||
log.error(null, e);
|
||
} finally {
|
||
//释放连接
|
||
postMethod.releaseConnection();
|
||
}
|
||
return "";
|
||
}
|
||
|
||
|
||
}
|