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

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

Introduction

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

Prototype

void abort();

Source Link

Document

Aborts this http request.

Usage

From source file:com.makotosan.vimeodroid.ManageTransfersActivity.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    if (item.getItemId() == R.id.contextmenu_cancel) {
        Transfer transfer = getModel(info.position);
        AbortableHttpRequest request = transfer.getAbortableRequest();
        if (request != null) {
            request.abort();
        }//from w w w .  j av a  2  s. c om
    }

    return true;
}

From source file:cn.ctyun.amazonaws.http.HttpMethodReleaseInputStream.java

/**
 * Forces the release of an HttpMethod's connection in a way that will
 * perform all the necessary cleanup through the correct use of HttpClient
 * methods.//from ww  w  .  j  a  v a 2s.c  om
 *
 * @throws IOException
 */
protected void releaseConnection() throws IOException {
    if (!alreadyReleased) {
        if (!underlyingStreamConsumed) {
            // Underlying input stream has not been consumed, abort method
            // to force connection to be closed and cleaned-up.
            if (httpRequest instanceof AbortableHttpRequest) {
                AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) httpRequest;
                abortableHttpRequest.abort();
            }
        }
        inputStream.close();
        alreadyReleased = true;
    }
}

From source file:com.amazonaws.http.HttpMethodReleaseInputStream.java

/**
 * Forces the release of an HttpMethod's connection in a way that will
 * perform all the necessary cleanup through the correct use of HttpClient
 * methods./*from   w  w w  .  j  a  v a2  s  .co m*/
 *
 * @throws IOException
 */
protected void releaseConnection() throws IOException {
    if (!alreadyReleased) {
        if (!underlyingStreamConsumed) {
            // Underlying input stream has not been consumed, abort method
            // to force connection to be closed and cleaned-up.
            if (httpRequest instanceof AbortableHttpRequest) {
                AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) httpRequest;
                abortableHttpRequest.abort();
            }
        }
        in.close();
        alreadyReleased = true;
    }
}

From source file:io.mapzone.controller.vm.http.HttpResponseForwarder.java

protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {
    HttpResponse proxyResponse = requestForwarder.proxyResponse;
    HttpRequest proxyRequest = requestForwarder.proxyRequest;
    try {//from   w  w w . ja  v a  2  s  .  c om
        // Process the response
        int statusCode = proxyResponse.getStatusLine().getStatusCode();
        log.debug("RESPONSE: " + statusCode);

        // copying response headers to make sure SESSIONID or other Cookie which
        // comes from remote server will be saved in client when the proxied url
        // was redirected to another one.
        // see issue [#51](https://github.com/mitre/HTTP-Proxy-Servlet/issues/51)
        copyResponseHeaders(proxyResponse, servletRequest, servletResponse);

        if (doResponseRedirectOrNotModifiedLogic(servletRequest, servletResponse, proxyResponse, statusCode)) {
            // the response is already "committed" now without any body to send
            return;
        }

        // Pass the response code. This method with the "reason phrase" is
        // deprecated but it's the only way to pass the
        // reason along too.
        // noinspection deprecation
        servletResponse.setStatus(statusCode, proxyResponse.getStatusLine().getReasonPhrase());

        // Send the content to the client
        copyResponseEntity(proxyResponse, servletResponse);
    } catch (Exception e) {
        // abort request, according to best practice with HttpClient
        if (proxyRequest instanceof AbortableHttpRequest) {
            AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) proxyRequest;
            abortableHttpRequest.abort();
        }
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        if (e instanceof ServletException)
            throw (ServletException) e;
        // noinspection ConstantConditions
        if (e instanceof IOException)
            throw (IOException) e;
        throw new RuntimeException(e);

    } finally {
        // make sure the entire entity was consumed, so the connection is released
        // XXX CloseableHttpResponse.close()
        if (proxyResponse != null) {
            consumeQuietly(proxyResponse.getEntity());
        }
        // Note: Don't need to close servlet outputStream:
        // http://stackoverflow.com/questions/1159168/should-one-call-close-on-httpservletresponse-getoutputstream-getwriter
    }
}

