Java Code Examples for java.net.CookieStore

The following are top voted examples for showing how to use java.net.CookieStore. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: LuaViewPlayground   File: CookieManager.java   Source Code and License 4 votes vote down vote up
/**
 * net cookies
 */
private static String getNetRequestCookies() {
    final CookieStore cookieStore = getCookieManager().getCookieStore();
    //net cookie
    if (cookieStore != null) {
        final List<HttpCookie> cookies = cookieStore.getCookies();
        if (cookies != null && cookies.size() > 0) {
            //While joining the Cookies, use ',' or ';' as needed. Most of the server are using ';'
            final String netCookie = TextUtils.join(";", cookies);
            LogUtil.d(TAG, "get-net", netCookie);
            return netCookie;
        }
    }
    return null;
}
 
Example 2
Project: tcas-mobile   File: SessionManager.java   Source Code and License 4 votes vote down vote up
public SessionManager(CookieStore cookieJar) {
    cookieManager = new CookieManager(cookieJar, CookiePolicy.ACCEPT_ORIGINAL_SERVER);
    // Making sure cookies are enabled
    CookieHandler.setDefault(cookieManager);

}
 
Example 3
Project: tcas-mobile   File: SessionManager.java   Source Code and License 4 votes vote down vote up
public CookieStore getCookieStore() {
    return cookieManager.getCookieStore();
}
 
Example 4
Project: tcas-mobile   File: TCaSApp.java   Source Code and License 4 votes vote down vote up
@Override
  public void onCreate() {
super.onCreate();
//CookieStore cookies = PrefUtils.getCookieStoreFromPrefs(this, PrefUtils.PREF_COOKIESTORE_KEY);
CookieStore cookieStore = new PersistentCookieStore(this);
      //CookieStore cookieStore = new SiCookieStore2(this);
      sm = new SessionManager(cookieStore);
      im = new InfoManager(sm);
      qm = new QuestionManager(sm);
      am = new AnswerManager(sm);
      mm = new MessageManager(sm);
      //fm = new ForumManager(sm);
      pm = new ProfileManager(sm);

  }
 
Example 5
Project: byps   File: JcnnRequest.java   Source Code and License 4 votes vote down vote up
public void applySession(HHttpRequest req1) {
  JcnnRequest req = (JcnnRequest)req1;
  HttpURLConnection c = req.conn.get();
  if (c != null) {
    CookieStore cookies = cookieManager.getCookieStore();
    for (HttpCookie cookie : cookies.getCookies()) {
      c.setRequestProperty("Cookie", cookie.toString());
      if (log.isDebugEnabled()) log.debug("request cookie=" + cookie.toString());
    }
  }
}
 
Example 6
Project: openjdk-icedtea7   File: NullUriCookieTest.java   Source Code and License 4 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example 7
Project: utexas-utilities   File: UTilitiesApplication.java   Source Code and License 4 votes vote down vote up
public void logoutAll() {
    for (AuthCookie authCookie : authCookies.values()) {
        authCookie.logout();
    }
    CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
    cookies.removeAll();
}
 
