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.ettrema.httpclient.TransferService.java

public synchronized void get(String url, StreamReceiver receiver, List<Range> rangeList,
        ProgressListener listener) throws com.ettrema.httpclient.HttpException, Utils.CancelledException,
        NotAuthorizedException, BadRequestException, ConflictException, NotFoundException {
    LogUtils.trace(log, "get: ", url);
    notifyStartRequest();/*  w w w .  j  ava 2  s .  com*/
    HttpRequestBase m;
    if (rangeList != null) {
        try {
            m = new RangedGetMethod(url, rangeList);
        } catch (URISyntaxException ex) {
            throw new RuntimeException(ex);
        }
    } else {
        m = new HttpGet(url);
    }
    InputStream in = null;
    NotifyingFileInputStream nin = null;
    try {
        HttpResponse resp = client.execute(m);
        if (resp.getEntity() == null) {
            log.warn("Did not receive a response entity for GET");
            return;
        }
        HttpEntity entity = resp.getEntity();
        in = entity.getContent();
        Utils.processResultCode(resp.getStatusLine().getStatusCode(), url);
        nin = new NotifyingFileInputStream(in, entity.getContentLength(), url, listener);
        receiver.receive(nin);
    } catch (Utils.CancelledException ex) {
        m.abort();
        throw ex;
    } catch (IOException ex) {
        m.abort();
        throw new RuntimeException(ex);
    } finally {
        Utils.close(in);
        notifyFinishRequest();
    }
}

From source file:com.googlecode.sardine.SardineImpl.java

/**
 * Small wrapper around HttpClient.execute() in order to wrap the IOException into a SardineException.
 *//*  w w w.j a va2 s .  c  o m*/
private HttpResponse executeWrapper(HttpRequestBase base) throws SardineException {
    try {
        if (this.authEnabled) {
            Credentials creds = this.client.getCredentialsProvider().getCredentials(AuthScope.ANY);
            String value = "Basic " + new String(Base64.encodeBase64(
                    new String(creds.getUserPrincipal().getName() + ":" + creds.getPassword()).getBytes()));
            base.setHeader("Authorization", value);
        }

        return this.client.execute(base);
    } catch (IOException ex) {
        base.abort();
        throw new SardineException(ex);
    }
}

From source file:io.milton.httpclient.TransferService.java

public synchronized void get(String url, StreamReceiver receiver, List<Range> rangeList,
        ProgressListener listener, HttpContext context)
        throws io.milton.httpclient.HttpException, Utils.CancelledException, NotAuthorizedException,
        BadRequestException, ConflictException, NotFoundException {
    LogUtils.trace(log, "get: ", url);
    notifyStartRequest();/* www.  j  av a 2 s  . com*/
    HttpRequestBase m;
    if (rangeList != null) {
        try {
            m = new RangedGetMethod(url, rangeList);
        } catch (URISyntaxException ex) {
            throw new RuntimeException(ex);
        }
    } else {
        m = new HttpGet(url);
    }
    InputStream in = null;
    NotifyingFileInputStream nin;
    try {
        HttpResponse resp = client.execute(m, context);
        if (resp.getEntity() == null) {
            log.warn("Did not receive a response entity for GET");
            return;
        }
        HttpEntity entity = resp.getEntity();
        in = entity.getContent();
        Utils.processResultCode(resp.getStatusLine().getStatusCode(), url);
        nin = new NotifyingFileInputStream(in, entity.getContentLength(), url, listener);
        receiver.receive(nin);
    } catch (Utils.CancelledException ex) {
        m.abort();
        throw ex;
    } catch (IOException ex) {
        m.abort();
        throw new RuntimeException(ex);
    } finally {
        Utils.close(in);
        notifyFinishRequest();
    }
}

From source file:com.github.sardine.impl.SardineImpl.java

/**
 * No validation of the response. Aborts the request if there is an exception.
 *
 * @param request Request to execute/*from   w  w w.  ja  v  a2  s  . c  o  m*/
 * @return The response to check the reply status code
 */
protected HttpResponse execute(HttpRequestBase request) throws IOException {
    try {
        // Clear circular redirect cache
        this.context.removeAttribute(HttpClientContext.REDIRECT_LOCATIONS);
        // Execute with no response handler
        return this.client.execute(request, this.context);
    } catch (IOException e) {
        request.abort();
        throw e;
    }
}

From source file:com.googlecode.sardine.impl.SardineImpl.java

/**
 * No validation of the response. Aborts the request if there is an
 * exception.// ww  w .  j a  v a  2s .c  om
 * 
 * @param request
 *            Request to execute
 * @return The response to check the reply status code
 */
protected HttpResponse execute(HttpRequestBase request) throws IOException {
    try {
        // Clear circular redirect cache
        // this.context.removeAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);
        // Execute with no response handler
        return this.client.execute(request, this.context);
    } catch (IOException e) {
        request.abort();
        throw e;
    }
}

From source file:com.github.sardine.impl.SardineImpl.java

