Example usage for org.apache.http.impl.cookie BasicClientCookie isExpired

List of usage examples for org.apache.http.impl.cookie BasicClientCookie isExpired

Introduction

In this page you can find the example usage for org.apache.http.impl.cookie BasicClientCookie isExpired.

Prototype

public boolean isExpired(final Date date) 

Source Link

Document

Returns true if this cookie has expired.

Usage

From source file:com.digitalpebble.stormcrawler.util.CookieConverter.java

/**
 * Get a list of cookies based on the cookies string taken from response
 * header and the target url.//from  w ww .  j  av  a  2 s .co m
 * 
 * @param cookiesString
 *            the value of the http header for "Cookie" in the http
 *            response.
 * @param targetURL
 *            the url for which we wish to pass the cookies in the request.
 * @return List off cookies to add to the request.
 */
public static List<Cookie> getCookies(String[] cookiesStrings, URL targetURL) {
    ArrayList<Cookie> list = new ArrayList<Cookie>();

    for (String cs : cookiesStrings) {
        String name = null;
        String value = null;

        String expires = null;
        String domain = null;
        String path = null;

        boolean secure = false;

        String[] tokens = cs.split(";");

        int equals = tokens[0].indexOf("=");
        name = tokens[0].substring(0, equals);
        value = tokens[0].substring(equals + 1);

        for (int i = 1; i < tokens.length; i++) {
            String ti = tokens[i].trim();
            if (ti.equalsIgnoreCase("secure"))
                secure = true;
            if (ti.toLowerCase().startsWith("path=")) {
                path = ti.substring(5);
            }
            if (ti.toLowerCase().startsWith("domain=")) {
                domain = ti.substring(7);
            }
            if (ti.toLowerCase().startsWith("expires=")) {
                expires = ti.substring(8);
            }
        }

        BasicClientCookie cookie = new BasicClientCookie(name, value);

        // check domain
        if (domain != null) {
            cookie.setDomain(domain);

            if (!checkDomainMatchToUrl(domain, targetURL.getHost()))
                continue;
        }

        // check path
        if (path != null) {
            cookie.setPath(path);

            if (!path.equals("") && !path.equals("/") && !targetURL.getPath().startsWith(path))
                continue;
        }

        // check secure
        if (secure) {
            cookie.setSecure(secure);

            if (!targetURL.getProtocol().equalsIgnoreCase("https"))
                continue;
        }

        // check expiration
        if (expires != null) {
            try {
                Date expirationDate = DATE_FORMAT.parse(expires);
                cookie.setExpiryDate(expirationDate);

                // check that it hasn't expired?
                if (cookie.isExpired(new Date()))
                    continue;

                cookie.setExpiryDate(expirationDate);
            } catch (ParseException e) {
                // ignore exceptions
            }
        }

        // attach additional infos to cookie
        list.add(cookie);
    }

    return list;
}