Example usage for org.apache.commons.httpclient HttpMethod abort

List of usage examples for org.apache.commons.httpclient HttpMethod abort

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod abort.

Prototype

public abstract void abort();

Source Link

Usage

From source file:mitm.common.scheduler.HTTPMethodAbortTimeoutTask.java

@Override
public void doRun() {
    logger.warn("Timeout occurred. Aborting HttpMethod thread. Task: " + getName());

    HttpMethod httpMethod = weakHttpMethod.get();

    if (httpMethod != null) {
        try {/*w ww.  j ava2s  .  co m*/
            httpMethod.abort();
        } catch (Exception e) {
            // ignore
        }
    }
}

From source file:com.jaspersoft.ireport.jasperserver.ws.http.HCManager.java

public void cancel(HttpMethod method) {
    HttpClient client = null;//from w  w w. ja v  a2  s. c o m
    HttpConnectionManager cmanager = client.getHttpConnectionManager();

    for (IProgressMonitor m : cmap.keySet()) {
        if (m.isCanceled()) {
            method.abort();
            cmap.remove(m);
        }
    }

}

From source file:com.foglyn.fogbugz.DocumentResponseProcessor.java

@Override
Document processResponse(String url, HttpMethod method, InputStream input)
        throws FogBugzException, IOException, OperationCanceledException {
    Builder builder = new Builder();
    try {/* www  .ja va  2s  .c o m*/
        return builder.build(input);
    } catch (ParsingException e) {
        // abort method, because full response might have not been read
        method.abort();

        throw parsingError(e, url);
    }
}

From source file:com.clarkparsia.sbol.editor.sparql.StardogEndpoint.java

@Override
public boolean executeAskQuery(String query) throws QueryEvaluationException {
    try {// w  w  w .  ja v a 2  s. c om
        Boolean result = null;
        HttpMethod response = executeQuery(query);
        try {
            InputStream in = response.getResponseBodyAsStream();
            result = booleanParser.parse(in);
            return result.booleanValue();
        } catch (HttpException e) {
            throw new QueryEvaluationException(e);
        } catch (QueryResultParseException e) {
            throw new QueryEvaluationException(e);
        } finally {
            if (result == null) {
                response.abort();
            }
        }
    } catch (IOException e) {
        throw new QueryEvaluationException(e);
    }
}

From source file:com.clarkparsia.sbol.editor.sparql.StardogEndpoint.java

@Override
public void executeSelectQuery(String query, TupleQueryResultHandler handler) throws QueryEvaluationException {
    try {//from  w w  w .  j a  v  a2 s .c om
        boolean complete = false;
        HttpMethod response = executeQuery(query);
        try {
            tupleParser.setTupleQueryResultHandler(handler);
            byte[] bytes = response.getResponseBody();
            tupleParser.parse(new ByteArrayInputStream(bytes));
            complete = true;
        } catch (HttpException e) {
            throw new QueryEvaluationException(e);
        } catch (QueryResultParseException e) {
            throw new QueryEvaluationException(e);
        } catch (TupleQueryResultHandlerException e) {
            throw new QueryEvaluationException(e);
        } finally {
            if (!complete) {
                response.abort();
            }
        }
    } catch (IOException e) {
        throw new QueryEvaluationException(e);
    }
}

From source file:com.foglyn.fogbugz.Request.java

