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.http.impl.client;
28  
29  import java.util.Locale;
30  
31  import org.apache.http.auth.AuthSchemeProvider;
32  import org.apache.http.client.CredentialsProvider;
33  import org.apache.http.client.config.AuthSchemes;
34  import org.apache.http.config.Registry;
35  import org.apache.http.config.RegistryBuilder;
36  import org.apache.http.impl.auth.BasicSchemeFactory;
37  import org.apache.http.impl.auth.DigestSchemeFactory;
38  import org.apache.http.impl.auth.win.WindowsCredentialsProvider;
39  import org.apache.http.impl.auth.win.WindowsNTLMSchemeFactory;
40  import org.apache.http.impl.auth.win.WindowsNegotiateSchemeFactory;
41  
42  import com.sun.jna.platform.win32.Sspi;
43  
44  /**
45   * Factory methods for {@link CloseableHttpClient} instances configured to use integrated
46   * Windows authentication by default.
47   *
48   * @since 4.4
49   */
50  public class WinHttpClients {
51  
52      private WinHttpClients() {
53          super();
54      }
55  
56      public static boolean isWinAuthAvailable() {
57          String os = System.getProperty("os.name");
58          os = os != null ? os.toLowerCase(Locale.ROOT) : null;
59          if (os != null && os.contains("windows")) {
60              try {
61                  return Sspi.MAX_TOKEN_SIZE > 0;
62              } catch (final Exception ignore) { // Likely ClassNotFound
63                  return false;
64              }
65          }
66          return false;
67      }
68  
69      private static HttpClientBuilder createBuilder() {
70          if (isWinAuthAvailable()) {
71              final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
72                      .register(AuthSchemes.BASIC, new BasicSchemeFactory())
73                      .register(AuthSchemes.DIGEST, new DigestSchemeFactory())
74                      .register(AuthSchemes.NTLM, new WindowsNTLMSchemeFactory(null))
75                      .register(AuthSchemes.SPNEGO, new WindowsNegotiateSchemeFactory(null))
76                      .build();
77              final CredentialsProvider credsProvider = new WindowsCredentialsProvider(new SystemDefaultCredentialsProvider());
78              return HttpClientBuilder.create()
79                      .setDefaultCredentialsProvider(credsProvider)
80                      .setDefaultAuthSchemeRegistry(authSchemeRegistry);
81          } else {
82              return HttpClientBuilder.create();
83          }
84      }
85  
86      /**
87       * Creates builder object for construction of custom
88       * {@link CloseableHttpClient} instances.
89       */
90      public static HttpClientBuilder custom() {
91          return createBuilder();
92      }
93  
94      /**
95       * Creates {@link CloseableHttpClient} instance with default
96       * configuration.
97       */
98      public static CloseableHttpClient createDefault() {
99          return createBuilder().build();
100     }
101 
102     /**
103      * Creates {@link CloseableHttpClient} instance with default
104      * configuration based on system properties.
105      */
106     public static CloseableHttpClient createSystem() {
107         return createBuilder().useSystemProperties().build();
108     }
109 
110 
111 }