001 package org.mycore.frontend;
002
003 import java.io.File;
004 import java.io.FileOutputStream;
005 import java.io.IOException;
006
007 import javax.servlet.http.HttpServletRequest;
008 import javax.servlet.http.HttpServletResponse;
009
010 import org.jdom.Document;
011 import org.jdom.Element;
012 import org.jdom.JDOMException;
013 import org.jdom.input.SAXBuilder;
014 import org.jdom.output.DOMOutputter;
015 import org.jdom.output.XMLOutputter;
016
017 import org.mycore.common.MCRConfiguration;
018 import org.mycore.common.MCRSessionMgr;
019
020 public final class MCRWebsiteWriteProtection {
021 static final private String FS = System.getProperty("file.separator");
022
023 static MCRConfiguration MCR_CONFIG = MCRConfiguration.instance();
024
025 static final private String CONFIG_FOLDER_PATH = MCR_CONFIG.getString("MCR.datadir") + FS + "config";
026
027 static final private String CONFIG_FILE_PATH = new String(CONFIG_FOLDER_PATH + FS + "config-writeProtectionWebsite.xml");
028
029 static final private File CONFIG_FILE = new File(CONFIG_FILE_PATH);
030
031 static private long CONFIG_CACHE_INITTIME = 0;
032
033 static private Element CONFIG_CACHE = null;
034
035 /**
036 * speed up the check
037 *
038 * @return true if write access is currently active, false if not
039 */
040 public final static boolean isActive() {
041 // if superuser is online, return false
042 String superUser = MCR_CONFIG.getString("MCR.Users.Superuser.UserName");
043 if (MCRSessionMgr.getCurrentSession().getCurrentUserID().equals(superUser))
044 return false;
045 // init, if impossible return false
046 Element config = getConfiguration();
047 if (config == null)
048 return false;
049 // return value contained in config
050 String protection = config.getChildTextTrim("protectionEnabled");
051 return Boolean.valueOf(protection);
052 }
053
054 public final static org.w3c.dom.Document getMessage() throws JDOMException, IOException {
055 Element config = getConfiguration();
056 if (config == null)
057 return new DOMOutputter().output(new Document());
058 else {
059 Element messageElem = config.getChild("message");
060 Document message = new Document((Element) messageElem.clone());
061 return new DOMOutputter().output(message);
062 }
063 }
064
065 private final static Element getConfiguration() {
066 // try to get file
067 File configFolder = new File(CONFIG_FOLDER_PATH);
068 if (!configFolder.exists())
069 configFolder.mkdir();
070 // file exist?, return it's content
071 if (CONFIG_FILE.exists()) {
072 Element config = null;
073 // try to get from cache
074 if (cacheValid())
075 config = CONFIG_CACHE;
076 // parse it
077 else {
078 SAXBuilder builder = new SAXBuilder();
079 try {
080 config = builder.build(CONFIG_FILE).getRootElement();
081 // update cache
082 updateCache(config);
083 } catch (JDOMException e) {
084 e.printStackTrace();
085 return null;
086 } catch (IOException e) {
087 e.printStackTrace();
088 return null;
089 }
090 }
091 return config;
092 } else {
093 // create XML
094 Element config = configToJDOM(false, " ");
095 setConfiguration(config);
096 return config;
097 }
098 }
099
100 private final static void setConfiguration(Element configXML) {
101 try {
102 // save
103 XMLOutputter xmlOut = new XMLOutputter();
104 FileOutputStream fos = new FileOutputStream(CONFIG_FILE);
105 xmlOut.output(configXML, fos);
106 fos.flush();
107 fos.close();
108 } catch (IOException e) {
109 e.printStackTrace();
110 }
111 updateCache(configXML);
112 }
113
114 /**
115 * @param configXML
116 */
117 private static void updateCache(Element configXML) {
118 CONFIG_CACHE = configXML;
119 CONFIG_CACHE_INITTIME = System.currentTimeMillis();
120 }
121
122 private static Element configToJDOM(boolean protection, String message) {
123 Element xml = new Element("config-writeProtectionWebsite");
124 xml.addContent(new Element("protectionEnabled").setText(Boolean.toString(protection)));
125 xml.addContent(new Element("message").setText(message));
126 return xml;
127 }
128
129 // to be used by cli
130 public final static void activate() {
131 // create file, set param in file to true, add message to file
132 Element config = getConfiguration();
133 config.getChild("protectionEnabled").setText("true");
134 setConfiguration(config);
135 }
136
137 // to be used by cli
138 public final static void activate(String message) {
139 // create file, set param in file to true, add message to file
140 Element config = getConfiguration();
141 config.getChild("protectionEnabled").setText("true");
142 config.getChild("message").setText(message);
143 setConfiguration(config);
144 }
145
146 // to be used by cli
147 public final static void deactivate() {
148 // set param in file to false
149 Element config = getConfiguration();
150 config.getChild("protectionEnabled").setText("false");
151 setConfiguration(config);
152 }
153
154 public final static boolean printInfoPageIfNoAccess(HttpServletRequest request, HttpServletResponse response, String baseURL) throws IOException {
155 if (MCRWebsiteWriteProtection.isActive()) {
156 response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
157 String pageURL = baseURL + MCR_CONFIG.getString("MCR.WriteProtectionWebsite.ErrorPage");
158 response.sendRedirect(response.encodeRedirectURL(pageURL));
159 return true;
160 }
161 return false;
162 }
163
164 /**
165 * Verifies if the cache of configuration is valid.
166 *
167 * @return true if valid, false if note
168 */
169 private final static boolean cacheValid() {
170 if ((CONFIG_CACHE == null) || (CONFIG_CACHE_INITTIME < CONFIG_FILE.lastModified()))
171 return false;
172 else
173 return true;
174 }
175
176 }