Android Open Source - android-rest-client Rest Client Request






From Project

Back to project page android-rest-client.

License

The source code is released under:

Apache License

If you think the Android project android-rest-client listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.dg.libs.rest.requests;
/* ww w  . ja v  a  2  s .  com*/
import com.araneaapps.android.libs.asyncrunners.models.RequestOptions;
import com.araneaapps.android.libs.asyncrunners.models.TaskStore;
import com.araneaapps.android.libs.logger.ALog;
import com.dg.libs.rest.HttpRequest;
import com.dg.libs.rest.RestClientConfiguration;
import com.dg.libs.rest.authentication.AuthenticationProvider;
import com.dg.libs.rest.callbacks.HttpCallback;
import com.dg.libs.rest.client.RequestMethod;
import com.dg.libs.rest.domain.ResponseStatus;
import com.dg.libs.rest.handlers.DefaultResponseStatusHandler;
import com.dg.libs.rest.handlers.ResponseHandler;
import com.dg.libs.rest.handlers.ResponseStatusHandler;
import com.dg.libs.rest.handlers.UIThreadResponseHandler;
import com.dg.libs.rest.parsers.HttpResponseParser;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

public abstract class RestClientRequest<T> implements HttpRequest {

  private String TAG = RestClientRequest.class.getSimpleName();

  private HttpResponseParser<T> parser;
  private ResponseHandler<T> handler;
  private ResponseStatusHandler statusHandler;
  private HttpCallback<T> callback;
  private Request.Builder request = new Request.Builder();
  private StringBuilder queryParams;
  private String url;

  RequestOptions requestOptions = null;
  AuthenticationProvider authenticationProvider;

  protected RestClientRequest() {
    authenticationProvider = RestClientConfiguration.get().getAuthenticationProvider();
  }

  public RestClientRequest<T> setAuthenticationProvider(AuthenticationProvider authenticationProvider) {
    this.authenticationProvider = authenticationProvider;
    return this;
  }

  protected HttpCallback<T> getCallback() {
    return callback;
  }

  public RestClientRequest<T> setCallback(HttpCallback<T> callback) {
    this.callback = callback;
    return this;
  }

  public RestClientRequest<T> setParser(HttpResponseParser<T> parser) {
    this.parser = parser;
    return this;
  }

  public ResponseHandler<T> getHandler() {
    return handler;
  }

  /**
   * Adds a new header value, existing value with same key will not be overwritten
   *
   * @param key
   * @param value
   */
  public RestClientRequest<T> addHeader(final String key, final String value) {
    request.addHeader(key, value);
    return this;
  }

  /**
   * Overrides an existing header value
   *
   * @param key
   * @param value
   */
  public RestClientRequest<T> header(final String key, final String value) {
    request.header(key, value);
    return this;
  }

  @Override
  public void run() {
    doBeforeRunRequestInBackgroundThread();
    runRequest();
  }

  /**
   * Set a custom handler that will be triggered when the response returns
   * either Success or Fail. You can choose where this info is sent. **Default**
   * is the UIThreadREsponseHandler implementation which runs the appropriate
   * callback on the UI thread.
   *
   * @param handler
   */
  public RestClientRequest<T> setResponseHandler(ResponseHandler<T> handler) {
    this.handler = handler;
    return this;
  }

  /**
   * By default success is a code in the range of 2xx. Everything else triggers
   * an Error. You can set a handler which will take into account your own
   * custom error codes to determine if the response is a success or fail.
   *
   * @param statusHandler
   */
  public RestClientRequest<T> setStatusHandler(ResponseStatusHandler statusHandler) {
    this.statusHandler = statusHandler;
    return this;
  }

  protected HttpResponseParser<T> getParser() {
    return parser;
  }

  public RestClientRequest<T> setUrl(String url) {
    this.url = url;
    return this;
  }

  public RestClientRequest<T> setRequestMethod(RequestMethod requestMethod) {
    return setRequestMethod(requestMethod, null);

  }

