View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java,v 1.7 2003/01/28 04:40:23 jsdever Exp $ 3 * $Revision: 1.7 $ 4 * $Date: 2003/01/28 04:40:23 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient.cookie; 65 66 import java.util.StringTokenizer; 67 import java.util.Date; 68 import java.util.Locale; 69 import java.text.DateFormat; 70 import java.text.SimpleDateFormat; 71 import java.text.ParseException; 72 import org.apache.commons.httpclient.NameValuePair; 73 import org.apache.commons.httpclient.Cookie; 74 75 /*** 76 * <P>Netscape cookie draft specific cookie management functions 77 * 78 * @author B.C. Holmes 79 * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a> 80 * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a> 81 * @author Rod Waldhoff 82 * @author dIon Gillard 83 * @author Sean C. Sullivan 84 * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a> 85 * @author Marc A. Saegesser 86 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 87 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 88 * 89 * @since 2.0 90 */ 91 92 public class NetscapeDraftSpec extends CookieSpecBase { 93 94 /*** Default constructor */ 95 public NetscapeDraftSpec() { 96 super(); 97 } 98 99 100 /*** 101 * Parse the cookie attribute and update the corresponsing {@link Cookie} 102 * properties as defined by the Netscape draft specification 103 * 104 * @param attribute {@link NameValuePair} cookie attribute from the 105 * <tt>Set- Cookie</tt> 106 * @param cookie {@link Cookie} to be updated 107 * @throws MalformedCookieException if an exception occurs during parsing 108 */ 109 public void parseAttribute( 110 final NameValuePair attribute, final Cookie cookie) 111 throws MalformedCookieException { 112 113 if (attribute == null) { 114 throw new IllegalArgumentException("Attribute may not be null."); 115 } 116 if (cookie == null) { 117 throw new IllegalArgumentException("Cookie may not be null."); 118 } 119 final String paramName = attribute.getName().toLowerCase(); 120 final String paramValue = attribute.getValue(); 121 122 if (paramName.equals("expires")) { 123 124 if (paramValue == null) { 125 throw new MalformedCookieException( 126 "Missing value for expires attribute"); 127 } 128 try { 129 DateFormat expiryFormat = new SimpleDateFormat( 130 "EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US); 131 Date date = expiryFormat.parse(paramValue); 132 cookie.setExpiryDate(date); 133 } catch (ParseException e) { 134 throw new MalformedCookieException("Invalid expires " 135 + "attribute: " + e.getMessage()); 136 } 137 } else { 138 super.parseAttribute(attribute, cookie); 139 } 140 } 141 142 /*** 143 * Performs Netscape draft compliant {@link Cookie} validation 144 * 145 * @param host the host from which the {@link Cookie} was received 146 * @param port the port from which the {@link Cookie} was received 147 * @param path the path from which the {@link Cookie} was received 148 * @param secure <tt>true</tt> when the {@link Cookie} was received 149 * using a secure connection 150 * @param cookie The cookie to validate. 151 * @throws MalformedCookieException if an exception occurs during 152 * validation 153 */ 154 public void validate(String host, int port, String path, 155 boolean secure, final Cookie cookie) 156 throws MalformedCookieException { 157 158 LOG.trace("enterNetscapeDraftCookieProcessor " 159 + "RCF2109CookieProcessor.validate(Cookie)"); 160 // Perform generic validation 161 super.validate(host, port, path, secure, cookie); 162 // Perform Netscape Cookie draft specific validation 163 if (host.indexOf(".") >= 0) { 164 int domainParts = new StringTokenizer(cookie.getDomain(), ".") 165 .countTokens(); 166 167 if (isSpecialDomain(cookie.getDomain())) { 168 if (domainParts < 2) { 169 throw new MalformedCookieException("Domain attribute \"" 170 + cookie.getDomain() 171 + "\" violates the Netscape cookie specification for " 172 + "special domains"); 173 } 174 } else { 175 if (domainParts < 3) { 176 throw new MalformedCookieException("Domain attribute \"" 177 + cookie.getDomain() 178 + "\" violates the Netscape cookie specification"); 179 } 180 } 181 } 182 } 183 184 /*** 185 * Checks if the given domain is in one of the seven special 186 * top level domains defined by the Netscape cookie specification. 187 * @param domain The domain. 188 * @return True if the specified domain is "special" 189 */ 190 private static boolean isSpecialDomain(final String domain) { 191 final String ucDomain = domain.toUpperCase(); 192 if (ucDomain.endsWith(".COM") 193 || ucDomain.endsWith(".EDU") 194 || ucDomain.endsWith(".NET") 195 || ucDomain.endsWith(".GOV") 196 || ucDomain.endsWith(".MIL") 197 || ucDomain.endsWith(".ORG") 198 || ucDomain.endsWith(".INT")) { 199 return true; 200 } 201 return false; 202 } 203 }

This page was automatically generated by Maven