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