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);
}
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations