Example usage for org.apache.http.cookie Cookie isExpired

List of usage examples for org.apache.http.cookie Cookie isExpired

Introduction

In this page you can find the example usage for org.apache.http.cookie Cookie isExpired.

Prototype

boolean isExpired(final Date date);

Source Link

Document

Returns true if this cookie has expired.

Usage

From source file:com.domuslink.communication.ApiHandler.java

/**
 * Pull the raw text content of the given URL. This call blocks until the
 * operation has completed, and is synchronized because it uses a shared
 * buffer {@link #sBuffer}.//from w w w. j av a 2 s  . c o m
 *
 * @param type The type of either a GET or POST for the request
 * @param commandURI The constructed URI for the path
 * @return The raw content returned by the server.
 * @throws ApiException If any connection or server error occurs.
 */
protected static synchronized String urlContent(int type, URI commandURI, ApiCookieHandler cookieHandler)
        throws ApiException {
    HttpResponse response;
    HttpRequestBase request;

    if (sUserAgent == null) {
        throw new ApiException("User-Agent string must be prepared");
    }

    // Create client and set our specific user-agent string
    DefaultHttpClient client = new DefaultHttpClient();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("", sPassword);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);
    client.setCredentialsProvider(credsProvider);
    CookieStore cookieStore = cookieHandler.getCookieStore();
    if (cookieStore != null) {
        boolean expiredCookies = false;
        Date nowTime = new Date();
        for (Cookie theCookie : cookieStore.getCookies()) {
            if (theCookie.isExpired(nowTime))
                expiredCookies = true;
        }
        if (!expiredCookies)
            client.setCookieStore(cookieStore);
        else {
            cookieHandler.setCookieStore(null);
            cookieStore = null;
        }
    }

    try {
        if (type == POST_TYPE)
            request = new HttpPost(commandURI);
        else
            request = new HttpGet(commandURI);

        request.setHeader("User-Agent", sUserAgent);
        response = client.execute(request);

        // Check if server response is valid
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != HTTP_STATUS_OK) {
            Log.e(TAG,
                    "urlContent: Url issue: " + commandURI.toString() + " with status: " + status.toString());
            throw new ApiException("Invalid response from server: " + status.toString());
        }

        // Pull content stream from response
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();

        ByteArrayOutputStream content = new ByteArrayOutputStream();

        // Read response into a buffered stream
        int readBytes = 0;
        while ((readBytes = inputStream.read(sBuffer)) != -1) {
            content.write(sBuffer, 0, readBytes);
        }

        if (cookieStore == null) {
            List<Cookie> realCookies = client.getCookieStore().getCookies();
            if (!realCookies.isEmpty()) {
                BasicCookieStore newCookies = new BasicCookieStore();
                for (int i = 0; i < realCookies.size(); i++) {
                    newCookies.addCookie(realCookies.get(i));
                    //                      Log.d(TAG, "aCookie - " + realCookies.get(i).toString());
                }
                cookieHandler.setCookieStore(newCookies);
            }
        }

        // Return result from buffered stream
        return content.toString();
    } catch (IOException e) {
        Log.e(TAG, "urlContent: client execute: " + commandURI.toString());
        throw new ApiException("Problem communicating with API", e);
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "urlContent: client execute: " + commandURI.toString());
        throw new ApiException("Problem communicating with API", e);
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        client.getConnectionManager().shutdown();
    }
}

From source file:gov.nasa.arc.geocam.memo.service.test.SiteAuthCookieImplementationTest.java

private SiteAuthCookieImplementation prepSaciWithInjections(boolean loggedin) throws Exception {
    SiteAuthCookieImplementation saci = new SiteAuthCookieImplementation();
    setHiddenField(saci, "serverRootUrl", "");
    setHiddenField(saci, "appPath", "");

    Cookie cookie = mock(Cookie.class);
    when(cookie.isExpired(any(Date.class))).thenReturn(!loggedin);
    if (loggedin)
        ;// www .  j a v  a  2 s . c  om
    saci.setAuth(username, password);

    setHiddenField(saci, "sessionIdCookie", cookie);
    return saci;
}

From source file:com.ryan.ryanreader.cache.PersistentCookieStore.java

public synchronized boolean clearExpired(final Date date) {

    boolean purged = false;
    final LinkedList<Cookie> newSet = new LinkedList<Cookie>();

    for (final Cookie cookie : cookies) {

        if (!cookie.isExpired(date)) {
            newSet.add(cookie);/* w  ww  .java  2 s . c om*/
        } else {
            purged = true;
        }
    }

    cookies = newSet;
    return purged;
}

From source file:fedroot.dacs.http.DacsClientContextTest.java