From source file:io.mapzone.controller.vm.http.HttpRequestForwarder.java

public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, URISyntaxException {
    targetUriObj = new URI(targetUri.get());
    targetHost = URIUtils.extractHost(targetUriObj);

    // Make the Request
    // note: we won't transfer the protocol version because I'm not sure it would
    // truly be compatible
    String method = request.getMethod();
    String proxyRequestUri = rewriteUrlFromRequest(request);

    // spec: RFC 2616, sec 4.3: either of these two headers signal that there is
    // a message body.
    if (request.getHeader(HttpHeaders.CONTENT_LENGTH) != null
            || request.getHeader(HttpHeaders.TRANSFER_ENCODING) != null) {
        HttpEntityEnclosingRequest eProxyRequest = new BasicHttpEntityEnclosingRequest(method, proxyRequestUri);
        // Add the input entity (streamed)
        // note: we don't bother ensuring we close the servletInputStream since
        // the container handles it
        eProxyRequest.setEntity(new InputStreamEntity(request.getInputStream(), request.getContentLength()));
        proxyRequest = eProxyRequest;/*  w w  w .j  av  a2s . co  m*/
    } else {
        proxyRequest = new BasicHttpRequest(method, proxyRequestUri);
    }

    copyRequestHeaders(request);

    setXForwardedForHeader(request);

    // Execute the request
    try {
        active.set(this);

        log.debug("REQUEST " + "[" + StringUtils.right(Thread.currentThread().getName(), 2) + "] " + method
                + ": " + request.getRequestURI() + " -- " + proxyRequest.getRequestLine().getUri());
        proxyResponse = proxyClient.execute(targetHost, proxyRequest);
    } catch (Exception e) {
        // abort request, according to best practice with HttpClient
        if (proxyRequest instanceof AbortableHttpRequest) {
            AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) proxyRequest;
            abortableHttpRequest.abort();
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        if (e instanceof ServletException) {
            throw (ServletException) e;
        }
        // noinspection ConstantConditions
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new RuntimeException(e);
    } finally {
        active.set(null);
    }
    // Note: Don't need to close servlet outputStream:
    // http://stackoverflow.com/questions/1159168/should-one-call-close-on-httpservletresponse-getoutputstream-getwriter
}

From source file:org.overlord.apiman.service.client.http.HTTPServiceClient.java

/**
 * {@inheritDoc}/*  ww  w.  j  a  va2s. co  m*/
 */
@Override
public Response process(Request request) throws Exception {
    String method = "GET";

    if (request instanceof HTTPGatewayRequest) {
        method = ((HTTPGatewayRequest) request).getHTTPMethod();
    }

    String proxyRequestUri = rewriteUrlFromRequest(request);

    HttpRequest proxyRequest;

    //spec: RFC 2616, sec 4.3: either of these two headers signal that there is a message body.
    if (request.getHeader(HttpHeaders.CONTENT_LENGTH) != null
            || request.getHeader(HttpHeaders.TRANSFER_ENCODING) != null) {
        HttpEntityEnclosingRequest eProxyRequest = new BasicHttpEntityEnclosingRequest(method, proxyRequestUri);

        java.io.InputStream is = new java.io.ByteArrayInputStream(request.getContent());

        InputStreamEntity entity = new InputStreamEntity(is, request.getContent().length);

        is.close();

        eProxyRequest.setEntity(entity);

        proxyRequest = eProxyRequest;
    } else {
        proxyRequest = new BasicHttpRequest(method, proxyRequestUri);
    }

    copyRequestHeaders(request, proxyRequest);

    try {
        // Execute the request
        if (LOG.isLoggable(Level.FINER)) {
            LOG.finer("proxy " + method + " uri: " + request.getSourceURI() + " -- "
                    + proxyRequest.getRequestLine().getUri());
        }

        HttpResponse proxyResponse = _proxyClient
                .execute(URIUtils.extractHost(new java.net.URI(request.getServiceURI())), proxyRequest);

        Response resp = new HTTPGatewayResponse(proxyResponse);

        return (resp);

    } catch (Exception e) {
        //abort request, according to best practice with HttpClient
        if (proxyRequest instanceof AbortableHttpRequest) {
            AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) proxyRequest;
            abortableHttpRequest.abort();
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        //noinspection ConstantConditions
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new RuntimeException(e);
    }
}

