Esempio n. 1
0
  public static Map<String, String> postData(EndPointInfo endPointInfo, Map<String, String> headers)
      throws IDPTokenManagerException {

    HTTP_METHODS httpMethod = endPointInfo.getHttpMethod();
    String url = endPointInfo.getEndPoint();

    String params = null;

    if (endPointInfo != null && endPointInfo.getRequestParams() != null) {
      params = endPointInfo.getRequestParams();
    }

    Map<String, String> responseParams = new HashMap<String, String>();

    switch (httpMethod) {
      case GET:
        responseParams = sendGetRequest(url, headers);
        break;
      case POST:
        responseParams = sendPostRequest(url, params, headers);
        break;
      case DELETE:
        responseParams = sendDeleteRequest(url, headers);
        break;
      case PUT:
        responseParams = sendPutRequest(url, params, headers);
        break;
    }

    return responseParams;
  }
Esempio n. 2
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;
  }
  /**
   * This method is used to send requests to backend. The reason to use this method because the
   * function which is already available for sending requests is secured with token. Therefor this
   * can be used to send requests without tokens.
   */
  private void sendRequest(
      final EndPointInfo endPointInfo,
      final APIResultCallBack apiResultCallback,
      final int requestCode) {
    RequestQueue queue = null;
    int requestMethod = 0;
    org.wso2.emm.agent.proxy.utils.Constants.HTTP_METHODS httpMethod = endPointInfo.getHttpMethod();
    switch (httpMethod) {
      case POST:
        requestMethod = Request.Method.POST;
        break;
      case DELETE:
        requestMethod = Request.Method.DELETE;
        break;
    }

    try {
      queue = ServerUtilities.getCertifiedHttpClient();
    } catch (IDPTokenManagerException e) {
      Log.e(TAG, "Failed to retrieve HTTP client", e);
    }

    JsonObjectRequest request = null;
    try {
      request =
          new JsonObjectRequest(
              requestMethod,
              endPointInfo.getEndPoint(),
              (endPointInfo.getRequestParams() != null)
                  ? new JSONObject(endPointInfo.getRequestParams())
                  : null,
              new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                  Log.d(TAG, response.toString());
                }
              },
              new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                  Log.d(TAG, error.toString());
                }
              }) {
            @Override
            protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
              String result = new String(response.data);
              if (org.wso2.emm.agent.proxy.utils.Constants.DEBUG_ENABLED) {
                if (result != null && !result.isEmpty()) {
                  Log.d(TAG, "Result :" + result);
                }
              }
              Map<String, String> responseParams = new HashMap<>();
              responseParams.put(
                  org.wso2.emm.agent.proxy.utils.Constants.SERVER_RESPONSE_BODY, result);
              responseParams.put(
                  org.wso2.emm.agent.proxy.utils.Constants.SERVER_RESPONSE_STATUS,
                  String.valueOf(response.statusCode));
              if (apiResultCallback != null) {
                apiResultCallback.onReceiveAPIResult(responseParams, requestCode);
              }
              return super.parseNetworkResponse(response);
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
              Map<String, String> headers = new HashMap<>();
              headers.put("Content-Type", "application/json");
              headers.put("Accept", "application/json");
              headers.put("User-Agent", Constants.USER_AGENT);
              if (endPointInfo.getHeader() != null && !endPointInfo.getHeader().trim().isEmpty()) {
                headers.put("Authorization", endPointInfo.getHeader().trim());
              }
              return headers;
            }
          };
    } catch (JSONException e) {
      Log.e(TAG, "Failed to parse request JSON", e);
    }

    queue.add(request);
  }