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

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

Introduction

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

Prototype

void abort() throws UnsupportedOperationException;

Source Link

Document

Aborts execution of the request.

Usage

From source file:com.nexmo.messaging.sdk.NexmoSmsClient.java

/**
 * submit a message submission request object.
 * This will use the supplied object to construct a request and post it to the Nexmo REST interface.<br>
 * This method will respond with an array of SmsSubmissionResult objects. Depending on the nature and length of the submitted message, Nexmo may automatically
 * split the message into multiple sms messages in order to deliver to the handset. For example, a long text sms of greater than 160 chars will need to be split
 * into multiple 'concatenated' sms messages. The Nexmo service will handle this automatically for you.<br>
 * The array of SmsSubmissionResult objects will contain a SmsSubmissionResult object for every actual sms that was required to submit the message.
 * each message can potentially have a different status result, and each message will have a different message id.
 * Delivery notifications will be generated for each sms message within this set and will be posted to your application containing the appropriate message id.
 *
 * @param message The message request object that describes the type of message and the contents to be submitted.
 * @param validityPeriod The validity period (Time-To-Live) for this message. Specifies the time before this mesage will be expired if not yet delivered
 * @param networkCode (Optional) use this parameter to force this message to be associated with and delivered on this network. Use this in cases where you want to over-ride
 *                               the automatic network detection provided by Nexmo. This value will be used in order to determine the pricing and routing for this message.<br>
 *                               (Note) This feature must be enabled and available on your account or else this value will be ignored.
 * @param performReachabilityCheck Flag to indicate wether a reachability check should be performed on this message before delivery is attempted. If the destination is
 *                                 not reachable, the message will be rejected and a reachability status will be returned in the response field smsSubmissionReachabilityStatus<br>
 *                                 (Note) This feature must be enabled and available on your account or else the message request will be rejected. There may be additional cost
 *                                 associated with the use of this feature.
 *
 * @return SmsSubmissionResult[] an array of results, 1 object for each sms message that was required to submit this message in its entirety
 *
 * @throws Exception There has been a general failure either within the Client class, or whilst attempting to communicate with the Nexmo service (eg, Network failure)
 *///www .  j a v a 2 s.c o  m
