Example usage for org.apache.http.client.protocol HttpClientContext getAttribute

List of usage examples for org.apache.http.client.protocol HttpClientContext getAttribute

Introduction

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

Prototype

public Object getAttribute(String str) 

Source Link

Usage

From source file:org.opensaml.security.httpclient.HttpClientSecuritySupport.java

/**
 * Check that trust engine evaluation of the server TLS credential was actually performed when the 
 * scheme is HTTPS.//from   www .jav  a 2s  . c om
 * 
 * @param context the current HTTP context instance in use
 * @param scheme the HTTP request scheme
 * @throws SSLPeerUnverifiedException thrown if the TLS credential was not actually evaluated by the trust engine
 */
public static void checkTLSCredentialEvaluated(@Nonnull final HttpClientContext context,
        @Nonnull final String scheme) throws SSLPeerUnverifiedException {
    if (context.getAttribute(CONTEXT_KEY_TRUST_ENGINE) != null && "https".equalsIgnoreCase(scheme)) {
        if (context.getAttribute(CONTEXT_KEY_SERVER_TLS_CREDENTIAL_TRUSTED) == null) {
            LOG.warn("Configured TLS trust engine was not used to verify server TLS credential, "
                    + "the appropriate socket factory was likely not configured");
            throw new SSLPeerUnverifiedException(
                    "Evaluation of server TLS credential with configured TrustEngine was not performed");
        }
    }
}

From source file:org.opensaml.security.httpclient.HttpClientSecuritySupport.java

/**
 * Set the supplied attribute value in the client context.
 * //from  ww  w.  j  a v  a  2s  .c  o m
 * @param context the client context instance
 * @param attributeName the context attribute name to 
 * @param attributeValue the context attribute value to set, may be null
 * @param replace whether a non-null argument value should replace an existing context value
 */
public static void setContextValue(@Nonnull final HttpClientContext context,
        @Nonnull final String attributeName, @Nullable Object attributeValue, boolean replace) {
    if (attributeValue == null) {
        return;
    }
    Constraint.isNotNull(context, "HttpClientContext was null");
    Constraint.isNotNull(attributeName, "Context attribute name was null");

    if (replace || context.getAttribute(attributeName) == null) {
        context.setAttribute(attributeName, attributeValue);
    }
}

From source file:com.ibm.team.build.internal.hjplugin.util.HttpUtils.java

@SuppressWarnings("unused")
private static void printCookies(HttpClientContext httpContext) {
    if (LOGGER.isLoggable(Level.FINER)) {
        LOGGER.finer("Cookies:"); //$NON-NLS-1$
        CookieStore cookieStore = (CookieStore) httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
        List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("\tNone"); //$NON-NLS-1$
        } else {/*  www . j  a  va 2 s  . c o  m*/
            for (int i = 0; i < cookies.size(); i++) {
                LOGGER.finer("\t- " + cookies.get(i).toString()); //$NON-NLS-1$
            }
        }
    }
}

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

/**
 * Returns the final URI in a redirect chain of a request.
 *
 * @param context/*ww  w  . j  a v  a2  s  .c  o  m*/
 *            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:org.opensaml.security.httpclient.HttpClientSecuritySupportTest.java

@Test
public void testMarshalNullSecurityParameters() {
    HttpClientContext context = HttpClientContext.create();

    HttpClientSecuritySupport.marshalSecurityParameters(context, null, false);

    Assert.assertNull(context.getCredentialsProvider());
    Assert.assertNull(context.getAttribute(CONTEXT_KEY_TRUST_ENGINE));
    Assert.assertNull(context.getAttribute(CONTEXT_KEY_CRITERIA_SET));
    Assert.assertNull(context.getAttribute(CONTEXT_KEY_TLS_PROTOCOLS));
    Assert.assertNull(context.getAttribute(CONTEXT_KEY_TLS_CIPHER_SUITES));
    Assert.assertNull(context.getAttribute(CONTEXT_KEY_CLIENT_TLS_CREDENTIAL));
    Assert.assertNull(context.getAttribute(CONTEXT_KEY_HOSTNAME_VERIFIER));
}

