Example usage for org.apache.http.client.methods HttpPost equals

List of usage examples for org.apache.http.client.methods HttpPost equals

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPost equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:org.mumod.util.HttpManager.java

public InputStream requestData(String url, String httpMethod, ArrayList<NameValuePair> params, int loop)
        throws IOException, MustardException, AuthException {

    URI uri;/*w  w w  . ja  v  a  2s .c om*/

    try {
        uri = new URI(url);
    } catch (URISyntaxException e) {
        throw new IOException("Invalid URL.");
    }
    if (MustardApplication.DEBUG)
        Log.d("HTTPManager", "Requesting " + uri);
    HttpUriRequest method;

    if (POST.equals(httpMethod)) {
        HttpPost post = new HttpPost(uri);
        if (params != null)
            post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        method = post;
    } else if (DELETE.equals(httpMethod)) {
        method = new HttpDelete(uri);
    } else {
        method = new HttpGet(uri);
    }

    if (mHeaders != null) {
        Iterator<String> headKeys = mHeaders.keySet().iterator();
        while (headKeys.hasNext()) {
            String key = headKeys.next();
            method.setHeader(key, mHeaders.get(key));
        }
    }
    if (consumer != null) {
        try {
            consumer.sign(method);
        } catch (OAuthMessageSignerException e) {

        } catch (OAuthExpectationFailedException e) {

        } catch (OAuthCommunicationException e) {

        }
    }

    HttpResponse response;
    try {
        response = mClient.execute(method);
    } catch (ClientProtocolException e) {
        throw new IOException("HTTP protocol error.");
    }

    int statusCode = response.getStatusLine().getStatusCode();
    //      Log.d("HttpManager", url + " >> " + statusCode);
    if (statusCode == 401) {
        throw new AuthException(401, "Unauthorized");
    } else if (statusCode == 400 || statusCode == 403 || statusCode == 406) {
        try {
            JSONObject json = null;
            try {
                json = new JSONObject(StreamUtil.toString(response.getEntity().getContent()));
            } catch (JSONException e) {
                throw new MustardException(998, "Non json response: " + e.toString());
            }
            throw new MustardException(statusCode, json.getString("error"));
        } catch (IllegalStateException e) {
            throw new IOException("Could not parse error response.");
        } catch (JSONException e) {
            throw new IOException("Could not parse error response.");
        }
    } else if (statusCode == 404) {
        // User/Group or page not found
        throw new MustardException(404, "Not found: " + url);
    } else if ((statusCode == 301 || statusCode == 302 || statusCode == 303) && GET.equals(httpMethod)
            && loop < 3) {
        //         Log.v("HttpManager", "Got : " + statusCode);
        Header hLocation = response.getLastHeader("Location");
        if (hLocation != null) {
            Log.v("HttpManager", "Got : " + hLocation.getValue());
            return requestData(hLocation.getValue(), httpMethod, params, loop + 1);
        } else
            throw new MustardException(statusCode, "Too many redirect: " + url);
    } else if (statusCode != 200) {
        throw new MustardException(999, "Unmanaged response code: " + statusCode);
    }

    return response.getEntity().getContent();
}

From source file:org.mustard.util.HttpManager.java