private <T> T request(String url, HttpMethod method, IProgressMonitor monitor, ResponseProcessor<T> processor)
        throws FogBugzException {
    Utils.checkCancellation(monitor);/*  w ww. jav a  2s . com*/

    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, repositoryLocation,
            monitor);

    if (allowGzip) {
        method.addRequestHeader("Accept-Encoding", "gzip");
    }

    InputStream responseStream = null;
    CancellableInputStream cancellableStream = null;
    try {
        log.debug("Sending request to server");
        int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);

        log.debug("Got " + code + " response");

        if (!processor.checkHttpStatus(code)) {
            Map<String, String> headers = Utils.convertHeadersToMap(method);

            method.abort();

            throw unexpectedStatus(code, url, headers);
        }

        log.debug("Downloading data");

        responseStream = method.getResponseBodyAsStream();

        InputStream processed = responseStream;

        // may be null, for example for HEAD request
        if (processed != null) {
            Header contentEncoding = method.getResponseHeader("Content-Encoding");
            if (allowGzip && contentEncoding != null && "gzip".equals(contentEncoding.getValue())) {
                processed = new GZIPInputStream(processed);
            }

            cancellableStream = new CancellableInputStream(processed, monitor, threadFactory);
            processed = cancellableStream;
        }

        log.debug("Processing response");

        return processor.processResponse(url, method, processed);
    } catch (RuntimeException e) {
        // also catches OperationCanceledException

        // we don't know what happened to method, so we better abort processing
        method.abort();

        log.error("Error while executing request", e);

        throw e;
    } catch (IOException e) {
        // we don't know what happened... better abort connection
        method.abort();

        log.error("IO Error while executing request", e);

        throw ioError(e, url);
    } finally {
        if (cancellableStream != null) {
            cancellableStream.shutdownBackgroundThread();
        }

        // don't use cancellable stream to close responseStream -- because in case of cancelled monitor, it would ignore close request 
        Utils.close(responseStream);

        method.releaseConnection();
    }
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

protected void doAbort(CrawlURI curi, HttpMethod method, String annotation) {
    curi.addAnnotation(annotation);/*from   w  w  w . j a  va2s . c om*/
    curi.getHttpRecorder().close();
    method.abort();
}

From source file:com.dtolabs.client.utils.HttpClientChannel.java

/**
 * Perform the HTTP request.  Can only be performed once.
 *//*from ww w . jav a2s. c o  m*/
public void makeRequest() throws IOException, HttpClientException {
    if (requestMade) {
        return;
    }

    requestMade = true;
    RequestEntity reqEntity = null;
    NameValuePair[] postBody = null;
    if (isPostMethod()) {
        setMethodType("POST");
    }
    HttpMethod method = initMethod();
    if (isPostMethod()) {
        reqEntity = getRequestEntity((PostMethod) method);
        if (null != reqEntity) {
            logger.debug("preparing to post request entity data: " + reqEntity.getContentType());

            ((PostMethod) method).setRequestEntity(reqEntity);
        } else {
            logger.debug("preparing to post form data");
            postBody = getRequestBody((PostMethod) method);
            ((PostMethod) method).setRequestBody(postBody);
        }
    }
    logger.debug("calling preMakeRequest");
    if (!preMakeRequest(method)) {
        return;
    }
    logger.debug("calling doAuthentication...");
    if (!doAuthentication(method)) {
        return;
    }
    int bytesread = 0;
    try {
        if (!isPostMethod()) {
            method.setFollowRedirects(true);
        }
        logger.debug("make request...");
        resultCode = httpc.executeMethod(method);
        reasonCode = method.getStatusText();
        if (isPostMethod()) {
            //check redirect after post
            method = checkFollowRedirect(method, resultCode);
        }
        logger.debug("check needs reauth...");

        if (needsReAuthentication(resultCode, method)) {
            logger.debug("re-authentication needed, performing...");
            method.releaseConnection();
            method.abort();
            //need to re-authenticate.
            method = initMethod();
            if (isPostMethod() && null != reqEntity) {
                ((PostMethod) method).setRequestEntity(reqEntity);
            } else if (isPostMethod() && null != postBody) {
                ((PostMethod) method).setRequestBody(postBody);
            }
            if (!doAuthentication(method)) {
                //user login failed
                return;
            }
            //user login has succeeded
            logger.debug("remaking original request...");
            resultCode = httpc.executeMethod(method);
            reasonCode = method.getStatusText();
            if (needsReAuthentication(resultCode, method)) {
                //user request was unauthorized
                throw new HttpClientException("Unauthorized Action: "
                        + (null != method.getResponseHeader(Constants.X_RUNDECK_ACTION_UNAUTHORIZED_HEADER)
                                ? method.getResponseHeader(Constants.X_RUNDECK_ACTION_UNAUTHORIZED_HEADER)
                                        .getValue()
                                : reasonCode));
            }
        }

        logger.debug("finish...");
        if (null != method.getResponseHeader("Content-Type")) {
            resultType = method.getResponseHeader("Content-Type").getValue();
        }
        String type = resultType;
        if (type != null && type.indexOf(";") > 0) {
            type = type.substring(0, type.indexOf(";")).trim();
        }
        if (null == expectedContentType || expectedContentType.equals(type)) {
            if (null != destinationStream && resultCode >= 200 && resultCode < 300) {
                //read the input stream and write it to the destination
                contentLengthRetrieved = Streams.copyStreamCount(method.getResponseBodyAsStream(),
                        destinationStream);
            } else {
                final ByteArrayOutputStream outputBytes = new ByteArrayOutputStream(1024 * 50);
                Streams.copyStream(method.getResponseBodyAsStream(), outputBytes);
                resultStream = new ByteArrayInputStream(outputBytes.toByteArray());
            }
        }
        reqMadeMethod = method;
    } catch (HttpException e) {
        logger.error("HTTP error: " + e.getMessage(), e);
    } finally {
        method.releaseConnection();
    }

    logger.debug("Response received");
    postMakeRequest();
}

