Example usage for org.apache.http.client HttpResponseException HttpResponseException

List of usage examples for org.apache.http.client HttpResponseException HttpResponseException

Introduction

In this page you can find the example usage for org.apache.http.client HttpResponseException HttpResponseException.

Prototype

public HttpResponseException(final int statusCode, final String s) 

Source Link

Usage

From source file:cn.caimatou.canting.utils.http.asynchttp.AsyncHttpResponseHandler.java

void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    String responseBody = null;//  w  w w .ja v  a  2  s  .c o  m
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
            responseBody = EntityUtils.toString(entity, "UTF-8");
        }
    } catch (IOException e) {
        sendFailureMessage(e, (String) null);
    }

    if (status.getStatusCode() >= 300) {
        sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()),
                responseBody);
    } else {
        sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
    }
}

From source file:com.hdsfed.cometapi.ThreadTrackerDB.java

static private synchronized int HttpCatchError(HttpRequestBase httpRequest, HttpResponse httpResponse)
        throws IOException {
    if (2 != (int) (httpResponse.getStatusLine().getStatusCode() / 100)) {
        // Clean up after ourselves and release the HTTP connection to the connection manager.
        EntityUtils.consume(httpResponse.getEntity());
        //ScreenLog.out(Headers,"BEGIN header dump");
        throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(),
                "Unexpected status returned from " + httpRequest.getMethod() + " ("
                        + httpResponse.getStatusLine().getStatusCode() + ": "
                        + httpResponse.getStatusLine().getReasonPhrase() + ")");
    }/*from   w ww. ja va  2 s.  com*/
    return (int) (httpResponse.getStatusLine().getStatusCode());
}

From source file:com.qk.applibrary.http.AsyncHttpResponseHandler.java

void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    String responseBody = null;//from w  ww. ja  va 2s.c o  m
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
            responseBody = EntityUtils.toString(entity, "UTF-8");
        }
    } catch (IOException e) {
        sendFailureMessage(e, (String) null);
    }
    if (status.getStatusCode() == 200) {
        sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
    } else {
        sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()),
                responseBody);
    }
}

From source file:com.puppetlabs.puppetdb.javaclient.impl.HttpComponentsConnector.java

@Override
public void download(String urlStr, Map<String, String> params, final OutputStream output) throws IOException {
    HttpGet request = createGetRequest(urlStr, params);
    configureRequest(request);/*from w ww  . ja v  a2 s  .c o  m*/
    httpClient.execute(request, new ResponseHandler<Void>() {
        @Override
        public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            StatusLine statusLine = response.getStatusLine();
            int code = statusLine.getStatusCode();
            if (code != HttpStatus.SC_OK)
                throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());

            HttpEntity entity = response.getEntity();
            entity.writeTo(output);
            return null;
        }
    });
}

From source file:com.pyj.http.AsyncHttpResponseHandler.java

void sendResponseMessage(HttpResponse response, int reqType) {
    StatusLine status = response.getStatusLine();
    //      response.getAllHeaders()[1].get
    try {/* w  ww. j a v  a2  s .  c o m*/
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();

        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
        }
        if (status.getStatusCode() >= 300) {
            String responseBody = EntityUtils.toString(entity, "UTF-8");
            sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()),
                    responseBody, reqType);
        } else {
            //            byte[] result = EntityUtils.toByteArray(response.getEntity());
            sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), entity, reqType);
        }
    } catch (IOException e) {
        sendFailureMessage(e, (String) null, reqType);
    }
}

From source file:fr.lissi.belilif.om2m.rest.WebServiceActions.java

/**
 * Do delete.//from   www  . ja  v a 2s .c  o m
 *
 * @param uri the uri
 * @param headers the headers
 * @return the http get simple resp
 * @throws ClientProtocolException the client protocol exception
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws HttpResponseException the http response exception
 */
public static HttpGetSimpleResp doDelete(String uri, HashMap<String, String> headers)
        throws ClientProtocolException, IOException, HttpResponseException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGetSimpleResp resp = new HttpGetSimpleResp();
    try {
        HttpDelete httpDelete = new HttpDelete(uri);
        for (String key : headers.keySet()) {
            httpDelete.addHeader(key, headers.get(key));
        }
        CloseableHttpResponse response = httpclient.execute(httpDelete);

        resp.setStatusCode(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    // TODO to use for performance in the future
                    resp.setResult(new BasicResponseHandler().handleResponse(response));
                }
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        } else {
            // TODO optimiz (repeating code)
            throw new HttpResponseException(response.getStatusLine().getStatusCode(),
                    response.getStatusLine().getReasonPhrase());
        }
    } finally {
        httpclient.close();
    }
    return resp;
}

From source file:com.jkoolcloud.tnt4j.streams.inputs.RestStream.java

private static String executeRequest(HttpClient client, HttpUriRequest req, String username, String password)
        throws IOException {
    HttpResponse response;/*from w w  w.  j a v  a 2 s .  com*/

    if (StringUtils.isNotEmpty(username)) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(req.getURI().getAuthority(), DEFAULT_AUTH_PORT),
                new UsernamePasswordCredentials(username, password));

        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credentialsProvider);
        // context.setAuthSchemeRegistry(authRegistry);
        // context.setAuthCache(authCache);

        response = client.execute(req, context);
    } else {
        response = client.execute(req);
    }

    int responseCode = response.getStatusLine().getStatusCode();
    if (responseCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
        throw new HttpResponseException(responseCode, response.getStatusLine().getReasonPhrase());
    }

    String respStr = EntityUtils.toString(response.getEntity(), Utils.UTF8);

    return respStr;
}

From source file:com.example.openfirechat.http.AsyncHttpResponseHandler.java

void sendResponseMessage(HttpResponse response) {
    StatusLine status = response.getStatusLine();
    String responseBody = null;//from  w  ww  .j a v  a  2  s  .  c  o m
    try {
        HttpEntity entity = null;
        HttpEntity temp = response.getEntity();
        if (temp != null) {
            entity = new BufferedHttpEntity(temp);
        }
        responseBody = DecryptUtil.decrytEntity(entity);
    } catch (IOException e) {
        sendFailureMessage(e, null);
    }

    if (status.getStatusCode() >= 300) {
        sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()),
                responseBody);
    } else {
        sendSuccessMessage(responseBody);
    }
}

From source file:com.unifonic.sdk.resources.http.AccountResourceImpl.java

@Override
public ResponseModel<Voids> changeAppDefaultSenderID(String senderID) throws IOException {
    ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
    param.add(new BasicNameValuePair(PARAM_SENDER_ID, senderID));
    OTSRestResponse response = sendRequest(accountUrl.urlChangeDefaultSenderID(), param);
    if (response.getStatusCode() < 400) {
        Type type = new TypeToken<ResponseModel<Voids>>() {
        }.getType();/*from ww w  .  java2s .  c om*/
        ResponseModel<Voids> respData = GSON.fromJson(response.getData(), type);
        return respData;
    } else if (response.getStatusCode() == 400) {
        ResponseModel<Voids> respData = GSON.fromJson(response.getData(), ResponseModel.class);
        return respData;
    } else {
        throw new HttpResponseException(response.getStatusCode(), response.getReasonPhrase());
    }
}