Search in sources :

Example 36 with CookieManager

use of java.net.CookieManager in project scout.rt by eclipse.

the class MultiSessionCookieStoreInstaller method install.

public void install() {
    CookieHandler cookieHandler = CookieHandler.getDefault();
    if (!checkMultiSessionCookieStoreAlreadyInstalled(cookieHandler)) {
        // Install MultiSessionCookieStore
        m_oldCookieHandler = cookieHandler;
        m_newCookieHandler = new CookieManager(BEANS.get(MultiSessionCookieStore.class), CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(m_newCookieHandler);
        LOG.info("Successfully installed {}", cookieHandlerToString(m_newCookieHandler));
    }
}
Also used : CookieManager(java.net.CookieManager) CookieHandler(java.net.CookieHandler)

Example 37 with CookieManager

use of java.net.CookieManager in project scout.rt by eclipse.

the class MultiSessionCookieStoreInstallerTest method checkOtherCookieStoreAlreadyInstalled.

@Test
public void checkOtherCookieStoreAlreadyInstalled() {
    // Other CookieHandler implementation is installed -> OK
    CookieHandler cookieHandler = Mockito.mock(CookieHandler.class);
    s_installer.checkMultiSessionCookieStoreAlreadyInstalled(cookieHandler);
    // Other CookieManager implementation is installed -> OK
    CookieManager cookieManager = Mockito.mock(CookieManager.class);
    s_installer.checkMultiSessionCookieStoreAlreadyInstalled(cookieManager);
    // Other CookieManager with other CookieStore implementation is installed -> OK
    Mockito.when(cookieManager.getCookieStore()).thenReturn(Mockito.mock(CookieStore.class));
    s_installer.checkMultiSessionCookieStoreAlreadyInstalled(cookieManager);
}
Also used : CookieStore(java.net.CookieStore) CookieManager(java.net.CookieManager) CookieHandler(java.net.CookieHandler) Test(org.junit.Test)

Example 38 with CookieManager

use of java.net.CookieManager in project opacclient by opacapp.

the class HttpClientFactory method getOkHttpClientBuilder.

protected OkHttpClient.Builder getOkHttpClientBuilder(boolean customssl, boolean tls_only, boolean allCipherSuites) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    builder.cookieJar(new JavaNetCookieJar(cookieManager));
    builder.addNetworkInterceptor(new CustomRedirectInterceptor());
    builder.connectTimeout(60, TimeUnit.SECONDS);
    builder.readTimeout(60, TimeUnit.SECONDS);
    builder.writeTimeout(60, TimeUnit.SECONDS);
    if (customssl && ssl_store_path != null) {
        try {
            if (trust_store == null) {
                trust_store = getKeyStore();
            }
            X509TrustManager trustManager = new AdditionalKeyStoresSSLSocketFactory.AdditionalKeyStoresTrustManager(trust_store);
            SSLSocketFactory sf = AdditionalKeyStoresSSLSocketFactory.createForOkHttp(trustManager);
            if (allCipherSuites) {
                sf = new AllCiphersProxySocketFactory(sf);
            }
            sf = new TLS12ProxySocketFactory(sf);
            builder.sslSocketFactory(sf, trustManager);
            List<ConnectionSpec> connectionSpecs = new ArrayList<ConnectionSpec>();
            connectionSpecs.add(ConnectionSpec.MODERN_TLS);
            connectionSpecs.add(new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS).allEnabledCipherSuites().build());
            if (!tls_only) {
                connectionSpecs.add(new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS).tlsVersions(TlsVersion.SSL_3_0, TlsVersion.TLS_1_0).allEnabledCipherSuites().build());
            } else if (allCipherSuites) {
                connectionSpecs.add(new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS).allEnabledCipherSuites().build());
            }
            connectionSpecs.add(ConnectionSpec.CLEARTEXT);
            builder.connectionSpecs(connectionSpecs);
            return builder;
        } catch (Exception e) {
            e.printStackTrace();
            return builder;
        }
    } else {
        try {
            X509TrustManager trustManager = getSystemDefaultTrustManager();
            SSLSocketFactory socketFactory = getSystemDefaultSSLSocketFactory(trustManager);
            builder.sslSocketFactory(new TLS12ProxySocketFactory(socketFactory), trustManager);
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ignored) {
        }
        return builder;
    }
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) ConnectionSpec(okhttp3.ConnectionSpec) RegistryBuilder(org.apache.http.config.RegistryBuilder) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) ProtocolException(org.apache.http.ProtocolException) URISyntaxException(java.net.URISyntaxException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CircularRedirectException(org.apache.http.client.CircularRedirectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) KeyManagementException(java.security.KeyManagementException) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CookieManager(java.net.CookieManager)

Example 39 with CookieManager

use of java.net.CookieManager in project okhttp by square.

the class CookiesTest method acceptOriginalServerMatchesSubdomain.

@Test
public void acceptOriginalServerMatchesSubdomain() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
    HttpUrl url = HttpUrl.get("https://www.squareup.com/");
    cookieJar.saveFromResponse(url, asList(Cookie.parse(url, "a=android; Domain=squareup.com")));
    List<Cookie> actualCookies = cookieJar.loadForRequest(url);
    assertThat(actualCookies.size()).isEqualTo(1);
    assertThat(actualCookies.get(0).name()).isEqualTo("a");
    assertThat(actualCookies.get(0).value()).isEqualTo("android");
}
Also used : HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

Example 40 with CookieManager

use of java.net.CookieManager in project okhttp by square.

the class CookiesTest method acceptOriginalServerMatchesRfc2965Dot.

@Test
public void acceptOriginalServerMatchesRfc2965Dot() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
    HttpUrl url = HttpUrl.get("https://www.squareup.com/");
    cookieJar.saveFromResponse(url, asList(Cookie.parse(url, "a=android; Domain=.squareup.com")));
    List<Cookie> actualCookies = cookieJar.loadForRequest(url);
    assertThat(actualCookies.size()).isEqualTo(1);
    assertThat(actualCookies.get(0).name()).isEqualTo("a");
    assertThat(actualCookies.get(0).value()).isEqualTo("android");
}
Also used : HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

Aggregations

CookieManager (java.net.CookieManager)162 URI (java.net.URI)89 HttpCookie (java.net.HttpCookie)82 CookieStore (java.net.CookieStore)49 MockResponse (com.google.mockwebserver.MockResponse)20 List (java.util.List)20 ArrayList (java.util.ArrayList)18 MockWebServer (com.google.mockwebserver.MockWebServer)15 IOException (java.io.IOException)13 HashMap (java.util.HashMap)12 Test (org.junit.jupiter.api.Test)12 Test (org.junit.Test)11 RecordedRequest (com.google.mockwebserver.RecordedRequest)8 MockResponse (mockwebserver3.MockResponse)8 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 HttpURLConnection (java.net.HttpURLConnection)6 URL (java.net.URL)6 RecordedRequest (mockwebserver3.RecordedRequest)5 JavaNetCookieJar (okhttp3.JavaNetCookieJar)4