  public RestClientRequest<T> setRequestMethod(RequestMethod requestMethod, RequestBody requestBody) {
    request.method(requestMethod.name(), requestBody);
    return this;
  }

  protected void runRequest() {

    if (handler == null) {
      handler = new UIThreadResponseHandler<T>(callback);
    }
    if (statusHandler == null) {
      statusHandler = new DefaultResponseStatusHandler();
    }

    OkHttpClient client = generateClient();

    StringBuilder url = new StringBuilder(this.url);

    StringBuilder queryParams = this.queryParams;
    if (queryParams != null) {
      url.append(queryParams);
    }

    request.url(url.toString());

    if (this.authenticationProvider != null) {
      authenticationProvider.authenticateRequest(this);
    }

    Request preparedRequest = request.build();

    Response response;
    try {
      response = client.newCall(preparedRequest).execute();
    } catch (final Exception e) {
      ResponseStatus responseStatus = ResponseStatus.getConnectionErrorStatus();
      ALog.e(TAG, responseStatus.toString(), e);
      handler.handleError(responseStatus);
      return;
    }

    final ResponseStatus status = new ResponseStatus(response.code(), response.message());
    ALog.d(TAG, status.toString());
    if (handleResponseStatus(status)) {
      return;
    }

    try {

      if (parser != null) {
        InputStream instream = response.body().byteStream();
        final T responseData = parser.parse(instream);
        close(response.body());
        handler.handleSuccess(responseData, status);
        doAfterSuccessfulRequestInBackgroundThread(responseData);
      } else {
        ALog.i("You haven't added a parser for your request");
        handler.handleSuccess(null, status);
        doAfterSuccessfulRequestInBackgroundThread(null);
      }

    } catch (final Exception e) {
      ResponseStatus responseStatus = ResponseStatus.getParseErrorStatus();
      ALog.d(TAG, responseStatus.toString(), e);
      handler.handleError(responseStatus);
    }

  }

  /**
   * Return true if you have handled the status yourself.
   */
  protected boolean handleResponseStatus(ResponseStatus status) {
    if (statusHandler.hasErrorInStatus(status)) {
      handler.handleError(status);
      return true;
    }
    return false;
  }

  @Override
  public void executeAsync() {
    TaskStore.get(RestClientConfiguration.get().getContext()).queue(this, getRequestOptions());
  }

  public RequestOptions getRequestOptions() {
    return requestOptions;
  }

  public RestClientRequest<T> setRequestOptions(RequestOptions requestOptions) {
    this.requestOptions = requestOptions;
    return this;
  }

  /**
   * Use this method to add the required data to the request. This will happen
   * in the background thread which enables you to pre-process the parameters,
   * do queries etc..
   */
  protected void doBeforeRunRequestInBackgroundThread() {
  }

  /**
   * This will happen in the background thread which enables you to do some
   * cleanup in the background after the request finishes
   */
  protected void doAfterSuccessfulRequestInBackgroundThread(T data) {
  }

  private OkHttpClient generateClient() {
    OkHttpClient client = new OkHttpClient();
    customizeClient(client);
    return client;
  }

  protected void customizeClient(OkHttpClient client) {
    client.setConnectTimeout(RestClientConfiguration.get().getConnectionTimeout(), TimeUnit.MILLISECONDS);
    client.setReadTimeout(RestClientConfiguration.get().getSocketTimeout(), TimeUnit.MILLISECONDS);
  }

  public RestClientRequest<T> addQueryParam(String name, String value) {
    addQueryParam(name, value, false, true);
    return this;
  }

  public RestClientRequest<T> addEncodedQueryParam(String name, String value) {
    addQueryParam(name, value, false, false);
    return this;
  }