public SmsSubmissionResult[] submitMessage(final Message message, final ValidityPeriod validityPeriod,
        final String networkCode, final boolean performReachabilityCheck) throws Exception {

    log.debug("HTTP-Message-Submission Client .. from [ " + message.getFrom() + " ] to [ " + message.getTo()
            + " ] msg [ " + message.getMessageBody() + " ] ");

    // From the Message object supplied, construct an appropriate request to be submitted to the Nexmo REST Service.

    // Determine what 'product' type we are submitting, and select the appropriate endpoint path
    String submitPath = SUBMISSION_PATH_SMS;

    // Determine the type parameter based on the type of Message object.
    boolean binary = message.getType() == Message.MESSAGE_TYPE_BINARY;
    boolean unicode = message.isUnicode();
    boolean wapPush = message.getType() == Message.MESSAGE_TYPE_WAPPUSH;

    String mode = "text";

    if (binary)
        mode = "binary";

    if (unicode)
        mode = "unicode";

    if (wapPush)
        mode = "wappush";

    // Construct a query string as a list of NameValuePairs

    List<NameValuePair> params = new ArrayList<>();

    boolean doPost = false;

    params.add(new BasicNameValuePair("api_key", this.apiKey));
    if (!this.signRequests)
        params.add(new BasicNameValuePair("api_secret", this.apiSecret));
    params.add(new BasicNameValuePair("from", message.getFrom()));
    params.add(new BasicNameValuePair("to", message.getTo()));
    params.add(new BasicNameValuePair("type", mode));
    if (wapPush) {
        params.add(new BasicNameValuePair("url", message.getWapPushUrl()));
        params.add(new BasicNameValuePair("title", message.getWapPushTitle()));
        if (message.getWapPushValidity() > 0)
            params.add(new BasicNameValuePair("validity", "" + message.getWapPushValidity()));
    } else if (binary) {
        // Binary Message
        if (message.getBinaryMessageUdh() != null)
            params.add(new BasicNameValuePair("udh", HexUtil.bytesToHex(message.getBinaryMessageUdh())));
        params.add(new BasicNameValuePair("body", HexUtil.bytesToHex(message.getBinaryMessageBody())));
    } else {
        // Text Message
        params.add(new BasicNameValuePair("text", message.getMessageBody()));
        if (message.getMessageBody() != null && message.getMessageBody().length() > 255)
            doPost = true;
    }

    if (message.getClientReference() != null)
        params.add(new BasicNameValuePair("client-ref", message.getClientReference()));

    params.add(new BasicNameValuePair("status-report-req", "" + message.getStatusReportRequired()));

    if (message.getMessageClass() != null)
        params.add(new BasicNameValuePair("message-class", "" + message.getMessageClass().getMessageClass()));

    if (message.getProtocolId() != null)
        params.add(new BasicNameValuePair("protocol-id", "" + message.getProtocolId()));

    if (validityPeriod != null) {
        if (validityPeriod.getTimeToLive() != null)
            params.add(new BasicNameValuePair("ttl", "" + validityPeriod.getTimeToLive().intValue()));
        if (validityPeriod.getValidityPeriodHours() != null)
            params.add(new BasicNameValuePair("ttl-hours",
                    "" + validityPeriod.getValidityPeriodHours().intValue()));
        if (validityPeriod.getValidityPeriodMinutes() != null)
            params.add(new BasicNameValuePair("ttl-minutes",
                    "" + validityPeriod.getValidityPeriodMinutes().intValue()));
        if (validityPeriod.getValidityPeriodSeconds() != null)
            params.add(new BasicNameValuePair("ttl-seconds",
                    "" + validityPeriod.getValidityPeriodSeconds().intValue()));
    }

    if (networkCode != null)
        params.add(new BasicNameValuePair("network-code", networkCode));

    if (performReachabilityCheck)
        params.add(new BasicNameValuePair("test-reachable", "true"));

    if (this.signRequests)
        RequestSigning.constructSignatureForRequestParameters(params, this.signatureSecretKey);

    String baseUrl = this.baseUrlHttps + submitPath;

    // Now that we have generated a query string, we can instanciate a HttpClient,
    // construct a POST or GET method and execute to submit the request
    String response = null;
    for (int pass = 1; pass <= 2; pass++) {
        HttpUriRequest method = null;
        doPost = true;
        String url = null;
        if (doPost) {
            HttpPost httpPost = new HttpPost(baseUrl);
            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            method = httpPost;
            url = baseUrl + "?" + URLEncodedUtils.format(params, "utf-8");
        } else {
            String query = URLEncodedUtils.format(params, "utf-8");
            method = new HttpGet(baseUrl + "?" + query);
            url = method.getRequestLine().getUri();
        }

        try {
            if (this.httpClient == null)
                this.httpClient = HttpClientUtils.getInstance(this.connectionTimeout, this.soTimeout)
                        .getNewHttpClient();
            HttpResponse httpResponse = this.httpClient.execute(method);
            int status = httpResponse.getStatusLine().getStatusCode();
            if (status != 200)
                throw new Exception(
                        "got a non-200 response [ " + status + " ] from Nexmo-HTTP for url [ " + url + " ] ");
            response = new BasicResponseHandler().handleResponse(httpResponse);
            log.info(".. SUBMITTED NEXMO-HTTP URL [ " + url + " ] -- response [ " + response + " ] ");
            break;
        } catch (Exception e) {
            method.abort();
            log.info("communication failure: " + e);
            String exceptionMsg = e.getMessage();
            if (exceptionMsg.indexOf("Read timed out") >= 0) {
                log.info(
                        "we're still connected, but the target did not respond in a timely manner ..  drop ...");
            } else {
                if (pass == 1) {
                    log.info("... re-establish http client ...");
                    this.httpClient = null;
                    continue;
                }
            }

            // return a COMMS failure ...
            SmsSubmissionResult[] results = new SmsSubmissionResult[1];
            results[0] = new SmsSubmissionResult(SmsSubmissionResult.STATUS_COMMS_FAILURE, null, null,
                    "Failed to communicate with NEXMO-HTTP url [ " + url + " ] ..." + e,
                    message.getClientReference(), null, null, true, null, null);
            return results;
        }
    }

    // parse the response doc ...

    /*
    We receive a response from the api that looks like this, parse the document
    and turn it into an array of SmsSubmissionResult, one object per <message> node
            
        <mt-submission-response>
            <messages count='x'>
                <message>
                    <to>xxx</to>
                    <messageId>xxx</messageId>
                    <status>xx</status>
                    <errorText>ff</errorText>
                    <clientRef>xxx</clientRef>
                    <remainingBalance>##.##</remainingBalance>
                    <messagePrice>##.##</messagePrice>
                    <reachability status='x' description='xxx' />
                    <network>23410</network>
                </message>
            </messages>
        </mt-submission-response>
    */

    List<SmsSubmissionResult> results = new ArrayList<>();

    Document doc = null;
    synchronized (this.documentBuilder) {
        try {
            doc = this.documentBuilder.parse(new InputSource(new StringReader(response)));
        } catch (Exception e) {
            throw new Exception("Failed to build a DOM doc for the xml document [ " + response + " ] ", e);
        }
    }

    NodeList replies = doc.getElementsByTagName("mt-submission-response");
    for (int i = 0; i < replies.getLength(); i++) {
        Node reply = replies.item(i);
        NodeList messageLists = reply.getChildNodes();
        for (int i2 = 0; i2 < messageLists.getLength(); i2++) {
            Node messagesNode = messageLists.item(i2);
            if (messagesNode.getNodeType() != Node.ELEMENT_NODE)
                continue;
            if (messagesNode.getNodeName().equals("messages")) {
                NodeList messages = messagesNode.getChildNodes();
                for (int i3 = 0; i3 < messages.getLength(); i3++) {
                    Node messageNode = messages.item(i3);
                    if (messageNode.getNodeType() != Node.ELEMENT_NODE)
                        continue;

                    int status = -1;
                    String messageId = null;
                    String destination = null;
                    String errorText = null;
                    String clientReference = null;
                    BigDecimal remainingBalance = null;
                    BigDecimal messagePrice = null;
                    SmsSubmissionReachabilityStatus smsSubmissionReachabilityStatus = null;
                    String network = null;

                    NodeList nodes = messageNode.getChildNodes();
                    for (int i4 = 0; i4 < nodes.getLength(); i4++) {
                        Node node = nodes.item(i4);
                        if (node.getNodeType() != Node.ELEMENT_NODE)
                            continue;
                        if (node.getNodeName().equals("messageId")) {
                            messageId = node.getFirstChild() == null ? null
                                    : node.getFirstChild().getNodeValue();
                        } else if (node.getNodeName().equals("to")) {
                            destination = node.getFirstChild() == null ? null
                                    : node.getFirstChild().getNodeValue();
                        } else if (node.getNodeName().equals("status")) {
                            String str = node.getFirstChild() == null ? null
                                    : node.getFirstChild().getNodeValue();
                            try {
                                status = Integer.parseInt(str);
                            } catch (NumberFormatException e) {
                                log.error("xml parser .. invalid value in <status> node [ " + str + " ] ");
                                status = SmsSubmissionResult.STATUS_INTERNAL_ERROR;
                            }
                        } else if (node.getNodeName().equals("errorText")) {
                            errorText = node.getFirstChild() == null ? null
                                    : node.getFirstChild().getNodeValue();
                        } else if (node.getNodeName().equals("clientRef")) {
                            clientReference = node.getFirstChild() == null ? null
                                    : node.getFirstChild().getNodeValue();
                        } else if (node.getNodeName().equals("remainingBalance")) {
                            String str = node.getFirstChild() == null ? null
                                    : node.getFirstChild().getNodeValue();
                            try {
                                if (str != null)
                                    remainingBalance = new BigDecimal(str);
                            } catch (NumberFormatException e) {
                                log.error("xml parser .. invalid value in <remainingBalance> node [ " + str
                                        + " ] ");
                            }
                        } else if (node.getNodeName().equals("messagePrice")) {
                            String str = node.getFirstChild() == null ? null
                                    : node.getFirstChild().getNodeValue();
                            try {
                                if (str != null)
                                    messagePrice = new BigDecimal(str);
                            } catch (NumberFormatException e) {
                                log.error(
                                        "xml parser .. invalid value in <messagePrice> node [ " + str + " ] ");
                            }
                        } else if (node.getNodeName().equals("reachability")) {
                            NamedNodeMap attributes = node.getAttributes();
                            Node attr = attributes.getNamedItem("status");
                            String str = attr == null
                                    ? "" + SmsSubmissionReachabilityStatus.REACHABILITY_STATUS_UNKNOWN
                                    : attr.getNodeValue();
                            int reachabilityStatus = SmsSubmissionReachabilityStatus.REACHABILITY_STATUS_UNKNOWN;
                            try {
                                reachabilityStatus = Integer.parseInt(str);
                            } catch (NumberFormatException e) {
                                log.error(
                                        "xml parser .. invalid value in 'status' attribute in <reachability> node [ "
                                                + str + " ] ");
                                reachabilityStatus = SmsSubmissionReachabilityStatus.REACHABILITY_STATUS_UNKNOWN;
                            }

                            attr = attributes.getNamedItem("description");
                            String description = attr == null ? "-UNKNOWN-" : attr.getNodeValue();

                            smsSubmissionReachabilityStatus = new SmsSubmissionReachabilityStatus(
                                    reachabilityStatus, description);
                        } else if (node.getNodeName().equals("network")) {
                            network = node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue();
                        } else
                            log.error(
                                    "xml parser .. unknown node found in status-return, expected [ messageId, to, status, errorText, clientRef, messagePrice, remainingBalance, reachability, network ] -- found [ "
                                            + node.getNodeName() + " ] ");
                    }

                    if (status == -1)
                        throw new Exception("Xml Parser - did not find a <status> node");

                    // Is this a temporary error ?
                    boolean temporaryError = (status == SmsSubmissionResult.STATUS_THROTTLED
                            || status == SmsSubmissionResult.STATUS_INTERNAL_ERROR
                            || status == SmsSubmissionResult.STATUS_TOO_MANY_BINDS);

                    results.add(new SmsSubmissionResult(status, destination, messageId, errorText,
                            clientReference, remainingBalance, messagePrice, temporaryError,
                            smsSubmissionReachabilityStatus, network));
                }
            }
        }
    }

    return results.toArray(new SmsSubmissionResult[results.size()]);
}