Example 8
Project: utexas-utilities   File: AuthCookie.java   Source Code and License 4 votes vote down vote up
private void resetCookie() {
    settings.edit().remove(prefKey).apply();
    authCookie = "";
    try {
        /*
        This step is *required* for PNA, and nicety for other services. PNA won't let you
        log in if you're still holding on to a valid authcookie, so we clear them out.
         */
        URI loginURI = URI.create(url.getHost());
        CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
        for (HttpCookie cookie : cookies.get(loginURI)) {
            cookies.remove(loginURI, cookie);
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    cookieHasBeenSet = false;
}
 
Example 9
Project: utexas-utilities   File: AuthCookie.java   Source Code and License 4 votes vote down vote up
protected void setAuthCookieVal(String authCookie, String domain) {
    this.authCookie = authCookie;
    settings.edit().putString(prefKey, authCookie).apply();

    /*
    this is technically unnecessary if OkHttp handled the authentication, because it will
    have already set the cookies in the CookieHandler. It doesn't seem to cause any issues
    just to re-add the cookies, though
     */
    HttpCookie httpCookie = new HttpCookie(authCookieKey, authCookie);
    httpCookie.setDomain(domain);
    try {
        CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
        cookies.add(URI.create(domain), httpCookie);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    cookieHasBeenSet = true;
    android.webkit.CookieManager.getInstance().setCookie(domain, httpCookie.getName() + "=" + httpCookie.getValue());
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
        android.webkit.CookieManager.getInstance().flush();
    } else {
        CookieSyncManager.getInstance().sync();
    }
}
 
Example 10
Project: restlet-jetty9   File: JettyHttpClientHelper.java   Source Code and License 4 votes vote down vote up
/**
 * Creates a Jetty HTTP client.
 * 
 * @return A new HTTP client.
 */
private HttpClient createHttpClient()
{
	SslContextFactory sslContextFactory = null;
	try
	{
		final org.restlet.engine.ssl.SslContextFactory restletSslContextFactory = org.restlet.engine.ssl.SslUtils.getSslContextFactory( this );
		sslContextFactory = new org.eclipse.jetty.util.ssl.SslContextFactory();
		sslContextFactory.setSslContext( restletSslContextFactory.createSslContext() );
	}
	catch( Exception e )
	{
	}

	final HttpClient httpClient = new HttpClient( sslContextFactory );

	httpClient.setAddressResolutionTimeout( getAddressResolutionTimeout() );
	httpClient.setBindAddress( getBindAddress() );
	final ByteBufferPool byteBufferPool = getByteBufferPool();
	if( byteBufferPool != null )
		httpClient.setByteBufferPool( byteBufferPool );
	httpClient.setConnectTimeout( getConnectTimeout() );
	final CookieStore cookieStore = getCookieStore();
	if( cookieStore != null )
		httpClient.setCookieStore( cookieStore );
	httpClient.setExecutor( getExecutor() );
	httpClient.setFollowRedirects( isFollowRedirects() );
	httpClient.setIdleTimeout( getIdleTimeout() );
	httpClient.setMaxConnectionsPerDestination( getMaxConnectionsPerDestination() );
	httpClient.setMaxRedirects( getMaxRedirects() );
	httpClient.setMaxRequestsQueuedPerDestination( getMaxRequestsQueuedPerDestination() );
	httpClient.setRemoveIdleDestinations( getRemoveIdleDestinations() );
	httpClient.setRequestBufferSize( getRequestBufferSize() );
	httpClient.setResponseBufferSize( getResponseBufferSize() );
	httpClient.setScheduler( getScheduler() );
	httpClient.setStopTimeout( getStopTimeout() );
	httpClient.setStrictEventOrdering( isStrictEventOrdering() );
	httpClient.setTCPNoDelay( isTcpNoDelay() );
	final String userAgentField = getUserAgentField();
	if( userAgentField != null )
		httpClient.setUserAgentField( new HttpField( HttpHeader.USER_AGENT, userAgentField ) );

	return httpClient;
}
 
Example 11
Project: apps-android-wikipedia   File: SharedPreferenceCookieManager.java   Source Code and License 4 votes vote down vote up
@Override
public CookieStore getCookieStore() {
    // We don't actually have one. hehe
    throw new UnsupportedOperationException("We poor. We no have CookieStore");
}
 
Example 12
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldReturnNoCookiesOnEmptyStore() {
    CookieStore store = new CookieTray(preferences);
    assertTrue(store.getCookies().isEmpty());
}
 
Example 13
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldReturnNoUrisOnEmptyStore() {
    CookieStore store = new CookieTray(preferences);
    assertTrue(store.getURIs().isEmpty());
}
 
Example 14
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldReturnAllCookies() {
    addCookies(10, false);
    CookieStore store = new CookieTray(preferences);
    assertEquals(10, store.getCookies().size());
}
 
Example 15
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldFilterCookiesOutByUri() {
    addCookies(10, false);
    CookieStore store = new CookieTray(preferences);
    assertEquals(1, store.get(URI.create("http://google.com/home0")).size());
}
 
Example 16
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldFilterOutExpiredCookies() {
    addCookies(10, true);
    CookieStore store = new CookieTray(preferences);
    assertEquals(0, store.getCookies().size());
}
 
Example 17
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldReturnStoredUris() {
    addCookies(7, false);
    CookieStore store = new CookieTray(preferences);
    assertEquals(7, store.getURIs().size());
}
 
Example 18
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldAddCookieToSharedPreferences() throws URISyntaxException {
    URI uri = URI.create("http://google.com");
    CookieStore store = new CookieTray(preferences);
    HttpCookie httpCookie = new HttpCookie("name", "value");
    store.add(uri, httpCookie);
    String cookieVal = new SerializableCookie(httpCookie).asString();
    verify(editor).putString(uri.toString() + "|" + httpCookie.getName(), cookieVal);
}
 
Example 19
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldBeAbleToRemoveCookie() {
    addCookies(1, false);
    CookieStore store = new CookieTray(preferences);
    store.remove(URI.create("http://google.com/home0"), new HttpCookie("name0", "value0"));
    verify(editor).remove("http://google.com/home0|name0");
}
 
Example 20
Project: cookietray   File: CookieTrayTest.java   Source Code and License 3 votes vote down vote up
@Test
public void shouldBeAbleToRemoveAllCookies() {
    doReturn(editor).when(editor).clear();
    CookieStore store = new CookieTray(preferences);
    store.removeAll();
    verify(editor).clear();
    assertTrue("No cookies should be present after removing", store.getCookies().isEmpty());
}
 
Example 21
Project: LuaViewPlayground   File: CookieManager.java   Source Code and License 3 votes vote down vote up
/**
 * clear net cookies
 */
public static void clearNetCookies() {
    java.net.CookieManager cookieManager = getCookieManager();
    CookieStore cookieStore = cookieManager.getCookieStore();
    if (cookieStore != null) {
        cookieStore.removeAll();
    }
}
 
Example 22
Project: LuaViewPlayground   File: CookieManager.java   Source Code and License 3 votes vote down vote up
/**
 * @param cookiesHeader
 */
private static String updateWebkitResponseCookies(List<String> cookiesHeader, String requestUrl) {
    //write webkit cookie
    final CookieStore cookieStore = getCookieManager().getCookieStore();
    if (cookieStore != null && cookiesHeader != null) {

        StringBuffer cookie = new StringBuffer();
        for (String cookieHeader : cookiesHeader) {
            cookie.append(cookieHeader).append(";");
        }
        android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
        if (cookieManager != null && cookie.length() > 0) {
            String cookieStr = cookie.toString();
            LogUtil.d(TAG, "write-webkit", cookieStr);
            cookieManager.setCookie(requestUrl, cookieStr);
        }
    }
    return null;
}
 
Example 23
Project: LuaViewPlayground   File: CookieManager.java   Source Code and License 3 votes vote down vote up
/**
 * write net cookies
 *
 * @param cookiesHeader
 */
private static void updateNetResponseCookies(List<String> cookiesHeader) {
    //write net cookie
    final CookieStore cookieStore = getCookieManager().getCookieStore();
    if (cookieStore != null && cookiesHeader != null) {
        HttpCookie cookieStr = null;
        for (String cookieHeader : cookiesHeader) {
            if (cookieHeader != null) {
                List<HttpCookie> cookies = HttpCookie.parse(cookieHeader);
                if (cookies != null && cookies.size() > 0) {
                    cookieStr = cookies.get(0);
                    LogUtil.d(TAG, "write-net", cookieStr);
                    cookieStore.add(null, cookieStr);
                }
            }
        }
    }
}
 
Example 24
Project: smartqq-client   File: SmartQQClient.java   Source Code and License 3 votes vote down vote up
private void getUinAndPsessionid()
		throws InterruptedException, ExecutionException, TimeoutException, URISyntaxException {
	LOGGER.debug("开始获取uin和psessionid");

	{
		CookieStore cookieStore = httpClient.getCookieStore();
		cookieStore.add(new URI("qq.com"), new HttpCookie("pgv_info", "ssid=s" + RandomUtil.numberString(10)));
		cookieStore.add(new URI("qq.com"), new HttpCookie("pgv_pvid", RandomUtil.numberString(10)));
		httpClient
				.newRequest(
						"https://ui.ptlogin2.qq.com/cgi-bin/login?daid=164&target=self&style=16&mibao_css=m_webqq&appid=501004106&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fw.qq.com%2Fproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=20131024001")
				.method(HttpMethod.GET).agent(ApiURL.USER_AGENT).header("Referer", "http://w.qq.com/")
				.header("Upgrade-Insecure-Requests", "1").send();
	}

	httpClient.newRequest("http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2").method(HttpMethod.GET)
			.agent(ApiURL.USER_AGENT).header("Referer", "http://w.qq.com/").header("Upgrade-Insecure-Requests", "1")
			.send();

	JsonObject r = new JsonObject();
	r.addProperty("ptwebqq", ptwebqq);
	r.addProperty("clientid", Client_ID);
	r.addProperty("psessionid", "");
	r.addProperty("status", "online");

	ContentResponse response = post(ApiURL.GET_UIN_AND_PSESSIONID, r);
	JsonObject result = getJsonObjectResult(response);
	this.psessionid = result.get("psessionid").getAsString();
	this.uin = result.get("uin").getAsLong();
	this.selfUserStatus = result.get("status").getAsString();
}
 
Example 25
Project: Auto.js   File: WebkitCookieManagerProxy.java   Source Code and License 3 votes vote down vote up
WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy) {
    super(null, cookiePolicy);
    this.webkitCookieManager = android.webkit.CookieManager.getInstance();
}
 
Example 26
Project: Auto.js   File: WebkitCookieManagerProxy.java   Source Code and License 3 votes vote down vote up
@Override
public CookieStore getCookieStore() {
    // we don't want anyone to work with this cookie store directly
    throw new UnsupportedOperationException();
}
 
Example 27
Project: bottler   File: CookieStoreUtils.java   Source Code and License 3 votes vote down vote up
public static void writeTo(
        @NotNull CookieStore store,
        @NotNull OutputStream stream
) throws IOException {
    HashMap<URI, List<SerializableHttpCookie>> cookieMap =
            new HashMap<URI, List<SerializableHttpCookie>>();

    List<URI> uris = store.getURIs();
    for (URI uri : uris) {
        List<HttpCookie> cookies = store.get(uri);
        List<SerializableHttpCookie> serializableHttpCookies =
                new ArrayList<SerializableHttpCookie>(cookies.size());
        for (HttpCookie cookie : cookies) {
            serializableHttpCookies.add(SerializableHttpCookie.of(cookie));
        }
        cookieMap.put(uri, serializableHttpCookies);
    }

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(os);
        oos.writeObject(cookieMap);
        oos.flush();
        stream.write(os.toByteArray());
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(oos);
    }
}
 
