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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
40  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
41  *     for further details.
42 */
43 
44 @Deprecated
45 public class BasicPathHandler implements CookieAttributeHandler {
46 
BasicPathHandler()47     public BasicPathHandler() {
48         super();
49     }
50 
parse(final SetCookie cookie, String value)51     public void parse(final SetCookie cookie, String value)
52             throws MalformedCookieException {
53         if (cookie == null) {
54             throw new IllegalArgumentException("Cookie may not be null");
55         }
56         if (value == null || value.trim().length() == 0) {
57             value = "/";
58         }
59         cookie.setPath(value);
60     }
61 
validate(final Cookie cookie, final CookieOrigin origin)62     public void validate(final Cookie cookie, final CookieOrigin origin)
63             throws MalformedCookieException {
64         if (!match(cookie, origin)) {
65             throw new MalformedCookieException(
66                 "Illegal path attribute \"" + cookie.getPath()
67                 + "\". Path of origin: \"" + origin.getPath() + "\"");
68         }
69     }
70 
match(final Cookie cookie, final CookieOrigin origin)71     public boolean match(final Cookie cookie, final CookieOrigin origin) {
72         if (cookie == null) {
73             throw new IllegalArgumentException("Cookie may not be null");
74         }
75         if (origin == null) {
76             throw new IllegalArgumentException("Cookie origin may not be null");
77         }
78         String targetpath = origin.getPath();
79         String topmostPath = cookie.getPath();
80         if (topmostPath == null) {
81             topmostPath = "/";
82         }
83         if (topmostPath.length() > 1 && topmostPath.endsWith("/")) {
84             topmostPath = topmostPath.substring(0, topmostPath.length() - 1);
85         }
86         boolean match = targetpath.startsWith (topmostPath);
87         // if there is a match and these values are not exactly the same we have
88         // to make sure we're not matcing "/foobar" and "/foo"
89         if (match && targetpath.length() != topmostPath.length()) {
90             if (!topmostPath.endsWith("/")) {
91                 match = (targetpath.charAt(topmostPath.length()) == '/');
92             }
93         }
94         return match;
95     }
96 
97 }
98