From source file:org.redhat.jboss.httppacemaker.ProxyServlet.java

@SuppressWarnings("deprecation")
protected void sendProxyRequestToBackend(final HttpRequest proxyRequest,
        final HttpServletRequest servletRequest, final HttpServletResponse servletResponse)
        throws ServletException, IOException {
    try {/*from  w  w w.  j ava2 s  .  c o m*/
        // Execute the request
        HttpResponse proxyResponse = proxyClient.execute(new HttpHost(targetUri.getHost(), targetUri.getPort()),
                proxyRequest);
        // Process the response
        int statusCode = proxyResponse.getStatusLine().getStatusCode();

        if (doResponseRedirectOrNotModifiedLogic(servletRequest, servletResponse, proxyResponse, statusCode)) {
            //just to be sure, but is probably a no-op
            EntityUtils.consume(proxyResponse.getEntity());
            return;
        }
        // Pass the response code. This method with the "reason phrase" is deprecated but it's the only way to pass the
        //  reason along too.
        servletResponse.setStatus(statusCode, proxyResponse.getStatusLine().getReasonPhrase());
        copyResponseHeaders(proxyResponse, servletResponse);
        // Send the content to the client
        copyResponseEntity(proxyResponse, servletResponse);
    } catch (Exception e) {
        //abort request, according to best practice with HttpClient
        if (proxyRequest instanceof AbortableHttpRequest) {
            AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) proxyRequest;
            abortableHttpRequest.abort();
        }
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        if (e instanceof ServletException)
            throw (ServletException) e;
        //noinspection ConstantConditions
        if (e instanceof IOException)
            throw (IOException) e;
        throw new RuntimeException(e);
    }
}

From source file:io.hops.hopsworks.api.kibana.KibanaProxyServlet.java

/**
 * Authorize user to access particular index.
 *
 * @param servletRequest//from  www  .  ja  v a  2 s  .  co  m
 * @param servletResponse
 * @throws ServletException
 * @throws IOException
 */
