Only allow files in the root directory to be selected as index files.
[jSite.git] / src / de / todesbaum / jsite / main / Configuration.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.main;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Locale;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
37 import de.todesbaum.jsite.application.FileOption;
38 import de.todesbaum.jsite.application.Node;
39 import de.todesbaum.jsite.application.Project;
40 import de.todesbaum.util.io.Closer;
41 import de.todesbaum.util.io.StreamCopier;
42 import de.todesbaum.util.xml.SimpleXML;
43 import de.todesbaum.util.xml.XML;
44
45 /**
46  * The configuration.
47  *
48  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
49  */
50 public class Configuration {
51
52         /** The name of the file the configuration is stored to. */
53         private String filename;
54
55         /** The name of the lock file. */
56         private String lockFilename;
57
58         /** The root node of the configuration. */
59         private SimpleXML rootNode;
60
61         /**
62          * Creates a new configuration with the default name of the configuration
63          * file.
64          */
65         public Configuration() {
66                 this(System.getProperty("user.home") + "/.jSite/config7");
67         }
68
69         /**
70          * Creates a new configuration that is read from the given file.
71          *
72          * @param filename
73          *            The name of the configuration file
74          */
75         public Configuration(String filename) {
76                 this(filename, filename + ".lock");
77         }
78
79         /**
80          * Creates a new configuration that is read from the given file and uses the
81          * given lock file.
82          *
83          * @param filename
84          *            The name of the configuration file
85          * @param lockFilename
86          *            The name of the lock file
87          */
88         public Configuration(String filename, String lockFilename) {
89                 this.filename = filename;
90                 this.lockFilename = lockFilename;
91                 readConfiguration();
92         }
93
94         /**
95          * Creates the directory of the configuration file.
96          *
97          * @return <code>true</code> if the directory exists, or if it could be
98          *         created, <code>false</code> otherwise
99          */
100         private boolean createConfigDirectory() {
101                 File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
102                 return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
103         }
104
105         /**
106          * Creates the lock file.
107          *
108          * @return <code>true</code> if the lock file did not already exist and
109          *         could be created, <code>false</code> otherwise
110          */
111         public boolean createLockFile() {
112                 if (!createConfigDirectory()) {
113                         return false;
114                 }
115                 File lockFile = new File(lockFilename);
116                 try {
117                         boolean fileLocked = lockFile.createNewFile();
118                         if (fileLocked) {
119                                 lockFile.deleteOnExit();
120                         }
121                         return fileLocked;
122                 } catch (IOException e) {
123                         /* ignore. */
124                 }
125                 return false;
126         }
127
128         /**
129          * Tells the VM to remove the lock file on program exit.
130          */
131         public void removeLockfileOnExit() {
132                 new File(lockFilename).deleteOnExit();
133         }
134
135         /**
136          * Reads the configuration from the file.
137          */
138         private void readConfiguration() {
139                 File configurationFile = new File(filename);
140                 if (configurationFile.exists()) {
141                         ByteArrayOutputStream fileByteOutputStream = null;
142                         FileInputStream fileInputStream = null;
143                         try {
144                                 fileByteOutputStream = new ByteArrayOutputStream();
145                                 fileInputStream = new FileInputStream(configurationFile);
146                                 StreamCopier.copy(fileInputStream, fileByteOutputStream, configurationFile.length());
147                                 fileByteOutputStream.close();
148                                 byte[] fileBytes = fileByteOutputStream.toByteArray();
149                                 rootNode = SimpleXML.fromDocument(XML.transformToDocument(fileBytes));
150                                 return;
151                         } catch (FileNotFoundException e) {
152                                 /* ignore. */
153                         } catch (IOException e) {
154                                 /* ignore. */
155                         } finally {
156                                 Closer.close(fileInputStream);
157                                 Closer.close(fileByteOutputStream);
158                         }
159                 }
160                 rootNode = new SimpleXML("configuration");
161         }
162
163         /**
164          * Saves the configuration.
165          *
166          * @return <code>true</code> if the configuration could be saved,
167          *         <code>false</code> otherwise
168          */
169         public boolean save() {
170                 File configurationFile = new File(filename);
171                 if (!configurationFile.exists()) {
172                         File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
173                         if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
174                                 return false;
175                         }
176                 }
177                 FileOutputStream fileOutputStream = null;
178                 ByteArrayInputStream configurationInputStream = null;
179                 try {
180                         byte[] configurationBytes = XML.transformToByteArray(rootNode.getDocument());
181                         configurationInputStream = new ByteArrayInputStream(configurationBytes);
182                         fileOutputStream = new FileOutputStream(configurationFile);
183                         StreamCopier.copy(configurationInputStream, fileOutputStream, configurationBytes.length);
184                         return true;
185                 } catch (IOException ioe1) {
186                         /* ignore. */
187                 } finally {
188                         Closer.close(configurationInputStream);
189                         Closer.close(fileOutputStream);
190                 }
191                 return false;
192         }
193
194         /**
195          * Returns the value of a node.
196          *
197          * @param nodeNames
198          *            The name of all nodes in the chain
199          * @param defaultValue
200          *            The default value to return if the node could not be found
201          * @return The value of the node, or the default value if the node could not
202          *         be found
203          */
204         private String getNodeValue(String[] nodeNames, String defaultValue) {
205                 SimpleXML node = rootNode;
206                 int nodeIndex = 0;
207                 while ((node != null) && (nodeIndex < nodeNames.length)) {
208                         node = node.getNode(nodeNames[nodeIndex++]);
209                 }
210                 if (node == null) {
211                         return defaultValue;
212                 }
213                 return node.getValue();
214         }
215
216         /**
217          * Returns the integer value of a node.
218          *
219          * @param nodeNames
220          *            The names of all nodes in the chain
221          * @param defaultValue
222          *            The default value to return if the node can not be found
223          * @return The parsed integer value, or the default value if the node can
224          *         not be found or the value can not be parsed into an integer
225          */
226         private int getNodeIntValue(String[] nodeNames, int defaultValue) {
227                 try {
228                         return Integer.parseInt(getNodeValue(nodeNames, String.valueOf(defaultValue)));
229                 } catch (NumberFormatException nfe1) {
230                         /* ignore. */
231                 }
232                 return defaultValue;
233         }
234
235         /**
236          * Returns the boolean value of a node.
237          *
238          * @param nodeNames
239          *            The names of all nodes in the chain
240          * @param defaultValue
241          *            The default value to return if the node can not be found
242          * @return The parsed boolean value, or the default value if the node can
243          *         not be found
244          */
245         private boolean getNodeBooleanValue(String[] nodeNames, boolean defaultValue) {
246                 String nodeValue = getNodeValue(nodeNames, null);
247                 if (nodeValue == null) {
248                         return defaultValue;
249                 }
250                 return Boolean.parseBoolean(nodeValue);
251         }
252
253         /**
254          * Returns the hostname of the node.
255          *
256          * @return The hostname of the node
257          * @deprecated Use {@link #getSelectedNode()} instead
258          */
259         @Deprecated
260         public String getNodeAddress() {
261                 return getNodeValue(new String[] { "node-address" }, "localhost");
262         }
263
264         /**
265          * Sets the hostname of the node.
266          *
267          * @param nodeAddress
268          *            The hostname of the node
269          * @deprecated Use {@link #setSelectedNode(Node)} instead
270          */
271         @Deprecated
272         public void setNodeAddress(String nodeAddress) {
273                 rootNode.replace("node-address", nodeAddress);
274         }
275
276         /**
277          * The port number of the node
278          *
279          * @return The port number of the node
280          * @deprecated Use {@link #getSelectedNode()} instead.
281          */
282         @Deprecated
283         public int getNodePort() {
284                 return getNodeIntValue(new String[] { "node-port" }, 9481);
285         }
286
287         /**
288          * Sets the port number of the node.
289          *
290          * @param nodePort
291          *            The port number of the node
292          * @deprecated Use {@link #setSelectedNode(Node)} instead
293          */
294         @Deprecated
295         public void setNodePort(int nodePort) {
296                 rootNode.replace("node-port", String.valueOf(nodePort));
297         }
298
299         /**
300          * Returns whether the node configuration page should be skipped on startup.
301          *
302          * @return <code>true</code> to skip the node configuration page on startup,
303          *         <code>false</code> to show it
304          */
305         public boolean isSkipNodePage() {
306                 return getNodeBooleanValue(new String[] { "skip-node-page" }, false);
307         }
308
309         /**
310          * Sets whether the node configuration page should be skipped on startup.
311          *
312          * @param skipNodePage
313          *            <code>true</code> to skip the node configuration page on
314          *            startup, <code>false</code> to show it
315          */
316         public void setSkipNodePage(boolean skipNodePage) {
317                 rootNode.replace("skip-node-page", String.valueOf(skipNodePage));
318         }
319
320         /**
321          * Returns all configured projects.
322          *
323          * @return A list of all projects
324          */
325         public Project[] getProjects() {
326                 List<Project> projects = new ArrayList<Project>();
327                 SimpleXML projectsNode = rootNode.getNode("project-list");
328                 if (projectsNode != null) {
329                         SimpleXML[] projectNodes = projectsNode.getNodes("project");
330                         for (SimpleXML projectNode : projectNodes) {
331                                 try {
332                                         Project project = new Project();
333                                         projects.add(project);
334                                         project.setDescription(projectNode.getNode("description").getValue(""));
335                                         String indexFile = projectNode.getNode("index-file").getValue("");
336                                         if (indexFile.indexOf('/') > -1) {
337                                                 indexFile = "";
338                                         }
339                                         project.setIndexFile(indexFile);
340                                         project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue("0")));
341                                         project.setLocalPath(projectNode.getNode("local-path").getValue(""));
342                                         project.setName(projectNode.getNode("name").getValue(""));
343                                         project.setPath(projectNode.getNode("path").getValue(""));
344                                         if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
345                                                 project.setPath(project.getPath().replaceAll("/", ""));
346                                         }
347                                         project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue("0")));
348                                         project.setInsertURI(projectNode.getNode("insert-uri").getValue(""));
349                                         project.setRequestURI(projectNode.getNode("request-uri").getValue(""));
350                                         if (projectNode.getNode("ignore-hidden-files") != null) {
351                                                 project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getNode("ignore-hidden-files").getValue("true")));
352                                         } else {
353                                                 project.setIgnoreHiddenFiles(true);
354                                         }
355                                         SimpleXML fileOptionsNode = projectNode.getNode("file-options");
356                                         Map<String, FileOption> fileOptions = new HashMap<String, FileOption>();
357                                         if (fileOptionsNode != null) {
358                                                 SimpleXML[] fileOptionNodes = fileOptionsNode.getNodes("file-option");
359                                                 for (SimpleXML fileOptionNode : fileOptionNodes) {
360                                                         String filename = fileOptionNode.getNode("filename").getValue();
361                                                         FileOption fileOption = project.getFileOption(filename);
362                                                         fileOption.setInsert(Boolean.parseBoolean(fileOptionNode.getNode("insert").getValue()));
363                                                         if (fileOptionNode.getNode("insert-redirect") != null) {
364                                                                 fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue()));
365                                                         }
366                                                         fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue(""));
367                                                         if (fileOptionNode.getNode("changed-name") != null) {
368                                                                 fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue());
369                                                         }
370                                                         fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue(""));
371                                                         fileOption.setContainer(fileOptionNode.getNode("container").getValue());
372                                                         if (fileOptionNode.getNode("replace-edition") != null) {
373                                                                 fileOption.setReplaceEdition(Boolean.parseBoolean(fileOptionNode.getNode("replace-edition").getValue()));
374                                                                 fileOption.setEditionRange(Integer.parseInt(fileOptionNode.getNode("edition-range").getValue()));
375                                                         }
376                                                         fileOptions.put(filename, fileOption);
377                                                 }
378                                         }
379                                         project.setFileOptions(fileOptions);
380                                 } catch (NumberFormatException nfe1) {
381                                         nfe1.printStackTrace();
382                                 }
383                         }
384                 }
385                 return projects.toArray(new Project[projects.size()]);
386         }
387
388         /**
389          * Sets the list of all projects.
390          *
391          * @param projects
392          *            The list of all projects
393          */
394         public void setProjects(Project[] projects) {
395                 SimpleXML projectsNode = new SimpleXML("project-list");
396                 for (Project project : projects) {
397                         SimpleXML projectNode = projectsNode.append("project");
398                         projectNode.append("edition", String.valueOf(project.getEdition()));
399                         projectNode.append("description", project.getDescription());
400                         projectNode.append("index-file", project.getIndexFile());
401                         projectNode.append("last-insertion-time", String.valueOf(project.getLastInsertionTime()));
402                         projectNode.append("local-path", project.getLocalPath());
403                         projectNode.append("name", project.getName());
404                         projectNode.append("path", project.getPath());
405                         projectNode.append("insert-uri", project.getInsertURI());
406                         projectNode.append("request-uri", project.getRequestURI());
407                         projectNode.append("ignore-hidden-files", String.valueOf(project.isIgnoreHiddenFiles()));
408                         SimpleXML fileOptionsNode = projectNode.append("file-options");
409                         Iterator<Entry<String, FileOption>> entries = project.getFileOptions().entrySet().iterator();
410                         while (entries.hasNext()) {
411                                 Entry<String, FileOption> entry = entries.next();
412                                 FileOption fileOption = entry.getValue();
413                                 if (fileOption.isCustom()) {
414                                         SimpleXML fileOptionNode = fileOptionsNode.append("file-option");
415                                         fileOptionNode.append("filename", entry.getKey());
416                                         fileOptionNode.append("insert", String.valueOf(fileOption.isInsert()));
417                                         fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect()));
418                                         fileOptionNode.append("custom-key", fileOption.getCustomKey());
419                                         fileOptionNode.append("changed-name", fileOption.getChangedName());
420                                         fileOptionNode.append("mime-type", fileOption.getMimeType());
421                                         fileOptionNode.append("container", fileOption.getContainer());
422                                         fileOptionNode.append("replace-edition", String.valueOf(fileOption.getReplaceEdition()));
423                                         fileOptionNode.append("edition-range", String.valueOf(fileOption.getEditionRange()));
424                                 }
425                         }
426                 }
427                 rootNode.replace(projectsNode);
428         }
429
430         /**
431          * Returns the stored locale.
432          *
433          * @return The stored locale
434          */
435         public Locale getLocale() {
436                 String language = getNodeValue(new String[] { "i18n", "language" }, "en");
437                 String country = getNodeValue(new String[] { "i18n", "country" }, null);
438                 if (country != null) {
439                         return new Locale(language, country);
440                 }
441                 return new Locale(language);
442         }
443
444         /**
445          * Sets the locale to store.
446          *
447          * @param locale
448          *            The locale to store
449          */
450         public void setLocale(Locale locale) {
451                 SimpleXML i18nNode = new SimpleXML("i18n");
452                 if (locale.getCountry().length() != 0) {
453                         i18nNode.append("country", locale.getCountry());
454                 }
455                 i18nNode.append("language", locale.getLanguage());
456                 rootNode.replace(i18nNode);
457                 return;
458         }
459
460         /**
461          * Returns a list of configured nodes.
462          *
463          * @return The list of the configured nodes
464          */
465         public Node[] getNodes() {
466                 SimpleXML nodesNode = rootNode.getNode("nodes");
467                 if (nodesNode == null) {
468                         String hostname = getNodeAddress();
469                         int port = getNodePort();
470                         if (hostname == null) {
471                                 hostname = "127.0.0.1";
472                                 port = 9481;
473                         }
474                         return new Node[] { new Node(hostname, port, "Node") };
475                 }
476                 SimpleXML[] nodeNodes = nodesNode.getNodes("node");
477                 Node[] returnNodes = new Node[nodeNodes.length];
478                 int nodeIndex = 0;
479                 for (SimpleXML nodeNode : nodeNodes) {
480                         String name = nodeNode.getNode("name").getValue();
481                         String hostname = nodeNode.getNode("hostname").getValue();
482                         int port = Integer.parseInt(nodeNode.getNode("port").getValue());
483                         Node node = new Node(hostname, port, name);
484                         returnNodes[nodeIndex++] = node;
485                 }
486                 return returnNodes;
487         }
488
489         /**
490          * Sets the list of configured nodes.
491          *
492          * @param nodes
493          *            The list of configured nodes
494          */
495         public void setNodes(Node[] nodes) {
496                 SimpleXML nodesNode = new SimpleXML("nodes");
497                 for (Node node : nodes) {
498                         SimpleXML nodeNode = nodesNode.append("node");
499                         nodeNode.append("name", node.getName());
500                         nodeNode.append("hostname", node.getHostname());
501                         nodeNode.append("port", String.valueOf(node.getPort()));
502                 }
503                 rootNode.replace(nodesNode);
504                 rootNode.remove("node-address");
505                 rootNode.remove("node-port");
506         }
507
508         /**
509          * Sets the selected node.
510          *
511          * @param selectedNode
512          *            The selected node
513          */
514         public void setSelectedNode(Node selectedNode) {
515                 SimpleXML selectedNodeNode = new SimpleXML("selected-node");
516                 selectedNodeNode.append("name", selectedNode.getName());
517                 selectedNodeNode.append("hostname", selectedNode.getHostname());
518                 selectedNodeNode.append("port", String.valueOf(selectedNode.getPort()));
519                 rootNode.replace(selectedNodeNode);
520         }
521
522         /**
523          * Returns the selected node.
524          *
525          * @return The selected node
526          */
527         public Node getSelectedNode() {
528                 SimpleXML selectedNodeNode = rootNode.getNode("selected-node");
529                 if (selectedNodeNode == null) {
530                         String hostname = getNodeAddress();
531                         int port = getNodePort();
532                         if (hostname == null) {
533                                 hostname = "127.0.0.1";
534                                 port = 9481;
535                         }
536                         return new Node(hostname, port, "Node");
537                 }
538                 String name = selectedNodeNode.getNode("name").getValue();
539                 String hostname = selectedNodeNode.getNode("hostname").getValue();
540                 int port = Integer.valueOf(selectedNodeNode.getNode("port").getValue());
541                 return new Node(hostname, port, name);
542         }
543
544         /**
545          * Returns the temp directory to use.
546          *
547          * @return The temp directoy, or {@code null} to use the default temp
548          *         directory
549          */
550         public String getTempDirectory() {
551                 return getNodeValue(new String[] { "temp-directory" }, null);
552         }
553
554         /**
555          * Sets the temp directory to use.
556          *
557          * @param tempDirectory
558          *            The temp directory to use, or {@code null} to use the default
559          *            temp directory
560          */
561         public void setTempDirectory(String tempDirectory) {
562                 if (tempDirectory != null) {
563                         SimpleXML tempDirectoryNode = new SimpleXML("temp-directory");
564                         tempDirectoryNode.setValue(tempDirectory);
565                         rootNode.replace(tempDirectoryNode);
566                 } else {
567                         rootNode.remove("temp-directory");
568                 }
569         }
570
571 }