public InputStream requestData(String url, String httpMethod, ArrayList<NameValuePair> params, int loop)
        throws IOException, MustardException, AuthException {

    URI uri;/*from w  w  w  .j  ava2 s . c  o  m*/

    try {
        uri = new URI(url);
    } catch (URISyntaxException e) {
        throw new IOException("Invalid URL.");
    }
    if (MustardApplication.DEBUG)
        Log.d("HTTPManager", "Requesting " + uri);
    HttpUriRequest method;

    if (POST.equals(httpMethod)) {
        HttpPost post = new HttpPost(uri);
        if (params != null)
            post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        method = post;
    } else if (DELETE.equals(httpMethod)) {
        method = new HttpDelete(uri);
    } else {
        method = new HttpGet(uri);
    }

    if (mHeaders != null) {
        Iterator<String> headKeys = mHeaders.keySet().iterator();
        while (headKeys.hasNext()) {
            String key = headKeys.next();
            method.setHeader(key, mHeaders.get(key));
        }
    }
    if (consumer != null) {
        try {
            consumer.sign(method);
        } catch (OAuthMessageSignerException e) {

        } catch (OAuthExpectationFailedException e) {

        } catch (OAuthCommunicationException e) {

        }
    }

    HttpResponse response;
    try {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        String accessToken = preferences.getString("oauth2_access_token", "no token specified");
        method.setHeader("Authorization", "Bearer " + accessToken);
        response = mClient.execute(method);
    } catch (ClientProtocolException e) {
        throw new IOException("HTTP protocol error.");
    }

    int statusCode = response.getStatusLine().getStatusCode();
    //      Log.d("HttpManager", url + " >> " + statusCode);
    if (statusCode == 401) {
        throw new AuthException(401, "Unauthorized");
    } else if (statusCode == 403 || statusCode == 406) {
        try {
            JSONObject json = null;
            try {
                json = new JSONObject(StreamUtil.toString(response.getEntity().getContent()));
            } catch (JSONException e) {
                throw new MustardException(998, "Non json response: " + e.toString());
            }
            throw new MustardException(statusCode, json.getString("error"));
        } catch (IllegalStateException e) {
            throw new IOException("Could not parse error response.");
        } catch (JSONException e) {
            throw new IOException("Could not parse error response.");
        }
    } else if (statusCode == 404) {
        // User/Group or page not found
        throw new MustardException(404, "Not found: " + url);
    } else if ((statusCode == 301 || statusCode == 302 || statusCode == 303) && GET.equals(httpMethod)
            && loop < 3) {
        //         Log.v("HttpManager", "Got : " + statusCode);
        Header hLocation = response.getLastHeader("Location");
        if (hLocation != null) {
            Log.v("HttpManager", "Got : " + hLocation.getValue());
            return requestData(hLocation.getValue(), httpMethod, params, loop + 1);
        } else
            throw new MustardException(statusCode, "Too many redirect: " + url);
    } else if (statusCode != 200) {
        throw new MustardException(999, "Unmanaged response code: " + statusCode);
    }

    return response.getEntity().getContent();
}

From source file:org.opendatakit.aggregate.externalservice.AbstractExternalService.java

protected HttpResponse sendHttpRequest(String method, String url, HttpEntity entity,
        List<NameValuePair> qparams, CallingContext cc) throws IOException {

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, SOCKET_ESTABLISHMENT_TIMEOUT_MILLISECONDS);
    HttpConnectionParams.setSoTimeout(httpParams, SERVICE_TIMEOUT_MILLISECONDS);

    // setup client
    HttpClientFactory factory = (HttpClientFactory) cc.getBean(BeanDefs.HTTP_CLIENT_FACTORY);
    HttpClient client = factory.createHttpClient(httpParams);

    // support redirecting to handle http: => https: transition
    HttpClientParams.setRedirecting(httpParams, true);
    // support authenticating
    HttpClientParams.setAuthenticating(httpParams, true);

    // redirect limit is set to some unreasonably high number
    // resets of the socket cause a retry up to MAX_REDIRECTS times,
    // so we should be careful not to set this too high...
    httpParams.setParameter(ClientPNames.MAX_REDIRECTS, 32);
    httpParams.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

    // context holds authentication state machine, so it cannot be
    // shared across independent activities.
    HttpContext localContext = new BasicHttpContext();

    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);

    HttpUriRequest request = null;//from   w  ww. j  ava2 s . co m
    if (entity == null && (POST.equals(method) || PATCH.equals(method) || PUT.equals(method))) {
        throw new IllegalStateException("No body supplied for POST, PATCH or PUT request");
    } else if (entity != null && !(POST.equals(method) || PATCH.equals(method) || PUT.equals(method))) {
        throw new IllegalStateException("Body was supplied for GET or DELETE request");
    }

    URI nakedUri;
    try {
        nakedUri = new URI(url);
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException(e);
    }

    if (qparams == null) {
        qparams = new ArrayList<NameValuePair>();
    }
    URI uri;
    try {
        uri = new URI(nakedUri.getScheme(), nakedUri.getUserInfo(), nakedUri.getHost(), nakedUri.getPort(),
                nakedUri.getPath(), URLEncodedUtils.format(qparams, HtmlConsts.UTF8_ENCODE), null);
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
        throw new IllegalStateException(e1);
    }
    System.out.println(uri.toString());

    if (GET.equals(method)) {
        HttpGet get = new HttpGet(uri);
        request = get;
    } else if (DELETE.equals(method)) {
        HttpDelete delete = new HttpDelete(uri);
        request = delete;
    } else if (PATCH.equals(method)) {
        HttpPatch patch = new HttpPatch(uri);
        patch.setEntity(entity);
        request = patch;
    } else if (POST.equals(method)) {
        HttpPost post = new HttpPost(uri);
        post.setEntity(entity);
        request = post;
    } else if (PUT.equals(method)) {
        HttpPut put = new HttpPut(uri);
        put.setEntity(entity);
        request = put;
    } else {
        throw new IllegalStateException("Unexpected request method");
    }

    HttpResponse resp = client.execute(request);
    return resp;
}