From source file:cz.vity.freerapid.plugins.services.rapidshare_premium.RapidShareRunner.java

private void checkFile() throws Exception {
    Matcher matcher = PlugUtils//  w  ww .  java 2s .co m
            .matcher("!download(?:%7C|\\|)(?:[^%\\|]+)(?:%7C|\\|)(\\d+)(?:%7C|\\|)([^%\\|]+)", fileURL);
    if (matcher.find()) {
        fileURL = "http://rapidshare.com/files/" + matcher.group(1) + "/" + matcher.group(2);
        httpFile.setNewURL(new URL(fileURL));
    } else {
        matcher = PlugUtils.matcher("/share/([A-Z0-9]+)", fileURL);
        if (matcher.find()) {
            HttpMethod method = getMethodBuilder().setReferer(fileURL)
                    .setAction("https://api.rapidshare.com/cgi-bin/rsapi.cgi").setParameter("rsource", "web")
                    .setParameter("sub", "sharelinkcontent").setParameter("share", matcher.group(1))
                    .setParameter("cbid", "2").setParameter("cbf", "rsapi.system.jsonp.callback")
                    .setParameter("callt", String.valueOf(System.currentTimeMillis())).toGetMethod();
            if (!makeRedirectedRequest(method)) {
                checkFileProblems();
                throw new ServiceConnectionProblemException();
            }
            checkFileProblems();
            matcher = getMatcherAgainstContent("\"file:(\\d+),([^,]+),");
            if (!matcher.find()) {
                throw new PluginImplementationException("Error getting file ID and file name");
            }
            fileURL = "http://rapidshare.com/files/" + matcher.group(1) + "/" + matcher.group(2);
            httpFile.setNewURL(new URL(fileURL));
        }
    }
    matcher = PlugUtils.matcher("/files/(\\d+)/(.+)", fileURL);
    if (!matcher.find()) {
        throw new PluginImplementationException("Error parsing file URL");
    }
    final String fileId = matcher.group(1);
    final String fileName = URLDecoder.decode(matcher.group(2), "UTF-8");

    HttpMethod method = getMethodBuilder().setAction("http://api.rapidshare.com/cgi-bin/rsapi.cgi")
            .setParameter("sub", "checkfiles").setParameter("files", fileId).setParameter("filenames", fileName)
            .toGetMethod();

    int status = 0;
    String responseString = "";
    try {
        status = client.makeRequest(method, true);
        responseString = client.getContentAsString();
        logger.log(Level.INFO, "Response check:{0}", responseString);
    } finally {
        method.abort();
        method.releaseConnection();
    }
    if (status == HttpStatus.SC_OK && responseString != null && !responseString.isEmpty()) {
        String[] response = responseString.split(",");
        int fileStatus = Integer.parseInt(response[4]);

        if (fileStatus == 1 || fileStatus == 2 || fileStatus == 6 || fileStatus >= 50) {
            //http://rs$serverid$shorthost.rapidshare.com/files/$fileid/$filename)
            finalUrl = String.format("http://rs%s%s.rapidshare.com/files/%s/%s?directstart=1", response[3],
                    response[5], response[0], response[1]);
            logger.info(finalUrl);
            httpFile.setFileName(response[1]);
            httpFile.setFileSize(Long.parseLong(response[2]));
            httpFile.setFileState(FileState.CHECKED_AND_EXISTING);
        }
        if (fileStatus == 0) {
            throw new URLNotAvailableAnymoreException("File not found");
        }
        if (fileStatus == 4) {
            throw new URLNotAvailableAnymoreException("File marked as illegal");
        }
        if (fileStatus == 5) {
            throw new URLNotAvailableAnymoreException(
                    "Anonymous file locked, because it has more than 10 downloads already");
        }
        if (fileStatus == 3) {
            throw new InvalidURLOrServiceProblemException("Server down");
        }
    } else {
        throw new ServiceConnectionProblemException("Server return status " + status);
    }
}