From source file:com.bigdata.journal.jini.ha.AbstractHAJournalServerTestCase.java

/**
 * Http connection./*from w  ww. j  a  va 2s .c o  m*/
 * 
 * @param opts
 *            The connection options.
 * 
 * @return The connection.
 */
protected HttpResponse doConnect(final HttpClient httpClient, final ConnectOptions opts) throws Exception {

    /*
     * Generate the fully formed and encoded URL.
     */

    final StringBuilder urlString = new StringBuilder(opts.serviceURL);

    ConnectOptions.addQueryParams(urlString, opts.requestParams);

    if (log.isDebugEnabled()) {
        log.debug("*** Request ***");
        log.debug(opts.serviceURL);
        log.debug(opts.method);
        log.debug("query=" + opts.getRequestParam("query"));
    }

    HttpUriRequest request = null;
    try {

        request = newRequest(urlString.toString(), opts.method);

        if (opts.requestHeaders != null) {

            for (Map.Entry<String, String> e : opts.requestHeaders.entrySet()) {

                request.addHeader(e.getKey(), e.getValue());

                if (log.isDebugEnabled())
                    log.debug(e.getKey() + ": " + e.getValue());

            }

        }

        if (opts.entity != null) {

            ((HttpEntityEnclosingRequestBase) request).setEntity(opts.entity);

        }

        final HttpResponse response = httpClient.execute(request);

        return response;

        //            // connect.
        //            conn.connect();
        //
        //            return conn;

    } catch (Throwable t) {
        /*
         * If something goes wrong, then close the http connection.
         * Otherwise, the connection will be closed by the caller.
         */
        try {

            if (request != null)
                request.abort();

            //                // clean up the connection resources
            //                if (conn != null)
            //                    conn.disconnect();

        } catch (Throwable t2) {
            // ignored.
        }
        throw new RuntimeException(opts.serviceURL + " : " + t, t);
    }

}

