1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl.win;
28
29 import java.util.Locale;
30
31 import org.apache.hc.client5.http.auth.AuthSchemeFactory;
32 import org.apache.hc.client5.http.auth.StandardAuthScheme;
33 import org.apache.hc.client5.http.impl.auth.BasicSchemeFactory;
34 import org.apache.hc.client5.http.impl.auth.DigestSchemeFactory;
35 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
36 import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
37 import org.apache.hc.core5.http.config.Registry;
38 import org.apache.hc.core5.http.config.RegistryBuilder;
39
40
41
42
43
44
45
46 public class WinHttpClients {
47
48 private WinHttpClients() {
49 super();
50 }
51
52 public static boolean isWinAuthAvailable() {
53 String os = System.getProperty("os.name");
54 os = os != null ? os.toLowerCase(Locale.ROOT) : null;
55 return os != null && os.contains("windows");
56 }
57
58 private static HttpClientBuilder createBuilder() {
59 if (isWinAuthAvailable()) {
60 final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
61 .register(StandardAuthScheme.BASIC, BasicSchemeFactory.INSTANCE)
62 .register(StandardAuthScheme.DIGEST, DigestSchemeFactory.INSTANCE)
63 .register(StandardAuthScheme.NTLM, WindowsNTLMSchemeFactory.DEFAULT)
64 .register(StandardAuthScheme.SPNEGO, WindowsNegotiateSchemeFactory.DEFAULT)
65 .build();
66 return HttpClientBuilder.create()
67 .setDefaultAuthSchemeRegistry(authSchemeRegistry);
68 }
69 return HttpClientBuilder.create();
70 }
71
72
73
74
75
76 public static HttpClientBuilder custom() {
77 return createBuilder();
78 }
79
80
81
82
83
84 public static CloseableHttpClient createDefault() {
85 return createBuilder().build();
86 }
87
88
89
90
91
92 public static CloseableHttpClient createSystem() {
93 return createBuilder().useSystemProperties().build();
94 }
95
96
97 }