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   * @param <T> The HttpClientContext type or subtype.
52   * @since 5.4
53   */
54  public abstract class AbstractClientContextBuilder<T extends HttpClientContext> {
55  
56      private final SchemePortResolver schemePortResolver;
57  
58      private Lookup<CookieSpecFactory> cookieSpecRegistry;
59      private Lookup<AuthSchemeFactory> authSchemeRegistry;
60      private CookieStore cookieStore;
61      private CredentialsProvider credentialsProvider;
62      private AuthCache authCache;
63      private Map<HttpHost, AuthScheme> authSchemeMap;
64  
65      protected AbstractClientContextBuilder(final SchemePortResolver schemePortResolver) {
66          this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
67      }
68  
69      public AbstractClientContextBuilder<T> useCookieSpecRegistry(final Lookup<CookieSpecFactory> cookieSpecRegistry) {
70          this.cookieSpecRegistry = cookieSpecRegistry;
71          return this;
72      }
73  
74      public AbstractClientContextBuilder<T> useAuthSchemeRegistry(final Lookup<AuthSchemeFactory> authSchemeRegistry) {
75          this.authSchemeRegistry = authSchemeRegistry;
76          return this;
77      }
78  
79      public AbstractClientContextBuilder<T> useCookieStore(final CookieStore cookieStore) {
80          this.cookieStore = cookieStore;
81          return this;
82      }
83  
84      public AbstractClientContextBuilder<T> useCredentialsProvider(final CredentialsProvider credentialsProvider) {
85          this.credentialsProvider = credentialsProvider;
86          return this;
87      }
88  
89      public AbstractClientContextBuilder<T> useAuthCache(final AuthCache authCache) {
90          this.authCache = authCache;
91          return this;
92      }
93  
94      public AbstractClientContextBuilder<T> preemptiveAuth(final HttpHost host, final AuthScheme authScheme) {
95          Args.notNull(host, "HTTP host");
96          if (authSchemeMap == null) {
97              authSchemeMap = new HashMap<>();
98          }
99          authSchemeMap.put(RoutingSupport.normalize(host, schemePortResolver), authScheme);
100         return this;
101     }
102 
103     public AbstractClientContextBuilder<T> preemptiveBasicAuth(final HttpHost host, final UsernamePasswordCredentials credentials) {
104         Args.notNull(host, "HTTP host");
105         final BasicScheme authScheme = new BasicScheme();
106         authScheme.initPreemptive(credentials);
107         preemptiveAuth(host, authScheme);
108         return this;
109     }
110 
111     protected abstract T createContext();
112 
113     public T build() {
114         final T context = createContext();
115         context.setCookieSpecRegistry(cookieSpecRegistry);
116         context.setAuthSchemeRegistry(authSchemeRegistry);
117         context.setCookieStore(cookieStore);
118         context.setCredentialsProvider(credentialsProvider);
119         context.setAuthCache(authCache);
120         if (authSchemeMap != null) {
121             for (final Map.Entry<HttpHost, AuthScheme> entry : authSchemeMap.entrySet()) {
122                 context.resetAuthExchange(entry.getKey(), entry.getValue());
123             }
124         }
125         return context;
126     }
127 
128 }