From source file:org.codegist.crest.HttpClientRestService.java

public HttpResponse exec(HttpRequest httpRequest) throws HttpException {
    HttpUriRequest request;
    try {/*from   ww w. j  a  v a 2s.  co  m*/
        request = toHttpUriRequest(httpRequest);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    org.apache.http.HttpResponse response;
    HttpEntity entity = null;
    boolean inError = false;
    try {
        logger.debug("%4s %s", httpRequest.getMeth(), request.getURI());
        logger.trace(request);
        response = http.execute(request);
        if (response == null) {
            throw new HttpException("No Response!", new HttpResponse(httpRequest, -1));
        }

        entity = response.getEntity();
        HttpResponse res;
        if (entity != null) {
            res = new HttpResponse(httpRequest, response.getStatusLine().getStatusCode(),
                    toHeaders(response.getAllHeaders()), new HttpResourceImpl(request, entity));
            if (res.getStatusCode() != HttpStatus.SC_OK) {
                throw new HttpException(response.getStatusLine().getReasonPhrase(), res);
            }
        } else if (httpRequest.getMeth().equals(HttpMethod.HEAD)) {
            res = new HttpResponse(httpRequest, response.getStatusLine().getStatusCode(),
                    toHeaders(response.getAllHeaders()));
        } else {
            throw new HttpException(response.getStatusLine().getReasonPhrase(), new HttpResponse(httpRequest,
                    response.getStatusLine().getStatusCode(), toHeaders(response.getAllHeaders())));
        }
        logger.trace("HTTP Response %s", response);
        return res;
    } catch (HttpException e) {
        inError = true;
        throw e;
    } catch (Throwable e) {
        inError = true;
        throw new HttpException(e, new HttpResponse(httpRequest, -1));
    } finally {
        if (inError) {
            if (entity != null) {
                try {
                    entity.consumeContent();
                } catch (IOException e1) {
                    //ignore
                }
            }
            request.abort();
        }
    }
}

From source file:com.healthcit.cacure.dao.CouchDBDao.java

private String doHttp(HttpUriRequest request) throws IOException {
    StringBuffer responseBody = new StringBuffer();
    HttpClient httpclient = new DefaultHttpClient();
    // Execute the request
    HttpResponse response = httpclient.execute(request);

    // 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 {//from   w  w  w .j ava 2  s .com

            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
            // do something useful with the response
            String line;
            while ((line = reader.readLine()) != null) {
                responseBody.append(line).append("\n");
            }

        } 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.
            request.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();

    }
    return responseBody.toString();
}

