Example usage for org.apache.http.client.methods HttpRequestBase abort

List of usage examples for org.apache.http.client.methods HttpRequestBase abort

Introduction

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

Prototype

public void abort() 

Source Link

Usage

From source file:com.bluexml.side.deployer.alfresco.directcopy.AlfrescoHotDeployerHelper.java

public static String executeRequest(HttpClient httpclient, HttpRequestBase post) throws Exception {
    String responseS = "";
    // Execute the request
    HttpResponse response;//w  w w .  j av  a 2  s .c  o m

    System.out.println("AlfrescoHotDeployerHelper.executeRequest() request:" + post);
    System.out.println("AlfrescoHotDeployerHelper.executeRequest() URI :" + post.getURI());

    response = httpclient.execute(post);

    // Examine the response status
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();

    System.out.println(response.getStatusLine());

    // Get hold of the response entity
    HttpEntity entity = response.getEntity();

    // If the response does not enclose an entity, there is no need
    // to worry about connection release
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
            // do something useful with the response

            responseS = readBuffer(responseS, reader);

        } catch (IOException ex) {

            // In case of an IOException the connection will be released
            // back to the connection manager automatically
            throw ex;

        } catch (RuntimeException ex) {

            // In case of an unexpected exception you may want to abort
            // the HTTP request in order to shut down the underlying 
            // connection and release it back to the connection manager.

            post.abort();
            throw ex;

        } finally {

            // Closing the input stream will trigger connection release
            instream.close();

        }

        // When HttpClient instance is no longer needed, 
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
    if (statusCode != 200) {
        throw new Exception("Request Fail HTTP response :" + statusLine);
    }
    return responseS;
}

From source file:org.lightcouch.CouchDbClientBase.java

/**
 * Executes a HTTP request./*from  w  ww.  j  a v  a  2 s . co  m*/
 * @param request The HTTP request to execute.
 * @return {@link HttpResponse}
 */
public HttpResponse executeRequest(HttpRequestBase request) {
    HttpResponse response = null;
    try {
        response = httpClient.execute(host, request, context);
    } catch (IOException e) {
        request.abort();
        log.error("Error executing request. " + e.getMessage());
        throw new CouchDbException(e);
    }
    return response;
}

From source file:com.cloudant.mazha.HttpRequests.java

/**
 * Executes a HTTP request.//  w  w w. ja v a  2s .  c  om
 *
 * @param request The HTTP request to execute.
 * @return {@link org.apache.http.HttpResponse}
 */
private HttpResponse executeRequest(HttpRequestBase request) {
    try {
        if (authHeaderValue != null) {
            request.addHeader("Authorization", authHeaderValue);
        }
        HttpResponse response = httpClient.execute(host, request, context);
        validate(request, response);
        return response;
    } catch (IOException e) {
        request.abort();
        throw new ServerException(e);
    }
}

From source file:com.aliyun.oss.common.comm.TimeoutServiceClient.java