Example 28
Project: bottler   File: CookieStoreUtils.java   Source Code and License 3 votes vote down vote up
@SuppressWarnings("unchecked")
public static void readFrom(
        @NotNull CookieStore store,
        @NotNull InputStream stream
) throws IOException {
    HashMap<URI, List<SerializableHttpCookie>> cookieMap = null;

    ByteArrayInputStream is = new ByteArrayInputStream(IOUtils.toByteArray(stream));
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(is);
        Object object = ois.readObject();
        if (object instanceof HashMap) {
            cookieMap = (HashMap<URI, List<SerializableHttpCookie>>) object;
        }
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(ois);
    }

    if (cookieMap == null) return;

    for (URI uri : cookieMap.keySet()) {
        List<SerializableHttpCookie> cookies = cookieMap.get(uri);
        for (SerializableHttpCookie cookie : cookies) {
            store.add(uri, cookie.toHttpCookie());
        }
    }

}
 
Example 29
Project: jdk8u-jdk   File: NullUriCookieTest.java   Source Code and License 3 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example 30
Project: openjdk-jdk10   File: NullUriCookieTest.java   Source Code and License 3 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example 31
Project: appinventor-extensions   File: GingerbreadUtil.java   Source Code and License 3 votes vote down vote up
/**
 * Clears the cookies in the given cookie handler. Cookies can only be cleared if the
 * cookieHandler is a CookieManager with a non-null CookieStore.
 *
 * @param cookieHandler the cookie handler where cookies should be cleared
 * @return true if cookies were cleared; false otherwise
 */
