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.Contract;
38  import org.apache.http.annotation.ThreadingBehavior;
39  import org.apache.http.cookie.CommonCookieAttributeHandler;
40  import org.apache.http.cookie.Cookie;
41  import org.apache.http.cookie.CookieAttributeHandler;
42  import org.apache.http.cookie.CookieOrigin;
43  import org.apache.http.cookie.MalformedCookieException;
44  import org.apache.http.util.Args;
45  
46  /**
47   * Cookie management functions shared by all specification.
48   *
49   * @since 4.0
50   */
51  @Contract(threading = ThreadingBehavior.SAFE)
52  public abstract class CookieSpecBase extends AbstractCookieSpec {
53  
54      public CookieSpecBase() {
55          super();
56      }
57  
58      /**
59       * @since 4.4
60       */
61      protected CookieSpecBase(final HashMap<String, CookieAttributeHandler> map) {
62          super(map);
63      }
64  
65      /**
66       * @since 4.4
67       */
68      protected CookieSpecBase(final CommonCookieAttributeHandler... handlers) {
69          super(handlers);
70      }
71  
72      protected static String getDefaultPath(final CookieOrigin origin) {
73          String defaultPath = origin.getPath();
74          int lastSlashIndex = defaultPath.lastIndexOf('/');
75          if (lastSlashIndex >= 0) {
76              if (lastSlashIndex == 0) {
77                  //Do not remove the very first slash
78                  lastSlashIndex = 1;
79              }
80              defaultPath = defaultPath.substring(0, lastSlashIndex);
81          }
82          return defaultPath;
83      }
84  
85      protected static String getDefaultDomain(final CookieOrigin origin) {
86          return origin.getHost();
87      }
88  
89      protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin)
90                  throws MalformedCookieException {
91          final List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
92          for (final HeaderElement headerelement : elems) {
93              final String name = headerelement.getName();
94              final String value = headerelement.getValue();
95              if (name == null || name.isEmpty()) {
96                  continue;
97              }
98  
99              final BasicClientCookieentCookie.html#BasicClientCookie">BasicClientCookie cookie = new BasicClientCookie(name, value);
100             cookie.setPath(getDefaultPath(origin));
101             cookie.setDomain(getDefaultDomain(origin));
102 
103             // cycle through the parameters
104             final NameValuePair[] attribs = headerelement.getParameters();
105             for (int j = attribs.length - 1; j >= 0; j--) {
106                 final NameValuePair attrib = attribs[j];
107                 final String s = attrib.getName().toLowerCase(Locale.ROOT);
108 
109                 cookie.setAttribute(s, attrib.getValue());
110 
111                 final CookieAttributeHandler handler = findAttribHandler(s);
112                 if (handler != null) {
113                     handler.parse(cookie, attrib.getValue());
114                 }
115             }
116             cookies.add(cookie);
117         }
118         return cookies;
119     }
120 
121     @Override
122     public void validate(final Cookie cookie, final CookieOrigin origin)
123             throws MalformedCookieException {
124         Args.notNull(cookie, "Cookie");
125         Args.notNull(origin, "Cookie origin");
126         for (final CookieAttributeHandler handler: getAttribHandlers()) {
127             handler.validate(cookie, origin);
128         }
129     }
130 
131     @Override
132     public boolean match(final Cookie cookie, final CookieOrigin origin) {
133         Args.notNull(cookie, "Cookie");
134         Args.notNull(origin, "Cookie origin");
135         for (final CookieAttributeHandler handler: getAttribHandlers()) {
136             if (!handler.match(cookie, origin)) {
137                 return false;
138             }
139         }
140         return true;
141     }
142 
143 }