package com.worklight.wlclient.api;

import com.worklight.common.Logger;
import com.worklight.nativeandroid.common.WLUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.json.JSONException;
import org.json.JSONObject;

public class WLResponse
{
  private static Logger logger = Logger.getInstance("wl.response");
  private Header[] headers;
  private HttpResponse httpResponseCache;
  private WLRequestOptions requestOptions;
  private JSONObject responseJSON;
  protected String responseText;
  private int status;

  WLResponse(int paramInt, String paramString, WLRequestOptions paramWLRequestOptions)
  {
    this.status = paramInt;
    this.requestOptions = paramWLRequestOptions;
    this.responseText = paramString;
    responseTextToJSON(paramString);
  }

  WLResponse(WLResponse paramWLResponse)
  {
    this.status = paramWLResponse.status;
    this.requestOptions = paramWLResponse.requestOptions;
    this.responseText = paramWLResponse.responseText;
    this.responseJSON = paramWLResponse.responseJSON;
    this.httpResponseCache = paramWLResponse.httpResponseCache;
  }

  public WLResponse(HttpResponse paramHttpResponse)
  {
    this.status = paramHttpResponse.getStatusLine().getStatusCode();
    this.headers = paramHttpResponse.getAllHeaders();
    this.httpResponseCache = paramHttpResponse;
    try
    {
      if (this.status == 204)
        return;
      if (this.status == 201)
      {
        paramHttpResponse.getEntity().consumeContent();
        return;
      }
    }
    catch (Exception localException)
    {
      logger.error("Error getting content from server response: " + localException.getMessage(), localException);
    }
    while (true)
    {
      responseTextToJSON(this.responseText);
      return;
      if (paramHttpResponse.getHeaders("Content-Encoding").length > 0)
      {
        logger.trace("Content encoding is " + paramHttpResponse.getHeaders("Content-Encoding")[0].getValue());
        if (paramHttpResponse.getHeaders("Content-Encoding")[0].getValue().equalsIgnoreCase("gzip"))
          this.responseText = WLUtils.convertGZIPStreamToString(paramHttpResponse.getEntity().getContent());
        else
          this.responseText = WLUtils.convertStreamToString(paramHttpResponse.getEntity().getContent());
      }
      else
      {
        if (paramHttpResponse.getFirstHeader("Content-Length") != null)
          logger.trace("Response does not include a Content-Encoding header. Attempting to read response body as a string.");
        this.responseText = WLUtils.convertStreamToString(paramHttpResponse.getEntity().getContent());
      }
    }
  }

  private void responseTextToJSON(String paramString)
  {
    int i = this.responseText.indexOf('{');
    int j = this.responseText.lastIndexOf('}');
    if ((i == -1) || (j == -1))
    {
      this.responseJSON = null;
      return;
    }
    String str = this.responseText.substring(i, j + 1);
    try
    {
      this.responseJSON = new JSONObject(str);
      return;
    }
    catch (JSONException localJSONException)
    {
      logger.error("Response from Worklight server failed because could not read JSON from response with text " + str, localJSONException);
      this.responseJSON = null;
    }
  }

  public Header getHeader(String paramString)
  {
    for (int i = 0; i < this.headers.length; i++)
      if (this.headers[i].getName().equalsIgnoreCase(paramString))
        return this.headers[i];
    return null;
  }

  public Header[] getHeaders()
  {
    return this.headers;
  }

  public Object getInvocationContext()
  {
    return this.requestOptions.getInvocationContext();
  }

  public WLRequestOptions getOptions()
  {
    return this.requestOptions;
  }

  public JSONObject getResponseJSON()
  {
    return this.responseJSON;
  }

  public String getResponseText()
  {
    return this.responseText;
  }

  public int getStatus()
  {
    return this.status;
  }

  void setInvocationContext(Object paramObject)
  {
    this.requestOptions.setInvocationContext(paramObject);
  }

  public void setOptions(WLRequestOptions paramWLRequestOptions)
  {
    this.requestOptions = paramWLRequestOptions;
  }

  void setResponseHeader(String paramString1, String paramString2)
  {
    this.httpResponseCache.setHeader(paramString1, paramString2);
    this.headers = this.httpResponseCache.getAllHeaders();
  }

  public String toString()
  {
    return "WLResponse [invocationContext=" + this.requestOptions.getInvocationContext() + ", responseText=" + this.responseText + ", status=" + this.status + "]";
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.wlclient.api.WLResponse
 * JD-Core Version:    0.6.2
 */