public static boolean clearCookies(CookieHandler cookieHandler) {
  if (cookieHandler instanceof CookieManager) {
    CookieManager cookieManager = (CookieManager) cookieHandler;
    CookieStore cookieStore = cookieManager.getCookieStore();
    if (cookieStore != null) {
      cookieStore.removeAll();
      return true;
    }
  }
  return false;
}
 
Example 32
Project: ulogger-android   File: WebHelper.java   Source Code and License 3 votes vote down vote up
/**
 * Remove authorization by removing session cookie
 */
static void deauthorize() {
    if (cookieManager != null) {
        CookieStore store = cookieManager.getCookieStore();
        store.removeAll();
    }
}
 
Example 33
Project: openjdk9   File: NullUriCookieTest.java   Source Code and License 3 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example 34
Project: mimi-reader   File: HttpClientFactory.java   Source Code and License 3 votes vote down vote up
public CookieStore getCookieStore() {
    return cookieStore;
}
 
Example 35
Project: mimi-reader   File: MimiUtil.java   Source Code and License 3 votes vote down vote up
public boolean isLoggedIn() {
    final CookieStore cookieStore = HttpClientFactory.getInstance().getCookieStore();
    boolean found = false;

    for (final HttpCookie cookie : cookieStore.getCookies()) {
        if ("pass_enabled".equals(cookie.getName()) && "1".equals(cookie.getValue())) {
            found = true;
        }
    }

    return found;
}
 