From source file:ch.ksfx.web.services.spidering.http.HttpClientHelper.java

private HttpMethod executeMethod(HttpMethod httpMethod) {
    for (Header header : this.headers.getHeaders()) {
        httpMethod.setRequestHeader(header);
    }/*ww w  . jav a 2  s  . co m*/

    httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new KsfxHttpRetryHandler(retryCount, retryDelay));

    try {
        int tryCount = 0;
        int statusCode;
        do {
            if (tryCount > 1) {
                httpMethod = createNewHttpMethod(httpMethod);
                try {
                    if (retryDelay == 0) {
                        retryDelay = DEFAULT_RETRY_DELAY;
                    }
                    Thread.sleep(retryDelay);
                } catch (InterruptedException e) {
                    logger.severe("InterruptedException");
                }
            }

            //PROXY Configuration
            /*
            if (torify) {
                    
            String proxyHost = "";
            Integer proxyPort = 0;
                    
            try {
                    proxyHost = SpiderConfiguration.getConfiguration().getString("torifyHost");
                    proxyPort = SpiderConfiguration.getConfiguration().getInt("torifyPort");
            } catch (Exception e) {
                logger.severe("Cannot get Proxy information");
            }
                    
            this.httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
            }
            */

            statusCode = this.httpClient.executeMethod(httpMethod);
            tryCount++;
        } while (!(statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_FORBIDDEN
                || statusCode == HttpStatus.SC_NOT_FOUND) && tryCount < retryCount);
        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("HTTP method failed: " + httpMethod.getStatusLine() + " - "
                    + httpMethod.getURI().toString());
        }
    } catch (HttpException e) {
        e.printStackTrace();
        httpMethod.abort();
        try {
            logger.log(Level.SEVERE, "Redirrex " + e.getClass(), e);
            if (e.getClass().equals(RedirectException.class)) {
                logger.log(Level.SEVERE, "Is real redirect exception", e);
                throw new RuntimeException("HttpRedirectException");
            }
            logger.log(Level.SEVERE, "HTTP protocol error for URL: " + httpMethod.getURI().toString(), e);
        } catch (URIException e1) {
            e.printStackTrace();
            logger.log(Level.SEVERE, "URI exception", e);
        }
        throw new RuntimeException("HttpException");
    } catch (IOException e) {
        try {
            e.printStackTrace();
            logger.log(Level.SEVERE, "HTTP transport error for URL: " + httpMethod.getURI().toString(), e);

        } catch (URIException e1) {
            e.printStackTrace();
            logger.log(Level.SEVERE, "URI exception", e);

        }
        throw new RuntimeException("IOException");
    }
    return httpMethod;
}