From source file:net.yacy.cora.protocol.http.HTTPClient.java

private byte[] getContentBytes(final HttpUriRequest httpUriRequest, final int maxBytes,
        final boolean concurrent) throws IOException {
    byte[] content = null;
    try {//from w  w w  . ja  v  a  2s.c  o  m
        execute(httpUriRequest, concurrent);
        if (this.httpResponse == null)
            return null;
        // get the response body
        final HttpEntity httpEntity = this.httpResponse.getEntity();
        if (httpEntity != null) {
            if (getStatusCode() == HttpStatus.SC_OK) {
                if (maxBytes >= 0 && httpEntity.getContentLength() > maxBytes) {
                    /* When anticipated content length is already known and exceed the specified limit : 
                     * throw an exception and abort the connection, consistently with getByteArray() implementation 
                     * Otherwise returning null and consuming fully the entity can be very long on large resources */
                    throw new IOException(
                            "Content to download exceed maximum value of " + Formatter.bytesToString(maxBytes));
                }
                content = getByteArray(httpEntity, maxBytes);
            }
            // Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
            EntityUtils.consume(httpEntity);
        }
    } catch (final IOException e) {
        httpUriRequest.abort();
        throw e;
    } finally {
        if (this.httpResponse != null)
            this.httpResponse.close();
        ConnectionInfo.removeConnection(httpUriRequest.hashCode());
    }
    return content;
}

