001package jmri.web.server;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.Set;
006import jmri.implementation.AbstractInstanceInitializer;
007import jmri.jmrit.XmlFile;
008import jmri.util.FileUtil;
009import org.jdom2.Attribute;
010import org.jdom2.Document;
011import org.jdom2.Element;
012import org.jdom2.JDOMException;
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Provide interface for managing the JMRI web server, including migrating from
018 * the older 2.n Mini Web Server.
019 *
020 * @author Randall Wood Copyright 2017, 2019
021 * @deprecated since 4.19.2 without direct replacement; users migrating from
022 *             JMRI versions older than 4.0 will need to manually migrate web
023 *             server preferences
024 */
025@Deprecated
026public class WebServerPreferencesInstanceInitializer extends AbstractInstanceInitializer {
027
028    private static final Logger log = LoggerFactory.getLogger(WebServerPreferencesInstanceInitializer.class);
029
030    @Override
031    public <T> Object getDefault(Class<T> type) {
032        if (type == WebServerPreferences.class) {
033            File webServerPrefsFile = new File(
034                    FileUtil.getUserFilesPath() + "networkServices" + File.separator + "WebServerPreferences.xml"); // NOI18N
035            File miniServerPrefsFile =
036                    new File(FileUtil.getUserFilesPath() + "miniserver" + File.separator + "MiniServerPreferences.xml"); // NOI18N
037            if (!webServerPrefsFile.exists() && miniServerPrefsFile.exists()) {
038                // import Mini Server preferences into Web Server preferences
039                preferencesFromMiniServerPreferences(miniServerPrefsFile, webServerPrefsFile);
040            }
041            if (!webServerPrefsFile.exists()) {
042                return new WebServerPreferences();
043            } else {
044                return new WebServerPreferences(webServerPrefsFile.getAbsolutePath());
045            }
046        }
047        return super.getDefault(type);
048    }
049
050    @Override
051    public Set<Class<?>> getInitalizes() {
052        Set<Class<?>> set = super.getInitalizes();
053        set.add(WebServerPreferences.class);
054        return set;
055    }
056
057    private void preferencesFromMiniServerPreferences(File msFile, File wsFile) {
058        XmlFile xmlFile = new XmlFile() {
059        };
060        try {
061            Element msRoot = xmlFile.rootFromFile(msFile);
062            Element wsRoot = new Element(WebServerPreferences.WEB_SERVER_PREFERENCES);
063            Element msPrefs = msRoot.getChild("MiniServerPreferences"); // NOI18N
064            msPrefs.getChildren().forEach(wsRoot::addContent);
065            for (Attribute attr : msPrefs.getAttributes()) {
066                switch (attr.getName()) {
067                    case "getDisallowedFrames": // NOI18N
068                        Element df = new Element(WebServerPreferences.DISALLOWED_FRAMES);
069                        String[] frames = attr.getValue().split("\\n"); // NOI18N
070                        for (String frame : frames) {
071                            df.addContent(new Element(WebServerPreferences.FRAME).addContent(frame));
072                        }
073                        wsRoot.addContent(df);
074                        break;
075                    case "getPort": // NOI18N
076                        wsRoot.setAttribute(WebServerPreferences.PORT, attr.getValue());
077                        break;
078                    case "getClickDelay": // NOI18N
079                        wsRoot.setAttribute(WebServerPreferences.CLICK_DELAY, attr.getValue());
080                        break;
081                    case "getRefreshDelay": // NOI18N
082                        wsRoot.setAttribute(WebServerPreferences.REFRESH_DELAY, attr.getValue());
083                        break;
084                    default:
085                        // double cast because clone() is Protected on Object
086                        wsRoot.setAttribute(attr.clone());
087                        break;
088                }
089            }
090            Document wsDoc = XmlFile.newDocument(wsRoot);
091            File parent = new File(wsFile.getParent());
092            if (!parent.exists()) {
093                boolean created = parent.mkdir(); // directory known to not
094                                                  // exist from previous
095                                                  // conditional
096                if (!created) {
097                    log.error("Failed to create directory {}", parent);
098                    throw new java.io.IOException("Failed to create directory " + parent.toString());
099                }
100            }
101
102            boolean created = wsFile.createNewFile(); // known to not exist or
103                                                      // this method would not
104                                                      // have been called
105            if (!created) {
106                log.error("Failed to new create file {}", wsFile);
107                throw new java.io.IOException("Failed to create new file " + wsFile.toString());
108            }
109
110            xmlFile.writeXML(wsFile, wsDoc);
111
112        } catch (
113                IOException |
114                JDOMException ex) {
115            log.error("Error converting miniServer preferences to Web Server preferences.", ex);
116        }
117    }
118}