/**
 * Validate the response using the response handler. Aborts the request if there is an exception.
 *
 * @param <T>             Return type
 * @param request         Request to execute
 * @param responseHandler Determines the return type.
 * @return parsed response//from w ww  .  j a  v a 2  s.c  o m
 */
protected <T> T execute(HttpRequestBase request, ResponseHandler<T> responseHandler) throws IOException {
    try {
        // Clear circular redirect cache
        this.context.removeAttribute(HttpClientContext.REDIRECT_LOCATIONS);
        // Execute with response handler
        return this.client.execute(request, responseHandler, this.context);
    } catch (IOException e) {
        request.abort();
        throw e;
    }
}

From source file:com.googlecode.sardine.impl.SardineImpl.java

/**
 * Validate the response using the response handler. Aborts the request if
 * there is an exception./*from  w  w w. j a  va 2s .c om*/
 * 
 * @param <T>
 *            Return type
 * @param request
 *            Request to execute
 * @param responseHandler
 *            Determines the return type.
 * @return parsed response
 */
protected <T> T execute(HttpRequestBase request, ResponseHandler<T> responseHandler) throws IOException {
    try {
        // Clear circular redirect cache
        // this.context.removeAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);
        // Execute with response handler
        return this.client.execute(request, responseHandler, this.context);
    } catch (IOException e) {
        request.abort();
        throw e;
    }
}

From source file:com.nesscomputing.tinyhttp.HttpFetcher.java

public <T> T get(final URI uri, final String login, final String pw, final HttpContentConverter<T> converter)
        throws IOException {
    final HttpRequestBase httpRequest = new HttpGet(uri);
    final DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, params);
    // Maximum of three retries...
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, false));

    if (login != null) {
        httpClient.setCredentialsProvider(new HttpFetcherCredentialsProvider(login, pw));
    }// w w w .j a  va 2 s.  co m

    final HttpContentResponseHandler<T> responseHandler = new HttpContentResponseHandler<T>(converter);

    try {
        final HttpContext httpContext = new BasicHttpContext();
        final HttpResponse httpResponse = httpClient.execute(httpRequest, httpContext);

        try {
            return responseHandler.handle(httpRequest, httpResponse);
        } finally {
            // Make sure that the content has definitely been consumed. Otherwise,
            // keep-alive does not work.
            final HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                Closeables.closeQuietly(entity.getContent());
            }
        }
    } catch (Exception e) {
        LOG.warn("Aborting Request!", e);

        httpRequest.abort();
        throw Throwables.propagate(e);
    }
}

From source file:immf.ImodeNetClient.java

/**
 * POST????????//ww  w .  j  a  va  2 s.co  m
 * @param req
 * @return
 * @throws IOException
 */
private HttpResponse executeHttp(HttpRequestBase req) throws IOException {
    try {
        for (int i = 0; i < 4; i++) {
            HttpResponse res = this.httpClient.execute(req);
            int status = res.getStatusLine().getStatusCode();
            if (300 <= status && status <= 399) {
                req.abort();

                URI location = httpClient.getRedirectHandler().getLocationURI(res, new BasicHttpContext());

                //System.out.println("Redirect "+location);
                req = new HttpGet(location);
                req.setHeader("User-Agent", "Mozilla/4.0 (compatible;MSIE 7.0; Windows NT 6.0;)");
            } else {
                return res;
            }
        }
    } catch (IllegalStateException e) {
        e.printStackTrace();
        log.info("HttpClient Fatal Error. Restarting HttpCient");
        HttpParams params = this.httpClient.getParams();
        this.httpClient.getConnectionManager().shutdown();
        this.httpClient = new DefaultHttpClient();
        this.httpClient.setParams(params);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        req.abort();
    }
    return null;
}

From source file:org.apache.manifoldcf.crawler.connectors.confluence.ConfluenceSession.java

/**
 * Get rest response and fill the query results
 * /*w w w . j a  va2 s .c o  m*/
 * @param rightside
 * @param response
 * @throws IOException
 * @throws ResponseException
 */
public void getRest(String rightside, ConfluenceJSONResponse 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);

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

    String executionUrl = host.toURI() + path + (path.endsWith("/") ? "" : "/") + rightside;
    if (Logging.connectors != null)
        Logging.connectors.info("Execution url is :" + executionUrl);
    final HttpRequestBase method = new HttpGet(executionUrl);
    method.addHeader("Accept", "application/json");

    try {
        HttpResponse httpResponse = httpClient.execute(method, localContext);

        int resultCode = httpResponse.getStatusLine().getStatusCode();
        if (resultCode == 200) {
            // Logging.connectors.info("Successfully retrived response");
            Object jo = convertToJSON(httpResponse);
            response.acceptJSONObject(jo);
        } else if (resultCode == 401) {
            throw new IOException("There is Authentication failure, this may be due to capcha : "
                    + convertToString(httpResponse));
        } else {
            throw new IOException(
                    "Unexpected result code " + resultCode + ": " + convertToString(httpResponse));
        }

    } finally {
        method.abort();
    }
}