use of org.apache.logging.log4j.core.util.AuthorizationProvider in project logging-log4j2 by apache.
the class ConfigurationSource method getConfigurationSource.
private static ConfigurationSource getConfigurationSource(final URL url) {
try {
final URLConnection urlConnection = url.openConnection();
final AuthorizationProvider provider = ConfigurationFactory.authorizationProvider(PropertiesUtil.getProperties());
provider.addAuthorization(urlConnection);
if (url.getProtocol().equals(HTTPS)) {
final SslConfiguration sslConfiguration = SslConfigurationFactory.getSslConfiguration();
if (sslConfiguration != null) {
((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslConfiguration.getSslSocketFactory());
if (!sslConfiguration.isVerifyHostName()) {
((HttpsURLConnection) urlConnection).setHostnameVerifier(LaxHostnameVerifier.INSTANCE);
}
}
}
final File file = FileUtils.fileFromUri(url.toURI());
try {
if (file != null) {
return new ConfigurationSource(urlConnection.getInputStream(), FileUtils.fileFromUri(url.toURI()));
} else {
return new ConfigurationSource(urlConnection.getInputStream(), url, urlConnection.getLastModified());
}
} catch (final FileNotFoundException ex) {
ConfigurationFactory.LOGGER.info("Unable to locate file {}, ignoring.", url.toString());
return null;
}
} catch (final IOException | URISyntaxException ex) {
ConfigurationFactory.LOGGER.warn("Error accessing {} due to {}, ignoring.", url.toString(), ex.getMessage());
return null;
}
}
use of org.apache.logging.log4j.core.util.AuthorizationProvider in project logging-log4j2 by apache.
the class ConfigurationFactory method authorizationProvider.
public static AuthorizationProvider authorizationProvider(final PropertiesUtil props) {
final String authClass = props.getStringProperty(PREFIXES, AUTHORIZATION_PROVIDER, null);
AuthorizationProvider provider = null;
if (authClass != null) {
try {
final Object obj = LoaderUtil.newInstanceOf(authClass);
if (obj instanceof AuthorizationProvider) {
provider = (AuthorizationProvider) obj;
} else {
LOGGER.warn("{} is not an AuthorizationProvider, using default", obj.getClass().getName());
}
} catch (final Exception ex) {
LOGGER.warn("Unable to create {}, using default: {}", authClass, ex.getMessage());
}
}
if (provider == null) {
provider = new BasicAuthorizationProvider(props);
}
return provider;
}
use of org.apache.logging.log4j.core.util.AuthorizationProvider in project logging-log4j2 by apache.
the class Log4j2CloudConfigLoggingSystem method getConfigurationSource.
private ConfigurationSource getConfigurationSource(URL url) throws IOException, URISyntaxException {
URLConnection urlConnection = url.openConnection();
AuthorizationProvider provider = ConfigurationFactory.authorizationProvider(PropertiesUtil.getProperties());
provider.addAuthorization(urlConnection);
if (url.getProtocol().equals(HTTPS)) {
SslConfiguration sslConfiguration = SslConfigurationFactory.getSslConfiguration();
if (sslConfiguration != null) {
((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslConfiguration.getSslSocketFactory());
if (!sslConfiguration.isVerifyHostName()) {
((HttpsURLConnection) urlConnection).setHostnameVerifier(LaxHostnameVerifier.INSTANCE);
}
}
}
File file = FileUtils.fileFromUri(url.toURI());
try {
if (file != null) {
return new ConfigurationSource(urlConnection.getInputStream(), FileUtils.fileFromUri(url.toURI()));
} else {
return new ConfigurationSource(urlConnection.getInputStream(), url, urlConnection.getLastModified());
}
} catch (FileNotFoundException ex) {
LOGGER.info("Unable to locate file {}, ignoring.", url.toString());
return null;
}
}
use of org.apache.logging.log4j.core.util.AuthorizationProvider in project logging-log4j2 by apache.
the class UrlConnectionFactory method createConnection.
public static HttpURLConnection createConnection(final URL url, final long lastModifiedMillis, final SslConfiguration sslConfiguration) throws IOException {
PropertiesUtil props = PropertiesUtil.getProperties();
List<String> allowed = Arrays.asList(Strings.splitList(props.getStringProperty(ALLOWED_PROTOCOLS, HTTPS).toLowerCase(Locale.ROOT)));
if (allowed.size() == 1 && NO_PROTOCOLS.equals(allowed.get(0))) {
throw new ProtocolException("No external protocols have been enabled");
}
String protocol = url.getProtocol();
if (protocol == null) {
throw new ProtocolException("No protocol was specified on " + url.toString());
}
if (!allowed.contains(protocol)) {
throw new ProtocolException("Protocol " + protocol + " has not been enabled as an allowed protocol");
}
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
final AuthorizationProvider provider = ConfigurationFactory.authorizationProvider(props);
if (provider != null) {
provider.addAuthorization(urlConnection);
}
urlConnection.setAllowUserInteraction(false);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("GET");
if (connectTimeoutMillis > 0) {
urlConnection.setConnectTimeout(connectTimeoutMillis);
}
if (readTimeoutMillis > 0) {
urlConnection.setReadTimeout(readTimeoutMillis);
}
final String[] fileParts = url.getFile().split("\\.");
final String type = fileParts[fileParts.length - 1].trim();
final String contentType = isXml(type) ? XML : isJson(type) ? JSON : isProperties(type) ? PROPERTIES : TEXT;
urlConnection.setRequestProperty("Content-Type", contentType);
if (lastModifiedMillis > 0) {
urlConnection.setIfModifiedSince(lastModifiedMillis);
}
if (url.getProtocol().equals(HTTPS) && sslConfiguration != null) {
((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslConfiguration.getSslSocketFactory());
if (!sslConfiguration.isVerifyHostName()) {
((HttpsURLConnection) urlConnection).setHostnameVerifier(LaxHostnameVerifier.INSTANCE);
}
}
return urlConnection;
}
Aggregations