View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.cookie;
29  
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Locale;
34  
35  import org.apache.http.HeaderElement;
36  import org.apache.http.NameValuePair;
37  import org.apache.http.annotation.ThreadSafe;
38  import org.apache.http.cookie.CommonCookieAttributeHandler;
39  import org.apache.http.cookie.Cookie;
40  import org.apache.http.cookie.CookieAttributeHandler;
41  import org.apache.http.cookie.CookieOrigin;
42  import org.apache.http.cookie.MalformedCookieException;
43  import org.apache.http.util.Args;
44  
45  /**
46   * Cookie management functions shared by all specification.
47   *
48   * @since 4.0
49   */
50  @ThreadSafe
51  public abstract class CookieSpecBase extends AbstractCookieSpec {
52  
53      public CookieSpecBase() {
54          super();
55      }
56  
57      /**
58       * @since 4.4
59       */
60      protected CookieSpecBase(final HashMap<String, CookieAttributeHandler> map) {
61          super(map);
62      }
63  
64      /**
65       * @since 4.4
66       */
67      protected CookieSpecBase(final CommonCookieAttributeHandler... handlers) {
68          super(handlers);
69      }
70  
71      protected static String getDefaultPath(final CookieOrigin origin) {
72          String defaultPath = origin.getPath();
73          int lastSlashIndex = defaultPath.lastIndexOf('/');
74          if (lastSlashIndex >= 0) {
75              if (lastSlashIndex == 0) {
76                  //Do not remove the very first slash
77                  lastSlashIndex = 1;
78              }
79              defaultPath = defaultPath.substring(0, lastSlashIndex);
80          }
81          return defaultPath;
82      }
83  
84      protected static String getDefaultDomain(final CookieOrigin origin) {
85          return origin.getHost();
86      }
87  
88      protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin)
89                  throws MalformedCookieException {
90          final List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
91          for (final HeaderElement headerelement : elems) {
92              final String name = headerelement.getName();
93              final String value = headerelement.getValue();
94              if (name == null || name.isEmpty()) {
95                  throw new MalformedCookieException("Cookie name may not be empty");
96              }
97  
98              final BasicClientCookie cookie = new BasicClientCookie(name, value);
99              cookie.setPath(getDefaultPath(origin));
100             cookie.setDomain(getDefaultDomain(origin));
101 
102             // cycle through the parameters
103             final NameValuePair[] attribs = headerelement.getParameters();
104             for (int j = attribs.length - 1; j >= 0; j--) {
105                 final NameValuePair attrib = attribs[j];
106                 final String s = attrib.getName().toLowerCase(Locale.ROOT);
107 
108                 cookie.setAttribute(s, attrib.getValue());
109 
110                 final CookieAttributeHandler handler = findAttribHandler(s);
111                 if (handler != null) {
112                     handler.parse(cookie, attrib.getValue());
113                 }
114             }
115             cookies.add(cookie);
116         }
117         return cookies;
118     }
119 
120     @Override
121     public void validate(final Cookie cookie, final CookieOrigin origin)
122             throws MalformedCookieException {
123         Args.notNull(cookie, "Cookie");
124         Args.notNull(origin, "Cookie origin");
125         for (final CookieAttributeHandler handler: getAttribHandlers()) {
126             handler.validate(cookie, origin);
127         }
128     }
129 
130     @Override
131     public boolean match(final Cookie cookie, final CookieOrigin origin) {
132         Args.notNull(cookie, "Cookie");
133         Args.notNull(origin, "Cookie origin");
134         for (final CookieAttributeHandler handler: getAttribHandlers()) {
135             if (!handler.match(cookie, origin)) {
136                 return false;
137             }
138         }
139         return true;
140     }
141 
142 }