/** * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies. * If the given cookie has already expired it will not be added, but existing * values will still be removed. * * @param cookie the {@link Cookie cookie} to be added * @see #addCookies(Cookie[]) */ @Override public synchronized void addCookie(final Cookie cookie) { if (cookie != null) { // first remove any old cookie that is equivalent cookies.remove(cookie); Date now = new Date(); if (!cookie.isExpired(now)) { Date targetExpiryDate = new Date(System.currentTimeMillis() + maxExpiresMillis); if (!cookie.isExpired(targetExpiryDate)) { try { if (cookie instanceof BasicClientCookie) { ((BasicClientCookie) cookie).setExpiryDate(targetExpiryDate); } else if (cookie instanceof BasicClientCookie2) { ((BasicClientCookie2) cookie).setExpiryDate(targetExpiryDate); } } catch (Exception e) { } } cookies.add(cookie); } } }
@Before public void setUp() { this.target = new HttpHost("localhost.local", 80); this.cookieStore = new BasicCookieStore(); final BasicClientCookie2 cookie1 = new BasicClientCookie2("name1", "value1"); cookie1.setVersion(1); cookie1.setDomain("localhost.local"); cookie1.setPath("/"); this.cookieStore.addCookie(cookie1); final BasicClientCookie2 cookie2 = new BasicClientCookie2("name2", "value2"); cookie2.setVersion(1); cookie2.setDomain("localhost.local"); cookie2.setPath("/"); this.cookieStore.addCookie(cookie2); this.cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create() .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider()) .register(CookieSpecs.STANDARD, new RFC2965SpecProvider()) .register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider()) .register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider()) .build(); }
public Cookie toApacheCookie() { BasicClientCookie2 cookie = new BasicClientCookie2(name, value); cookie.setComment(cookieComment); cookie.setDomain(cookieDomain); cookie.setExpiryDate(cookieExpiryDate); cookie.setPath(cookiePath); cookie.setSecure(isSecure); cookie.setVersion(cookieVersion); cookie.setPorts(ports); // copy over attributes /* for(Entry<String, String> entry : attribs.entrySet()) { cookie.setAttribute(entry.getKey(), entry.getValue()); }*/ return cookie; }
@Test public void testExcludeExpiredCookies() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", "/"); final BasicClientCookie2 cookie3 = new BasicClientCookie2("name3", "value3"); cookie3.setVersion(1); cookie3.setDomain("localhost.local"); cookie3.setPath("/"); cookie3.setExpiryDate(new Date(System.currentTimeMillis() + 100)); this.cookieStore.addCookie(cookie3); Assert.assertEquals(3, this.cookieStore.getCookies().size()); this.cookieStore = Mockito.spy(this.cookieStore); final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target); context.setAttribute(HttpClientContext.HTTP_ROUTE, route); context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); // Make sure the third cookie expires Thread.sleep(200); final HttpRequestInterceptor interceptor = new RequestAddCookies(); interceptor.process(request, context); final Header[] headers1 = request.getHeaders(SM.COOKIE); Assert.assertNotNull(headers1); Assert.assertEquals(2, headers1.length); Assert.assertEquals("$Version=1; name1=\"value1\"", headers1[0].getValue()); Assert.assertEquals("$Version=1; name2=\"value2\"", headers1[1].getValue()); final Header[] headers2 = request.getHeaders(SM.COOKIE2); Assert.assertNotNull(headers2); Assert.assertEquals(0, headers2.length); Mockito.verify(this.cookieStore, Mockito.times(1)).clearExpired(Mockito.<Date>any()); }
public Cookie(String name, String value) { this.httpCookie = new BasicClientCookie2(name, value); }
public boolean isRepresentingBasicClientCookie2() { return httpCookie instanceof BasicClientCookie2; }
public BasicClientCookie2 getHttpCookieAsBasicClientCookie2() { return (BasicClientCookie2)httpCookie; }
/** * Set the cookie locale to en_US. */ private void setCookieLocale(){ cookieStore.addCookie(new BasicClientCookie2("box_locale", "en_US")); cookieStore.addCookie(new BasicClientCookie2("country", "US")); cookieStore.addCookie(new BasicClientCookie2("lang", "en-US")); }
@Override protected List<Cookie> loadCookies() { final List<Cookie> list = new LinkedList<Cookie>(); final Cursor cursor = myDatabase.rawQuery( "SELECT cookie_id,host,path,name,value,date_of_expiration,secure FROM Cookie", null ); while (cursor.moveToNext()) { final long id = cursor.getLong(0); final String host = cursor.getString(1); final String path = cursor.getString(2); final String name = cursor.getString(3); final String value = cursor.getString(4); final Date date = SQLiteUtil.getDate(cursor, 5); final boolean secure = cursor.getLong(6) == 1; Set<Integer> portSet = null; final Cursor portsCursor = myDatabase.rawQuery( "SELECT port FROM CookiePort WHERE cookie_id = " + id, null ); while (portsCursor.moveToNext()) { if (portSet == null) { portSet = new HashSet<Integer>(); } portSet.add((int)portsCursor.getLong(1)); } portsCursor.close(); final BasicClientCookie2 c = new BasicClientCookie2(name, value); c.setDomain(host); c.setPath(path); if (portSet != null) { final int ports[] = new int[portSet.size()]; int index = 0; for (int p : portSet) { ports[index] = p; ++index; } c.setPorts(ports); } c.setExpiryDate(date); c.setSecure(secure); c.setDiscard(false); list.add(c); } cursor.close(); return list; }