@Override
public ResponseMessage sendRequestCore(ServiceClient.Request request, ExecutionContext context)
        throws IOException {
    HttpRequestBase httpRequest = httpRequestFactory.createHttpRequest(request, context);
    HttpClientContext httpContext = HttpClientContext.create();
    httpContext.setRequestConfig(this.requestConfig);

    CloseableHttpResponse httpResponse = null;
    HttpRequestTask httpRequestTask = new HttpRequestTask(httpRequest, httpContext);
    Future<CloseableHttpResponse> future = executor.submit(httpRequestTask);

    try {//from   ww w  .java  2 s  . co m
        httpResponse = future.get(this.config.getRequestTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        logException("[ExecutorService]The current thread was interrupted while waiting: ", e);

        httpRequest.abort();
        throw new ClientException(e.getMessage(), e);
    } catch (ExecutionException e) {
        RuntimeException ex;
        httpRequest.abort();

        if (e.getCause() instanceof IOException) {
            ex = ExceptionFactory.createNetworkException((IOException) e.getCause());
        } else {
            ex = new OSSException(e.getMessage(), e);
        }

        logException("[ExecutorService]The computation threw an exception: ", ex);
        throw ex;
    } catch (TimeoutException e) {
        logException("[ExecutorService]The wait " + this.config.getRequestTimeout() + " timed out: ", e);

        httpRequest.abort();
        throw new ClientException(e.getMessage(), OSSErrorCode.REQUEST_TIMEOUT, "Unknown", e);
    }

    return buildResponse(request, httpResponse);
}

From source file:com.xively.client.http.DefaultRequestHandler.java

/**
 * * Make the request to Xively API and return the response string
 *
 * @param <T extends ConnectedObject>
 *
 * @param requestMethod/*from   www  .j  a va 2s. c om*/
 *            http request methods
 * @param appPath
 *            restful app path
 * @param bodyObjects
 *            objects to be parsed as body for api call
 * @param params
 *            key-value of params for api call
 *
 * @return response string
 */

private <T extends DomainObject> Response<T> doRequest(HttpMethod requestMethod, String appPath,
        Map<String, Object> params, T... bodyObjects) {
    Response<T> response = null;
    HttpRequestBase request = buildRequest(requestMethod, appPath, params, bodyObjects);

    DefaultRequestHandler.log.debug(String.format("Requesting %s", request.getURI()));

    try {
        DefaultResponseHandler<T> responseHandler = new DefaultResponseHandler<T>();
        response = getClient().execute(request, responseHandler);
    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw new HttpException("Http request did not return successfully.", e);
    } catch (RuntimeException e) {
        // release resources manually on unexpected exceptions
        request.abort();
        throw new HttpException("Http request did not return successfully.", e);
    }

    return response;
}

From source file:org.apache.manifoldcf.authorities.authorities.jira.JiraSession.java

private void getRest(String rightside, JiraJSONResponse response) throws IOException, ResponseException {

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local
    // auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);//from www .j a v a  2  s.  co m

    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    final HttpRequestBase method = new HttpGet(host.toURI() + path + rightside);
    method.addHeader("Accept", "application/json");

    try {
        HttpResponse httpResponse = httpClient.execute(method, localContext);
        int resultCode = httpResponse.getStatusLine().getStatusCode();
        if (resultCode != 200)
            throw new ResponseException(
                    "Unexpected result code " + resultCode + ": " + convertToString(httpResponse));
        Object jo = convertToJSON(httpResponse);
        response.acceptJSONObject(jo);
    } finally {
        method.abort();
    }
}

From source file:com.windigo.http.client.ApacheHttpClient.java

/**
 * Execute the given {@link HttpRequestBase}. Closes all the expired
 * connection pool items and set heeaders if present.
 * //from  w  w  w . jav  a  2 s.  c  o m
 * @param httpRequest
 * @throws IOException
 * @return {@link HttpResponse}
 */
public HttpResponse executeHttpRequest(HttpRequestBase httpRequest) throws IOException {

    Logger.log("[Request] Executing http request for: " + httpRequest.getURI().toString());

    // set headers for request
    if (headers != null && headers.length > 0) {
        httpRequest.setHeaders(headers);
        Logger.log("[Request] Found " + headers.length + " header");
    }

    try {
        mHttpClient.getConnectionManager().closeExpiredConnections();
        return mHttpClient.execute(httpRequest);
    } catch (IOException e) {
        httpRequest.abort();
        throw e;
    }

}

From source file:org.apache.manifoldcf.crawler.connectors.jira.JiraSession.java

private void getRest(String rightside, JiraJSONResponse response) throws IOException, ResponseException {

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local
    // auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);//from   w w  w.j  a  va 2s  .c  om

    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    final HttpRequestBase method = new HttpGet(host.toURI() + path + rightside);
    method.addHeader("Accept", "application/json");

    try {
        HttpResponse httpResponse = httpClient.execute(method, localContext);
        int resultCode = httpResponse.getStatusLine().getStatusCode();
        if (resultCode != 200)
            throw new IOException(
                    "Unexpected result code " + resultCode + ": " + convertToString(httpResponse));
        Object jo = convertToJSON(httpResponse);
        response.acceptJSONObject(jo);
    } finally {
        method.abort();
    }
}