Example 36
Project: xowa_android   File: SharedPreferenceCookieManager.java   Source Code and License 3 votes vote down vote up
@Override
public CookieStore getCookieStore() {
    // We don't actually have one. hehe
    throw new UnsupportedOperationException("We poor. We no have CookieStore");
}
 
Example 37
Project: android-oss   File: RefTagUtils.java   Source Code and License 3 votes vote down vote up
/**
 * Finds the ref tag cookie associated with a project. Returns `null` if no cookie has yet been set.
 */
protected static @Nullable HttpCookie findRefTagCookieForProject(final @NonNull Project project,
  final @NonNull CookieManager cookieManager, final @NonNull SharedPreferences sharedPreferences) {

  final String cookieName = cookieNameForProject(project);

  // First try finding the cookie in the cookie store
  final CookieStore cookieStore = cookieManager.getCookieStore();
  for (final HttpCookie cookie : cookieStore.getCookies()) {
    if (cookieName.equals(cookie.getName())) {
      return cookie;
    }
  }

  // If we can't find it in the cookie store let's look in shared prefs
  final String cookieValue = sharedPreferences.getString(cookieName, null);
  if (cookieValue != null) {
    return buildCookieWithValueAndProject(cookieValue, project);
  }

  return null;
}
 
Example 38
Project: android-oss   File: RefTagUtilsTest.java   Source Code and License 3 votes vote down vote up
@Test
public void testStoredCookieRefTagForProject() {
  final CookieManager cookieManager = new CookieManager();
  final CookieStore cookieStore = cookieManager.getCookieStore();
  final Project project = ProjectFactory.project();
  final RefTag refTag = RefTag.recommended();

  // set the cookie and retrieve the ref tag
  cookieStore.add(null, new HttpCookie("ref_" + project.id(), refTag.tag() + "%3F" + SystemUtils.secondsSinceEpoch()));
  final RefTag retrievedRefTag = RefTagUtils.storedCookieRefTagForProject(project, cookieManager, sharedPreferences);

  assertNotNull(retrievedRefTag);
  assertEquals(refTag, retrievedRefTag);
}
 