From source file:com.buffalokiwi.api.API.java

/**
 * Prepare the response entity for usage
 * @param response HTTP Response//  w  w w  . java2 s.  c  o m
 * @param get HTTP Get
 * @return The response results
 * @throws BrowserException
 * @throws RedirectException if a redirect needs to happen
 */
private IAPIResponse processResponse(final HttpResponse response, final HttpUriRequest get)
        throws APIException {
    if (response == null)
        throw new APIException("Endpoint response was null");

    final HttpEntity entity = response.getEntity();

    try {
        if (entity != null) {
            //..Get the charset
            String charset = "";

            try {
                java.nio.charset.Charset cs = ContentType.getOrDefault(entity).getCharset();

                if (cs != null)
                    charset = cs.displayName();
            } catch (ParseException | UnsupportedCharsetException e) {
                //..No nothing, use defaults
            }

            if ((charset == null) || (charset.isEmpty())) {
                charset = "UTF-8";

                final Header[] headers = response.getHeaders("Content-Type");
                if (headers.length > 0) {
                    if (headers[0].getValue().equals("application/octet-stream"))
                        charset = "";
                }
            }

            //..Get content length header 

            final Header[] clen = response.getHeaders("Content-Length");
            final int contentLength = (clen.length > 0) ? Integer.valueOf(clen[0].getValue()) : 0;

            //..Process the stream
            try (final InputStream in = entity.getContent()) {
                final byte[] content = processEntity(in, contentLength);

                //..set the character set used to create the htmlBuffer
                if (LOG.isTraceEnabled()) {
                    if (!charset.isEmpty())
                        APILog.trace(LOG, new String(content, 0, content.length, charset));
                    else
                        APILog.trace(LOG, new String(content));
                }

                final IAPIResponse res = createResponseObject(response, content, charset);

                APILog.debug(LOG, String.valueOf(res.getStatusLine().getStatusCode()),
                        res.getStatusLine().getReasonPhrase(), "for", get.getURI().toString());

                return res;

            } catch (RuntimeException e) {
                APILog.error(LOG, e);
                //..Abort
                get.abort();

                throw new APIException(e.getMessage(), e);
            }
        } else {
            final IAPIResponse res = createResponseObject(response, null, "");
            APILog.debug(LOG, String.valueOf(res.getStatusLine().getStatusCode()),
                    res.getStatusLine().getReasonPhrase(), "for", get.getURI().toString());

            return res;
        }
    } catch (IOException e) {
        APILog.error(LOG, e);
        throw new APIException("Failed to retrieve entity content (IOException)", e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
        }
    }
}

From source file:crawler.java.edu.uci.ics.crawler4j.fetcher.PageFetcher.java

