Search in sources :

Example 1 with AuthorizationProvider

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;
    }
}
Also used : SslConfiguration(org.apache.logging.log4j.core.net.ssl.SslConfiguration) AuthorizationProvider(org.apache.logging.log4j.core.util.AuthorizationProvider) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 2 with AuthorizationProvider

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;
}
Also used : BasicAuthorizationProvider(org.apache.logging.log4j.core.util.BasicAuthorizationProvider) BasicAuthorizationProvider(org.apache.logging.log4j.core.util.BasicAuthorizationProvider) AuthorizationProvider(org.apache.logging.log4j.core.util.AuthorizationProvider) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with AuthorizationProvider

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;
    }
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) SslConfiguration(org.apache.logging.log4j.core.net.ssl.SslConfiguration) BasicAuthorizationProvider(org.apache.logging.log4j.core.util.BasicAuthorizationProvider) AuthorizationProvider(org.apache.logging.log4j.core.util.AuthorizationProvider) FileNotFoundException(java.io.FileNotFoundException) LogFile(org.springframework.boot.logging.LogFile) File(java.io.File) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 4 with AuthorizationProvider

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;
}
Also used : ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) AuthorizationProvider(org.apache.logging.log4j.core.util.AuthorizationProvider) BasicAuthorizationProvider(org.apache.logging.log4j.core.util.BasicAuthorizationProvider) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

AuthorizationProvider (org.apache.logging.log4j.core.util.AuthorizationProvider)4 FileNotFoundException (java.io.FileNotFoundException)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 BasicAuthorizationProvider (org.apache.logging.log4j.core.util.BasicAuthorizationProvider)3 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 URLConnection (java.net.URLConnection)2 SslConfiguration (org.apache.logging.log4j.core.net.ssl.SslConfiguration)2 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 ProtocolException (java.net.ProtocolException)1 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)1 PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)1 LogFile (org.springframework.boot.logging.LogFile)1