com.alipay.api.internal.util.AlipayLogger Java Examples

The following examples show how to use com.alipay.api.internal.util.AlipayLogger. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AlipayMsgClient.java    From alipay-sdk-java-all with Apache License 2.0 6 votes vote down vote up
public void run() {
    try {
        if (isConnected()) {
            webSocketConnector.sendPing();
            reConnectTimes = 0;
        } else {
            ReconnectStrategy[] strategies = ReconnectStrategy.values();
            while ((!isConnected())
                    && (System.currentTimeMillis() - waitTime >=
                    strategies[reConnectTimes].getWatiTime())) {
                doConnect();
                waitTime = System.currentTimeMillis();
                reConnectTimes = (++reConnectTimes) % strategies.length;
            }
        }
    } catch (Throwable t) {
        AlipayLogger.logBizError(t);
    }
}
 
Example #2
Source File: SM2Encryptor.java    From alipay-sdk-java-all with Apache License 2.0 6 votes vote down vote up
protected String doDecrypt(String cipherTextBase64, String charset, String privateKey) throws Exception {
    //加载私钥参数
    byte[] cipher = Base64.decodeBase64String(cipherTextBase64);
    byte[] privateKeyByte = Base64.decodeBase64String(privateKey);
    // 解析X509格式SM2私钥
    PrivateKey sm2PrivateKey = parsePKCS8PrivateKey(privateKeyByte);
    // 使用SM2私钥解密
    byte[] buf = sm2Decrypt(cipher, sm2PrivateKey);
    //将解密后的明文按指定字符集编码后返回
    try {
        String strContent = new String(buf, charset);
        return strContent;
    } catch (UnsupportedEncodingException e) {
        AlipayLogger.logBizError(e);
        throw new AlipayApiException(e);
    }
}
 
Example #3
Source File: SM2Encryptor.java    From alipay-sdk-java-all with Apache License 2.0 6 votes vote down vote up
private static byte[] sm2Sign(byte[] message, PrivateKey sm2PrivateKey, String sm2UserId) throws AlipayApiException {
    try {
        String userId = "1234567812345678";
        if (!StringUtils.isEmpty(sm2UserId)) {
            userId = sm2UserId;
        }
        Signature sm2SignEngine = Signature.getInstance("SM3withSM2");
        sm2SignEngine.setParameter(new SM2ParameterSpec(
                Strings.toByteArray(userId)));
        sm2SignEngine.initSign(sm2PrivateKey);
        sm2SignEngine.update(message);
        return sm2SignEngine.sign();
    } catch (Exception e) {
        AlipayLogger.logBizError(e);
        throw new AlipayApiException(e);
    }
}
 
Example #4
Source File: SM2Encryptor.java    From alipay-sdk-java-all with Apache License 2.0 6 votes vote down vote up
private static boolean sm2Verify(byte[] signature, byte[] message, PublicKey publicKey, String sm2UserId) {
    try {
        String userId = "1234567812345678";
        if (!StringUtils.isEmpty(sm2UserId)) {
            userId = sm2UserId;
        }
        Signature sm2SignEngine = Signature.getInstance("SM3withSM2");
        sm2SignEngine.setParameter(new SM2ParameterSpec(Strings.toByteArray(userId)));
        sm2SignEngine.initVerify(publicKey);
        sm2SignEngine.update(message);
        return sm2SignEngine.verify(signature);
    } catch (Exception e) {
        AlipayLogger.logBizError(e);
        return false;
    }
}
 
Example #5
Source File: SM2Encryptor.java    From alipay-sdk-java-all with Apache License 2.0 5 votes vote down vote up
protected boolean doVerify(String content, String charset, String publicKey, String sign) throws Exception {
    // 使用SM2公钥验签
    byte[] signature = Base64.decodeBase64String(sign);
    byte[] message = content.getBytes(charset);
    byte[] publicKeyByte = Base64.decodeBase64String(publicKey);
    PublicKey sm2PublicKey = parseX509PublicKey(publicKeyByte);
    boolean valid = sm2Verify(signature, message, sm2PublicKey, null);
    if (!valid) {
        AlipayLogger.logBizError("验签失败");
    }
    return valid;
}
 
