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