@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {
    if (servletRequest.getUserPrincipal() == null) {
        servletResponse.sendError(401, "User is not logged in");
        return;
    }
    String email = servletRequest.getUserPrincipal().getName();

    if (servletRequest.getParameterMap().containsKey("projectId")) {
        String projectId = servletRequest.getParameterMap().get("projectId")[0];
        try {
            ProjectDTO projectDTO = projectController.getProjectByID(Integer.parseInt(projectId));
            currentProjects.put(email, projectDTO.getProjectName());
        } catch (ProjectException ex) {
            LOG.log(Level.SEVERE, null, ex);
            servletResponse.sendError(403,
                    "Kibana was not accessed from Hopsworks, no current project information is available.");
            return;
        }
    }

    //Do not authorize admin
    if (email.equals(Settings.AGENT_EMAIL)) {
        super.service(servletRequest, servletResponse);
        return;
    }

    MyRequestWrapper myRequestWrapper = new MyRequestWrapper((HttpServletRequest) servletRequest);
    KibanaFilter kibanaFilter = null;
    //Filter requests based on path
    if (servletRequest.getRequestURI().contains("api/saved_objects")) {
        kibanaFilter = KibanaFilter.KIBANA_SAVED_OBJECTS_API;
    } else if (servletRequest.getRequestURI().contains("elasticsearch/*/_search")) {
        kibanaFilter = KibanaFilter.ELASTICSEARCH_SEARCH;
    } else if (servletRequest.getRequestURI().contains("legacy_scroll_start")
            || servletRequest.getRequestURI().contains("settings/defaultIndex")) {
        return;
    }

    //initialize request attributes from caches if unset by a subclass by this point
    if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) {
        servletRequest.setAttribute(ATTR_TARGET_URI, targetUri);
    }
    if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) {
        servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost);
    }

    // Make the Request
    //note: we won't transfer the protocol version because I'm not sure it would truly be compatible
    String method = servletRequest.getMethod();
    String proxyRequestUri = rewriteUrlFromRequest(servletRequest);
    HttpRequest proxyRequest;
    //spec: RFC 2616, sec 4.3: either of these two headers signal that there is a message body.
    if (servletRequest.getHeader(HttpHeaders.CONTENT_LENGTH) != null
            || servletRequest.getHeader(HttpHeaders.TRANSFER_ENCODING) != null) {
        HttpEntityEnclosingRequest eProxyRequest = new BasicHttpEntityEnclosingRequest(method, proxyRequestUri);
        // Add the input entity (streamed)
        //  note: we don't bother ensuring we close the servletInputStream since the container handles it
        eProxyRequest.setEntity(
                new InputStreamEntity(myRequestWrapper.getInputStream(), servletRequest.getContentLength()));
        proxyRequest = eProxyRequest;
    } else {
        proxyRequest = new BasicHttpRequest(method, proxyRequestUri);
    }

    copyRequestHeaders(servletRequest, proxyRequest);

    super.setXForwardedForHeader(servletRequest, proxyRequest);

    HttpResponse proxyResponse = null;
    try {
        // Execute the request
        LOG.log(Level.FINE, "proxy " + method + " uri: " + servletRequest.getRequestURI() + " -- "
                + proxyRequest.getRequestLine().getUri());

        proxyResponse = super.proxyClient.execute(super.getTargetHost(myRequestWrapper), proxyRequest);

        // Process the response
        int statusCode = proxyResponse.getStatusLine().getStatusCode();

        if (doResponseRedirectOrNotModifiedLogic(myRequestWrapper, servletResponse, proxyResponse,
                statusCode)) {
            //the response is already "committed" now without any body to send
            //TODO copy response headers?
            return;
        }

        // Pass the response code. This method with the "reason phrase" is 
        // deprecated but it's the only way to pass the reason along too.
        //noinspection deprecation
        servletResponse.setStatus(statusCode, proxyResponse.getStatusLine().getReasonPhrase());

        copyResponseHeaders(proxyResponse, servletRequest, servletResponse);

        // Send the content to the client
        copyResponseEntity(proxyResponse, servletResponse, kibanaFilter, email);

    } catch (Exception e) {
        //abort request, according to best practice with HttpClient
        if (proxyRequest instanceof AbortableHttpRequest) {
            AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) proxyRequest;
            abortableHttpRequest.abort();
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        if (e instanceof ServletException) {
            throw (ServletException) e;
        }
        //noinspection ConstantConditions
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new RuntimeException(e);

    } finally {
        // make sure the entire entity was consumed, so the connection is released
        if (proxyResponse != null) {
            consumeQuietly(proxyResponse.getEntity());
        }
        //Note: Don't need to close servlet outputStream:
        // http://stackoverflow.com/questions/1159168/should-one-call-close-on-
        //httpservletresponse-getoutputstream-getwriter
    }
}

From source file:be.milieuinfo.core.proxy.controller.ProxyServlet.java

