Java Code Examples for org.apache.commons.httpclient.Cookie#setExpiryDate()

The following examples show how to use org.apache.commons.httpclient.Cookie#setExpiryDate() . 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: RFC2965Spec.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Parse cookie max-age attribute.
 */
public void parse(final Cookie cookie, final String value)
        throws MalformedCookieException {
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
    }
    if (value == null) {
        throw new MalformedCookieException(
                "Missing value for max-age attribute");
    }
    int age = -1;
    try {
        age = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        age = -1;
    }
    if (age < 0) {
        throw new MalformedCookieException ("Invalid max-age attribute.");
    }
    cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L));
}
 
Example 2
Source File: CrawlerPack.java    From CrawlerPack with Apache License 2.0 6 votes vote down vote up
/**
 * Return a Cookie array
 * and auto importing domain and path when domain was empty.
 *
 * @param uri required Apache Common VFS supported file systems and response JSON format content.
 * @return Cookie[]
 */
Cookie[] getCookies(String uri){
    if( null == cookies || 0 == cookies.size()) return null;

    for(Cookie cookie: cookies){

        if("".equals(cookie.getDomain())){
            String domain = uri.replaceAll("^.*:\\/\\/([^\\/]+)[\\/]?.*$", "$1");
            cookie.setDomain(domain);
            cookie.setPath("/");
            cookie.setExpiryDate(null);
            cookie.setSecure(false);
        }
    }

    return cookies.toArray(new Cookie[cookies.size()]);
}
 
Example 3
Source File: NetscapeDraftSpec.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
  * Parse the cookie attribute and update the corresponsing {@link Cookie}
  * properties as defined by the Netscape draft specification
  *
  * @param attribute {@link NameValuePair} cookie attribute from the
  * <tt>Set- Cookie</tt>
  * @param cookie {@link Cookie} to be updated
  * @throws MalformedCookieException if an exception occurs during parsing
  */
public void parseAttribute(
    final NameValuePair attribute, final Cookie cookie)
    throws MalformedCookieException {
        
    if (attribute == null) {
        throw new IllegalArgumentException("Attribute may not be null.");
    }
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null.");
    }
    final String paramName = attribute.getName().toLowerCase();
    final String paramValue = attribute.getValue();

    if (paramName.equals("expires")) {

        if (paramValue == null) {
            throw new MalformedCookieException(
                "Missing value for expires attribute");
        }
        try {
            DateFormat expiryFormat = new SimpleDateFormat(
                "EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
            Date date = expiryFormat.parse(paramValue);
            cookie.setExpiryDate(date);
        } catch (ParseException e) {
            throw new MalformedCookieException("Invalid expires "
                + "attribute: " + e.getMessage());
        }
    } else {
        super.parseAttribute(attribute, cookie);
    }
}
 
Example 4
Source File: CookieSpecBase.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
  * Parse the cookie attribute and update the corresponsing {@link Cookie}
  * properties.
  *
  * @param attribute {@link HeaderElement} cookie attribute from the
  * <tt>Set- Cookie</tt>
  * @param cookie {@link Cookie} to be updated
  * @throws MalformedCookieException if an exception occurs during parsing
  */

public void parseAttribute(
    final NameValuePair attribute, final Cookie cookie)
    throws MalformedCookieException {
        
    if (attribute == null) {
        throw new IllegalArgumentException("Attribute may not be null.");
    }
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null.");
    }
    final String paramName = attribute.getName().toLowerCase();
    String paramValue = attribute.getValue();

    if (paramName.equals("path")) {

        if ((paramValue == null) || (paramValue.trim().equals(""))) {
            paramValue = "/";
        }
        cookie.setPath(paramValue);
        cookie.setPathAttributeSpecified(true);

    } else if (paramName.equals("domain")) {

        if (paramValue == null) {
            throw new MalformedCookieException(
                "Missing value for domain attribute");
        }
        if (paramValue.trim().equals("")) {
            throw new MalformedCookieException(
                "Blank value for domain attribute");
        }
        cookie.setDomain(paramValue);
        cookie.setDomainAttributeSpecified(true);

    } else if (paramName.equals("max-age")) {

        if (paramValue == null) {
            throw new MalformedCookieException(
                "Missing value for max-age attribute");
        }
        int age;
        try {
            age = Integer.parseInt(paramValue);
        } catch (NumberFormatException e) {
            throw new MalformedCookieException ("Invalid max-age "
                + "attribute: " + e.getMessage());
        }
        cookie.setExpiryDate(
            new Date(System.currentTimeMillis() + age * 1000L));

    } else if (paramName.equals("secure")) {

        cookie.setSecure(true);

    } else if (paramName.equals("comment")) {

        cookie.setComment(paramValue);

    } else if (paramName.equals("expires")) {

        if (paramValue == null) {
            throw new MalformedCookieException(
                "Missing value for expires attribute");
        }

        try {
            cookie.setExpiryDate(DateUtil.parseDate(paramValue, this.datepatterns));
        } catch (DateParseException dpe) {
            LOG.debug("Error parsing cookie date", dpe);
            throw new MalformedCookieException(
                "Unable to parse expiration date parameter: " 
                + paramValue);
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unrecognized cookie attribute: " 
                + attribute.toString());
        }
    }
}