public void testGetAllCookies() throws Exception {
    URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search",
            "q=httpclient&btnG=Google+Search&aq=f&oq=", null);
    DacsGetRequest dacsGetRequest = new DacsGetRequest(uri);
    DacsResponse dacsResponse = dacsClientContext.executeGetRequest(dacsGetRequest);
    List<Cookie> cookies = dacsClientContext.getAllCookies();
    assertEquals(2, cookies.size());/*from  w  ww .  j a  v  a  2  s.c om*/
    for (Cookie cookie : cookies) {
        System.out.println(cookie.getName());
        assertFalse(cookie.isSecure());
        assertFalse(cookie.isExpired(new Date()));
        assertFalse(DacsCookie.isDacsCookie(cookie));
    }

}

From source file:com.grendelscan.commons.http.CookieJar.java

/**
 * Removes all of {@link Cookie cookies} in this HTTP state that have expired by the specified {@link java.util.Date date}.
 * /*from  ww w .j  ava 2  s .  c  om*/
 * @return true if any cookies were purged.
 * 
 * @see Cookie#isExpired(Date)
 */
public synchronized boolean clearExpired(final Date date) {
    if (date == null) {
        return false;
    }
    boolean removed = false;
    Set<Cookie> keys = new HashSet<Cookie>(cookies);
    for (Cookie cookie : keys) {
        if (cookie.isExpired(date)) {
            cookies.remove(cookie);
            removed = true;
        }
    }
    return removed;
}

From source file:com.lurencun.cfuture09.androidkit.http.async.PersistentCookieStore.java

@Override
public boolean clearExpired(Date date) {
    boolean clearedAny = false;
    SharedPreferences.Editor editor = cookiePrefs.edit();

    for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {
        String name = entry.getKey();
        Cookie cookie = entry.getValue();
        if (cookie.isExpired(date)) {
            // Clear cookies from local store
            cookies.remove(name);/*w  w w  .  j a v a 2 s .c om*/

            // Clear cookies from persistent store
            editor.remove(COOKIE_NAME_PREFIX + name);

            // We've cleared at least one
            clearedAny = true;
        }
    }

    // Update names in persistent store
    if (clearedAny) {
        editor.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    }
    editor.commit();

    return clearedAny;
}

From source file:ti.modules.titanium.network.TiCookieStore.java

@Override
public boolean clearExpired(Date date) {
    synchronized (cookieStore) {
        List<Cookie> cookies = cookieStore.getCookies();
        boolean clearedExpired = cookieStore.clearExpired(date);

        if (clearedExpired) {
            SharedPreferences.Editor prefWriter = pref.edit();

            for (Cookie cookie : cookies) {
                if (cookie.isExpired(date)) {
                    prefWriter.remove(COOKIE_PREFIX + cookie.getName());
                }//from ww  w  .j a va  2 s.  c  om
            }
            prefWriter.commit();
        }

        return clearedExpired;
    }
}

From source file:com.http_asny.http.PersistentCookieStore.java

@Override
public boolean clearExpired(Date date) {
    boolean clearedAny = false;
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();

    for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {
        String name = entry.getKey();
        Cookie cookie = entry.getValue();
        if (cookie.isExpired(date)) {
            // Clear cookies from local store
            cookies.remove(name);//  ww  w.ja v a  2 s.  co m

            // Clear cookies from persistent store
            prefsWriter.remove(COOKIE_NAME_PREFIX + name);

            // We've cleared at least one
            clearedAny = true;
        }
    }

    // Update names in persistent store
    if (clearedAny) {
        prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    }
    prefsWriter.apply();

    return clearedAny;
}

From source file:cn.caimatou.canting.utils.http.asynchttp.PersistentCookieStore.java

@Override
public boolean clearExpired(Date date) {
    boolean clearedAny = false;
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();

    for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {
        String name = entry.getKey();
        Cookie cookie = entry.getValue();
        if (cookie.isExpired(date)) {
            // Clear cookies from local store
            cookies.remove(name);/* w  w w  . j a  v a 2  s.c  o  m*/

            // Clear cookies from persistent store
            prefsWriter.remove(COOKIE_NAME_PREFIX + name);

            // We've cleared at least one
            clearedAny = true;
        }
    }

    // Update names in persistent store
    if (clearedAny) {
        prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    }
    prefsWriter.commit();

    return clearedAny;
}

From source file:com.blazeroni.reddit.http.PersistentCookieStore.java

@Override
public boolean clearExpired(Date date) {
    boolean clearedAny = false;
    SharedPreferences.Editor prefsWriter = this.cookiePrefs.edit();

    for (ConcurrentHashMap.Entry<String, Cookie> entry : this.cookies.entrySet()) {
        String name = entry.getKey();
        Cookie cookie = entry.getValue();
        if (cookie.isExpired(date)) {
            // Clear cookies from local store
            this.cookies.remove(name);

            // Clear cookies from persistent store
            prefsWriter.remove(COOKIE_NAME_PREFIX + name);

            // We've cleared at least one
            clearedAny = true;//from   ww  w.  j  av a  2  s  .c  o m
        }
    }

    // Update names in persistent store
    if (clearedAny) {
        prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", this.cookies.keySet()));
        prefsWriter.commit();
    }

    return clearedAny;
}