FileDocCategorySizeDatePackage
Cookie.javaAPI DocExample3610Sun Dec 12 10:52:26 GMT 2004com.macfaq.http

Cookie

public class Cookie extends Object

Fields Summary
private String
version
private String
name
private String
value
private URI
uri
private String
domain
private Date
expires
private String
path
private boolean
secure
private static DateFormat
expiresFormat
Constructors Summary
private Cookie()

  
  // prevent instantiation
    
Methods Summary
public static com.macfaq.http.Cookiebake(java.lang.String header, java.net.URI uri)

    
    try {
      String[] attributes = header.split(";");
      String nameValue = attributes[0];
      Cookie cookie = new Cookie();
      cookie.uri = uri;
      cookie.name = nameValue.substring(0, nameValue.indexOf('="));
      cookie.value = nameValue.substring(nameValue.indexOf('=")+1);
      cookie.path = "/";
      cookie.domain = uri.getHost();
      
      if (attributes[attributes.length-1].trim().equals("secure")) {
        cookie.secure = true;
      }
      
      for (int i=1; i < attributes.length; i++) {
        nameValue = attributes[i].trim();
        int equals = nameValue.indexOf('=");
        if (equals == -1) continue;
        String attributeName = nameValue.substring(0, equals);
        String attributeValue = nameValue.substring(equals+1); 
        if (attributeName.equalsIgnoreCase("domain")) {
          String uriDomain = uri.getHost();
          if (uriDomain.equals(attributeValue)) {
            cookie.domain = attributeValue;
          }
          else {
            if (!attributeValue.startsWith(".")) {
              attributeValue = "." + attributeValue;
            }
            uriDomain = uriDomain.substring(uriDomain.indexOf('."));
            if (!uriDomain.equals(attributeValue)) {
              throw new CookieException(
               "Server tried to set cookie in another domain");
            }
            cookie.domain = attributeValue;
          }
        }
        else if (attributeName.equalsIgnoreCase("path")) {
          cookie.path = attributeValue;
        }
        else if (attributeName.equalsIgnoreCase("expires")) {
          cookie.expires = expiresFormat.parse(attributeValue);
        }
        else if (attributeName.equalsIgnoreCase("Version")) {
          if (!"1".equals(attributeValue)) {
            throw new CookieException("Unexpected version " + attributeValue);
          }
          cookie.version = attributeValue;
        }
      }
      
      return cookie;
    }
    catch (Exception ex) { 
      // ParseException, StringIndexOutOfBoundsException etc.
      throw new CookieException(ex);
    }
    
  
public java.lang.StringgetName()

    return name;
  
public java.net.URIgetURI()

    return uri;
  
public java.lang.StringgetVersion()

    return version;
  
public booleanisExpired()

    if (expires == null) return false;
    Date now = new Date();
    return now.after(expires);
  
public booleanisSecure()

    return secure;
  
public booleanmatches(java.net.URI u)

    
    if (isExpired()) return false;
    
    String path = u.getPath();
    if (path == null) path = "/";
    
    if (path.startsWith(this.path)) return true;
    
    return false;
  
public java.lang.StringtoExternalForm()

    StringBuffer result = new StringBuffer(name);
    result.append("=");
    result.append(value);
    if ("1".equals(version)) {
       result.append(" Version=1");   
    }
    return result.toString();