Example #6
Source File: SM2Encryptor.java    From alipay-sdk-java-all with Apache License 2.0 5 votes vote down vote up
private static byte[] sm2Encrypt(byte[] plain, PublicKey sm2PublicKey) throws AlipayApiException {
    try {
        Cipher sm2CipherEngine = Cipher.getInstance("SM2", "BC");
        sm2CipherEngine.init(Cipher.ENCRYPT_MODE, sm2PublicKey);
        return sm2CipherEngine.doFinal(plain);
    } catch (Exception e) {
        AlipayLogger.logBizError(e);
        throw new AlipayApiException(e);
    }
}
 
Example #7
Source File: SM2Encryptor.java    From alipay-sdk-java-all with Apache License 2.0 5 votes vote down vote up
private static byte[] sm2Decrypt(byte[] cipher, PrivateKey sm2PrivateKey) throws AlipayApiException {
    try {
        Cipher sm2CipherEngine = Cipher.getInstance("SM2", "BC");
        sm2CipherEngine.init(Cipher.DECRYPT_MODE, sm2PrivateKey);
        return sm2CipherEngine.doFinal(cipher);
    } catch (Exception e) {
        AlipayLogger.logBizError(e);
        throw new AlipayApiException(e);
    }
}
 
Example #8
Source File: AbstractAlipayClient.java    From alipay-sdk-java-all with Apache License 2.0 5 votes vote down vote up
/**
 * @param request
 * @return
 * @throws AlipayApiException
 */
private Map<String, Object> doPost(BatchAlipayRequest request) throws AlipayApiException {

    Map<String, Object> result = new HashMap<String, Object>();
    RequestParametersHolder requestHolder = getRequestHolderWithSign(request);

    String url = getRequestUrl(requestHolder);

    // 打印完整请求报文
    if (AlipayLogger.isBizDebugEnabled()) {
        AlipayLogger.logBizDebug(getRedirectUrl(requestHolder));
    }
    result.put("prepareTime", System.currentTimeMillis());

    String rsp = null;
    try {
        rsp = WebUtils.doPost(url, requestHolder.getApplicationParams(), charset,
                connectTimeout, readTimeout, proxyHost, proxyPort);
    } catch (IOException e) {
        throw new AlipayApiException(e);
    }
    result.put("requestTime", System.currentTimeMillis());
    result.put("rsp", rsp);
    result.put("textParams", requestHolder.getApplicationParams());
    result.put("protocalMustParams", requestHolder.getProtocalMustParams());
    result.put("protocalOptParams", requestHolder.getProtocalOptParams());
    result.put("url", url);
    return result;
}
 
Example #9
Source File: DefaultAlipayClient.java    From alipay-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * 
 * @param request
 * @param accessToken
 * @param appAuthToken
 * @return
 * @throws AlipayApiException
 */
private <T extends AlipayResponse> Map<String, Object> doPost(AlipayRequest<T> request,
                                                              String accessToken,
                                                              String appAuthToken) throws AlipayApiException {
    Map<String, Object> result = new HashMap<String, Object>();
    RequestParametersHolder requestHolder = getRequestHolderWithSign(request, accessToken,
        appAuthToken);

    String url = getRequestUrl(requestHolder);

    // 打印完整请求报文
    if (AlipayLogger.isBizDebugEnabled()) {
        AlipayLogger.logBizDebug(getRedirectUrl(requestHolder));
    }

    String rsp = null;
    try {
        if (request instanceof AlipayUploadRequest) {
            AlipayUploadRequest<T> uRequest = (AlipayUploadRequest<T>) request;
            Map<String, FileItem> fileParams = AlipayUtils.cleanupMap(uRequest.getFileParams());
            rsp = WebUtils.doPost(url, requestHolder.getApplicationParams(), fileParams,
                charset, connectTimeout, readTimeout, proxyHost, proxyPort);
        } else {
            rsp = WebUtils.doPost(url, requestHolder.getApplicationParams(), charset,
                connectTimeout, readTimeout, proxyHost, proxyPort);
        }
    } catch (IOException e) {
        throw new AlipayApiException(e);
    }
    result.put("rsp", rsp);
    result.put("textParams", requestHolder.getApplicationParams());
    result.put("protocalMustParams", requestHolder.getProtocalMustParams());
    result.put("protocalOptParams", requestHolder.getProtocalOptParams());
    result.put("url", url);
    return result;
}
 
