1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.net;
18
19 import java.io.IOException;
20 import java.net.HttpURLConnection;
21 import java.net.URL;
22 import java.net.URLConnection;
23 import javax.net.ssl.HttpsURLConnection;
24
25 import org.apache.logging.log4j.core.config.ConfigurationFactory;
26 import org.apache.logging.log4j.core.net.ssl.LaxHostnameVerifier;
27 import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
28 import org.apache.logging.log4j.core.net.ssl.SslConfigurationFactory;
29 import org.apache.logging.log4j.core.util.AuthorizationProvider;
30
31
32
33
34 public class UrlConnectionFactory {
35
36 private static int DEFAULT_TIMEOUT = 60000;
37 private static int connectTimeoutMillis = DEFAULT_TIMEOUT;
38 private static int readTimeoutMillis = DEFAULT_TIMEOUT;
39 private static final String JSON = "application/json";
40 private static final String XML = "application/xml";
41 private static final String PROPERTIES = "text/x-java-properties";
42 private static final String TEXT = "text/plain";
43 private static final String HTTP = "http";
44 private static final String HTTPS = "https";
45
46 public static HttpURLConnection createConnection(URL url, long lastModifiedMillis, SslConfiguration sslConfiguration)
47 throws IOException {
48 final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
49 AuthorizationProvider provider = ConfigurationFactory.getAuthorizationProvider();
50 if (provider != null) {
51 provider.addAuthorization(urlConnection);
52 }
53 urlConnection.setAllowUserInteraction(false);
54 urlConnection.setDoOutput(true);
55 urlConnection.setDoInput(true);
56 urlConnection.setRequestMethod("GET");
57 if (connectTimeoutMillis > 0) {
58 urlConnection.setConnectTimeout(connectTimeoutMillis);
59 }
60 if (readTimeoutMillis > 0) {
61 urlConnection.setReadTimeout(readTimeoutMillis);
62 }
63 String[] fileParts = url.getFile().split("\\.");
64 String type = fileParts[fileParts.length - 1].trim();
65 String contentType = isXml(type) ? XML : isJson(type) ? JSON : isProperties(type) ? PROPERTIES : TEXT;
66 urlConnection.setRequestProperty("Content-Type", contentType);
67 if (lastModifiedMillis > 0) {
68 urlConnection.setIfModifiedSince(lastModifiedMillis);
69 }
70 if (url.getProtocol().equals(HTTPS) && sslConfiguration != null) {
71 ((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslConfiguration.getSslSocketFactory());
72 if (!sslConfiguration.isVerifyHostName()) {
73 ((HttpsURLConnection) urlConnection).setHostnameVerifier(LaxHostnameVerifier.INSTANCE);
74 }
75 }
76 return urlConnection;
77 }
78
79 public static URLConnection createConnection(URL url) throws IOException {
80 URLConnection urlConnection = null;
81 if (url.getProtocol().equals(HTTPS) || url.getProtocol().equals(HTTP)) {
82 urlConnection = createConnection(url, 0, SslConfigurationFactory.getSslConfiguration());
83 } else {
84 urlConnection = url.openConnection();
85 }
86 return urlConnection;
87 }
88
89
90 private static boolean isXml(String type) {
91 return type.equalsIgnoreCase("xml");
92 }
93
94 private static boolean isJson(String type) {
95 return type.equalsIgnoreCase("json") || type.equalsIgnoreCase("jsn");
96 }
97
98 private static boolean isProperties(String type) {
99 return type.equalsIgnoreCase("properties");
100 }
101 }