Example usage for org.apache.http.client ResponseHandler handleResponse

List of usage examples for org.apache.http.client ResponseHandler handleResponse

Introduction

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

Prototype

T handleResponse(HttpResponse response) throws ClientProtocolException, IOException;

Source Link

Document

Processes an HttpResponse and returns some value corresponding to that response.

Usage

From source file:httpfailover.FailoverHttpClient.java

/**
 * Executes a request using the default context and processes the
 * response using the given response handler.
 *
 * @param targets   the candidate target hosts for the request.
 *                  The request is executed on each hosts until one succeeds.
 * @param request   the request to execute
 * @param responseHandler the response handler
 * @param context   the context to use for the execution, or
 *                  <code>null</code> to use the default context
 *
 * @return  the response object as generated by the response handler.
 * @throws IOException in case of a problem or the connection was aborted
 * @throws ClientProtocolException in case of an http protocol error
 *//*from  w ww  .  ja  v a  2s  .  c o  m*/
public <T> T execute(List<HttpHost> targets, HttpRequest request, ResponseHandler<? extends T> responseHandler,
        HttpContext context) throws IOException, ClientProtocolException {

    HttpResponse response = execute(targets, request, context);
    T result;
    try {
        result = responseHandler.handleResponse(response);
    } catch (Exception t) {
        HttpEntity entity = response.getEntity();
        try {
            EntityUtils.consume(entity);
        } catch (Exception t2) {
            // Log this exception. The original exception is more
            // important and will be thrown to the caller.
            this.log.warn("Error consuming content after an exception.", t2);
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        if (t instanceof IOException) {
            throw (IOException) t;
        }
        throw new UndeclaredThrowableException(t);
    }

    // Handling the response was successful. Ensure that the content has
    // been fully consumed.
    HttpEntity entity = response.getEntity();
    EntityUtils.consumeQuietly(entity);

    return result;
}

From source file:com.gooddata.http.client.GoodDataHttpClient.java

@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
        throws IOException {
    final HttpResponse resp = execute(request, context);
    return responseHandler.handleResponse(resp);
}

From source file:com.gooddata.http.client.GoodDataHttpClient.java

@Override
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler,
        HttpContext context) throws IOException {
    HttpResponse resp = execute(target, request, context);
    return responseHandler.handleResponse(resp);
}

From source file:co.paralleluniverse.fibers.httpclient.FiberHttpClient.java

/**
 * {@inheritDoc}/*from  w w w . j  a v  a 2 s .  c o m*/
 */
