List of usage examples for com.squareup.okhttp OkHttpClient getCookieHandler
public CookieHandler getCookieHandler()
From source file:com.ayuget.redface.util.UserUtils.java
License:Apache License
/** * Ugly method to fetch a user id from stored cookies *///from w w w.j a v a 2 s .co m public static Optional<Integer> getLoggedInUserId(User user, OkHttpClient httpClient) { CookieManager cookieManager = (CookieManager) httpClient.getCookieHandler(); CookieStore cookieStore = cookieManager.getCookieStore(); for (HttpCookie cookie : cookieStore.getCookies()) { if (cookie.getName().equals("md_id")) { return Optional.of(Integer.valueOf(cookie.getValue())); } } return Optional.absent(); }
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClientsTest.java
License:Apache License
private void assertThatClientsAreEqual(OkHttpClient clientA, OkHttpClient clientB) { assertThat(clientA.getDispatcher()).isEqualTo(clientB.getDispatcher()); assertThat(clientA.getProxy()).isEqualTo(clientB.getProxy()); assertThat(clientA.getProtocols()).isEqualTo(clientB.getProtocols()); assertThat(clientA.getConnectionSpecs()).isEqualTo(clientB.getConnectionSpecs()); assertThat(clientA.getProxySelector()).isEqualTo(clientB.getProxySelector()); assertThat(clientA.getCookieHandler()).isEqualTo(clientB.getCookieHandler()); assertThat(clientA.getCache()).isEqualTo(clientB.getCache()); assertThat(clientA.getSocketFactory()).isEqualTo(clientB.getSocketFactory()); assertThat(clientA.getSslSocketFactory()).isEqualTo(clientB.getSslSocketFactory()); assertThat(clientA.getHostnameVerifier()).isEqualTo(clientB.getHostnameVerifier()); assertThat(clientA.getCertificatePinner()).isEqualTo(clientB.getCertificatePinner()); assertThat(clientA.getAuthenticator()).isEqualTo(clientB.getAuthenticator()); assertThat(clientA.getConnectionPool()).isEqualTo(clientB.getConnectionPool()); assertThat(clientA.getFollowSslRedirects()).isEqualTo(clientB.getFollowSslRedirects()); assertThat(clientA.getFollowRedirects()).isEqualTo(clientB.getFollowRedirects()); assertThat(clientA.getRetryOnConnectionFailure()).isEqualTo(clientB.getRetryOnConnectionFailure()); assertThat(clientA.getConnectTimeout()).isEqualTo(clientB.getConnectTimeout()); assertThat(clientA.getReadTimeout()).isEqualTo(clientB.getReadTimeout()); assertThat(clientA.getWriteTimeout()).isEqualTo(clientB.getWriteTimeout()); }
From source file:com.tinspx.util.net.okhttp.OkHttpContext.java
License:Open Source License
private OkHttpContext(@NonNull OkHttpClient client, boolean debug) { //todo: always set no follow redirects, rather should the user be allowed //to set this? this will effect the request creation/submisstion //document info about the cookie handler client = client.clone();// w ww . j a v a2s . c o m client.setFollowRedirects(false); if (client.getCookieHandler() == null) { client.setCookieHandler(new CookieManager()); } this.client = client; this.debug = debug; }
From source file:keywhiz.cli.CommandExecutor.java
License:Apache License
public void executeCommand() throws IOException { if (command == null) { if (config.version) { System.out.println("Version: " + APP_VERSION); } else {//ww w . j a v a 2 s . c o m System.err.println("Must specify a command."); parentCommander.usage(); } return; } HttpUrl url; if (Strings.isNullOrEmpty(config.url)) { url = HttpUrl.parse("https://localhost:4444"); System.out.println("Server URL not specified (--url flag), assuming " + url); } else { url = HttpUrl.parse(config.url); if (url == null) { System.err.print("Invalid URL " + config.url); return; } } KeywhizClient client; OkHttpClient httpClient; String user = config.getUser().orElse(USER_NAME.value()); Path cookiePath = cookieDir.resolve(format(".keywhiz.%s.cookie", user)); try { List<HttpCookie> cookieList = ClientUtils.loadCookies(cookiePath); httpClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), cookieList); client = new KeywhizClient(mapper, httpClient, url); // Try a simple get request to determine whether or not the cookies are still valid if (!client.isLoggedIn()) { throw new UnauthorizedException(); } } catch (IOException e) { // Either could not find the cookie file, or the cookies were expired -- must login manually. httpClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), ImmutableList.of()); client = new KeywhizClient(mapper, httpClient, url); char[] password = ClientUtils.readPassword(user); client.login(user, password); Arrays.fill(password, '\0'); } // Save/update the cookies if we logged in successfully ClientUtils.saveCookies((CookieManager) httpClient.getCookieHandler(), cookiePath); Printing printing = new Printing(client); Command cmd = Command.valueOf(command.toUpperCase().trim()); switch (cmd) { case LIST: new ListAction((ListActionConfig) commands.get(command), client, printing).run(); break; case DESCRIBE: new DescribeAction((DescribeActionConfig) commands.get(command), client, printing).run(); break; case ADD: new AddAction((AddActionConfig) commands.get(command), client, mapper).run(); break; case DELETE: new DeleteAction((DeleteActionConfig) commands.get(command), client).run(); break; case ASSIGN: new AssignAction((AssignActionConfig) commands.get(command), client).run(); break; case UNASSIGN: new UnassignAction((UnassignActionConfig) commands.get(command), client).run(); break; case LOGIN: // User is already logged in at this point break; default: commander.usage(); } }
From source file:keywhiz.client.ClientUtilsTest.java
License:Apache License
@Test public void testSslOkHttpClientCreation() throws Exception { OkHttpClient sslClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), ImmutableList.of()); assertThat(sslClient.getFollowSslRedirects()).isFalse(); assertThat(sslClient.getSslSocketFactory()).isNotNull(); assertThat(sslClient.networkInterceptors()).isNotEmpty(); assertThat(sslClient.getCookieHandler()).isNotNull(); java.util.List<HttpCookie> cookieList = ((CookieManager) sslClient.getCookieHandler()).getCookieStore() .getCookies();//ww w . j a v a 2 s . co m assertThat(cookieList).isEmpty(); }
From source file:keywhiz.client.ClientUtilsTest.java
License:Apache License
@Test public void testSslOkHttpClientCreationWithCookies() throws Exception { OkHttpClient sslClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), cookieList); assertThat(sslClient.getFollowSslRedirects()).isFalse(); assertThat(sslClient.getCookieHandler()).isNotNull(); assertThat(sslClient.getSslSocketFactory()).isNotNull(); assertThat(sslClient.networkInterceptors()).isNotEmpty(); java.util.List<HttpCookie> cookieList = ((CookieManager) sslClient.getCookieHandler()).getCookieStore() .getCookies();/* w w w .j av a 2s . c o m*/ assertThat(cookieList).contains(xsrfCookie); assertThat(cookieList).contains(sessionCookie); }