Esempio n. 1
0
  public static Map<String, String> postDataAPI(
      EndPointInfo endPointInfo, Map<String, String> headers) throws IDPTokenManagerException {
    HTTP_METHODS httpMethod = endPointInfo.getHttpMethod();
    String url = endPointInfo.getEndPoint();
    Map<String, String> params = endPointInfo.getRequestParamsMap();

    Map<String, String> responseParams = new HashMap<String, String>();
    HttpClient httpclient = getCertifiedHttpClient();
    String payload = buildPayload(params);

    if (httpMethod.equals(HTTP_METHODS.POST)) {
      HttpPost httpPost = new HttpPost(url);
      httpPost = (HttpPost) buildHeaders(httpPost, headers);
      byte[] postData = payload.getBytes();
      try {
        httpPost.setEntity(new ByteArrayEntity(postData));
        HttpResponse response = httpclient.execute(httpPost);
        String status = String.valueOf(response.getStatusLine().getStatusCode());

        responseParams.put(Constants.SERVER_RESPONSE_BODY, getResponseBody(response));
        responseParams.put(Constants.SERVER_RESPONSE_STATUS, status);
        return responseParams;
      } catch (ClientProtocolException e) {
        String errorMsg =
            "Error occurred while sending 'Post' request due to an invalid client protocol being used";
        responseParams.put(Constants.SERVER_RESPONSE_BODY, "Internal Server Error");
        responseParams.put(Constants.SERVER_RESPONSE_STATUS, Constants.INTERNAL_SERVER_ERROR);
        Log.e(TAG, errorMsg);
        throw new IDPTokenManagerException(errorMsg, e);
      } catch (IOException e) {
        String errorMsg =
            "Error occurred while sending 'Post' request due to failure of server connection";
        responseParams.put(Constants.SERVER_RESPONSE_BODY, "Internal Server Error");
        responseParams.put(Constants.SERVER_RESPONSE_STATUS, Constants.INTERNAL_SERVER_ERROR);
        Log.e(TAG, errorMsg);
        throw new IDPTokenManagerException(errorMsg, e);
      } catch (IllegalArgumentException e) {
        String errorMsg = "Error occurred while sending 'Get' request due to empty host name";
        responseParams.put(Constants.SERVER_RESPONSE_BODY, "Internal Server Error");
        responseParams.put(Constants.SERVER_RESPONSE_STATUS, Constants.INTERNAL_SERVER_ERROR);
        Log.e(TAG, errorMsg);
        throw new IDPTokenManagerException(errorMsg, e);
      }
    }
    return responseParams;
  }