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  package org.apache.hc.client5.http.cache;
28  
29  import org.apache.hc.client5.http.AbstractClientContextBuilder;
30  import org.apache.hc.client5.http.SchemePortResolver;
31  import org.apache.hc.client5.http.auth.AuthCache;
32  import org.apache.hc.client5.http.auth.AuthScheme;
33  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
34  import org.apache.hc.client5.http.auth.CredentialsProvider;
35  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
36  import org.apache.hc.client5.http.cookie.CookieSpecFactory;
37  import org.apache.hc.client5.http.cookie.CookieStore;
38  import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
39  import org.apache.hc.core5.http.HttpHost;
40  import org.apache.hc.core5.http.config.Lookup;
41  
42  public class CacheContextBuilder extends AbstractClientContextBuilder<HttpCacheContext> {
43  
44      public static CacheContextBuilder create(final SchemePortResolver schemePortResolver) {
45          return new CacheContextBuilder(schemePortResolver);
46      }
47  
48      public static CacheContextBuilder create() {
49          return new CacheContextBuilder(DefaultSchemePortResolver.INSTANCE);
50      }
51  
52      private RequestCacheControl cacheControl;
53  
54      protected CacheContextBuilder(final SchemePortResolver schemePortResolver) {
55          super(schemePortResolver);
56      }
57  
58      @Override
59      public CacheContextBuilder useCookieSpecRegistry(final Lookup<CookieSpecFactory> cookieSpecRegistry) {
60          super.useCookieSpecRegistry(cookieSpecRegistry);
61          return this;
62      }
63  
64      @Override
65      public CacheContextBuilder useAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
66          super.useAuthSchemeRegistry(authSchemeRegistry);
67          return this;
68      }
69  
70      @Override
71      public CacheContextBuilder useCookieStore(final CookieStore cookieStore) {
72          super.useCookieStore(cookieStore);
73          return this;
74      }
75  
76      @Override
77      public CacheContextBuilder useCredentialsProvider(final CredentialsProvider credentialsProvider) {
78          super.useCredentialsProvider(credentialsProvider);
79          return this;
80      }
81  
82      @Override
83      public CacheContextBuilder useAuthCache(final AuthCache authCache) {
84          super.useAuthCache(authCache);
85          return this;
86      }
87  
88      @Override
89      public CacheContextBuilder preemptiveAuth(final HttpHost host, final AuthScheme authScheme) {
90          super.preemptiveAuth(host, authScheme);
91          return this;
92      }
93  
94      @Override
95      public CacheContextBuilder preemptiveBasicAuth(final HttpHost host, final UsernamePasswordCredentials credentials) {
96          super.preemptiveBasicAuth(host, credentials);
97          return this;
98      }
99  
100     public CacheContextBuilder setCacheControl(final RequestCacheControl cacheControl) {
101         this.cacheControl = cacheControl;
102         return this;
103     }
104 
105     @Override
106     protected HttpCacheContext createContext() {
107         return HttpCacheContext.create();
108     }
109 
110     @Override
111     public HttpCacheContext build() {
112         final HttpCacheContext context =  super.build();
113         context.setRequestCacheControl(cacheControl);
114         return context;
115     }
116 
117 }