Home | History | Annotate | Download | only in cookie
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/BasicPathHandler.java $
      3  * $Revision: 653041 $
      4  * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 package org.apache.http.impl.cookie;
     32 
     33 import org.apache.http.cookie.Cookie;
     34 import org.apache.http.cookie.CookieAttributeHandler;
     35 import org.apache.http.cookie.CookieOrigin;
     36 import org.apache.http.cookie.MalformedCookieException;
     37 import org.apache.http.cookie.SetCookie;
     38 
     39 public class BasicPathHandler implements CookieAttributeHandler {
     40 
     41     public BasicPathHandler() {
     42         super();
     43     }
     44 
     45     public void parse(final SetCookie cookie, String value)
     46             throws MalformedCookieException {
     47         if (cookie == null) {
     48             throw new IllegalArgumentException("Cookie may not be null");
     49         }
     50         if (value == null || value.trim().length() == 0) {
     51             value = "/";
     52         }
     53         cookie.setPath(value);
     54     }
     55 
     56     public void validate(final Cookie cookie, final CookieOrigin origin)
     57             throws MalformedCookieException {
     58         if (!match(cookie, origin)) {
     59             throw new MalformedCookieException(
     60                 "Illegal path attribute \"" + cookie.getPath()
     61                 + "\". Path of origin: \"" + origin.getPath() + "\"");
     62         }
     63     }
     64 
     65     public boolean match(final Cookie cookie, final CookieOrigin origin) {
     66         if (cookie == null) {
     67             throw new IllegalArgumentException("Cookie may not be null");
     68         }
     69         if (origin == null) {
     70             throw new IllegalArgumentException("Cookie origin may not be null");
     71         }
     72         String targetpath = origin.getPath();
     73         String topmostPath = cookie.getPath();
     74         if (topmostPath == null) {
     75             topmostPath = "/";
     76         }
     77         if (topmostPath.length() > 1 && topmostPath.endsWith("/")) {
     78             topmostPath = topmostPath.substring(0, topmostPath.length() - 1);
     79         }
     80         boolean match = targetpath.startsWith (topmostPath);
     81         // if there is a match and these values are not exactly the same we have
     82         // to make sure we're not matcing "/foobar" and "/foo"
     83         if (match && targetpath.length() != topmostPath.length()) {
     84             if (!topmostPath.endsWith("/")) {
     85                 match = (targetpath.charAt(topmostPath.length()) == '/');
     86             }
     87         }
     88         return match;
     89     }
     90 
     91 }
     92