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.apache.hadoop.gateway.dispatch.AppCookieManager.java

/**
 * Fetches hadoop.auth cookie from hadoop service authenticating using SpNego
 * //from w w w . jav a2  s .com
 * @param outboundRequest
 *          out going request
 * @param refresh
 *          flag indicating whether to refresh the cached cookie
 * @return hadoop.auth cookie from hadoop service authenticating using SpNego
 * @throws IOException
 *           in case of errors
 */
public String getAppCookie(HttpUriRequest outboundRequest, boolean refresh) throws IOException {

    URI uri = outboundRequest.getURI();
    String scheme = uri.getScheme();
    String host = uri.getHost();
    int port = uri.getPort();
    if (!refresh) {
        if (appCookie != null) {
            return appCookie;
        }
    }

    DefaultHttpClient client = new DefaultHttpClient();
    SPNegoSchemeFactory spNegoSF = new SPNegoSchemeFactory(/* stripPort */true);
    // spNegoSF.setSpengoGenerator(new BouncySpnegoTokenGenerator());
    client.getAuthSchemes().register(AuthPolicy.SPNEGO, spNegoSF);
    client.getCredentialsProvider().setCredentials(new AuthScope(/* host */null, /* port */-1, /* realm */null),
            EMPTY_JAAS_CREDENTIALS);

    clearAppCookie();
    String hadoopAuthCookie = null;
    HttpResponse httpResponse = null;
    try {
        HttpHost httpHost = new HttpHost(host, port, scheme);
        HttpRequest httpRequest = createKerberosAuthenticationRequest(outboundRequest);
        httpResponse = client.execute(httpHost, httpRequest);
        Header[] headers = httpResponse.getHeaders(SET_COOKIE);
        hadoopAuthCookie = getHadoopAuthCookieValue(headers);
        EntityUtils.consume(httpResponse.getEntity());
        if (hadoopAuthCookie == null) {
            LOG.failedSPNegoAuthn(uri.toString());
            auditor.audit(Action.AUTHENTICATION, uri.toString(), ResourceType.URI, ActionOutcome.FAILURE);
            throw new IOException("SPNego authn failed, can not get hadoop.auth cookie");
        }
    } finally {
        if (httpResponse != null) {
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                entity.getContent().close();
            }
        }

    }
    LOG.successfulSPNegoAuthn(uri.toString());
    auditor.audit(Action.AUTHENTICATION, uri.toString(), ResourceType.URI, ActionOutcome.SUCCESS);
    hadoopAuthCookie = HADOOP_AUTH_EQ + quote(hadoopAuthCookie);
    setAppCookie(hadoopAuthCookie);
    return appCookie;
}

From source file:com.catchnotes.api.VersionedCatchHttpClient.java

private void addAccessTokenToHttpUriRequest(HttpUriRequest httpUriRequest) {
    if (mEncodedAccessToken != null) {
        String query = httpUriRequest.getURI().getQuery();

        if (query == null || query.length() == 0) {
            ((HttpRequestBase) httpUriRequest)
                    .setURI(URI.create(httpUriRequest.getURI() + "?" + mEncodedAccessToken));
        } else {/*from  w ww. j  a  v  a2 s .  co  m*/
            ((HttpRequestBase) httpUriRequest)
                    .setURI(URI.create(httpUriRequest.getURI() + "&" + mEncodedAccessToken));
        }
    }
}

From source file:com.google.android.apps.authenticator.timesync.NetworkTimeProviderTest.java

public void testRequest() throws Exception {
    withHttpRequestThrowing(new IOException("arbitrary"));
    try {/*from  w w  w  . j av  a  2s  .c  om*/
        mProvider.getNetworkTime();
    } catch (Exception expected) {
    }

    ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
    verify(mMockHttpClient).execute(requestCaptor.capture());

    HttpUriRequest request = requestCaptor.getValue();
    assertEquals("HEAD", request.getMethod());
    assertEquals("https://www.google.com", request.getURI().toString());
    MoreAsserts.assertEmpty(Arrays.asList(request.getAllHeaders()));
}

From source file:com.ittm_solutions.ipacore.IpaApi.java

/**
 * Returns the final URI in a redirect chain of a request.
 *
 * @param context/*from   w  ww .  ja  va  2s. com*/
 *            client context as returned by {@code clientContext}
 * @return URI
 */
