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 org.apache.http.annotation.Contract;
31  import org.apache.http.annotation.ThreadingBehavior;
32  import org.apache.http.conn.util.PublicSuffixMatcher;
33  import org.apache.http.cookie.Cookie;
34  import org.apache.http.cookie.CookieOrigin;
35  import org.apache.http.cookie.CookieSpec;
36  import org.apache.http.cookie.CookieSpecProvider;
37  import org.apache.http.cookie.MalformedCookieException;
38  import org.apache.http.protocol.HttpContext;
39  
40  /**
41   * {@link org.apache.http.cookie.CookieSpecProvider} implementation that provides an instance of
42   * {@link org.apache.http.impl.cookie.DefaultCookieSpec}. The instance returned by this factory can
43   * be shared by multiple threads.
44   *
45   * @since 4.4
46   */
47  @Contract(threading = ThreadingBehavior.IMMUTABLE)
48  public class DefaultCookieSpecProvider implements CookieSpecProvider {
49  
50      public enum CompatibilityLevel {
51          DEFAULT,
52          IE_MEDIUM_SECURITY
53      }
54  
55      private final CompatibilityLevel compatibilityLevel;
56      private final PublicSuffixMatcher publicSuffixMatcher;
57      private final String[] datepatterns;
58      private final boolean oneHeader;
59  
60      private volatile CookieSpec cookieSpec;
61  
62      public DefaultCookieSpecProvider(
63              final CompatibilityLevel compatibilityLevel,
64              final PublicSuffixMatcher publicSuffixMatcher,
65              final String[] datepatterns,
66              final boolean oneHeader) {
67          super();
68          this.compatibilityLevel = compatibilityLevel != null ? compatibilityLevel : CompatibilityLevel.DEFAULT;
69          this.publicSuffixMatcher = publicSuffixMatcher;
70          this.datepatterns = datepatterns;
71          this.oneHeader = oneHeader;
72      }
73  
74      public DefaultCookieSpecProvider(
75              final CompatibilityLevel compatibilityLevel,
76              final PublicSuffixMatcher publicSuffixMatcher) {
77          this(compatibilityLevel, publicSuffixMatcher, null, false);
78      }
79  
80      public DefaultCookieSpecProvider(final PublicSuffixMatcher publicSuffixMatcher) {
81          this(CompatibilityLevel.DEFAULT, publicSuffixMatcher, null, false);
82      }
83  
84      public DefaultCookieSpecProvider() {
85          this(CompatibilityLevel.DEFAULT, null, null, false);
86      }
87  
88      @Override
89      public CookieSpec create(final HttpContext context) {
90          if (cookieSpec == null) {
91              synchronized (this) {
92                  if (cookieSpec == null) {
93                      final RFC2965Spec strict = new RFC2965Spec(this.oneHeader,
94                              new RFC2965VersionAttributeHandler(),
95                              new BasicPathHandler(),
96                              PublicSuffixDomainFilter.decorate(
97                                      new RFC2965DomainAttributeHandler(), this.publicSuffixMatcher),
98                              new RFC2965PortAttributeHandler(),
99                              new BasicMaxAgeHandler(),
100                             new BasicSecureHandler(),
101                             new BasicCommentHandler(),
102                             new RFC2965CommentUrlAttributeHandler(),
103                             new RFC2965DiscardAttributeHandler());
104                     final RFC2109Spec obsoleteStrict = new RFC2109Spec(this.oneHeader,
105                             new RFC2109VersionHandler(),
106                             new BasicPathHandler(),
107                             PublicSuffixDomainFilter.decorate(
108                                     new RFC2109DomainHandler(), this.publicSuffixMatcher),
109                             new BasicMaxAgeHandler(),
110                             new BasicSecureHandler(),
111                             new BasicCommentHandler());
112                     final NetscapeDraftSpec netscapeDraft = new NetscapeDraftSpec(
113                             PublicSuffixDomainFilter.decorate(
114                                     new BasicDomainHandler(), this.publicSuffixMatcher),
115                             this.compatibilityLevel == CompatibilityLevel.IE_MEDIUM_SECURITY ?
116                                     new BasicPathHandler() {
117                                         @Override
118                                         public void validate(
119                                                 final Cookie cookie,
120                                                 final CookieOrigin origin) throws MalformedCookieException {
121                                             // No validation
122                                         }
123                                     } : new BasicPathHandler(),
124                             new BasicSecureHandler(),
125                             new BasicCommentHandler(),
126                             new BasicExpiresHandler(this.datepatterns != null ? this.datepatterns.clone() :
127                                     new String[]{NetscapeDraftSpec.EXPIRES_PATTERN}));
128                     this.cookieSpec = new DefaultCookieSpec(strict, obsoleteStrict, netscapeDraft);
129                 }
130             }
131         }
132         return this.cookieSpec;
133     }
134 
135 }