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;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import org.apache.hc.client5.http.auth.AuthCache;
34  import org.apache.hc.client5.http.auth.AuthScheme;
35  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
36  import org.apache.hc.client5.http.auth.CredentialsProvider;
37  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
38  import org.apache.hc.client5.http.cookie.CookieSpecFactory;
39  import org.apache.hc.client5.http.cookie.CookieStore;
40  import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
41  import org.apache.hc.client5.http.impl.auth.BasicScheme;
42  import org.apache.hc.client5.http.protocol.HttpClientContext;
43  import org.apache.hc.client5.http.routing.RoutingSupport;
44  import org.apache.hc.core5.http.HttpHost;
45  import org.apache.hc.core5.http.config.Lookup;
46  import org.apache.hc.core5.util.Args;
47  
48  /**
49   * Abstract {@link HttpClientContext} builder.
50   *
51   * @since 5.4
52   */
53  public abstract class AbstractClientContextBuilder<T extends HttpClientContext> {
54  
55      private final SchemePortResolver schemePortResolver;
56  
57      private Lookup<CookieSpecFactory> cookieSpecRegistry;
58      private Lookup<AuthSchemeFactory> authSchemeRegistry;
59      private CookieStore cookieStore;
60      private CredentialsProvider credentialsProvider;
61      private AuthCache authCache;
62      private Map<HttpHost, AuthScheme> authSchemeMap;
63  
64      protected AbstractClientContextBuilder(final SchemePortResolver schemePortResolver) {
65          this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
66      }
67  
68      public AbstractClientContextBuilder<T> useCookieSpecRegistry(final Lookup<CookieSpecFactory> cookieSpecRegistry) {
69          this.cookieSpecRegistry = cookieSpecRegistry;
70          return this;
71      }
72  
73      public AbstractClientContextBuilder<T> useAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
74          this.authSchemeRegistry = authSchemeRegistry;
75          return this;
76      }
77  
78      public AbstractClientContextBuilder<T> useCookieStore(final CookieStore cookieStore) {
79          this.cookieStore = cookieStore;
80          return this;
81      }
82  
83      public AbstractClientContextBuilder<T> useCredentialsProvider(final CredentialsProvider credentialsProvider) {
84          this.credentialsProvider = credentialsProvider;
85          return this;
86      }
87  
88      public AbstractClientContextBuilder<T> useAuthCache(final AuthCache authCache) {
89          this.authCache = authCache;
90          return this;
91      }
92  
93      public AbstractClientContextBuilder<T> preemptiveAuth(final HttpHost host, final AuthScheme authScheme) {
94          Args.notNull(host, "HTTP host");
95          if (authSchemeMap == null) {
96              authSchemeMap = new HashMap<>();
97          }
98          authSchemeMap.put(RoutingSupport.normalize(host, schemePortResolver), authScheme);
99          return this;
100     }
101 
102     public AbstractClientContextBuilder<T> preemptiveBasicAuth(final HttpHost host, final UsernamePasswordCredentials credentials) {
103         Args.notNull(host, "HTTP host");
104         final BasicScheme authScheme = new BasicScheme();
105         authScheme.initPreemptive(credentials);
106         preemptiveAuth(host, authScheme);
107         return this;
108     }
109 
110     protected abstract T createContext();
111 
112     public T build() {
113         final T context = createContext();
114         context.setCookieSpecRegistry(cookieSpecRegistry);
115         context.setAuthSchemeRegistry(authSchemeRegistry);
116         context.setCookieStore(cookieStore);
117         context.setCredentialsProvider(credentialsProvider);
118         context.setAuthCache(authCache);
119         if (authSchemeMap != null) {
120             for (final Map.Entry<HttpHost, AuthScheme> entry : authSchemeMap.entrySet()) {
121                 context.resetAuthExchange(entry.getKey(), entry.getValue());
122             }
123         }
124         return context;
125     }
126 
127 }