001package jmri.server.web;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.util.HashMap;
006import java.util.Properties;
007
008import javax.annotation.Nonnull;
009
010import org.openide.util.lookup.ServiceProvider;
011import org.slf4j.Logger;
012import org.slf4j.LoggerFactory;
013
014import jmri.server.web.spi.WebServerConfiguration;
015
016/**
017 * Default implementation of {@link jmri.server.web.spi.WebServerConfiguration}.
018 * This implementation reads two properties files to build the JMRI default
019 * configuration for the Web Server. Default forbidden paths are listed in the
020 * WebServlet annotation for {@link jmri.web.servlet.DenialServlet}.
021 *
022 * @author Randall Wood (C) 2016
023 */
024@ServiceProvider(service = WebServerConfiguration.class)
025public final class DefaultWebServerConfiguration extends AbstractWebServerConfiguration {
026
027    private final HashMap<String, String> redirections = new HashMap<>();
028    private final HashMap<String, String> files = new HashMap<>();
029    private static final Logger log = LoggerFactory.getLogger(DefaultWebServerConfiguration.class);
030
031    public DefaultWebServerConfiguration() {
032        loadMap(files, "FilePaths.properties"); // NOI18N
033        loadMap(redirections, "Redirections.properties"); // NOI18N
034    }
035
036    @Override
037    @Nonnull
038    public HashMap<String, String> getFilePaths() {
039        return files;
040    }
041
042    @Override
043    @Nonnull
044    public HashMap<String, String> getRedirectedPaths() {
045        return redirections;
046    }
047
048    private void loadMap(@Nonnull HashMap<String, String> map, @Nonnull String resource) {
049        try (InputStream in = getClass().getResourceAsStream(resource)) {
050            Properties properties = new Properties();
051            properties.load(in);
052            properties.stringPropertyNames().forEach(path -> map.put(path, properties.getProperty(path)));
053        } catch (IllegalArgumentException | NullPointerException | IOException ex) {
054            log.error("Unable to load {}", resource, ex);
055        }
056    }
057}