Search in sources :

Example 16 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project cloudstack by apache.

the class HttpClientHelper method createHttpClient.

public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
    final BasicCookieStore cookieStore = new BasicCookieStore();
    return HttpClientBuilder.create().setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry)).setRedirectStrategy(new LaxRedirectStrategy()).setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setMaxRedirects(maxRedirects).build()).setDefaultCookieStore(cookieStore).setRetryHandler(new StandardHttpRequestRetryHandler()).build();
}
Also used : ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler)

Example 17 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project wildfly by wildfly.

the class SSOTestBase method executeFormAuthSingleSignOnTest.

/**
     * Test single sign-on across two web apps using form based auth
     *
     * @throws Exception
     */
public static void executeFormAuthSingleSignOnTest(URL serverA, URL serverB, Logger log) throws Exception {
    URL warA1 = new URL(serverA, "/war1/");
    URL warB2 = new URL(serverB, "/war2/");
    // Start by accessing the secured index.html of war1
    CookieStore store = new BasicCookieStore();
    HttpClient httpclient = TestHttpClientUtils.promiscuousCookieHttpClientBuilder().setDefaultCookieStore(store).disableRedirectHandling().build();
    try {
        checkAccessDenied(httpclient, warA1 + "index.html");
        log.debug("Saw JSESSIONID=" + getSessionIdValueFromState(store));
        // Submit the login form
        executeFormLogin(httpclient, warA1);
        String ssoID = processSSOCookie(store, serverA.toString(), serverB.toString());
        log.debug("Saw JSESSIONIDSSO=" + ssoID);
        // Now try getting the war2 index using the JSESSIONIDSSO cookie
        log.debug("Prepare /war2/index.html get");
        checkAccessAllowed(httpclient, warB2 + "index.html");
        // Access a secured servlet that calls a secured ejb in war2 to test
        // propagation of the SSO identity to the ejb container.
        checkAccessAllowed(httpclient, warB2 + "EJBServlet");
        // Now try logging out of war2
        executeLogout(httpclient, warB2);
    } finally {
        HttpClientUtils.closeQuietly(httpclient);
    }
    try {
        // Reset Http client
        httpclient = HttpClients.createDefault();
        // Try accessing war1 again
        checkAccessDenied(httpclient, warA1 + "index.html");
        // Try accessing war2 again
        checkAccessDenied(httpclient, warB2 + "index.html");
    } finally {
        HttpClientUtils.closeQuietly(httpclient);
    }
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpClient(org.apache.http.client.HttpClient) URL(java.net.URL)

Example 18 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project undertow by undertow-io.

the class SsoTestCase method testSsoSuccess.

@Test
public void testSsoSuccess() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/test1");
    HttpResponse result = client.execute(get);
    assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode());
    Header[] values = result.getHeaders(WWW_AUTHENTICATE.toString());
    String header = getAuthHeader(BASIC, values);
    assertEquals(BASIC + " realm=\"Test Realm\"", header);
    HttpClientUtils.readResponse(result);
    get = new HttpGet(DefaultServer.getDefaultServerURL() + "/test1");
    get.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString("userOne:passwordOne".getBytes(), false));
    result = client.execute(get);
    assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
    values = result.getHeaders("ProcessedBy");
    assertEquals(1, values.length);
    assertEquals("ResponseHandler", values[0].getValue());
    HttpClientUtils.readResponse(result);
    assertSingleNotificationType(SecurityNotification.EventType.AUTHENTICATED);
    get = new HttpGet(DefaultServer.getDefaultServerURL() + "/test2");
    result = client.execute(get);
    assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
    values = result.getHeaders("ProcessedBy");
    assertEquals(1, values.length);
    assertEquals("ResponseHandler", values[0].getValue());
    HttpClientUtils.readResponse(result);
    assertSingleNotificationType(SecurityNotification.EventType.AUTHENTICATED);
    //now test that logout will invalidate the SSO session
    get = new HttpGet(DefaultServer.getDefaultServerURL() + "/test1?logout=true");
    get.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString("userOne:passwordOne".getBytes(), false));
    result = client.execute(get);
    assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
    values = result.getHeaders("ProcessedBy");
    assertEquals(1, values.length);
    assertEquals("ResponseHandler", values[0].getValue());
    HttpClientUtils.readResponse(result);
    assertNotifiactions(SecurityNotification.EventType.AUTHENTICATED, SecurityNotification.EventType.LOGGED_OUT);
    get = new HttpGet(DefaultServer.getDefaultServerURL() + "/test2");
    result = client.execute(get);
    assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode());
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 19 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project undertow by undertow-io.

the class URLRewritingSessionTestCase method testURLRewritingWithQueryParameters.

@Test
public void testURLRewritingWithQueryParameters() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath?a=b;c");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String url = HttpClientUtils.readResponse(result);
        Header[] header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
        Assert.assertEquals("b;c", result.getHeaders("a")[0].getValue());
        get = new HttpGet(url);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        url = HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("1", header[0].getValue());
        Assert.assertEquals("b;c", result.getHeaders("a")[0].getValue());
        get = new HttpGet(url);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        url = HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("2", header[0].getValue());
        Assert.assertEquals("b;c", result.getHeaders("a")[0].getValue());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpString(io.undertow.util.HttpString) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 20 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project undertow by undertow-io.

the class URLRewritingSessionTestCase method testURLRewriting.

@Test
public void testURLRewriting() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath;foo=bar");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String url = HttpClientUtils.readResponse(result);
        Header[] header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
        get = new HttpGet(url);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        url = HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("1", header[0].getValue());
        get = new HttpGet(url);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        url = HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("2", header[0].getValue());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpString(io.undertow.util.HttpString) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)33 CookieStore (org.apache.http.client.CookieStore)12 HttpResponse (org.apache.http.HttpResponse)10 Test (org.junit.Test)10 IOException (java.io.IOException)9 HttpGet (org.apache.http.client.methods.HttpGet)9 Cookie (org.apache.http.cookie.Cookie)9 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)7 Header (org.apache.http.Header)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6 HttpContext (org.apache.http.protocol.HttpContext)6 TestHttpClient (io.undertow.testutils.TestHttpClient)5 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)5 HttpString (io.undertow.util.HttpString)4 URL (java.net.URL)4 HttpEntity (org.apache.http.HttpEntity)4 HttpClient (org.apache.http.client.HttpClient)4 Executor (org.apache.http.client.fluent.Executor)4 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)4