1 31 32 package org.apache.commons.httpclient.cookie; 33 34 import java.util.StringTokenizer ; 35 import java.util.Date ; 36 import java.util.Locale ; 37 import java.text.DateFormat ; 38 import java.text.SimpleDateFormat ; 39 import java.text.ParseException ; 40 import org.apache.commons.httpclient.NameValuePair; 41 import org.apache.commons.httpclient.Cookie; 42 43 59 60 public class NetscapeDraftSpec extends CookieSpecBase { 61 62 63 public NetscapeDraftSpec() { 64 super(); 65 } 66 67 68 77 public void parseAttribute( 78 final NameValuePair attribute, final Cookie cookie) 79 throws MalformedCookieException { 80 81 if (attribute == null) { 82 throw new IllegalArgumentException ("Attribute may not be null."); 83 } 84 if (cookie == null) { 85 throw new IllegalArgumentException ("Cookie may not be null."); 86 } 87 final String paramName = attribute.getName().toLowerCase(); 88 final String paramValue = attribute.getValue(); 89 90 if (paramName.equals("expires")) { 91 92 if (paramValue == null) { 93 throw new MalformedCookieException( 94 "Missing value for expires attribute"); 95 } 96 try { 97 DateFormat expiryFormat = new SimpleDateFormat ( 98 "EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US); 99 Date date = expiryFormat.parse(paramValue); 100 cookie.setExpiryDate(date); 101 } catch (ParseException e) { 102 throw new MalformedCookieException("Invalid expires " 103 + "attribute: " + e.getMessage()); 104 } 105 } else { 106 super.parseAttribute(attribute, cookie); 107 } 108 } 109 110 122 public void validate(String host, int port, String path, 123 boolean secure, final Cookie cookie) 124 throws MalformedCookieException { 125 126 LOG.trace("enterNetscapeDraftCookieProcessor " 127 + "RCF2109CookieProcessor.validate(Cookie)"); 128 super.validate(host, port, path, secure, cookie); 130 if (host.indexOf(".") >= 0) { 132 int domainParts = new StringTokenizer (cookie.getDomain(), ".") 133 .countTokens(); 134 135 if (isSpecialDomain(cookie.getDomain())) { 136 if (domainParts < 2) { 137 throw new MalformedCookieException("Domain attribute \"" 138 + cookie.getDomain() 139 + "\" violates the Netscape cookie specification for " 140 + "special domains"); 141 } 142 } else { 143 if (domainParts < 3) { 144 throw new MalformedCookieException("Domain attribute \"" 145 + cookie.getDomain() 146 + "\" violates the Netscape cookie specification"); 147 } 148 } 149 } 150 } 151 152 158 private static boolean isSpecialDomain(final String domain) { 159 final String ucDomain = domain.toUpperCase(); 160 if (ucDomain.endsWith(".COM") 161 || ucDomain.endsWith(".EDU") 162 || ucDomain.endsWith(".NET") 163 || ucDomain.endsWith(".GOV") 164 || ucDomain.endsWith(".MIL") 165 || ucDomain.endsWith(".ORG") 166 || ucDomain.endsWith(".INT")) { 167 return true; 168 } 169 return false; 170 } 171 } 172 | Popular Tags |