public PageFetchResult fetchPage(WebURL webUrl)
        throws InterruptedException, IOException, PageBiggerThanMaxSizeException {
    // Getting URL, setting headers & content
    PageFetchResult fetchResult = new PageFetchResult();
    String toFetchURL = webUrl.getURL();
    HttpUriRequest request = null;
    try {/*ww  w. ja  va2 s  .c  om*/
        request = newHttpUriRequest(toFetchURL);
        // Applying Politeness delay
        synchronized (mutex) {
            long now = (new Date()).getTime();
            if ((now - lastFetchTime) < config.getPolitenessDelay()) {
                Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
            }
            lastFetchTime = (new Date()).getTime();
        }

        CloseableHttpResponse response = httpClient.execute(request);
        fetchResult.setEntity(response.getEntity());
        fetchResult.setResponseHeaders(response.getAllHeaders());

        // Setting HttpStatus
        int statusCode = response.getStatusLine().getStatusCode();

        // If Redirect ( 3xx )
        if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY
                || statusCode == HttpStatus.SC_MULTIPLE_CHOICES || statusCode == HttpStatus.SC_SEE_OTHER
                || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT || statusCode == 308) { // todo follow https://issues.apache.org/jira/browse/HTTPCORE-389

            Header header = response.getFirstHeader("Location");
            if (header != null) {
                String movedToUrl = URLCanonicalizer.getCanonicalURL(header.getValue(), toFetchURL);
                fetchResult.setMovedToUrl(movedToUrl);
            }
        } else if (statusCode >= 200 && statusCode <= 299) { // is 2XX, everything looks ok
            fetchResult.setFetchedUrl(toFetchURL);
            String uri = request.getURI().toString();
            if (!uri.equals(toFetchURL)) {
                if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL)) {
                    fetchResult.setFetchedUrl(uri);
                }
            }

            // Checking maximum size
            if (fetchResult.getEntity() != null) {
                long size = fetchResult.getEntity().getContentLength();
                if (size == -1) {
                    Header length = response.getLastHeader("Content-Length");
                    if (length == null) {
                        length = response.getLastHeader("Content-length");
                    }
                    if (length != null) {
                        size = Integer.parseInt(length.getValue());
                    }
                }
                if (size > config.getMaxDownloadSize()) {
                    //fix issue #52 - consume entity
                    response.close();
                    throw new PageBiggerThanMaxSizeException(size);
                }
            }
        }

        fetchResult.setStatusCode(statusCode);
        return fetchResult;

    } finally { // occurs also with thrown exceptions
        if ((fetchResult.getEntity() == null) && (request != null)) {
            request.abort();
        }
    }
}

From source file:crawler.PageFetcher.java

public PageFetchResult fetchPage(WebURL webUrl)
        throws InterruptedException, IOException, PageBiggerThanMaxSizeException {
    // Getting URL, setting headers & content
    PageFetchResult fetchResult = new PageFetchResult();
    String toFetchURL = webUrl.getURL();
    HttpUriRequest request = null;
    try {//  w  w  w .j a  v  a2s. co m
        request = newHttpUriRequest(toFetchURL);
        // Applying Politeness delay
        synchronized (mutex) {
            long now = (new Date()).getTime();
            if ((now - lastFetchTime) < config.getPolitenessDelay()) {
                Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
            }
            lastFetchTime = (new Date()).getTime();
        }

        CloseableHttpResponse response = httpClient.execute(request);
        fetchResult.setEntity(response.getEntity());
        fetchResult.setResponseHeaders(response.getAllHeaders());

        // Setting HttpStatus
        int statusCode = response.getStatusLine().getStatusCode();

        // If Redirect ( 3xx )
        if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY
                || statusCode == HttpStatus.SC_MULTIPLE_CHOICES || statusCode == HttpStatus.SC_SEE_OTHER
                || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT || statusCode == 308) { // todo follow
            // https://issues.apache.org/jira/browse/HTTPCORE-389

            Header header = response.getFirstHeader("Location");
            if (header != null) {
                String movedToUrl = URLCanonicalizer.getCanonicalURL(header.getValue(), toFetchURL);
                fetchResult.setMovedToUrl(movedToUrl);
            }
        } else if (statusCode >= 200 && statusCode <= 299) { // is 2XX, everything looks ok
            fetchResult.setFetchedUrl(toFetchURL);
            String uri = request.getURI().toString();
            if (!uri.equals(toFetchURL)) {
                if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL)) {
                    fetchResult.setFetchedUrl(uri);
                }
            }

            // Checking maximum size
            if (fetchResult.getEntity() != null) {
                long size = fetchResult.getEntity().getContentLength();
                if (size == -1) {
                    Header length = response.getLastHeader("Content-Length");
                    if (length == null) {
                        length = response.getLastHeader("Content-length");
                    }
                    if (length != null) {
                        size = Integer.parseInt(length.getValue());
                    }
                }
                if (size > config.getMaxDownloadSize()) {
                    //fix issue #52 - consume entity
                    response.close();
                    throw new PageBiggerThanMaxSizeException(size);
                }
            }
        }

        fetchResult.setStatusCode(statusCode);
        return fetchResult;

    } finally { // occurs also with thrown exceptions
        if ((fetchResult.getEntity() == null) && (request != null)) {
            request.abort();
        }
    }
}