List of usage examples for org.apache.http.impl.cookie BasicClientCookie setSecure
public void setSecure(final boolean secure)
From source file:com.gooddata.http.client.CookieUtils.java
private static void replaceSst(final String sst, final CookieStore cookieStore, final String domain) { final BasicClientCookie cookie = new BasicClientCookie(SST_COOKIE_NAME, sst); cookie.setSecure(true); cookie.setPath(SST_COOKIE_PATH);//from www. ja va 2 s. c o m cookie.setDomain(domain); cookieStore.addCookie(cookie); }
From source file:org.springframework.test.web.servlet.htmlunit.MockWebResponseBuilder.java
static com.gargoylesoftware.htmlunit.util.Cookie createCookie(Cookie cookie) { Date expires = null;// w w w . ja v a 2s. com if (cookie.getMaxAge() > -1) { expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000); } BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue()); result.setDomain(cookie.getDomain()); result.setComment(cookie.getComment()); result.setExpiryDate(expires); result.setPath(cookie.getPath()); result.setSecure(cookie.getSecure()); if (cookie.isHttpOnly()) { result.setAttribute("httponly", "true"); } return new com.gargoylesoftware.htmlunit.util.Cookie(result); }
From source file:org.freaknet.gtrends.client.GoogleTrendsClientFactory.java
private static GoogleTrendsClient _parse(CmdLineParser cmdLine) throws GoogleTrendsClientRunException { setLogLevel(cmdLine);//from ww w .jav a 2 s .com try { // TODO - Move outside // Prints all available regions and exists if (cmdLine.getPrintRegionsOpt()) { RegionParser.getInstance().printAll(); System.exit(0); } // HTTP Client setup int timeout = 5; // seconds RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000) .setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); _httpClient = HttpClientBuilder.create().setDefaultCookieStore(_cookieStore) .setDefaultRequestConfig(config) //.setRedirectStrategy(new LaxRedirectStrategy()) .build(); BasicClientCookie cookie = new BasicClientCookie("I4SUserLocale", "it"); cookie.setVersion(0); cookie.setDomain("www.google.com"); cookie.setPath("/trends"); cookie.setSecure(false); _cookieStore.addCookie(cookie); cookie = new BasicClientCookie("PREF", ""); cookie.setVersion(0); cookie.setDomain("www.google.com"); cookie.setPath("/trends"); cookie.setSecure(false); _cookieStore.addCookie(cookie); // TODO - fix proxy // if (cmdLine.getProxyHostname() != null) { // HttpHost proxyHost = new HttpHost(cmdLine.getProxyHostname(), cmdLine.getProxyPort(), cmdLine.getProxyProtocol()); // Credentials credentials = cmdLine.getProxyCredentials(); // if (credentials != null) { // _httpClient.get // _httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), credentials); // } // _httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost); // } // Setup Google Authenticator _authenticator = new GoogleAuthenticator(cmdLine.getUsername(), cmdLine.getPassword(), _httpClient); } catch (FileNotFoundException ex) { throw new GoogleTrendsClientRunException(ex); } return new GoogleTrendsClient(_authenticator, _httpClient); }
From source file:io.vertigo.struts2.FileDownloader4Tests.java
/** * Load in all the cookies WebDriver currently knows about so that we can mimic the browser cookie state * * @param seleniumCookieSet/*from w w w . j ava 2s. com*/ * @return BasicCookieStore */ private static BasicCookieStore mimicCookieState(final Set<Cookie> seleniumCookieSet) { final BasicCookieStore mimicWebDriverCookieStore = new BasicCookieStore(); for (final Cookie seleniumCookie : seleniumCookieSet) { final BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue()); duplicateCookie.setDomain(seleniumCookie.getDomain()); duplicateCookie.setSecure(seleniumCookie.isSecure()); duplicateCookie.setExpiryDate(seleniumCookie.getExpiry()); duplicateCookie.setPath(seleniumCookie.getPath()); mimicWebDriverCookieStore.addCookie(duplicateCookie); } return mimicWebDriverCookieStore; }
From source file:org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection.java
private static com.gargoylesoftware.htmlunit.util.Cookie createCookie(javax.servlet.http.Cookie cookie) { Date expires = null;/*from www .jav a2s . c om*/ if (cookie.getMaxAge() > -1) { expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000); } BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue()); result.setDomain(cookie.getDomain()); result.setComment(cookie.getComment()); result.setExpiryDate(expires); result.setPath(cookie.getPath()); result.setSecure(cookie.getSecure()); if (cookie.isHttpOnly()) { result.setAttribute("httponly", "true"); } return new com.gargoylesoftware.htmlunit.util.Cookie(result); }
From source file:cn.ttyhuo.common.MyApplication.java
public static void setUpPersistentCookieStore() { if (context == null) return;//w ww .j a va 2 s .c om cookieStore = new PersistentCookieStore(context); CookieStore httpCookieStore = HttpRequestUtil.cookieManager.getCookieStore(); for (HttpCookie h : httpCookieStore.getCookies()) { BasicClientCookie newCookie = new BasicClientCookie(h.getName(), h.getValue()); newCookie.setVersion(h.getVersion()); newCookie.setDomain(h.getDomain()); newCookie.setPath(h.getPath()); newCookie.setSecure(h.getSecure()); newCookie.setComment(h.getComment()); cookieStore.addCookie(newCookie); } }
From source file:gov.nih.nci.firebird.commons.selenium2.util.FileDownloadUtils.java
private static BasicClientCookie translateCookie(Cookie webDriverCookie) { BasicClientCookie clientCookie = new BasicClientCookie(webDriverCookie.getName(), webDriverCookie.getValue()); clientCookie.setDomain(webDriverCookie.getDomain()); clientCookie.setExpiryDate(webDriverCookie.getExpiry()); clientCookie.setPath(webDriverCookie.getPath()); clientCookie.setSecure(webDriverCookie.isSecure()); return clientCookie; }
From source file:de.betterform.agent.web.WebUtil.java
/** * stores cookies that may exist in request and passes them on to processor for usage in * HTTPConnectors. Instance loading and submission then uses these cookies. Important for * applications using auth.//from w w w . ja v a 2 s . co m * <p/> * NOTE: this method should be called *before* the Adapter is initialized cause the cookies may * already be needed for setup (e.g. loading of XForms via Http) */ public static void storeCookies(List<Cookie> requestCookies, XFormsProcessor processor) { Vector<BasicClientCookie> commonsCookies = new Vector<BasicClientCookie>(); if (requestCookies != null && requestCookies.size() > 0) { commonsCookies = saveAsBasicClientCookie(requestCookies.iterator(), commonsCookies); } /* if (responseCookies != null && responseCookies.size() > 0) { commonsCookies= saveAsBasicClientCookie(responseCookies.iterator(), commonsCookies); } */ if (commonsCookies.size() == 0) { BasicClientCookie sessionCookie = new BasicClientCookie("JSESSIONID", ((WebProcessor) processor).httpSession.getId()); sessionCookie.setSecure(false); sessionCookie.setDomain(null); sessionCookie.setPath(null); commonsCookies.add(sessionCookie); } processor.setContextParam(AbstractHTTPConnector.REQUEST_COOKIE, commonsCookies.toArray(new BasicClientCookie[0])); }
From source file:bear.plugins.java.JenkinsCache.java
public static File download(String jdkVersion, File jenkinsCache, File tempDestDir, String jenkinsUri, String user, String pass) { try {//from ww w . j av a 2s. c o m Optional<JDKFile> optional = load(jenkinsCache, jenkinsUri, jdkVersion); if (!optional.isPresent()) { throw new RuntimeException("could not find: " + jdkVersion); } String uri = optional.get().filepath; SSLContext sslContext = SSLContext.getInstance("TLSv1"); sslContext.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers ============="); return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { System.out.println("checkClientTrusted ============="); } public void checkServerTrusted(X509Certificate[] certs, String authType) { System.out.println("checkServerTrusted ============="); } } }, new SecureRandom()); SSLSocketFactory sf = new SSLSocketFactory(sslContext); Scheme httpsScheme = new Scheme("https", 443, sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); Scheme httpScheme = new Scheme("http", 80, PlainSocketFactory.getSocketFactory()); schemeRegistry.register(httpsScheme); schemeRegistry.register(httpScheme); DefaultHttpClient httpClient = new DefaultHttpClient( new PoolingClientConnectionManager(schemeRegistry)); CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("gpw_e24", "."); cookie.setDomain("oracle.com"); cookie.setPath("/"); cookie.setSecure(true); cookieStore.addCookie(cookie); httpClient.setCookieStore(cookieStore); HttpPost httppost = new HttpPost("https://login.oracle.com"); httppost.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64((user + ":" + pass).getBytes()), "UTF-8")); HttpResponse response = httpClient.execute(httppost); int code = response.getStatusLine().getStatusCode(); if (code != 302) { System.out.println(IOUtils.toString(response.getEntity().getContent())); throw new RuntimeException("unable to auth: " + code); } // closes the single connection // EntityUtils.consumeQuietly(response.getEntity()); httppost = new HttpPost(uri); httppost.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64((user + ":" + pass).getBytes()), "UTF-8")); response = httpClient.execute(httppost); code = response.getStatusLine().getStatusCode(); if (code != 302) { System.out.println(IOUtils.toString(response.getEntity().getContent())); throw new RuntimeException("to download: " + uri); } File file = new File(tempDestDir, optional.get().name); HttpEntity entity = response.getEntity(); final long length = entity.getContentLength(); final CountingOutputStream os = new CountingOutputStream(new FileOutputStream(file)); System.out.printf("Downloading %s to %s...%n", uri, file); Thread progressThread = new Thread(new Runnable() { double lastProgress; @Override public void run() { while (!Thread.currentThread().isInterrupted()) { long copied = os.getCount(); double progress = copied * 100D / length; if (progress != lastProgress) { System.out.printf("\rProgress: %s%%", LangUtils.toConciseString(progress, 1)); } lastProgress = progress; try { Thread.sleep(500); } catch (InterruptedException e) { break; } } } }, "progressThread"); progressThread.start(); ByteStreams.copy(entity.getContent(), os); progressThread.interrupt(); System.out.println("Download complete."); return file; } catch (Exception e) { throw Exceptions.runtime(e); } }
From source file:bear.plugins.java.JenkinsCache.java
public static File download2(String jdkVersion, File jenkinsCache, File tempDestDir, String jenkinsUri, String user, String pass) { try {//from www . j a v a 2 s . co m Optional<JDKFile> optional = load(jenkinsCache, jenkinsUri, jdkVersion); if (!optional.isPresent()) { throw new RuntimeException("could not find: " + jdkVersion); } String uri = optional.get().filepath; // agent.get() // agent.get() SSLContext sslContext = SSLContext.getInstance("TLSv1"); sslContext.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers ============="); return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { System.out.println("checkClientTrusted ============="); } public void checkServerTrusted(X509Certificate[] certs, String authType) { System.out.println("checkServerTrusted ============="); } } }, new SecureRandom()); SSLSocketFactory sf = new SSLSocketFactory(sslContext); Scheme httpsScheme = new Scheme("https", 443, sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); DefaultHttpClient httpClient = new DefaultHttpClient( new PoolingClientConnectionManager(schemeRegistry)); MechanizeAgent agent = new MechanizeAgent(); Cookie cookie2 = agent.cookies().addNewCookie("gpw_e24", ".", "oracle.com"); cookie2.getHttpCookie().setPath("/"); cookie2.getHttpCookie().setSecure(false); CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("gpw_e24", "."); cookie.setDomain("oracle.com"); cookie.setPath("/"); cookie.setSecure(true); cookieStore.addCookie(cookie); httpClient.setCookieStore(cookieStore); HttpPost httppost = new HttpPost("https://login.oracle.com"); httppost.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64((user + ":" + pass).getBytes()), "UTF-8")); HttpResponse response = httpClient.execute(httppost); int code = response.getStatusLine().getStatusCode(); if (code != 302) { System.out.println(IOUtils.toString(response.getEntity().getContent())); throw new RuntimeException("unable to auth: " + code); } // EntityUtils.consumeQuietly(response.getEntity()); httppost = new HttpPost(uri); response = httpClient.execute(httppost); code = response.getStatusLine().getStatusCode(); if (code != 302) { System.out.println(IOUtils.toString(response.getEntity().getContent())); throw new RuntimeException("to download: " + uri); } File file = new File(tempDestDir, optional.get().name); HttpEntity entity = response.getEntity(); final long length = entity.getContentLength(); final CountingOutputStream os = new CountingOutputStream(new FileOutputStream(file)); System.out.printf("Downloading %s to %s...%n", uri, file); Thread progressThread = new Thread(new Runnable() { double lastProgress; @Override public void run() { while (!Thread.currentThread().isInterrupted()) { long copied = os.getCount(); double progress = copied * 100D / length; if (progress != lastProgress) { System.out.printf("\rProgress: %s%%", LangUtils.toConciseString(progress, 1)); } lastProgress = progress; try { Thread.sleep(500); } catch (InterruptedException e) { break; } } } }, "progressThread"); progressThread.start(); ByteStreams.copy(entity.getContent(), os); progressThread.interrupt(); System.out.println("Download complete."); return file; } catch (Exception e) { throw Exceptions.runtime(e); } }