Example #10
Source File: DefaultAlipayClient.java    From pay with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * 
 * @param request
 * @param accessToken
 * @param signType
 * @return
 * @throws AlipayApiException
 */
private <T extends AlipayResponse> Map<String, Object> doPost(AlipayRequest<T> request,
                                                              String accessToken,
                                                              String appAuthToken) throws AlipayApiException {
    Map<String, Object> result = new HashMap<String, Object>();
    RequestParametersHolder requestHolder = getRequestHolderWithSign(request, accessToken,
        appAuthToken);

    String url = getRequestUrl(requestHolder);

    // 打印完整请求报文
    if (AlipayLogger.isBizDebugEnabled()) {
        AlipayLogger.logBizDebug(getRedirectUrl(requestHolder));
    }

    String rsp = null;
    try {
        if (request instanceof AlipayUploadRequest) {
            AlipayUploadRequest<T> uRequest = (AlipayUploadRequest<T>) request;
            Map<String, FileItem> fileParams = AlipayUtils.cleanupMap(uRequest.getFileParams());
            rsp = WebUtils.doPost(url, requestHolder.getApplicationParams(), fileParams,
                charset, connectTimeout, readTimeout);
        } else {
            rsp = WebUtils.doPost(url, requestHolder.getApplicationParams(), charset,
                connectTimeout, readTimeout);
        }
    } catch (IOException e) {
        throw new AlipayApiException(e);
    }
    result.put("rsp", rsp);
    result.put("textParams", requestHolder.getApplicationParams());
    result.put("protocalMustParams", requestHolder.getProtocalMustParams());
    result.put("protocalOptParams", requestHolder.getProtocalOptParams());
    result.put("url", url);
    return result;
}
 
Example #11
Source File: MsgConnector.java    From alipay-sdk-java-all with Apache License 2.0 4 votes vote down vote up
public void onOpen(ServerHandshake handshakedata) {
    AlipayLogger.logBizDebug("conn open");
}
 
Example #12
Source File: MsgConnector.java    From alipay-sdk-java-all with Apache License 2.0 4 votes vote down vote up
public void onClose(int code, String reason, boolean remote) {
    AlipayLogger.logBizDebug("conn close");
}
 
Example #13
Source File: MsgConnector.java    From alipay-sdk-java-all with Apache License 2.0 4 votes vote down vote up
public void onError(Exception ex) {
    AlipayLogger.logBizError(ex);
}
 