@SuppressWarnings("deprecation")
@Override/*ww  w. j  av a  2s.  c o  m*/
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {
    // Make the Request
    //note: we won't transfer the protocol version because I'm not sure it would truly be compatible
    String method = servletRequest.getMethod();
    String proxyRequestUri = rewriteUrlFromRequest(servletRequest);
    HttpRequest proxyRequest;
    //spec: RFC 2616, sec 4.3: either these two headers signal that there is a message body.
    if (servletRequest.getHeader(HttpHeaders.CONTENT_LENGTH) != null
            || servletRequest.getHeader(HttpHeaders.TRANSFER_ENCODING) != null) {
        HttpEntityEnclosingRequest eProxyRequest = new BasicHttpEntityEnclosingRequest(method, proxyRequestUri);
        // Add the input entity (streamed)
        //  note: we don't bother ensuring we close the servletInputStream since the container handles it
        eProxyRequest.setEntity(
                new InputStreamEntity(servletRequest.getInputStream(), servletRequest.getContentLength()));
        proxyRequest = eProxyRequest;
    } else
        proxyRequest = new BasicHttpRequest(method, proxyRequestUri);

    copyRequestHeaders(servletRequest, proxyRequest);

    try {
        // Execute the request
        if (doLog) {
            log("proxy " + method + " uri: " + servletRequest.getRequestURI() + " -- "
                    + proxyRequest.getRequestLine().getUri());
        }
        HttpResponse proxyResponse = proxyClient.execute(URIUtils.extractHost(targetUri), proxyRequest);

        // Process the response
        int statusCode = proxyResponse.getStatusLine().getStatusCode();

        if (doResponseRedirectOrNotModifiedLogic(servletRequest, servletResponse, proxyResponse, statusCode)) {
            //just to be sure, but is probably a no-op
            EntityUtils.consume(proxyResponse.getEntity());
            return;
        }

        // Pass the response code. This method with the "reason phrase" is deprecated but it's the only way to pass the
        //  reason along too.
        //noinspection deprecation
        servletResponse.setStatus(statusCode, proxyResponse.getStatusLine().getReasonPhrase());

        copyResponseHeaders(proxyResponse, servletResponse);

        // Send the content to the client
        copyResponseEntity(proxyResponse, servletResponse);

    } catch (Exception e) {
        //abort request, according to best practice with HttpClient
        if (proxyRequest instanceof AbortableHttpRequest) {
            AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) proxyRequest;
            abortableHttpRequest.abort();
        }
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        if (e instanceof ServletException)
            throw (ServletException) e;
        if (e instanceof IOException)
            throw (IOException) e;
        throw new RuntimeException(e);
    }
}

From source file:uk.ac.ebi.phenotype.web.proxy.ExternalUrlConfiguratbleProxyServlet.java

@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {
    // Make the Request
    // note: we won't transfer the protocol version because I'm not sure it
    // would truly be compatible
    BasicHttpEntityEnclosingRequest proxyRequest = new BasicHttpEntityEnclosingRequest(
            servletRequest.getMethod(), rewriteUrlFromRequest(servletRequest));

    copyRequestHeaders(servletRequest, proxyRequest);

    // Add the input entity (streamed) then execute the request.
    HttpResponse proxyResponse = null;/*  w w  w  . ja v a2 s .c o m*/
    InputStream servletRequestInputStream = servletRequest.getInputStream();
    try {
        try {
            proxyRequest.setEntity(
                    new InputStreamEntity(servletRequestInputStream, servletRequest.getContentLength()));

            // Execute the request
            if (doLog) {
                log("proxy " + servletRequest.getMethod() + " uri: " + servletRequest.getRequestURI() + " -- "
                        + proxyRequest.getRequestLine().getUri());
            }
            proxyResponse = proxyClient.execute(URIUtils.extractHost(targetUri), proxyRequest);
        } finally {
            closeQuietly(servletRequestInputStream);
        }

        // Process the response
        int statusCode = proxyResponse.getStatusLine().getStatusCode();

        if (doResponseRedirectOrNotModifiedLogic(servletRequest, servletResponse, proxyResponse, statusCode)) {
            EntityUtils.consume(proxyResponse.getEntity());
            return;
        }

        // Pass the response code. This method with the "reason phrase" is
        // deprecated but it's the only way to pass the
        // reason along too.
        // noinspection deprecation
        servletResponse.setStatus(statusCode, proxyResponse.getStatusLine().getReasonPhrase());

        copyResponseHeaders(proxyResponse, servletResponse);

        // Send the content to the client
        copyResponseEntity(proxyResponse, servletResponse);

    } catch (Exception e) {
        // abort request, according to best practice with HttpClient
        if (proxyRequest instanceof AbortableHttpRequest) {
            AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) proxyRequest;
            abortableHttpRequest.abort();
        }
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        if (e instanceof ServletException)
            throw (ServletException) e;
        throw new RuntimeException(e);
    }
}