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:org.quizpoll.net.HttpHelper.java

/**
 * Makes HTTP request with JSON data.//from   w w  w .  ja  v  a  2  s . c om
 */
private HttpResponse doRequest(HttpUriRequest request) {
    try {
        httpClient = AndroidHttpClient
                .newInstance(USER_AGENT + Utils.getVersion(activity) + "/" + Utils.getVersionCode(activity));
        Log.i(TAG, "Request: " + request.getURI());
        HttpResponse response = httpClient.execute(request);
        Log.i(TAG, "Response: " + response.getStatusLine().toString());
        return response;
    } catch (IOException e) {
        return null;
    }
}

From source file:fr.ippon.wip.http.hc.HttpClientExecutor.java

private String getActualUrl(PortletRequest portletRequest, HttpContext context, RequestBuilder request,
        HttpResponse response) {// w  ww.  j  a v  a  2 s .c  o m
    boolean cacheUsed = (context
            .getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS) == CacheResponseStatus.CACHE_HIT);
    boolean notFoundResponse = (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND);

    /*
     * ExecutionContext.HTTP_REQUEST and ExecutionContext.HTTP_TARGET_HOST are not set when the cache is used
     * or when the resource is ignored by WIP
     */
    if (cacheUsed || notFoundResponse)
        return request.getRequestedURL();

    // Get final URL (ie. perhaps redirected)
    HttpUriRequest actualRequest = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    HttpHost actualHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    return (actualRequest.getURI().isAbsolute()) ? actualRequest.getURI().toString()
            : (actualHost.toURI() + actualRequest.getURI());
}

From source file:com.soulgalore.crawler.core.impl.HTTPClientResponseFetcher.java

public HTMLPageResponse get(CrawlerURL url, boolean getPage, Map<String, String> requestHeaders,
        boolean followRedirectsToNewDomain) {

    if (url.isWrongSyntax()) {
        return new HTMLPageResponse(url, StatusCode.SC_MALFORMED_URI.getCode(),
                Collections.<String, String>emptyMap(), "", "", 0, "", 0);
    }/*from w  w w.  ja  v  a  2 s .c o  m*/

    final HttpGet get = new HttpGet(url.getUri());

    for (String key : requestHeaders.keySet()) {
        get.setHeader(key, requestHeaders.get(key));
    }

    HttpEntity entity = null;
    final long start = System.currentTimeMillis();

    try {

        HttpContext localContext = new BasicHttpContext();
        final HttpResponse resp = httpClient.execute(get, localContext);

        final long fetchTime = System.currentTimeMillis() - start;

        // Get the last URL in the redirect chain
        final HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        final HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
        // Fix when using proxy, relative URI (no proxy used)
        String newURL;
        if (req.getURI().toString().startsWith("http")) {
            newURL = req.getURI().toString();
        } else {
            newURL = target + req.getURI().toString();
        }

        entity = resp.getEntity();

        // this is a hack to minimize the amount of memory used
        // should make this configurable maybe
        // don't fetch headers for request that don't fetch the body and
        // response isn't 200
        // these headers will not be shown in the results
        final Map<String, String> headersAndValues = getPage
                || !StatusCode.isResponseCodeOk(resp.getStatusLine().getStatusCode()) ? getHeaders(resp)
                        : Collections.<String, String>emptyMap();

        final String encoding = entity.getContentEncoding() != null ? entity.getContentEncoding().getValue()
                : "";

        final String body = getPage ? getBody(entity, "".equals(encoding) ? "UTF-8" : encoding) : "";
        final long size = entity.getContentLength();
        // TODO add log when null
        final String type = (entity.getContentType() != null) ? entity.getContentType().getValue() : "";
        final int sc = resp.getStatusLine().getStatusCode();
        EntityUtils.consume(entity);

        // If we want to only collect only URLS that don't redirect to a new domain
        // This solves the problem with local links like:
        // http://www.peterhedenskog.com/facebook that redirects to http://www.facebook.com/u/peter.hedenskog
        // TODO the host check can be done better :)
        if (!followRedirectsToNewDomain && !newURL.contains(url.getHost())) {
            return new HTMLPageResponse(url, StatusCode.SC_SERVER_REDIRECT_TO_NEW_DOMAIN.getCode(),
                    Collections.<String, String>emptyMap(), "", "", 0, "", fetchTime);
        }
        return new HTMLPageResponse(
                !url.getUrl().equals(newURL) ? new CrawlerURL(newURL, url.getReferer()) : url, sc,
                headersAndValues, body, encoding, size, type, fetchTime);

    } catch (SocketTimeoutException e) {
        System.err.println(e);
        return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_TIMEOUT.getCode(),
                Collections.<String, String>emptyMap(), "", "", 0, "", System.currentTimeMillis() - start);
    }

    catch (ConnectTimeoutException e) {
        System.err.println(e);
        return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_TIMEOUT.getCode(),
                Collections.<String, String>emptyMap(), "", "", 0, "", System.currentTimeMillis() - start);
    }

    catch (IOException e) {
        System.err.println(e);
        return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_UNKNOWN.getCode(),
                Collections.<String, String>emptyMap(), "", "", 0, "", -1);
    } finally {
        get.releaseConnection();
    }

}

From source file:com.comcast.cim.rest.client.xhtml.TestRequestBuilder.java

