Java Code Examples for java.net.HttpCookie#getMaxAge()

The following examples show how to use java.net.HttpCookie#getMaxAge() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransportableHttpCookie.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: PersistentCookieStore.java    From httplite with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 3
Source File: CookieEntity.java    From NoHttp with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: FileCookieJar.java    From rawhttp with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: FileBackedCookieStore.java    From http-builder-ng with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 6
Source File: InMemoryCookieStore.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 7
Source File: NonBlockingCookieStore.java    From http-builder-ng with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 8
Source File: InMemoryCookieStore.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 9
Source File: InMemoryCookieStore.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 10
Source File: InMemoryCookieStore.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 11
Source File: FileCookieJar.java    From rawhttp with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 12
Source File: InMemoryCookieStore.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 13
Source File: InMemoryCookieStore.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 14
Source File: InMemoryCookieStore.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 15
Source File: InMemoryCookieStore.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 16
Source File: InMemoryCookieStore.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 17
Source File: Cookie.java    From Kalle with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 18
Source File: InMemoryCookieStore.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 19
Source File: InMemoryCookieStore.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 20
Source File: HttpCookieWithId.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
public HttpCookieWithId(long id, HttpCookie httpCookie) {
    this.id = id;
    this.httpCookie = httpCookie;
    mMaxAge = httpCookie.getMaxAge();
    mWhenCreated = System.currentTimeMillis();
}