コード例 #1
0
    @Override
    protected Map<String, String> doInBackground(EndPointInfo... params) {
      EndPointInfo endPointInfo = params[0];

      Map<String, String> responseParams = null;
      Map<String, String> headers = new HashMap<String, String>();
      headers.put("Content-Type", "application/json");
      headers.put("Accept", "application/json");
      headers.put("User-Agent", "Mozilla/5.0 ( compatible ), Android");

      try {
        responseParams = ServerUtilities.postData(endPointInfo, headers);
        if (Constants.DEBUG_MODE_ENABLED) {
          Iterator<Map.Entry<String, String>> iterator = responseParams.entrySet().iterator();
          while (iterator.hasNext()) {
            Map.Entry<String, String> respParams = iterator.next();
            StringBuilder paras = new StringBuilder();
            paras.append("response-params: key:");
            paras.append(respParams.getKey());
            paras.append(", value:");
            paras.append(respParams.getValue());
            Log.d(TAG, paras.toString());
          }
        }

      } catch (IDPTokenManagerException e) {
        Log.e(TAG, "Failed to contact server." + e);
      }
      return responseParams;
    }
コード例 #2
0
  /** Method to contact authorization server and get access token, refresh token as a result */
  public void obtainAccessToken() {
    RequestQueue queue = null;
    try {
      queue = ServerUtilities.getCertifiedHttpClient();
    } catch (IDPTokenManagerException e) {
      Log.e(TAG, "Failed to retrieve HTTP client", e);
    }

    StringRequest request =
        new StringRequest(
            Request.Method.POST,
            info.getTokenEndPoint(),
            new Response.Listener<String>() {
              @Override
              public void onResponse(String response) {
                Log.d(TAG, response);
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                Log.e(TAG, error.toString());
                JSONObject errorObj = new JSONObject();
                try {
                  errorObj.put(Constants.ERROR_DESCRIPTION_LABEL, error.toString());
                } catch (JSONException e) {
                  Log.e(TAG, "Invalid JSON format", e);
                } finally {
                  processTokenResponse(Constants.ACCESS_FAILURE, errorObj.toString());
                }
              }
            }) {
          @Override
          protected Response<String> parseNetworkResponse(NetworkResponse response) {
            processTokenResponse(String.valueOf(response.statusCode), new String(response.data));
            return super.parseNetworkResponse(response);
          }

          @Override
          protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> requestParams = new HashMap<>();
            requestParams.put(Constants.GRANT_TYPE, Constants.GRANT_TYPE_PASSWORD);
            requestParams.put(USERNAME_LABEL, info.getUsername());
            requestParams.put(PASSWORD_LABEL, info.getPassword());
            if (info.getTenantDomain() != null) {
              requestParams.put(TENANT_DOMAIN_LABEL, info.getTenantDomain());
            }
            requestParams.put(Constants.SCOPE, SCOPE);
            return requestParams;
          }

          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
            byte[] credentials =
                Base64.encodeBase64(
                    (info.getClientID() + COLON + info.getClientSecret()).getBytes());
            String encodedCredentials = new String(credentials);

            Map<String, String> headers = new HashMap<>();
            String authorizationString = Constants.AUTHORIZATION_MODE + encodedCredentials;
            headers.put(Constants.AUTHORIZATION_HEADER, authorizationString);
            headers.put(Constants.CONTENT_TYPE_HEADER, Constants.DEFAULT_CONTENT_TYPE);
            return headers;
          }
        };

    queue.add(request);
  }
コード例 #3
0
  /**
   * 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);
  }