@Override
@Suspendable
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler,
        HttpContext context) throws IOException, ClientProtocolException {
    final HttpResponse response = doExecute(target, request, context);

    final T result;
    try {
        result = responseHandler.handleResponse(response);
    } catch (final Exception t) {
        final HttpEntity entity = response.getEntity();
        try {
            EntityUtils.consume(entity);
        } catch (final Exception t2) {
            // Log this exception. The original exception is more
            // important and will be thrown to the caller.
            this.log.warn("Error consuming content after an exception.", t2);
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        if (t instanceof IOException) {
            throw (IOException) t;
        }
        throw new UndeclaredThrowableException(t);
    }

    // Handling the response was successful. Ensure that the content has
    // been fully consumed.
    final HttpEntity entity = response.getEntity();
    EntityUtils.consume(entity);
    return result;
}

From source file:io.soabase.client.apache.WrappedHttpClient.java

private <T> T internalExecute(final HttpUriRequest uriRequest, final HttpHost target, final HttpRequest request,
        final ResponseHandler<? extends T> responseHandler, final HttpContext context) throws IOException {
    Args.notNull(responseHandler, "Response handler");

    final HttpResponse response = (uriRequest != null) ? execute(uriRequest, context)
            : execute(target, request, context);

    final T result;
    try {// w  w  w .ja  v a  2  s  .  com
        result = responseHandler.handleResponse(response);
    } catch (final Exception t) {
        final HttpEntity entity = response.getEntity();
        try {
            EntityUtils.consume(entity);
        } catch (final Exception t2) {
            // Log this exception. The original exception is more
            // important and will be thrown to the caller.
            // TODO this.log.warn("Error consuming content after an exception.", t2);
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        if (t instanceof IOException) {
            throw (IOException) t;
        }
        throw new UndeclaredThrowableException(t);
    }

    // Handling the response was successful. Ensure that the content has
    // been fully consumed.
    final HttpEntity entity = response.getEntity();
    EntityUtils.consume(entity);
    return result;
}

From source file:com.osbitools.ws.shared.web.BasicWebUtils.java

public WebResponse uploadFile(String path, String fname, InputStream in, String stoken)
        throws ClientProtocolException, IOException {

    HttpPost post = new HttpPost(path);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    StringBody fn = new StringBody(fname, ContentType.MULTIPART_FORM_DATA);

    builder.addPart("fname", fn);
    builder.addBinaryBody("file", in, ContentType.APPLICATION_XML, fname);

    BasicCookieStore cookieStore = new BasicCookieStore();

    if (stoken != null) {
        BasicClientCookie cookie = new BasicClientCookie(Constants.SECURE_TOKEN_NAME, stoken);
        cookie.setDomain(TestConstants.JETTY_HOST);
        cookie.setPath("/");
        cookieStore.addCookie(cookie);//from  w ww  .j  ava  2  s. c o m
    }

    TestConstants.LOG.debug("stoken=" + stoken);
    HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
    HttpEntity entity = builder.build();

    post.setEntity(entity);
    HttpResponse response = client.execute(post);

    String body;
    ResponseHandler<String> handler = new BasicResponseHandler();
    try {
        body = handler.handleResponse(response);
    } catch (HttpResponseException e) {
        return new WebResponse(e.getStatusCode(), e.getMessage());
    }

    return new WebResponse(response.getStatusLine().getStatusCode(), body);
}

From source file:org.bungeni.ext.integration.bungeniportal.BungeniAppConnector.java

/**
 * Negotiate the Oauth access URL , this will forward to a login page
 * @return/*from   ww w .j  ava 2  s  .  c o  m*/
 * @throws IOException 
 */
private String oauthNegotiate() throws IOException {
    final HttpGet hget = new HttpGet(this.oauthLoginUrl);
    HttpContext context = new BasicHttpContext();
    HttpResponse oauthResponse = getClient().execute(hget, context);
    // if the OAuth page retrieval failed throw an exception
    if (oauthResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException(oauthResponse.getStatusLine().toString());
    }
    // if the OAuth page retrieval succeeded we get the redirected page, 
    // which in this case is the login page
    String currentUrl = getRequestEndContextURL(context);
    // consume the response
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    responseHandler.handleResponse(oauthResponse);
    consumeContent(oauthResponse.getEntity());
    return currentUrl;
}

From source file:org.bungeni.ext.integration.bungeniportal.BungeniAppConnector.java

private WebResponse doMultiPartPost(String pageURL, MultipartEntity entity) {
    WebResponse wr = null;/*from  www .ja  v a2 s .  co  m*/
    HttpPost webPost = new HttpPost(pageURL);
    webPost.setEntity(entity);
    HttpResponse response = null;
    try {
        response = client.execute(webPost);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String sBody = responseHandler.handleResponse(response);
        wr = new WebResponse(sBody, response);
    } catch (IOException ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        if (response != null) {
            consumeContent(response.getEntity());
        }
    }
    return wr;
}

From source file:org.bungeni.ext.integration.bungeniportal.BungeniAppConnector.java

public WebResponse postUrl(String sPage, boolean prefix, List<BasicNameValuePair> nameValuePairs)
        throws UnsupportedEncodingException {
    WebResponse wr = null;/*from  w  w w . j av a 2 s .  co  m*/

    String pageURL = (prefix ? this.urlBase + sPage : sPage);
    HttpPost webPost = new HttpPost(pageURL);
    HttpEntity postEntity = new UrlEncodedFormEntity(nameValuePairs);
    webPost.setEntity(postEntity);
    HttpResponse response = null;
    try {
        response = getClient().execute(webPost);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String sBody = responseHandler.handleResponse(response);
        wr = new WebResponse(sBody, response);
    } catch (IOException ex) {
        log.error("Error while posting transition ", ex);
    } finally {
        consumeContent(response.getEntity());
    }
    return wr;
}