001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.net; 018 019import java.io.IOException; 020import java.net.HttpURLConnection; 021import java.net.URL; 022import java.net.URLConnection; 023import javax.net.ssl.HttpsURLConnection; 024 025import org.apache.logging.log4j.core.config.ConfigurationFactory; 026import org.apache.logging.log4j.core.net.ssl.LaxHostnameVerifier; 027import org.apache.logging.log4j.core.net.ssl.SslConfiguration; 028import org.apache.logging.log4j.core.net.ssl.SslConfigurationFactory; 029import org.apache.logging.log4j.core.util.AuthorizationProvider; 030 031/** 032 * Constructs an HTTPURLConnection. This class should be considered to be internal 033 */ 034public class UrlConnectionFactory { 035 036 private static int DEFAULT_TIMEOUT = 60000; 037 private static int connectTimeoutMillis = DEFAULT_TIMEOUT; 038 private static int readTimeoutMillis = DEFAULT_TIMEOUT; 039 private static final String JSON = "application/json"; 040 private static final String XML = "application/xml"; 041 private static final String PROPERTIES = "text/x-java-properties"; 042 private static final String TEXT = "text/plain"; 043 private static final String HTTP = "http"; 044 private static final String HTTPS = "https"; 045 046 public static HttpURLConnection createConnection(URL url, long lastModifiedMillis, SslConfiguration sslConfiguration) 047 throws IOException { 048 final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 049 AuthorizationProvider provider = ConfigurationFactory.getAuthorizationProvider(); 050 if (provider != null) { 051 provider.addAuthorization(urlConnection); 052 } 053 urlConnection.setAllowUserInteraction(false); 054 urlConnection.setDoOutput(true); 055 urlConnection.setDoInput(true); 056 urlConnection.setRequestMethod("GET"); 057 if (connectTimeoutMillis > 0) { 058 urlConnection.setConnectTimeout(connectTimeoutMillis); 059 } 060 if (readTimeoutMillis > 0) { 061 urlConnection.setReadTimeout(readTimeoutMillis); 062 } 063 String[] fileParts = url.getFile().split("\\."); 064 String type = fileParts[fileParts.length - 1].trim(); 065 String contentType = isXml(type) ? XML : isJson(type) ? JSON : isProperties(type) ? PROPERTIES : TEXT; 066 urlConnection.setRequestProperty("Content-Type", contentType); 067 if (lastModifiedMillis > 0) { 068 urlConnection.setIfModifiedSince(lastModifiedMillis); 069 } 070 if (url.getProtocol().equals(HTTPS) && sslConfiguration != null) { 071 ((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslConfiguration.getSslSocketFactory()); 072 if (!sslConfiguration.isVerifyHostName()) { 073 ((HttpsURLConnection) urlConnection).setHostnameVerifier(LaxHostnameVerifier.INSTANCE); 074 } 075 } 076 return urlConnection; 077 } 078 079 public static URLConnection createConnection(URL url) throws IOException { 080 URLConnection urlConnection = null; 081 if (url.getProtocol().equals(HTTPS) || url.getProtocol().equals(HTTP)) { 082 urlConnection = createConnection(url, 0, SslConfigurationFactory.getSslConfiguration()); 083 } else { 084 urlConnection = url.openConnection(); 085 } 086 return urlConnection; 087 } 088 089 090 private static boolean isXml(String type) { 091 return type.equalsIgnoreCase("xml"); 092 } 093 094 private static boolean isJson(String type) { 095 return type.equalsIgnoreCase("json") || type.equalsIgnoreCase("jsn"); 096 } 097 098 private static boolean isProperties(String type) { 099 return type.equalsIgnoreCase("properties"); 100 } 101}