Example 39
Project: android-oss   File: RefTagUtilsTest.java   Source Code and License 3 votes vote down vote up
@Test
public void testFindRefTagCookieForProject_WhenCookieExists() {
  final CookieManager cookieManager = new CookieManager();
  final CookieStore cookieStore = cookieManager.getCookieStore();
  final Project project = ProjectFactory.project();
  final RefTag refTag = RefTag.recommended();

  // set and retrieve the cookie
  cookieStore.add(null, new HttpCookie("ref_" + project.id(), refTag.tag() + "%3F" + SystemUtils.secondsSinceEpoch()));
  final HttpCookie cookie = RefTagUtils.findRefTagCookieForProject(project, cookieManager, sharedPreferences);

  assertNotNull(cookie);
  assertEquals(RefTagUtils.cookieNameForProject(project), cookie.getName());
  assertEquals(RefTagUtils.cookieValueForRefTag(refTag), cookie.getValue());
}
 
Example 40
Project: mattermost-android-classic   File: WebkitCookieManagerProxy.java   Source Code and License 3 votes vote down vote up
public WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy) {
    super(null, cookiePolicy);

    this.webkitCookieManager = android.webkit.CookieManager.getInstance();
}
 
Example 41
Project: mattermost-android-classic   File: WebkitCookieManagerProxy.java   Source Code and License 3 votes vote down vote up
@Override
public CookieStore getCookieStore() {
    throw new UnsupportedOperationException();
}
 
Example 42
Project: httplite   File: URLite.java   Source Code and License 3 votes vote down vote up
public Builder setCookieStore(CookieStore cookieStore){
    cookieHandler = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
    return this;
}
 
Example 43
Project: httplite   File: URLite.java   Source Code and License 3 votes vote down vote up
public Builder setCookieStore(CookieStore cookieStore, CookiePolicy policy){
    cookieHandler = new CookieManager(cookieStore, policy);
    return this;
}
 
Example 44
Project: httplite   File: Ok2Lite.java   Source Code and License 3 votes vote down vote up
public Builder setCookieStore(CookieStore cookieStore){
    cookieHandler = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
    return this;
}
 
Example 45
Project: httplite   File: Ok2Lite.java   Source Code and License 3 votes vote down vote up
public Builder setCookieStore(CookieStore cookieStore, CookiePolicy policy){
    cookieHandler = new CookieManager(cookieStore, policy);
    return this;
}
 
Example 46
Project: httplite   File: Ok3Lite.java   Source Code and License 3 votes vote down vote up
public Builder setCookieStore(CookieStore cookieStore){
    if(cookieStore!=null)
        this.cookieJar = new CookieJarImpl(new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL));
    return this;
}
 
Example 47
Project: httplite   File: Ok3Lite.java   Source Code and License 3 votes vote down vote up
public Builder setCookieStore(CookieStore cookieStore, CookiePolicy policy){
    if(cookieStore!=null)
        this.cookieJar = new CookieJarImpl(new CookieManager(cookieStore, policy));
    return this;
}
 
Example 48
Project: jdk8u_jdk   File: NullUriCookieTest.java   Source Code and License 3 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example 49
Project: lookaside_java-1.8.0-openjdk   File: NullUriCookieTest.java   Source Code and License 3 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example 50
Project: EDDI   File: HttpClientWrapper.java   Source Code and License 3 votes vote down vote up
@Override
public CookieStore getCookieStore() {
    return httpClient.getCookieStore();
}
 