Example #14
Source File: AlipayMsgClient.java    From alipay-sdk-java-all with Apache License 2.0 4 votes vote down vote up
private void doConnect() throws Exception {
    if (isConnected()) {
        return;
    }

    synchronized (this) {
        if (isConnected()) {
            return;
        }

        RegisterResponse regResp = register();

        Map<String, String> httpHeaders = new HashMap<String, String>(1);
        if (regResp.getZone() != null && regResp.getZone().length() > 0) {
            httpHeaders.put("cookie", "zone=" + regResp.getZone() + ";");
        }
        if (loadTest) {
            httpHeaders.put("LoadTest", "true");
        }
        httpHeaders.put("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

        Map<String, String> params = new HashMap<String, String>(5);
        params.put("app_id", appId);
        params.put("charset", charset);
        params.put("link_token", regResp.getLinkToken());
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        params.put("sign_type", signType);
        params.put("sdk_version", AlipayConstants.SDK_VERSION);
        String signContent = AlipaySignature.getSignCheckContentV2(params);
        String sign = AlipaySignature.rsaSign(signContent, appPrivateKey, charset, signType);
        params.put("sign", sign);
        String query = WebUtils.buildQuery(params, charset);
        String urlStr = "ws" + (isSSL ? "s" : "") + "://" + serverHost + "/websocket?" + query;

        webSocketConnector = new MsgConnector(new URI(urlStr), httpHeaders, this, charset);
        if (!webSocketConnector.connectBlocking(10L, TimeUnit.SECONDS)) {
            throw new RuntimeException("connect timeout(10s)!");
        }
        if (AlipayLogger.isBizDebugEnabled()) {
            AlipayLogger.logBizDebug("connected");
        }
    }
}
 
Example #15
Source File: AlipayMsgClient.java    From alipay-sdk-java-all with Apache License 2.0 4 votes vote down vote up
public ProduceMsgAck sendMessage(AlipayRequest msgReq) throws InterruptedException {
    if (!isConnected()) {
        throw new NotYetConnectedException();
    }
    Message message = new Message();
    message.setxCmd(MsgConstants.MSG_CMD_PRODUCE);
    message.setxSignType(signType);
    message.setxCharset(charset);
    message.setAppId(appId);
    message.setMsgApi(msgReq.getApiMethodName());
    message.setxTimestamp(System.currentTimeMillis());
    message.setBizContent(new JSONWriter().write(msgReq.getBizModel(), true));
    Message.addSign(message, appPrivateKey);
    ProtocolData protocolData = new ProtocolData();
    protocolData.setMessage(message);
    CountDownLatch signal = new CountDownLatch(1);
    ProtocolDataContext protocolDataContext = new ProtocolDataContext();
    protocolDataContext.setSendData(protocolData);
    protocolDataContext.setSendSignal(signal);
    if (!sendingQueue.offer(protocolData.getStreamId(), 200, TimeUnit.MILLISECONDS)) {
        throw new RuntimeException("too many message not receive ack, refuse new send. streamId:"
                + protocolData.getStreamId());
    }
    sendingContexts.put(protocolData.getStreamId(), protocolDataContext);

    String protocolDataStr = ProtocolData.toStr(protocolData);
    if (AlipayLogger.isBizDebugEnabled()) {
        AlipayLogger.logBizDebug("send msg:" + protocolDataStr.replaceAll("[\r\n]", " "));
    }
    webSocketConnector.send(ProtocolData.toStr(protocolData));
    boolean signalNotify = signal.await(10000, TimeUnit.MILLISECONDS);

    sendingQueue.remove(protocolData.getStreamId());
    sendingContexts.remove(protocolData.getStreamId());
    if (!signalNotify) {
        AlipayLogger.logBizError("wait ack timeout(10s). streamId:" + protocolData.getStreamId());
        throw new RuntimeException("wait ack timeout(10s). streamId:" + protocolData.getStreamId());
    }

    ProtocolData ackData = protocolDataContext.getAckData();
    if (ackData == null) {
        throw new RuntimeException("ack protocol data null. streamId:" + protocolData.getStreamId());
    }
    Message ackMsg = ackData.getMessage();
    if (ackMsg == null) {
        throw new RuntimeException("ack msg null. streamId:" + protocolData.getStreamId());
    }
    ProduceMsgAck produceMsgAck = new ProduceMsgAck();
    produceMsgAck.setxStatus(MsgStatusEnum.fromStr(ackMsg.getxStatus()));
    produceMsgAck.setxCode(ackMsg.getxCode());
    produceMsgAck.setxError(ackMsg.getxError());
    produceMsgAck.setxMessageId(ackMsg.getxMessageId());
    return produceMsgAck;
}