View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/RFC2109Spec.java,v 1.13 2003/03/07 18:23:46 olegk Exp $ 3 * $Revision: 1.13 $ 4 * $Date: 2003/03/07 18:23:46 $ 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 org.apache.commons.httpclient.NameValuePair; 67 import org.apache.commons.httpclient.Cookie; 68 69 /*** 70 * <p>RFC 2109 specific cookie management functions 71 * 72 * @author B.C. Holmes 73 * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a> 74 * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a> 75 * @author Rod Waldhoff 76 * @author dIon Gillard 77 * @author Sean C. Sullivan 78 * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a> 79 * @author Marc A. Saegesser 80 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 81 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 82 * 83 * @since 2.0 84 */ 85 86 public class RFC2109Spec extends CookieSpecBase { 87 88 /*** Default constructor */ 89 public RFC2109Spec() { 90 super(); 91 } 92 93 94 /*** 95 * Parse RFC 2109 specific cookie attribute and update the corresponsing 96 * {@link Cookie} properties. 97 * 98 * @param attribute {@link NameValuePair} cookie attribute from the 99 * <tt>Set- Cookie</tt> 100 * @param cookie {@link Cookie} to be updated 101 * @throws MalformedCookieException if an exception occurs during parsing 102 */ 103 public void parseAttribute( 104 final NameValuePair attribute, final Cookie cookie) 105 throws MalformedCookieException { 106 107 if (attribute == null) { 108 throw new IllegalArgumentException("Attribute may not be null."); 109 } 110 if (cookie == null) { 111 throw new IllegalArgumentException("Cookie may not be null."); 112 } 113 final String paramName = attribute.getName().toLowerCase(); 114 final String paramValue = attribute.getValue(); 115 116 if (paramName.equals("version")) { 117 118 if (paramValue == null) { 119 throw new MalformedCookieException( 120 "Missing value for version attribute"); 121 } 122 try { 123 cookie.setVersion(Integer.parseInt(paramValue)); 124 } catch (NumberFormatException e) { 125 throw new MalformedCookieException("Invalid version: " 126 + e.getMessage()); 127 } 128 129 } else { 130 super.parseAttribute(attribute, cookie); 131 } 132 } 133 134 /*** 135 * Performs RFC 2109 compliant {@link Cookie} validation 136 * 137 * @param host the host from which the {@link Cookie} was received 138 * @param port the port from which the {@link Cookie} was received 139 * @param path the path from which the {@link Cookie} was received 140 * @param secure <tt>true</tt> when the {@link Cookie} was received using a 141 * secure connection 142 * @param cookie The cookie to validate 143 * @throws MalformedCookieException if an exception occurs during 144 * validation 145 */ 146 public void validate(String host, int port, String path, 147 boolean secure, final Cookie cookie) throws MalformedCookieException { 148 149 LOG.trace("enter RFC2109Spec.validate(String, int, String, " 150 + "boolean, Cookie)"); 151 152 // Perform generic validation 153 super.validate(host, port, path, secure, cookie); 154 // Perform RFC 2109 specific validation 155 if (cookie.isDomainAttributeSpecified() 156 && (!cookie.getDomain().equals(host))) { 157 158 // domain must start with dot 159 if (!cookie.getDomain().startsWith(".")) { 160 throw new MalformedCookieException("Domain attribute \"" 161 + cookie.getDomain() 162 + "\" violates RFC 2109: domain must start with a dot"); 163 } 164 // domain must have at least one embedded dot 165 int dotIndex = cookie.getDomain().indexOf('.', 1); 166 if (dotIndex < 0 || dotIndex == cookie.getDomain().length() - 1) { 167 throw new MalformedCookieException("Domain attribute \"" 168 + cookie.getDomain() 169 + "\" violates RFC 2109: domain must contain an embedded dot"); 170 } 171 host = host.toLowerCase(); 172 if (host.indexOf('.') >= 0) { 173 if (!host.endsWith(cookie.getDomain())) { 174 throw new MalformedCookieException( 175 "Illegal domain attribute \"" + cookie.getDomain() 176 + "\". Domain of origin: \"" + host + "\""); 177 } 178 // host minus domain may not contain any dots 179 String hostWithoutDomain = host.substring(0, host.length() 180 - cookie.getDomain().length()); 181 if (hostWithoutDomain.indexOf('.') != -1) { 182 throw new MalformedCookieException("Domain attribute \"" 183 + cookie.getDomain() 184 + "\" violates RFC 2109: host minus domain may not contain any dots"); 185 } 186 } 187 } 188 } 189 190 191 /*** 192 * Return a name/value string suitable for sending in a <tt>"Cookie"</tt> 193 * header as defined in RFC 2109 for backward compatibility with cookie 194 * version 0 195 * @param name The name. 196 * @param value The value 197 * @param version The cookie version 198 * @return a string suitable for sending in a <tt>"Cookie"</tt> header. 199 */ 200 201 private String formatNameValuePair( 202 final String name, final String value, int version) { 203 204 final StringBuffer buffer = new StringBuffer(); 205 if (version < 1) { 206 buffer.append(name); 207 buffer.append("="); 208 if (value != null) { 209 buffer.append(value); 210 } 211 } else { 212 buffer.append(name); 213 buffer.append("=\""); 214 if (value != null) { 215 buffer.append(value); 216 } 217 buffer.append("\""); 218 } 219 return buffer.toString(); 220 } 221 222 /*** 223 * Return a string suitable for sending in a <tt>"Cookie"</tt> header 224 * as defined in RFC 2109 for backward compatibility with cookie version 0 225 * @param cookie a {@link Cookie} to be formatted as string 226 * @param version The version to use. 227 * @return a string suitable for sending in a <tt>"Cookie"</tt> header. 228 */ 229 private String formatCookieAsVer(Cookie cookie, int version) { 230 LOG.trace("enter RFC2109Spec.formatCookieAsVer(Cookie)"); 231 if (cookie == null) { 232 throw new IllegalArgumentException("Cookie may not be null"); 233 } 234 StringBuffer buf = new StringBuffer(); 235 buf.append(formatNameValuePair(cookie.getName(), 236 cookie.getValue(), version)); 237 if (cookie.getDomain() != null 238 && cookie.isDomainAttributeSpecified()) { 239 240 buf.append("; "); 241 buf.append(formatNameValuePair("$Domain", 242 cookie.getDomain(), version)); 243 } 244 if (cookie.getPath() != null && cookie.isPathAttributeSpecified()) { 245 buf.append("; "); 246 buf.append(formatNameValuePair("$Path", cookie.getPath(), version)); 247 } 248 return buf.toString(); 249 } 250 251 252 /*** 253 * Return a string suitable for sending in a <tt>"Cookie"</tt> header as 254 * defined in RFC 2109 255 * @param cookie a {@link Cookie} to be formatted as string 256 * @return a string suitable for sending in a <tt>"Cookie"</tt> header. 257 */ 258 public String formatCookie(Cookie cookie) { 259 LOG.trace("enter RFC2109Spec.formatCookie(Cookie)"); 260 if (cookie == null) { 261 throw new IllegalArgumentException("Cookie may not be null"); 262 } 263 int ver = cookie.getVersion(); 264 StringBuffer buffer = new StringBuffer(); 265 buffer.append(formatNameValuePair("$Version", 266 Integer.toString(ver), ver)); 267 buffer.append("; "); 268 buffer.append(formatCookieAsVer(cookie, ver)); 269 return buffer.toString(); 270 } 271 272 /*** 273 * Create a RFC 2109 compliant <tt>"Cookie"</tt> header value containing all 274 * {@link Cookie}s in <i>cookies</i> suitable for sending in a <tt>"Cookie" 275 * </tt> header 276 * @param cookies an array of {@link Cookie}s to be formatted 277 * @return a string suitable for sending in a Cookie header. 278 */ 279 public String formatCookies(Cookie[] cookies) { 280 LOG.trace("enter RFC2109Spec.formatCookieHeader(Cookie[])"); 281 int version = Integer.MAX_VALUE; 282 // Pick the lowerest common denominator 283 for (int i = 0; i < cookies.length; i++) { 284 Cookie cookie = cookies[i]; 285 if (cookie.getVersion() < version) { 286 version = cookie.getVersion(); 287 } 288 } 289 final StringBuffer buffer = new StringBuffer(); 290 buffer.append(formatNameValuePair("$Version", 291 Integer.toString(version), version)); 292 for (int i = 0; i < cookies.length; i++) { 293 buffer.append("; "); 294 buffer.append(formatCookieAsVer(cookies[i], version)); 295 } 296 return buffer.toString(); 297 } 298 }

This page was automatically generated by Maven