From source file:org.opensaml.security.httpclient.HttpClientSecuritySupportTest.java

@Test
public void testSetContextValue() {
    String attribName = "MyAttrib";
    HttpClientContext context = null;

    // Empty context
    context = HttpClientContext.create();
    HttpClientSecuritySupport.setContextValue(context, attribName, "foo", false);
    Assert.assertEquals(context.getAttribute(attribName), "foo");

    // Empty context, null value
    context = HttpClientContext.create();
    HttpClientSecuritySupport.setContextValue(context, attribName, null, false);
    Assert.assertNull(context.getAttribute(attribName));

    // Don't replace existing
    context = HttpClientContext.create();
    context.setAttribute(attribName, "bar");
    HttpClientSecuritySupport.setContextValue(context, attribName, "foo", false);
    Assert.assertEquals(context.getAttribute(attribName), "bar");

    // Replace existing
    context = HttpClientContext.create();
    context.setAttribute(attribName, "bar");
    HttpClientSecuritySupport.setContextValue(context, attribName, "foo", true);
    Assert.assertEquals(context.getAttribute(attribName), "foo");

    // Don't replace because null value
    context = HttpClientContext.create();
    context.setAttribute(attribName, "bar");
    HttpClientSecuritySupport.setContextValue(context, attribName, null, true);
    Assert.assertEquals(context.getAttribute(attribName), "bar");

    try {/* ww  w .j  a  v  a  2 s . c o  m*/
        HttpClientSecuritySupport.setContextValue(null, attribName, "foo", false);
        Assert.fail("Null context value");
    } catch (ConstraintViolationException e) {
        // Expected
    }

    try {
        context = HttpClientContext.create();
        HttpClientSecuritySupport.setContextValue(context, null, "foo", false);
        Assert.fail("Null attribute name");
    } catch (ConstraintViolationException e) {
        // Expected
    }

}

From source file:org.callimachusproject.client.HttpClientRedirectTest.java

@Test
public void testAbsoluteRequestURIWithFragment() throws Exception {
    register("*", new SimpleService());
    final HttpHost target = getServerHttp();

    final URI uri = new URIBuilder().setHost(target.getHostName()).setPort(target.getPort())
            .setScheme(target.getSchemeName()).setPath("/stuff").setFragment("blahblah").build();

    final HttpGet httpget = new HttpGet(uri);
    final HttpClientContext context = HttpClientContext.create();

    final HttpResponse response = this.httpclient.execute(httpget, context);
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());

    final HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
    Assert.assertEquals("/stuff", request.getRequestLine().getUri());

    final URI location = getHttpLocation(httpget, context);
    Assert.assertEquals(uri, location);//  w  w  w. j  av a2  s  .  c o  m
}

From source file:org.opensaml.soap.client.http.AbstractPipelineHttpSOAPClient.java

/**
 * Check that trust engine evaluation of the server TLS credential was actually performed.
 * /*w  ww.j ava2  s. c  o  m*/
 * @param context the current HTTP context instance in use
 * @param request the HTTP URI request
 * @throws SSLPeerUnverifiedException thrown if the TLS credential was not actually evaluated by the trust engine
 */
protected void checkTLSCredentialTrusted(@Nonnull final HttpClientContext context,
        @Nonnull final HttpUriRequest request) throws SSLPeerUnverifiedException {
    if (context.getAttribute(HttpClientSecurityConstants.CONTEXT_KEY_TRUST_ENGINE) != null
            && "https".equalsIgnoreCase(request.getURI().getScheme())) {
        if (context
                .getAttribute(HttpClientSecurityConstants.CONTEXT_KEY_SERVER_TLS_CREDENTIAL_TRUSTED) == null) {
            log.warn("Configured TLS trust engine was not used to verify server TLS credential, "
                    + "the appropriate socket factory was likely not configured");
            throw new SSLPeerUnverifiedException(
                    "Evaluation of server TLS credential with configured TrustEngine was not performed");
        }
    }
}

