Example usage for org.apache.http.protocol BasicHttpContext getAttribute

List of usage examples for org.apache.http.protocol BasicHttpContext getAttribute

Introduction

In this page you can find the example usage for org.apache.http.protocol BasicHttpContext getAttribute.

Prototype

public Object getAttribute(String str) 

Source Link

Usage

From source file:org.ops4j.pax.web.itest.base.HttpTestClient.java

public HttpResponse getHttpResponse(String path, boolean authenticate, BasicHttpContext basicHttpContext,
        boolean async) throws IOException, KeyManagementException, UnrecoverableKeyException,
        NoSuchAlgorithmException, KeyStoreException, CertificateException, AuthenticationException,
        InterruptedException, ExecutionException {
    HttpGet httpget = null;/*from  w  w w  .  j ava  2  s .c o m*/

    HttpHost targetHost = getHttpHost(path);

    BasicHttpContext localcontext = basicHttpContext == null ? new BasicHttpContext() : basicHttpContext;

    httpget = new HttpGet(path);
    httpget.addHeader("Accept-Language", "en");
    LOG.info("calling remote {} ...", path);
    HttpResponse response = null;
    if (!authenticate && basicHttpContext == null) {
        if (localcontext.getAttribute(ClientContext.AUTH_CACHE) != null) {
            localcontext.removeAttribute(ClientContext.AUTH_CACHE);
        }
        if (!async) {
            response = httpclient.execute(httpget, context);
        } else {
            Future<HttpResponse> future = httpAsyncClient.execute(httpget, context, null);
            response = future.get();
        }
    } else {
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);

        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
        httpget.addHeader(basicAuth.authenticate(creds, httpget, localcontext));
        httpget.addHeader("Accept-Language", "en-us;q=0.8,en;q=0.5");
        if (!async) {
            response = httpclient.execute(targetHost, httpget, localcontext);
        } else {
            Future<HttpResponse> future = httpAsyncClient.execute(targetHost, httpget, localcontext, null);
            response = future.get();
        }
    }

    LOG.info("... responded with: {}", response.getStatusLine().getStatusCode());
    return response;
}

From source file:org.ops4j.pax.web.itest.jetty.WarFormAuthIntegrationTest.java

private BasicHttpContext testFormWebPath(String path, String user, String passwd, int httpRC)
        throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpHost targetHost = new HttpHost("localhost", 8181, "http");
    BasicHttpContext localcontext = new BasicHttpContext();

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    formparams.add(new BasicNameValuePair("j_username", user));
    formparams.add(new BasicNameValuePair("j_password", passwd));
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
    HttpPost httppost = new HttpPost(path);
    httppost.setEntity(entity);/*from ww w .j  ava  2  s .c o  m*/

    HttpResponse response = httpclient.execute(targetHost, httppost, localcontext);

    CookieOrigin cookieOrigin = (CookieOrigin) localcontext.getAttribute(ClientContext.COOKIE_ORIGIN);
    CookieSpec cookieSpec = (CookieSpec) localcontext.getAttribute(ClientContext.COOKIE_SPEC);

    assertEquals("HttpResponseCode", httpRC, response.getStatusLine().getStatusCode());

    return localcontext;
}

From source file:org.ops4j.pax.web.itest.base.client.HttpComponentsWrapper.java

private HttpResponse getHttpResponse(String path, boolean authenticate, BasicHttpContext basicHttpContext,
        boolean async) throws IOException, KeyManagementException, UnrecoverableKeyException,
        NoSuchAlgorithmException, KeyStoreException, CertificateException, AuthenticationException,
        InterruptedException, ExecutionException {

    HttpHost targetHost = getHttpHost(path);

    BasicHttpContext localcontext = basicHttpContext == null ? new BasicHttpContext() : basicHttpContext;

    HttpGet httpget = new HttpGet(path);
    for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
        LOG.info("adding request-header: {}={}", entry.getKey(), entry.getValue());
        httpget.addHeader(entry.getKey(), entry.getValue());
    }//from  w  ww  . ja  va 2  s  .c  o  m

    LOG.info("calling remote {} ...", path);
    HttpResponse response = null;
    if (!authenticate && basicHttpContext == null) {
        if (localcontext.getAttribute(ClientContext.AUTH_CACHE) != null) {
            localcontext.removeAttribute(ClientContext.AUTH_CACHE);
        }
        if (!async) {
            response = httpclient.execute(httpget, context);
        } else {
            Future<HttpResponse> future = httpAsyncClient.execute(httpget, context, null);
            response = future.get();
        }
    } else {
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth
        // cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);

        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
        httpget.addHeader(basicAuth.authenticate(creds, httpget, localcontext));
        if (!async) {
            response = httpclient.execute(targetHost, httpget, localcontext);
        } else {
            Future<HttpResponse> future = httpAsyncClient.execute(targetHost, httpget, localcontext, null);
            response = future.get();
        }
    }

    LOG.info("... responded with: {}", response.getStatusLine().getStatusCode());
    return response;
}

From source file:org.elasticsearch.xpack.security.authc.saml.SamlAuthenticationIT.java

/**
 * Navigates to the login page on the local (in memory) HTTP UI.
 *
 * @return A URI to which the "login form" should be submitted.
 *//*from  w  ww.java 2 s . c  om*/
private URI goToLoginPage(CloseableHttpClient client, BasicHttpContext context) throws IOException {
    HttpGet login = new HttpGet(getUrl(SP_LOGIN_PATH));
    String target = execute(client, login, context, response -> {
        assertHttpOk(response.getStatusLine());
        return getFormTarget(response.getEntity().getContent());
    });

    assertThat("Cannot find form target", target, Matchers.notNullValue());
    assertThat("Target must be an absolute path", target, startsWith("/"));
    final Object host = context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
    assertThat(host, instanceOf(HttpHost.class));

    final String uri = ((HttpHost) host).toURI() + target;
    return toUri(uri);
}

From source file:ste.web.http.beanshell.BugFreeBeanShellUtils.java

@Test
public void setRequestAttributes() throws Exception {
    Interpreter i = new Interpreter();
    BasicHttpContext c = new BasicHttpContext();

    try {//w w w.j  av  a 2s.  com
        BeanShellUtils.setVariablesAttributes(null, (BasicHttpContext) null);
        fail("illegal argument exception expected");
    } catch (IllegalArgumentException e) {
        //
        // OK
        //
    }

    try {
        BeanShellUtils.setVariablesAttributes(null, c);
        fail("illegal argument exception expected");
    } catch (IllegalArgumentException e) {
        //
        // OK
        //
    }

    try {
        BeanShellUtils.setVariablesAttributes(i, (BasicHttpContext) null);
        fail("illegal argument exception expected");
    } catch (IllegalArgumentException e) {
        //
        // OK
        //
    }

    i.eval("one=1; two=2;");
    BeanShellUtils.setVariablesAttributes(i, c);
    then(c.getAttribute("one")).isEqualTo(1);
    then(c.getAttribute("two")).isEqualTo(2);
    then(c.getAttribute("three")).isNull(); // just to make sure it
                                            // does not always return
                                            // the same
}