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

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

Introduction

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

Prototype

URI getURI();

Source Link

Document

Returns the URI this request uses, such as <code>http://example.org/path/to/file</code>.

Usage

From source file:com.activiti.service.activiti.ActivitiClientService.java

public ResponseInfo execute(HttpUriRequest request, String userName, String password,
        int... expectedStatusCodes) {

    ActivitiServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {//from w  w w  .j a va2s  .  c o  m
        CloseableHttpResponse response = client.execute(request);

        try {
            JsonNode bodyNode = readJsonContent(response.getEntity().getContent());

            int statusCode = -1;
            if (response.getStatusLine() != null) {
                statusCode = response.getStatusLine().getStatusCode();
            }
            boolean success = Arrays.asList(expectedStatusCodes).contains(statusCode);

            if (success) {
                return new ResponseInfo(statusCode, bodyNode);

            } else {
                exception = new ActivitiServiceException(
                        extractError(readJsonContent(response.getEntity().getContent()),
                                "An error occured while calling Activiti: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            log.warn("Error consuming response from uri " + request.getURI(), e);
            exception = wrapException(e, request);
        } finally {
            response.close();
        }

    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return null;
}

From source file:com.benefit.buy.library.http.query.callback.AbstractAjaxCallback.java

private void httpDo(HttpUriRequest hr, String url, Map<String, String> headers, AjaxStatus status)
        throws ClientProtocolException, IOException {
    if (AGENT != null) {
        hr.addHeader("User-Agent", AGENT);
    }//ww w .  j a v a2 s  .c  om
    if (headers != null) {
        for (String name : headers.keySet()) {
            hr.addHeader(name, headers.get(name));
        }
    }
    if (GZIP && ((headers == null) || !headers.containsKey("Accept-Encoding"))) {
        hr.addHeader("Accept-Encoding", "gzip");
    }
    String cookie = makeCookie();
    if (cookie != null) {
        hr.addHeader("Cookie", cookie);
    }
    if (ah != null) {
        ah.applyToken(this, hr);
    }
    DefaultHttpClient client = getClient();
    HttpParams hp = hr.getParams();
    if (proxy != null) {
        hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    if (timeout > 0) {
        hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
        hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    }
    HttpContext context = new BasicHttpContext();
    //        CookieStore cookieStore = new BasicCookieStore();
    //        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    request = hr;
    if (abort) {
        throw new IOException("Aborted");
    }
    HttpResponse response = null;
    try {
        //response = client.execute(hr, context);
        response = execute(hr, client, context);
        getCookie(client);
    } catch (HttpHostConnectException e) {
        //if proxy is used, automatically retry without proxy
        if (proxy != null) {
            AQUtility.debug("proxy failed, retrying without proxy");
            hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
            //response = client.execute(hr, context);
            response = execute(hr, client, context);
        } else {
            throw e;
        }
    }
    byte[] data = null;
    String redirect = url;
    int code = response.getStatusLine().getStatusCode();
    String message = response.getStatusLine().getReasonPhrase();
    String error = null;
    HttpEntity entity = response.getEntity();
    File file = null;
    if ((code < 200) || (code >= 300)) {
        InputStream is = null;
        try {
            if (entity != null) {
                is = entity.getContent();
                byte[] s = toData(getEncoding(entity), is);
                error = new String(s, "UTF-8");
                AQUtility.debug("error", error);
            }
        } catch (Exception e) {
            AQUtility.debug(e);
        } finally {
            AQUtility.close(is);
        }
    } else {
        HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        redirect = currentHost.toURI() + currentReq.getURI();
        int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));
        OutputStream os = null;
        InputStream is = null;
        try {
            file = getPreFile();
            if (file == null) {
                os = new PredefinedBAOS(size);
            } else {
                file.createNewFile();
                os = new BufferedOutputStream(new FileOutputStream(file));
            }
            is = entity.getContent();
            if ("gzip".equalsIgnoreCase(getEncoding(entity))) {
                is = new GZIPInputStream(is);
            }
            copy(is, os, (int) entity.getContentLength());
            os.flush();
            if (file == null) {
                data = ((PredefinedBAOS) os).toByteArray();
            } else {
                if (!file.exists() || (file.length() == 0)) {
                    file = null;
                }
            }
        } finally {
            AQUtility.close(is);
            AQUtility.close(os);
        }
    }
    AQUtility.debug("response", code);
    if (data != null) {
        AQUtility.debug(data.length, url);
    }
    status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file)
            .client(client).context(context).headers(response.getAllHeaders());
}

From source file:com.activiti.service.activiti.ActivitiClientService.java

/**
 * Execute the given request, without using the response body.
 * In case the response returns a different status-code than expected, an {@link ActivitiServiceException} is thrown with the error message received
 * from the client, if possible.//w ww  . ja va  2 s . c  om
 */
public void executeRequestNoResponseBody(HttpUriRequest request, ServerConfig serverConfig,
        int expectedStatusCode) {

    ActivitiServiceException exception = null;

    CloseableHttpClient client = getHttpClient(serverConfig);
    try {
        CloseableHttpResponse response = client.execute(request);
        boolean success = response.getStatusLine() != null
                && response.getStatusLine().getStatusCode() == expectedStatusCode;

        if (!success) {
            String errorMessage = null;
            try {
                if (response.getEntity() != null && response.getEntity().getContentLength() != 0) {
                    InputStream responseContent = response.getEntity().getContent();
                    JsonNode errorBody = objectMapper.readTree(responseContent);
                    errorMessage = extractError(errorBody,
                            "An error occured while calling Activiti: " + response.getStatusLine());

                } else {
                    errorMessage = "An error was returned when calling the Activiti server";
                }
            } catch (Exception e) {
                log.warn("Error consuming response from uri " + request.getURI(), e);
                exception = wrapException(e, request);
            } finally {
                response.close();
            }
            exception = new ActivitiServiceException(errorMessage);
        }
    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = wrapException(e, request);

    } finally {
        try {
            client.close();
        } catch (Exception e) {
            // No need to throw upwards, as this may hide exceptions/valid result
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }
}

From source file:nl.uva.mediamosa.impl.MediaMosaImpl.java

private String getStringResponse(HttpUriRequest request) throws IOException {
    HttpResponse response = httpclient.execute(request);
    HttpEntity entity = response.getEntity();
    InputStream is = null;//from   www. ja v  a2  s  . c  om
    String content = null;

    try {
        if (response != null && entity != null) {
            is = entity.getContent();
            content = IOUtils.toString(is);
        } else {
            log.warn("Empty response received from {}", request.getURI().toURL().toString());
        }
    } catch (UnsupportedOperationException | MalformedURLException e) {
        log.error("Unexpected error occurred", e);
    } finally {
        if (is != null) {
            is.close();
        }
    }
    return content;
}

From source file:com.activiti.service.activiti.ActivitiClientService.java

public void execute(HttpUriRequest request, HttpServletResponse httpResponse, String userName,
        String password) {//from  w w  w  .j av a 2 s  .  c  om

    ActivitiServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {
        CloseableHttpResponse response = client.execute(request);

        try {
            if (response.getStatusLine() != null
                    && response.getStatusLine().getStatusCode() != HttpStatus.SC_UNAUTHORIZED) {
                httpResponse.setStatus(response.getStatusLine().getStatusCode());
                if (response.getEntity() != null && response.getEntity().getContentType() != null) {
                    httpResponse.setContentType(response.getEntity().getContentType().getValue());
                    response.getEntity().writeTo(httpResponse.getOutputStream());
                }
            } else {
                exception = new ActivitiServiceException(
                        extractError(readJsonContent(response.getEntity().getContent()),
                                "An error occured while calling Activiti: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            log.warn("Error consuming response from uri " + request.getURI(), e);
            exception = wrapException(e, request);
        } finally {
            response.close();
        }

    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

}

From source file:com.cloudmine.api.rest.CMWebService.java

private <T extends ResponseBase> T executeCommand(HttpUriRequest message, ResponseConstructor<T> constructor)
        throws NetworkException {
    HttpResponse response = null;/*from   ww w  .j av  a2 s  .  c  o  m*/
    try {
        response = httpClient.execute(message);
        return constructor.construct(response);
    } catch (IOException e) {
        LOG.error("Error executing command: " + message.getURI(), e);
        throw new NetworkException("Couldn't execute command, IOException: ", e);
    } finally {
        CMWebService.consumeEntityResponse(response);
    }
}

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 {/*from w  w  w.j a  v a2s. c  o  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();
        }
    }
}

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  a2 s .c  o 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();
        }
    }
}

From source file:com.activiti.service.activiti.ActivitiClientService.java

public String executeRequestAsString(HttpUriRequest request, ServerConfig serverConfig,
        int expectedStatusCode) {

    ActivitiServiceException exception = null;
    String result = null;// w ww.java2  s.  c o m
    CloseableHttpClient client = getHttpClient(serverConfig);
    try {
        CloseableHttpResponse response = client.execute(request);
        boolean success = response.getStatusLine() != null
                && response.getStatusLine().getStatusCode() == expectedStatusCode;
        if (success) {
            result = IOUtils.toString(response.getEntity().getContent());
        } else {
            String errorMessage = null;
            try {
                if (response.getEntity().getContentLength() != 0) {
                    InputStream responseContent = response.getEntity().getContent();
                    JsonNode errorBody = objectMapper.readTree(responseContent);
                    errorMessage = extractError(errorBody,
                            "An error occured while calling Activiti: " + response.getStatusLine());
                } else {
                    errorMessage = "An error was returned when calling the Activiti server";
                }
            } catch (Exception e) {
                log.warn("Error consuming response from uri " + request.getURI(), e);
                exception = wrapException(e, request);

            } finally {
                response.close();
            }
            exception = new ActivitiServiceException(errorMessage);
        }
    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = wrapException(e, request);

    } finally {
        try {
            client.close();
        } catch (Exception e) {
            // No need to throw upwards, as this may hide exceptions/valid result
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return result;
}

From source file:com.library.loopj.android.http.AsyncHttpClient.java

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client          HttpClient to be used for request, can differ in single requests
 * @param contentType     MIME body type, for POST and PUT requests, may be null
 * @param context         Context of Android application, to hold the reference of request
 * @param httpContext     HttpContext in which the request will be executed
 * @param responseHandler ResponseHandler or its subclass to put the response into
 * @param uriRequest      instance of HttpUriRequest, which means it must be of HttpDelete,
 *                        HttpPost, HttpGet, HttpPut, etc.
 * @return RequestHandle of future request process
 *//*ww w. ja  va2 s .c om*/
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {
    if (uriRequest == null) {
        throw new IllegalArgumentException("HttpUriRequest must not be null");
    }

    if (responseHandler == null) {
        throw new IllegalArgumentException("ResponseHandler must not be null");
    }

    if (responseHandler.getUseSynchronousMode()) {
        throw new IllegalArgumentException(
                "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
    }

    if (contentType != null) {
        uriRequest.setHeader("Content-Type", contentType);
    }

    responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
    responseHandler.setRequestURI(uriRequest.getURI());

    AsyncHttpRequest request = new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler);
    threadPool.submit(request);
    RequestHandle requestHandle = new RequestHandle(request);

    if (context != null) {
        // Add request to request map
        List<RequestHandle> requestList = requestMap.get(context);
        if (requestList == null) {
            requestList = new LinkedList();
            requestMap.put(context, requestList);
        }

        if (responseHandler instanceof RangeFileAsyncHttpResponseHandler)
            ((RangeFileAsyncHttpResponseHandler) responseHandler).updateRequestHeaders(uriRequest);

        requestList.add(requestHandle);

        Iterator<RequestHandle> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().shouldBeGarbageCollected()) {
                iterator.remove();
            }
        }
    }

    return requestHandle;
}