Example 51
Project: S1-Next   File: WebViewUtils.java   Source Code and License 3 votes vote down vote up
public static void syncWebViewCookies(@NonNull Context context, @NonNull CookieStore cookieStore) {
    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.removeSessionCookie();
    cookieManager.removeAllCookie();

    List<URI> urls = cookieStore.getURIs();
    for (URI url : urls) {
        List<HttpCookie> cookies = cookieStore.get(url);
        for (HttpCookie cookie : cookies) {
            cookieManager.setCookie(url.toString(), cookie.toString());
        }

        L.w("Cookies", cookieManager.getCookie(url.toString()));
    }

    CookieSyncManager.getInstance().sync();
}
 
Example 52
Project: motu   File: CookieStoreHolder.java   Source Code and License 3 votes vote down vote up
public static CookieStore getCookieStore() {
    return threadLocal.get();
}
 
Example 53
Project: motu   File: CookieStoreHolder.java   Source Code and License 3 votes vote down vote up
public static void setCookieStore(CookieStore cookieStore) {
    threadLocal.set(cookieStore);
}
 
Example 54
Project: motu   File: CookieStoreHolder.java   Source Code and License 3 votes vote down vote up
public static CookieStore initInMemoryCookieStore() {
    CookieStore cookieStore = CookieStoreHolder.getCookieStore();
    if (cookieStore == null) {
        CookieManager cm = new CookieManager();
        cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieStoreHolder.setCookieStore(cm.getCookieStore());
    } else {
        cookieStore.removeAll();
    }

    return cookieStore;
}
 
Example 55
Project: extend-enhance-base   File: WebkitCookieManager.java   Source Code and License 3 votes vote down vote up
public WebkitCookieManager(Context context, CookieStore store, CookiePolicy cookiePolicy) {
    super(null, cookiePolicy);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        CookieSyncManager.createInstance(context);
    }
    webkitCookieManager = android.webkit.CookieManager.getInstance();
    webkitCookieManager.setAcceptCookie(true);
}
 
Example 56
Project: extend-enhance-base   File: WebkitCookieManager.java   Source Code and License 3 votes vote down vote up
@Override
public CookieStore getCookieStore() {
    // We don't want anyone to work with this cookie store directly
    throw new UnsupportedOperationException();
}
 
Example 57
Project: gwt-syncproxy   File: CookieServiceTest.java   Source Code and License 3 votes vote down vote up
private boolean searchCookieStoreForValue(String value, CookieStore store) {
	for (HttpCookie c : store.getCookies()) {
		if (c.getValue().equals(value)) {
			return true;
		}
	}
	return false;
}
 
Example 58
Project: gwt-syncproxy   File: CookieServiceTest.java   Source Code and License 3 votes vote down vote up
private boolean searchCookieStoreForValue(String value, CookieStore store) {
	for (HttpCookie c : store.getCookies()) {
		if (c.getValue().equals(value)) {
			return true;
		}
	}
	return false;
}
 
Example 59
Project: alternate-java-bridge-library   File: GingerbreadUtil.java   Source Code and License 3 votes vote down vote up
/**
 * Clears the cookies in the given cookie handler. Cookies can only be cleared if the
 * cookieHandler is a CookieManager with a non-null CookieStore.
 *
 * @param cookieHandler the cookie handler where cookies should be cleared
 * @return true if cookies were cleared; false otherwise
 */
public static boolean clearCookies(CookieHandler cookieHandler) {
  if (cookieHandler instanceof CookieManager) {
    CookieManager cookieManager = (CookieManager) cookieHandler;
    CookieStore cookieStore = cookieManager.getCookieStore();
    if (cookieStore != null) {
      cookieStore.removeAll();
      return true;
    }
  }
  return false;
}
 
Example 60
Project: mobile-android-studio   File: ExoPicasso.java   Source Code and License 3 votes vote down vote up
private static void clearDownloaderCookies() {
  CookieHandler handler = CookieHandler.getDefault();
  if (handler != null && handler instanceof CookieManager) {
    CookieStore cookies = ((CookieManager) handler).getCookieStore();
    cookies.removeAll();
    CookieHandler.setDefault(null);
  }
}