@Test
public void testAddsGetInputToUriAsQueryParameter() throws Exception {
    Element form = new Element("form", XhtmlParser.XHTML_NS_URI);
    form.setAttribute("method", "GET");
    form.setAttribute("action", "http://foo.example.com/");
    Element input = new Element("input", XhtmlParser.XHTML_NS_URI);
    input.setAttribute("type", "text");
    input.setAttribute("name", "arg0");
    form.addContent(input);/*from   w w w.ja v a  2  s  .co m*/
    buildDocument(form);
    URL context = new URL("http://www.example.com/foo/bar");
    args.put("arg0", "val0");

    HttpUriRequest result = impl.submitForm(form, context, args);
    Assert.assertEquals("http://foo.example.com/?arg0=val0", result.getURI().toString());
}

From source file:com.comcast.cim.rest.client.xhtml.TestRequestBuilder.java

@Test
public void testSkipsInputsWithNoName() throws Exception {
    Element form = new Element("form", XhtmlParser.XHTML_NS_URI);
    form.setAttribute("method", "GET");
    form.setAttribute("action", "http://foo.example.com/");
    Element input = new Element("input", XhtmlParser.XHTML_NS_URI);
    input.setAttribute("type", "text");
    input.setAttribute("name", "arg0");
    form.addContent(input);/*from w  ww.jav  a2s . com*/
    Element input2 = new Element("input", XhtmlParser.XHTML_NS_URI);
    input2.setAttribute("type", "submit");
    form.addContent(input2);
    buildDocument(form);
    URL context = new URL("http://www.example.com/foo/bar");
    args.put("arg0", "val0");

    HttpUriRequest result = impl.submitForm(form, context, args);
    Assert.assertEquals("http://foo.example.com/?arg0=val0", result.getURI().toString());
}

From source file:com.comcast.cim.rest.client.xhtml.TestRequestBuilder.java

@Test
public void testAddsHiddenInputsToQueryParams() throws Exception {
    Element form = new Element("form", XhtmlParser.XHTML_NS_URI);
    form.setAttribute("method", "GET");
    form.setAttribute("action", "http://foo.example.com/");
    Element input = new Element("input", XhtmlParser.XHTML_NS_URI);
    input.setAttribute("type", "hidden");
    input.setAttribute("name", "arg0");
    form.addContent(input);//from  w  ww  .  j  a va2s . c  om
    Element input2 = new Element("input", XhtmlParser.XHTML_NS_URI);
    input2.setAttribute("type", "submit");
    form.addContent(input2);
    buildDocument(form);
    URL context = new URL("http://www.example.com/foo/bar");
    args.put("arg0", "val0");

    HttpUriRequest result = impl.submitForm(form, context, args);
    Assert.assertEquals("http://foo.example.com/?arg0=val0", result.getURI().toString());
}

From source file:com.comcast.cim.rest.client.xhtml.TestRequestBuilder.java

@Test
public void testRetainsExistingInputValueIfNotSpecifiedInMap() throws Exception {
    Element form = new Element("form", XhtmlParser.XHTML_NS_URI);
    form.setAttribute("method", "GET");
    form.setAttribute("action", "http://foo.example.com/");
    Element input = new Element("input", XhtmlParser.XHTML_NS_URI);
    input.setAttribute("type", "hidden");
    input.setAttribute("name", "arg0");
    input.setAttribute("value", "val1");
    form.addContent(input);/*from www . ja va 2s.  c o  m*/
    Element input2 = new Element("input", XhtmlParser.XHTML_NS_URI);
    input2.setAttribute("type", "submit");
    form.addContent(input2);
    buildDocument(form);
    URL context = new URL("http://www.example.com/foo/bar");

    HttpUriRequest result = impl.submitForm(form, context, args);
    Assert.assertEquals("http://foo.example.com/?arg0=val1", result.getURI().toString());
}

From source file:org.wildfly.camel.test.olingo4.Olingo4IntegrationTest.java

private String getRealServiceUrl(String baseUrl) throws ClientProtocolException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(baseUrl);
    HttpContext httpContext = new BasicHttpContext();
    httpclient.execute(httpGet, httpContext);
    HttpUriRequest currentReq = (HttpUriRequest) httpContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
    HttpHost currentHost = (HttpHost) httpContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
    String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString()
            : (currentHost.toURI() + currentReq.getURI());

    return currentUrl;
}

From source file:tech.sirwellington.alchemy.http.AlchemyRequestMapperTest.java

@Test
public void testPut() throws Exception {
    instance = AlchemyRequestMapper.PUT;
    assertThat(instance, notNullValue());

    HttpUriRequest result = instance.convertToApacheRequest(request);
    assertThat(result, notNullValue());/*from   w  w w  .  j ava  2 s  . c  om*/
    assertThat(result, instanceOf(HttpPut.class));
    assertThat(result.getURI(), is(url.toURI()));

}

From source file:tech.sirwellington.alchemy.http.AlchemyRequestMapperTest.java

@Test
public void testPost() throws Exception {
    instance = AlchemyRequestMapper.POST;
    assertThat(instance, notNullValue());

    HttpUriRequest result = instance.convertToApacheRequest(request);
    assertThat(result, notNullValue());/*from   ww w  .j  ava  2  s .  co  m*/
    assertThat(result, instanceOf(HttpPost.class));
    assertThat(result.getURI(), is(url.toURI()));

}