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