Search in sources :

Example 71 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieRepository method addCookie.

public synchronized void addCookie(Cookie cookie) {
    // For cookie database
    Cookie toAdd = null;
    Cookie toUpdate = null;
    Cookie toRemove = null;
    CookieSet set = map.get(cookie.domain());
    if (set == null) {
        set = new CookieSet();
        map.put(cookie.domain(), set);
    }
    if (cookie.expiresAt() <= System.currentTimeMillis()) {
        toRemove = set.remove(cookie);
        // If the cookie is not persistent, it's not in database
        if (toRemove != null && !toRemove.persistent()) {
            toRemove = null;
        }
    } else {
        toAdd = cookie;
        toUpdate = set.add(cookie);
        // If the cookie is not persistent, it's not in database
        if (!toAdd.persistent())
            toAdd = null;
        if (toUpdate != null && !toUpdate.persistent())
            toUpdate = null;
        // Remove the cookie if it updates to null
        if (toAdd == null && toUpdate != null) {
            toRemove = toUpdate;
            toUpdate = null;
        }
    }
    if (toRemove != null) {
        db.remove(toRemove);
    }
    if (toAdd != null) {
        if (toUpdate != null) {
            db.update(toUpdate, toAdd);
        } else {
            db.add(toAdd);
        }
    }
}
Also used : Cookie(okhttp3.Cookie)

Example 72 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieDatabase method getAllCookies.

public Map<String, CookieSet> getAllCookies() {
    long now = System.currentTimeMillis();
    Map<String, CookieSet> map = new HashMap<>();
    List<Long> toRemove = new ArrayList<>();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_COOKIE + ";", null);
    try {
        while (cursor.moveToNext()) {
            long id = SqlUtils.getLong(cursor, COLUMN_ID, 0);
            Cookie cookie = getCookie(cursor, now);
            if (cookie != null) {
                // Save id of the cookie in db
                cookieIdMap.put(cookie, id);
                // Put cookie to set
                CookieSet set = map.get(cookie.domain());
                if (set == null) {
                    set = new CookieSet();
                    map.put(cookie.domain(), set);
                }
                set.add(cookie);
            } else {
                // Mark to remove the cookie
                toRemove.add(id);
            }
        }
    } finally {
        cursor.close();
    }
    // Remove invalid or expired cookie
    if (!toRemove.isEmpty()) {
        SQLiteStatement statement = db.compileStatement("DELETE FROM " + TABLE_COOKIE + " WHERE " + COLUMN_ID + " = ?;");
        db.beginTransaction();
        try {
            for (long id : toRemove) {
                statement.bindLong(1, id);
                statement.executeUpdateDelete();
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    return map;
}
Also used : Cookie(okhttp3.Cookie) HashMap(java.util.HashMap) SQLiteStatement(android.database.sqlite.SQLiteStatement) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 73 with Cookie

use of com.firenio.codec.http11.Cookie in project java-cloudant by cloudant.

the class MockWebServerResources method setCookie.

private static String setCookie(String name, String value, Long cookieLifetime) {
    Cookie.Builder cookieBuilder = new Cookie.Builder().domain("localhost.local").httpOnly().path("/").name(name).value(value);
    // Couch uses Expires not Max-age on its cookies, so that's what we use to test
    if (cookieLifetime != null && cookieLifetime > 0) {
        cookieBuilder.expiresAt(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(cookieLifetime));
    }
    String setCookie = cookieBuilder.build().toString();
    System.err.println("Cookie" + setCookie);
    return setCookie;
}
Also used : Cookie(okhttp3.Cookie) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 74 with Cookie

use of com.firenio.codec.http11.Cookie in project WanAndroid by dangxy.

the class PersistentCookieJar method loadForRequest.

@Override
public synchronized List<Cookie> loadForRequest(HttpUrl url) {
    List<Cookie> cookiesToRemove = new ArrayList<>();
    List<Cookie> validCookies = new ArrayList<>();
    for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
        Cookie currentCookie = it.next();
        if (isCookieExpired(currentCookie)) {
            cookiesToRemove.add(currentCookie);
            it.remove();
        } else if (currentCookie.matches(url)) {
            validCookies.add(currentCookie);
        }
    }
    persistor.removeAll(cookiesToRemove);
    return validCookies;
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList)

Example 75 with Cookie

use of com.firenio.codec.http11.Cookie in project WanAndroid by dangxy.

the class SerializableCookie method decode.

public Cookie decode(String encodedCookie) {
    byte[] bytes = hexStringToByteArray(encodedCookie);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    ObjectInputStream objectInputStream = null;
    try {
        objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableCookie) objectInputStream.readObject()).cookie;
    } catch (IOException e) {
        Log.d(TAG, "IOException in decodeCookie", e);
    } catch (ClassNotFoundException e) {
        Log.d(TAG, "ClassNotFoundException in decodeCookie", e);
    } finally {
        if (objectInputStream != null) {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                Log.d(TAG, "Stream not closed in decodeCookie", e);
            }
        }
    }
    return cookie;
}
Also used : Cookie(okhttp3.Cookie) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

Cookie (okhttp3.Cookie)102 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)20 HttpUrl (okhttp3.HttpUrl)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ObjectInputStream (java.io.ObjectInputStream)16 SharedPreferences (android.content.SharedPreferences)12 Context (android.content.Context)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 Test (org.junit.Test)8 Response (okhttp3.Response)7 SerializableCookie (com.lzy.okgo.cookie.SerializableCookie)6 Request (okhttp3.Request)6 SuppressLint (android.annotation.SuppressLint)4 CookieManager (android.webkit.CookieManager)4 EhCookieStore (com.hippo.ehviewer.client.EhCookieStore)4 OnClick (butterknife.OnClick)3 SerializableCookie (com.franmontiel.persistentcookiejar.persistence.SerializableCookie)3