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.hc.client5.http.impl.cookie;
29  
30  import org.apache.hc.client5.http.cookie.Cookie;
31  import org.apache.hc.client5.http.cookie.CookieOrigin;
32  import org.apache.hc.client5.http.cookie.CookieSpec;
33  import org.apache.hc.client5.http.cookie.CookieSpecFactory;
34  import org.apache.hc.client5.http.cookie.MalformedCookieException;
35  import org.apache.hc.client5.http.psl.PublicSuffixMatcher;
36  import org.apache.hc.core5.annotation.Contract;
37  import org.apache.hc.core5.annotation.ThreadingBehavior;
38  import org.apache.hc.core5.http.protocol.HttpContext;
39  
40  /**
41   * {@link CookieSpecFactory} implementation that provides an instance of
42   * RFC 6265 conformant cookie policy. The instance returned by this factory
43   * can be shared by multiple threads.
44   *
45   * @since 4.4
46   */
47  @Contract(threading = ThreadingBehavior.SAFE)
48  public class RFC6265CookieSpecFactory implements CookieSpecFactory {
49  
50      public enum CompatibilityLevel {
51          STRICT,
52          RELAXED,
53          IE_MEDIUM_SECURITY
54      }
55  
56      private final CompatibilityLevel compatibilityLevel;
57      private final PublicSuffixMatcher publicSuffixMatcher;
58  
59      private volatile CookieSpec cookieSpec;
60  
61      public RFC6265CookieSpecFactory(
62              final CompatibilityLevel compatibilityLevel,
63              final PublicSuffixMatcher publicSuffixMatcher) {
64          super();
65          this.compatibilityLevel = compatibilityLevel != null ? compatibilityLevel : CompatibilityLevel.RELAXED;
66          this.publicSuffixMatcher = publicSuffixMatcher;
67      }
68  
69      public RFC6265CookieSpecFactory(final PublicSuffixMatcher publicSuffixMatcher) {
70          this(CompatibilityLevel.RELAXED, publicSuffixMatcher);
71      }
72  
73      public RFC6265CookieSpecFactory() {
74          this(CompatibilityLevel.RELAXED, null);
75      }
76  
77      @Override
78      public CookieSpec create(final HttpContext context) {
79          if (cookieSpec == null) {
80              synchronized (this) {
81                  if (cookieSpec == null) {
82                      switch (this.compatibilityLevel) {
83                          case STRICT:
84                              this.cookieSpec = new RFC6265StrictSpec(
85                                      new BasicPathHandler(),
86                                      PublicSuffixDomainFilter.decorate(
87                                              new BasicDomainHandler(), this.publicSuffixMatcher),
88                                      new BasicMaxAgeHandler(),
89                                      new BasicSecureHandler(),
90                                      new BasicExpiresHandler(RFC6265StrictSpec.DATE_PATTERNS));
91                              break;
92                          case IE_MEDIUM_SECURITY:
93                              this.cookieSpec = new RFC6265LaxSpec(
94                                      new BasicPathHandler() {
95                                          @Override
96                                          public void validate(
97                                                  final Cookie cookie,
98                                                  final CookieOrigin origin) throws MalformedCookieException {
99                                              // No validation
100                                         }
101                                     },
102                                     PublicSuffixDomainFilter.decorate(
103                                             new BasicDomainHandler(), this.publicSuffixMatcher),
104                                     new BasicMaxAgeHandler(),
105                                     new BasicSecureHandler(),
106                                     new BasicExpiresHandler(RFC6265StrictSpec.DATE_PATTERNS));
107                             break;
108                         default:
109                             this.cookieSpec = new RFC6265LaxSpec(
110                                     new BasicPathHandler(),
111                                     PublicSuffixDomainFilter.decorate(
112                                             new BasicDomainHandler(), this.publicSuffixMatcher),
113                                     new LaxMaxAgeHandler(),
114                                     new BasicSecureHandler(),
115                                     new LaxExpiresHandler());
116                     }
117                 }
118             }
119         }
120         return this.cookieSpec;
121     }
122 
123 }