public static Result getInputStream()

in log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/HttpInputStreamUtil.java [61:146]


    public static Result getInputStream(
            final LastModifiedSource source, final AuthorizationProvider authorizationProvider) {
        final Result result = new Result();
        try {
            final long lastModified = source.getLastModified();
            final HttpURLConnection connection = UrlConnectionFactory.createConnection(
                    source.getURI().toURL(),
                    lastModified,
                    SslConfigurationFactory.getSslConfiguration(),
                    authorizationProvider);
            connection.connect();
            try {
                final int code = connection.getResponseCode();
                switch (code) {
                    case NOT_MODIFIED: {
                        LOGGER.debug(
                                "{} resource {}: not modified since {}",
                                formatProtocol(source),
                                () -> source,
                                () -> Instant.ofEpochMilli(lastModified));
                        result.status = Status.NOT_MODIFIED;
                        return result;
                    }
                    case NOT_FOUND: {
                        LOGGER.debug("{} resource {}: not found", formatProtocol(source), () -> source);
                        result.status = Status.NOT_FOUND;
                        return result;
                    }
                    case OK: {
                        try (final InputStream is = connection.getInputStream()) {
                            source.setLastModified(connection.getLastModified());
                            LOGGER.debug(
                                    "{} resource {}: last modified on {}",
                                    formatProtocol(source),
                                    () -> source,
                                    () -> Instant.ofEpochMilli(connection.getLastModified()));
                            result.status = Status.SUCCESS;
                            result.bytes = readStream(is);
                            return result;
                        } catch (final IOException e) {
                            try (final InputStream es = connection.getErrorStream()) {
                                if (LOGGER.isDebugEnabled()) {
                                    LOGGER.debug(
                                            "Error accessing {} resource at {}: {}",
                                            formatProtocol(source).get(),
                                            source,
                                            readStream(es),
                                            e);
                                }
                            } catch (final IOException ioe) {
                                LOGGER.debug(
                                        "Error accessing {} resource at {}",
                                        formatProtocol(source),
                                        () -> source,
                                        () -> e);
                            }
                            throw new ConfigurationException("Unable to access " + source, e);
                        }
                    }
                    case NOT_AUTHORIZED: {
                        throw new ConfigurationException("Authentication required for " + source);
                    }
                    case FORBIDDEN: {
                        throw new ConfigurationException("Access denied to " + source);
                    }
                    default: {
                        if (code < 0) {
                            LOGGER.debug("{} resource {}: invalid response code", formatProtocol(source), source);
                        } else {
                            LOGGER.debug(
                                    "{} resource {}: unexpected response code {}",
                                    formatProtocol(source),
                                    source,
                                    code);
                        }
                        throw new ConfigurationException("Unable to access " + source);
                    }
                }
            } finally {
                connection.disconnect();
            }
        } catch (IOException e) {
            LOGGER.debug("Error accessing {} resource at {}", formatProtocol(source), source, e);
            throw new ConfigurationException("Unable to access " + source, e);
        }
    }