From source file:org.opensaml.security.httpclient.HttpClientSecuritySupportTest.java

@Test
public void testMarshalSecurityParametersEmptyContext() {
    HttpClientContext context = HttpClientContext.create();

    HttpClientSecurityParameters params = new HttpClientSecurityParameters();
    params.setCredentialsProvider(new BasicCredentialsProvider());
    params.setTLSTrustEngine(new MockTrustEngine());
    params.setTLSCriteriaSet(new CriteriaSet());
    params.setTLSProtocols(Lists.newArrayList("foo"));
    params.setTLSCipherSuites(Lists.newArrayList("foo"));
    params.setClientTLSCredential(new BasicX509Credential(cert));
    params.setHostnameVerifier(new StrictHostnameVerifier());

    HttpClientSecuritySupport.marshalSecurityParameters(context, params, false);

    Assert.assertSame(context.getCredentialsProvider(), params.getCredentialsProvider());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_TRUST_ENGINE), params.getTLSTrustEngine());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_CRITERIA_SET), params.getTLSCriteriaSet());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_TLS_PROTOCOLS), params.getTLSProtocols());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_TLS_CIPHER_SUITES), params.getTLSCipherSuites());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_CLIENT_TLS_CREDENTIAL), params.getClientTLSCredential());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_HOSTNAME_VERIFIER), params.getHostnameVerifier());
}

From source file:org.opensaml.security.httpclient.HttpClientSecuritySupportTest.java

@Test
public void testMarshalSecurityParametersWithReplacement() {
    HttpClientContext context = HttpClientContext.create();

    context.setCredentialsProvider(new BasicCredentialsProvider());
    context.setAttribute(CONTEXT_KEY_TRUST_ENGINE, new MockTrustEngine());
    context.setAttribute(CONTEXT_KEY_CRITERIA_SET, new CriteriaSet());
    context.setAttribute(CONTEXT_KEY_TLS_PROTOCOLS, Lists.newArrayList("foo"));
    context.setAttribute(CONTEXT_KEY_TLS_CIPHER_SUITES, Lists.newArrayList("foo"));
    context.setAttribute(CONTEXT_KEY_CLIENT_TLS_CREDENTIAL, new BasicX509Credential(cert));
    context.setAttribute(CONTEXT_KEY_HOSTNAME_VERIFIER, new StrictHostnameVerifier());

    HttpClientSecurityParameters params = new HttpClientSecurityParameters();
    params.setCredentialsProvider(new BasicCredentialsProvider());
    params.setTLSTrustEngine(new MockTrustEngine());
    params.setTLSCriteriaSet(new CriteriaSet());
    params.setTLSProtocols(Lists.newArrayList("foo"));
    params.setTLSCipherSuites(Lists.newArrayList("foo"));
    params.setClientTLSCredential(new BasicX509Credential(cert));
    params.setHostnameVerifier(new StrictHostnameVerifier());

    HttpClientSecuritySupport.marshalSecurityParameters(context, params, true);

    Assert.assertSame(context.getCredentialsProvider(), params.getCredentialsProvider());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_TRUST_ENGINE), params.getTLSTrustEngine());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_CRITERIA_SET), params.getTLSCriteriaSet());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_TLS_PROTOCOLS), params.getTLSProtocols());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_TLS_CIPHER_SUITES), params.getTLSCipherSuites());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_CLIENT_TLS_CREDENTIAL), params.getClientTLSCredential());
    Assert.assertSame(context.getAttribute(CONTEXT_KEY_HOSTNAME_VERIFIER), params.getHostnameVerifier());
}