Esempio n. 1
0
  public <T extends TaobaoResponse> T execute(
      TaobaoRequest<T> request, String authToken, String version) throws AlipayApiException {
    TaobaoParser<T> parser = null;
    if (Constants.FORMAT_XML.equals(this.format)) {
      parser = new ObjectXmlParser<T>(request.getResponseClass());
    } else {
      parser = new ObjectJsonParser<T>(request.getResponseClass());
    }

    return _execute(request, parser, authToken, version);
  }
Esempio n. 2
0
  public <T extends TaobaoResponse> Map<String, Object> doPost(
      TaobaoRequest<T> request, String accessToken, String version) throws AlipayApiException {
    Map<String, Object> result = new HashMap<String, Object>();
    RequestParametersHolder requestHolder = new RequestParametersHolder();
    TaobaoHashMap appParams = new TaobaoHashMap(request.getTextParams());
    requestHolder.setApplicationParams(appParams);

    TaobaoHashMap protocalMustParams = new TaobaoHashMap();
    protocalMustParams.put(AlipayConstants.METHOD, request.getApiMethodName());
    protocalMustParams.put(AlipayConstants.VERSION, version);
    protocalMustParams.put(AlipayConstants.APP_ID, this.appId);

    Long timestamp = System.currentTimeMillis();
    DateFormat df = new SimpleDateFormat(Constants.DATE_TIME_FORMAT);
    df.setTimeZone(TimeZone.getTimeZone(Constants.DATE_TIMEZONE));
    protocalMustParams.put(AlipayConstants.TIMESTAMP, df.format(new Date(timestamp)));
    requestHolder.setProtocalMustParams(protocalMustParams);

    TaobaoHashMap protocalOptParams = new TaobaoHashMap();
    protocalOptParams.put(AlipayConstants.FORMAT, format);
    protocalOptParams.put(AlipayConstants.SIGN_TYPE, this.sign_type);
    protocalOptParams.put(AlipayConstants.ACCESS_TOKEN, accessToken);
    protocalOptParams.put(AlipayConstants.ALIPAY_SDK, Constants.SDK_VERSION);
    requestHolder.setProtocalOptParams(protocalOptParams);

    if (AlipayConstants.SIGN_TYPE_RSA.equals(this.sign_type)) {

      String signContent = AlipaySignature.getSignatureContent(requestHolder);

      protocalMustParams.put(
          AlipayConstants.SIGN,
          AlipaySignature.rsaSign(signContent, privateKey, Constants.CHARSET_UTF8));

    } else {
      protocalMustParams.put(AlipayConstants.SIGN, "");
    }

    StringBuffer urlSb = new StringBuffer(serverUrl);
    try {
      String sysMustQuery =
          WebUtils.buildQuery(requestHolder.getProtocalMustParams(), Constants.CHARSET_UTF8);
      String sysOptQuery =
          WebUtils.buildQuery(requestHolder.getProtocalOptParams(), Constants.CHARSET_UTF8);

      urlSb.append("?");
      urlSb.append(sysMustQuery);
      if (sysOptQuery != null & sysOptQuery.length() > 0) {
        urlSb.append("&");
        urlSb.append(sysOptQuery);
      }
    } catch (IOException e) {
      throw new AlipayApiException(e);
    }

    String rsp = null;
    try {
      if (request instanceof TaobaoUploadRequest) {
        TaobaoUploadRequest<T> uRequest = (TaobaoUploadRequest<T>) request;
        Map<String, FileItem> fileParams = TaobaoUtils.cleanupMap(uRequest.getFileParams());
        rsp = WebUtils.doPost(urlSb.toString(), appParams, fileParams, connectTimeout, readTimeout);
      } else {
        rsp = WebUtils.doPost(urlSb.toString(), appParams, connectTimeout, readTimeout);
      }
    } catch (IOException e) {
      throw new AlipayApiException(e);
    }
    result.put("rsp", rsp);
    result.put("textParams", appParams);
    result.put("protocalMustParams", protocalMustParams);
    result.put("protocalOptParams", protocalOptParams);
    result.put("url", urlSb.toString());
    return result;
  }