Search in sources :

Example 16 with CookieStore

use of org.apache.http.client.CookieStore in project nanohttpd by NanoHttpd.

the class CookieIntegrationTest method testCookieSentBackToClient.

@Test
public void testCookieSentBackToClient() throws Exception {
    this.testServer.cookiesToSend.add(new Cookie("name", "value", 30));
    HttpGet httpget = new HttpGet("http://localhost:8192/");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    this.httpclient.execute(httpget, responseHandler);
    CookieStore cookies = this.httpclient.getCookieStore();
    assertEquals(1, cookies.getCookies().size());
    assertEquals("name", cookies.getCookies().get(0).getName());
    assertEquals("value", cookies.getCookies().get(0).getValue());
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Cookie(org.nanohttpd.protocols.http.content.Cookie) CookieStore(org.apache.http.client.CookieStore) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Test(org.junit.Test)

Example 17 with CookieStore

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

the class ServletSessionTestCase method testSessionConfigNoCookies.

@Test
public void testSessionConfigNoCookies() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new CookieStore() {

        @Override
        public void addCookie(Cookie cookie) {
        }

        @Override
        public List<Cookie> getCookies() {
            return Collections.EMPTY_LIST;
        }

        @Override
        public boolean clearExpired(Date date) {
            return false;
        }

        @Override
        public void clear() {
        }
    });
    try {
        HttpResponse result = client.execute(new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa/b;foo=bar"));
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("1", response);
        String url = result.getHeaders("url")[0].getValue();
        result = client.execute(new HttpGet(url));
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        url = result.getHeaders("url")[0].getValue();
        Assert.assertEquals("2", response);
        result = client.execute(new HttpGet(url));
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("3", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Cookie(org.apache.http.cookie.Cookie) CookieStore(org.apache.http.client.CookieStore) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) List(java.util.List) Date(java.util.Date) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 18 with CookieStore

use of org.apache.http.client.CookieStore in project wildfly by wildfly.

the class SSOTestBase method executeFormAuthSSOTimeoutTest.

/**
     * Test single sign-on across two web apps using form based auth.
     *
     * Test that after session timeout SSO is destroyed.
     *
     * @throws Exception
     */
public static void executeFormAuthSSOTimeoutTest(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);
        // After login I should still have access + set session timeout to 5 seconds
        checkAccessAllowed(httpclient, warA1 + "set_session_timeout.jsp");
        // Also access to war2 should be granted + set session timeout to 5 seconds
        checkAccessAllowed(httpclient, warB2 + "set_session_timeout.jsp");
        // wait 5 seconds session timeout + 1 seconds reserve
        Thread.sleep((5 + 1) * 1000);
        // After timeout I should be not able to access the app
        checkAccessDenied(httpclient, warA1 + "index.html");
        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 19 with CookieStore

use of org.apache.http.client.CookieStore in project wildfly by wildfly.

the class SSOTestBase method executeNoAuthSingleSignOnTest.

public static void executeNoAuthSingleSignOnTest(URL serverA, URL serverB, Logger log) throws Exception {
    URL warA1 = new URL(serverA, "/war1/");
    URL warB2 = new URL(serverB + "/war2/");
    URL warB6 = new URL(serverB + "/war6/");
    // Start by accessing the secured index.html of war1
    CookieStore store = new BasicCookieStore();
    HttpClient httpclient = TestHttpClientUtils.promiscuousCookieHttpClientBuilder().setDefaultCookieStore(store).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");
        // Do the same test on war6 to test SSO auth replication with no auth
        // configured war
        checkAccessAllowed(httpclient, warB6 + "index.html");
        checkAccessAllowed(httpclient, warB2 + "EJBServlet");
    } 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 20 with CookieStore

use of org.apache.http.client.CookieStore in project XobotOS by xamarin.

the class ResponseProcessCookies method process.

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    // Obtain cookie store
    CookieStore cookieStore = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
    if (cookieStore == null) {
        this.log.info("Cookie store not available in HTTP context");
        return;
    }
    // Obtain actual CookieSpec instance
    CookieSpec cookieSpec = (CookieSpec) context.getAttribute(ClientContext.COOKIE_SPEC);
    if (cookieSpec == null) {
        this.log.info("CookieSpec not available in HTTP context");
        return;
    }
    // Obtain actual CookieOrigin instance
    CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(ClientContext.COOKIE_ORIGIN);
    if (cookieOrigin == null) {
        this.log.info("CookieOrigin not available in HTTP context");
        return;
    }
    HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
    processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    // see if the cookie spec supports cookie versioning.
    if (cookieSpec.getVersion() > 0) {
        // process set-cookie2 headers.
        // Cookie2 will replace equivalent Cookie instances
        it = response.headerIterator(SM.SET_COOKIE2);
        processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    }
}
Also used : CookieStore(org.apache.http.client.CookieStore) CookieSpec(org.apache.http.cookie.CookieSpec) HeaderIterator(org.apache.http.HeaderIterator) CookieOrigin(org.apache.http.cookie.CookieOrigin)

Aggregations

CookieStore (org.apache.http.client.CookieStore)25 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)12 IOException (java.io.IOException)7 HttpResponse (org.apache.http.HttpResponse)7 HttpGet (org.apache.http.client.methods.HttpGet)7 HttpHost (org.apache.http.HttpHost)6 CookieOrigin (org.apache.http.cookie.CookieOrigin)6 CookieSpec (org.apache.http.cookie.CookieSpec)6 ArrayList (java.util.ArrayList)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)5 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)5 Cookie (org.apache.http.cookie.Cookie)5 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)5 Test (org.junit.Test)5 URI (java.net.URI)4 Date (java.util.Date)4 Header (org.apache.http.Header)4 HttpEntity (org.apache.http.HttpEntity)4 HttpClient (org.apache.http.client.HttpClient)4 HttpContext (org.apache.http.protocol.HttpContext)4