Search in sources :

Example 41 with Cookie

use of com.firenio.codec.http11.Cookie in project apps-android-wikipedia by wikimedia.

the class CookieManagerTypeAdapter method read.

@Override
public SharedPreferenceCookieManager read(JsonReader in) throws IOException {
    Map<String, List<Cookie>> map = new HashMap<>();
    in.beginObject();
    while (in.hasNext()) {
        String key = in.nextName();
        List<Cookie> list = new ArrayList<>();
        map.put(key, list);
        in.beginArray();
        HttpUrl url = HttpUrl.parse(WikiSite.DEFAULT_SCHEME + "://" + key);
        while (in.hasNext()) {
            String str = in.nextString();
            if (url != null) {
                list.add(Cookie.parse(url, str));
            }
        }
        in.endArray();
    }
    in.endObject();
    return new SharedPreferenceCookieManager(map);
}
Also used : Cookie(okhttp3.Cookie) HashMap(java.util.HashMap) SharedPreferenceCookieManager(org.wikipedia.dataclient.SharedPreferenceCookieManager) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HttpUrl(okhttp3.HttpUrl)

Example 42 with Cookie

use of com.firenio.codec.http11.Cookie in project instagram-java-scraper by postaddictme.

the class CookieHashSet method addAll.

@Override
public void addAll(Collection<Cookie> cookies) {
    for (Cookie cookie : cookies) {
        CookieBox box = new CookieBox(cookie);
        boxes.remove(box);
        boxes.add(box);
    }
}
Also used : Cookie(okhttp3.Cookie)

Example 43 with Cookie

use of com.firenio.codec.http11.Cookie in project apps-android-commons by commons-app.

the class SharedPreferenceCookieManager method saveFromResponse.

@Override
public synchronized void saveFromResponse(@NonNull HttpUrl url, @NonNull List<Cookie> cookies) {
    if (cookies.isEmpty()) {
        return;
    }
    boolean cookieJarModified = false;
    for (Cookie cookie : cookies) {
        // Default to the URI's domain if cookie's domain is not explicitly set
        String domainSpec = TextUtils.isEmpty(cookie.domain()) ? url.uri().getAuthority() : cookie.domain();
        if (!cookieJar.containsKey(domainSpec)) {
            cookieJar.put(domainSpec, new ArrayList<>());
        }
        List<Cookie> cookieList = cookieJar.get(domainSpec);
        if (cookie.expiresAt() < System.currentTimeMillis() || "deleted".equals(cookie.value())) {
            Iterator<Cookie> i = cookieList.iterator();
            while (i.hasNext()) {
                if (i.next().name().equals(cookie.name())) {
                    i.remove();
                    cookieJarModified = true;
                }
            }
        } else {
            Iterator<Cookie> i = cookieList.iterator();
            boolean exists = false;
            while (i.hasNext()) {
                Cookie c = i.next();
                if (c.equals(cookie)) {
                    // an identical cookie already exists, so we don't need to update it.
                    exists = true;
                    break;
                } else if (c.name().equals(cookie.name())) {
                    // it's a cookie with the same name, but different contents, so remove the
                    // current cookie, so that the new one will be added.
                    i.remove();
                }
            }
            if (!exists) {
                cookieList.add(cookie);
                cookieJarModified = true;
            }
        }
    }
    if (cookieJarModified) {
        persistCookies();
    }
}
Also used : Cookie(okhttp3.Cookie)

Example 44 with Cookie

use of com.firenio.codec.http11.Cookie in project apps-android-commons by commons-app.

the class SharedPreferenceCookieManager method buildCookieList.

private void buildCookieList(@NonNull List<Cookie> outList, @NonNull List<Cookie> inList, @Nullable String prefix) {
    Iterator<Cookie> i = inList.iterator();
    boolean cookieJarModified = false;
    while (i.hasNext()) {
        Cookie cookie = i.next();
        if (prefix != null && !cookie.name().startsWith(prefix)) {
            continue;
        }
        // But wait, is the cookie expired?
        if (cookie.expiresAt() < System.currentTimeMillis()) {
            i.remove();
            cookieJarModified = true;
        } else {
            outList.add(cookie);
        }
    }
    if (cookieJarModified) {
        persistCookies();
    }
}
Also used : Cookie(okhttp3.Cookie)

Example 45 with Cookie

use of com.firenio.codec.http11.Cookie in project BaseProject by feer921.

the class SerializableCookie method bytesToCookie.

public static Cookie bytesToCookie(byte[] bytes) {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    try {
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableCookie) objectInputStream.readObject()).getCookie();
    } catch (Exception e) {
        OkLogger.printStackTrace(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