  private void addQueryParam(String name, Object value, boolean encodeName, boolean encodeValue) {
    if (value instanceof Iterable) {
      for (Object iterableValue : (Iterable<?>) value) {
        if (iterableValue != null) { // Skip null values
          addQueryParam(name, iterableValue.toString(), encodeName, encodeValue);
        }
      }
    } else if (value.getClass().isArray()) {
      for (int x = 0, arrayLength = Array.getLength(value); x < arrayLength; x++) {
        Object arrayValue = Array.get(value, x);
        if (arrayValue != null) { // Skip null values
          addQueryParam(name, arrayValue.toString(), encodeName, encodeValue);
        }
      }
    } else {
      addQueryParam(name, value.toString(), encodeName, encodeValue);
    }
  }

  private void addQueryParam(String name, String value, boolean encodeName, boolean encodeValue) {
    if (name == null) {
      throw new IllegalArgumentException("Query param name must not be null.");
    }
    if (value == null) {
      throw new IllegalArgumentException("Query param \"" + name + "\" value must not be null.");
    }
    try {
      StringBuilder queryParams = this.queryParams;
      if (queryParams == null) {
        this.queryParams = queryParams = new StringBuilder();
      }

      queryParams.append(queryParams.length() > 0 ? '&' : '?');

      if (encodeName) {
        name = URLEncoder.encode(name, "UTF-8");
      }
      if (encodeValue) {
        value = URLEncoder.encode(value, "UTF-8");
      }
      queryParams.append(name).append('=').append(value);
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(
        "Unable to convert query parameter \"" + name + "\" value to UTF-8: " + value, e);
    }
  }

  private void close(ResponseBody body) {
    try {
      if (body != null) {
        body.close();
      }
    } catch (IOException e) {
    }

  }
}




Java Source Code List

com.dg.examples.restclientdemo.MainActivity.java
com.dg.examples.restclientdemo.app.App.java
com.dg.examples.restclientdemo.communication.RestConstants.java
com.dg.examples.restclientdemo.communication.parsers.BlogsGoogleParser.java
com.dg.examples.restclientdemo.communication.requests.BlogsGoogleRequest.java
com.dg.examples.restclientdemo.communication.requests.CustomHandlersRequest.java
com.dg.examples.restclientdemo.communication.requests.PatchRequest.java
com.dg.examples.restclientdemo.domain.EntriesModel.java
com.dg.examples.restclientdemo.domain.ResponseDataModel.java
com.dg.examples.restclientdemo.domain.ResponseModel.java
com.dg.libs.rest.HttpRequest.java
com.dg.libs.rest.RestClientConfiguration.java
com.dg.libs.rest.authentication.AuthenticationProvider.java
com.dg.libs.rest.authentication.TokenAuthenticationProvider.java
com.dg.libs.rest.callbacks.HttpCallback.java
com.dg.libs.rest.client.RequestMethod.java
com.dg.libs.rest.domain.ResponseStatus.java
com.dg.libs.rest.entities.CountingInputStreamEntity.java
com.dg.libs.rest.entities.UnicodeBOMInputStream.java
com.dg.libs.rest.exceptions.HttpException.java
com.dg.libs.rest.exceptions.ParseException.java
com.dg.libs.rest.handlers.BackgroundThreadResponseHandler.java
com.dg.libs.rest.handlers.DefaultResponseStatusHandler.java
com.dg.libs.rest.handlers.ResponseHandler.java
com.dg.libs.rest.handlers.ResponseStatusHandler.java
com.dg.libs.rest.handlers.UIThreadResponseHandler.java
com.dg.libs.rest.parsers.BaseJacksonMapperResponseParser.java
com.dg.libs.rest.parsers.FileHttpResponseParser.java
com.dg.libs.rest.parsers.HttpResponseParser.java
com.dg.libs.rest.parsers.NoResponseParser.java
com.dg.libs.rest.parsers.StringHttpResponseParser.java
com.dg.libs.rest.requests.RequestBodyUtils.java
com.dg.libs.rest.requests.RestClientRequest.java