Example usage for org.apache.http.client CookieStore clearExpired

List of usage examples for org.apache.http.client CookieStore clearExpired

Introduction

In this page you can find the example usage for org.apache.http.client CookieStore clearExpired.

Prototype

boolean clearExpired(Date date);

Source Link

Document

Removes all of Cookie s in this store that have expired by the specified java.util.Date .

Usage

From source file:nya.miku.wishmaster.http.cloudflare.CloudflareChecker.java

static void removeCookie(CookieStore store, String name) {
    boolean flag = false;
    for (Cookie cookie : store.getCookies()) {
        if (cookie.getName().equals(name)) {
            if (cookie instanceof SetCookie) {
                flag = true;//ww  w .java  2 s . c o  m
                ((SetCookie) cookie).setExpiryDate(new Date(0));
            } else {
                Logger.e(TAG,
                        "cannot remove cookie (object does not implement SetCookie): " + cookie.toString());
            }
        }
    }
    if (flag)
        store.clearExpired(new Date());
}

From source file:net.emphased.vkclient.VkClient.java

protected void clearExpiredCookies(CookieStore cookieStore) {
    cookieStore.clearExpired(new Date());
}

From source file:org.apache.jena.atlas.web.auth.FormsAuthenticator.java

@Override
public void apply(AbstractHttpClient client, HttpContext httpContext, URI target) {
    if (client == null)
        return;/*from ww  w .  ja  v a 2s  .  co  m*/

    // Do we have a login available for the target server?
    FormLogin login = this.findCredentials(target);
    if (login == null)
        return;

    // We need to synchronize on the login because making a login attempt
    // may take a while and there is no point making multiple login attempts
    // against the same server
    synchronized (login) {

        // Have we already logged into this server?
        if (login.hasCookies()) {
            // Use existing cookies
            LOG.info("Using existing cookies to authenticate access to " + target.toString());
            CookieStore store = login.getCookies();
            if (store != null)
                client.setCookieStore(store);

            // Check if any of the cookies have expired
            if (!store.clearExpired(Calendar.getInstance().getTime())) {
                // No cookies were cleared so our cookies are still fresh
                // and we don't need to login again
                return;
            }

            // If we reach here then some of our cookies have expired and we
            // may no longer be logged in and should login again instead of
            // proceeding with the existing cookies
        }

        try {
            // Use a fresh Cookie Store for new login attempts
            CookieStore store = new BasicCookieStore();
            client.setCookieStore(store);

            // Try to login
            LOG.info("Making login attempt against " + login.getLoginFormURL()
                    + " to obtain authentication for access to " + target.toString());
            HttpPost post = new HttpPost(login.getLoginFormURL());
            post.setEntity(login.getLoginEntity());
            HttpResponse response = client.execute(post, httpContext);

            // Always read the payload to ensure reusable connections
            final String payload = HttpOp.readPayload(response.getEntity());

            // Check for successful login
            if (response.getStatusLine().getStatusCode() >= 400) {
                LOG.warn("Failed to login against " + login.getLoginFormURL()
                        + " to obtain authentication for access to " + target.toString());
                throw new HttpException(response.getStatusLine().getStatusCode(),
                        "Login attempt failed - " + response.getStatusLine().getReasonPhrase(), payload);
            }

            // Otherwise assume a successful login
            LOG.info("Successfully logged in against " + login.getLoginFormURL()
                    + " and obtained authentication for access to " + target.toString());
            login.setCookies(client.getCookieStore());

        } catch (UnsupportedEncodingException e) {
            throw new HttpException("UTF-8 encoding not supported on your platform", e);
        } catch (IOException e) {
            throw new HttpException("Error making login request", e);
        }
    }
}