private static URI currentRedirectUri(HttpClientContext context) {
    HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
    HttpHost currentHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
    URI uri = null;
    try {
        uri = ((currentReq.getURI().isAbsolute()) ? currentReq.getURI()
                : (new URI(currentHost.toURI() + currentReq.getURI())));
    } catch (URISyntaxException e) {
        // this should never happen
        throw new RuntimeException(e);
    }
    return uri;
}

From source file:com.groupon.jenkins.util.HttpPoster.java

private HttpHost getProxy(HttpUriRequest method) throws URIException {

    ProxyConfiguration proxy = Jenkins.getInstance().proxy;
    if (proxy == null)
        return null;

    Proxy p = proxy.createProxy(method.getURI().getHost());
    switch (p.type()) {
    case DIRECT://w  ww. j ava  2  s  .c  o m
        return null;
    case HTTP:
        InetSocketAddress sa = (InetSocketAddress) p.address();
        return new HttpHost(sa.getHostName(), sa.getPort());
    case SOCKS:
    default:
        return null;
    }
}

From source file:com.navercorp.pinpoint.plugin.httpclient4.interceptor.HttpClientExecuteMethodWithHttpUriRequestInterceptor.java

@Override
protected NameIntValuePair<String> getHost(Object[] args) {
    final HttpUriRequest httpUriRequest = getHttpUriRequest(args);
    if (httpUriRequest == null) {
        return null;
    }//from   w ww . ja va2  s .  c  om
    final URI uri = httpUriRequest.getURI();
    return extractHost(uri);
}

From source file:org.craftercms.engine.http.impl.HttpProxyImpl.java

private String getRequestDescription(HttpUriRequest httpRequest) {
    return httpRequest.getMethod() + "[" + httpRequest.getURI() + "]";
}

From source file:org.fcrepo.indexer.JcrXmlRetriever.java

@Override
/**/*from  w  ww.j a va  2 s  .  c  o m*/
 * Retrieve jcr/xml with no binary contents from the repository
 */
public InputStream get() {

    try {
        // make an initial HEAD request and check Link headers for descriptions located elsewhere
        final HttpHead headRequest = new HttpHead(identifier);
        final HttpResponse headResponse = httpClient.execute(headRequest);
        URI descriptionURI = null;
        final Header[] links = headResponse.getHeaders("Link");
        if (links != null) {
            for (Header h : headResponse.getHeaders("Link")) {
                final Link link = Link.valueOf(h.getValue());
                if (link.getRel().equals("describedby")) {
                    descriptionURI = link.getUri();
                    LOGGER.debug("Using URI from Link header: {}", descriptionURI);
                }
            }
        }
        if (descriptionURI == null) {
            descriptionURI = identifier;
        }

        final HttpUriRequest request = new HttpGet(descriptionURI.toString() + "/fcr:export?skipBinary=true");
        LOGGER.debug("Retrieving jcr/xml content from: {}...", request.getURI());
        final HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == SC_OK) {
            return response.getEntity().getContent();
        } else {
            throw new HttpException(response.getStatusLine().getStatusCode() + " : "
                    + EntityUtils.toString(response.getEntity()));
        }
    } catch (IOException | HttpException e) {
        throw propagate(e);
    }
}

From source file:com.cloud.network.nicira.NiciraRestClient.java

private CloseableHttpResponse handleSuccessResponse(final HttpUriRequest request,
        final CloseableHttpResponse response) {
    if (!request.getURI().getPath().contains(loginUrl)) {
        counter.resetExecutionCounter();
    }//from   w  ww  .  j  av a  2  s.  c  o  m
    return response;
}

From source file:lucee.commons.net.http.httpclient.HTTPResponse4Impl.java

public URL getTargetURL() {
    URL start = getURL();//from  w ww.  j  av a 2  s .c o  m

    HttpUriRequest req = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    URI uri = req.getURI();
    String path = uri.getPath();
    String query = uri.getQuery();
    if (!StringUtil.isEmpty(query))
        path += "?" + query;

    URL _url = start;
    try {
        _url = new URL(start.getProtocol(), start.getHost(), start.getPort(), path);
    } catch (MalformedURLException e) {
    }

    return _url;
}