下面列出了java.net.HttpCookie#getMaxAge ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static List<TransportableHttpCookie> from(URL url, List<HttpCookieWithId> list) {
List<TransportableHttpCookie> result = new ArrayList<>(list.size());
for (HttpCookieWithId hcwi : list) {
HttpCookie cookie = hcwi.httpCookie;
TransportableHttpCookie thc = new TransportableHttpCookie();
thc.name = cookie.getName();
thc.value = cookie.getValue();
thc.comment = cookie.getComment();
thc.commentURL = cookie.getCommentURL();
thc.discard = cookie.getDiscard();
thc.domain = cookie.getDomain();
thc.maxAge = cookie.getMaxAge();
thc.path = cookie.getPath();
thc.portList = cookie.getPortlist();
thc.secure = cookie.getSecure();
thc.version = cookie.getVersion();
thc.url = url.toString();
result.add(thc);
}
return result;
}
private int flush() throws IOException {
int count = 0;
try (FileWriter writer = new FileWriter(file)) {
for (Map.Entry<URI, List<HttpCookie>> entry : persistentCookies.entrySet()) {
URI uri = entry.getKey();
writer.write(uri.toString());
writer.write('\n');
for (HttpCookie httpCookie : entry.getValue()) {
long maxAge = httpCookie.getMaxAge();
if (maxAge > 0 && !httpCookie.getDiscard()) {
long expiresAt = maxAge + System.currentTimeMillis() / 1000L;
writer.write(' ');
writeCookie(writer, httpCookie, expiresAt);
writer.write('\n');
count++;
}
}
}
}
return count;
}
/**
* Cookie building database entities.
*
* @param uri cookie corresponding uri.
* @param cookie cookie.
*/
public CookieEntity(URI uri, HttpCookie cookie) {
this.uri = uri == null ? null : uri.toString();
this.name = cookie.getName();
this.value = cookie.getValue();
this.comment = cookie.getComment();
this.commentURL = cookie.getCommentURL();
this.discard = cookie.getDiscard();
this.domain = cookie.getDomain();
long maxAge = cookie.getMaxAge();
if (maxAge != -1 && maxAge > 0) {
this.expiry = (maxAge * 1000L) + System.currentTimeMillis();
if (this.expiry < 0L) // 溢出
this.expiry = HeaderUtils.getMaxExpiryMillis();
} else
this.expiry = -1L;
this.path = cookie.getPath();
if (!TextUtils.isEmpty(path) && path.length() > 1 && path.endsWith("/")) {
this.path = path.substring(0, path.length() - 1);
}
this.portList = cookie.getPortlist();
this.secure = cookie.getSecure();
this.version = cookie.getVersion();
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
addIndex(domainIndex, cookie.getDomain(), cookie);
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
} finally {
lock.unlock();
}
storeCookies();
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
public static Cookie toCookie(String url, HttpCookie httpCookie) {
Cookie cookie = new Cookie();
cookie.setUrl(url);
cookie.setName(httpCookie.getName());
cookie.setValue(httpCookie.getValue());
cookie.setComment(httpCookie.getComment());
cookie.setCommentURL(httpCookie.getCommentURL());
cookie.setDiscard(httpCookie.getDiscard());
cookie.setDomain(httpCookie.getDomain());
long maxAge = httpCookie.getMaxAge();
if (maxAge > 0) {
long expiry = (maxAge * 1000L) + System.currentTimeMillis();
if (expiry < 0L) {
expiry = System.currentTimeMillis() + 100L * 365L * 24L * 60L * 60L * 1000L;
}
cookie.setExpiry(expiry);
} else if (maxAge < 0) {
cookie.setExpiry(-1);
} else {
cookie.setExpiry(0);
}
String path = httpCookie.getPath();
if (!TextUtils.isEmpty(path) && path.length() > 1 && path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
cookie.setPath(path);
cookie.setPortList(httpCookie.getPortlist());
cookie.setSecure(httpCookie.getSecure());
cookie.setVersion(httpCookie.getVersion());
return cookie;
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
@Override
public void add(URI uri, HttpCookie cookie) {
inMemory.add(uri, cookie);
if (cookie.getMaxAge() > 0) {
persistentCookies.computeIfAbsent(uri, (u) -> new ArrayList<>(4)).add(cookie);
flushPolicy.onUpdate(inMemory);
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
public void add(final URI uri, final HttpCookie cookie) {
if(cookie.getMaxAge() == 0) {
return;
}
if(cookie.getDomain() != null) {
add(new DomainKey(cookie), cookie);
}
if(uri != null) {
add(new UriKey(uri, cookie), cookie);
}
}
@Override
public void add(final URI uri, final HttpCookie cookie) {
assertLive();
final Key key = Key.make(uri, cookie);
add(key, cookie);
if(cookie.getMaxAge() != -1L) {
store(key, cookie);
}
}
public HttpCookieWithId(long id, HttpCookie httpCookie) {
this.id = id;
this.httpCookie = httpCookie;
mMaxAge = httpCookie.getMaxAge();
mWhenCreated = System.currentTimeMillis();
}