Added OnlineGlomPropertiesTest.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / OnlineGlomProperties.java
1 /*
2  * Copyright (C) 2012 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.util.Properties;
23
24 import org.apache.commons.lang3.StringUtils;
25
26 /** A convenience class for dealing with the Online Glom configuration file
27  * TODO: test this.
28  */
29 class OnlineGlomProperties extends Properties {
30
31         private static final long serialVersionUID = 4290997725469072758L;
32         
33         public static class Credentials {
34                 public String userName = "";
35                 public String password = "";
36         };
37
38         public OnlineGlomProperties.Credentials getCredentials(final String filename) {
39                 OnlineGlomProperties.Credentials result = null;
40
41                 final String key = getKey(filename);
42                 if (key == null) {
43                         return result;
44                 }
45
46                 //Split the line at the . separators,
47                 final String[] keyArray = key.split("\\.");
48                 
49                 //Check that the third item is "filename", as expected:
50                 if (keyArray.length == 3 && "filename".equals(keyArray[2])) {
51                         result = new Credentials();
52                         
53                         //Get the username and password for this file:
54                         final String usernameKey = key.replaceAll(keyArray[2], "username");
55                         final String passwordKey = key.replaceAll(keyArray[2], "password");
56                         result.userName = getPropertyNonNull(usernameKey).trim();
57                         result.password = getPropertyNonNull(passwordKey);
58                 }
59                 
60                 return result;
61         }
62
63         public String getGlobalUsername() {
64                 return getPropertyNonNull("glom.document.username").trim();
65         }
66
67         public String getGlobalPassword() {
68                 return getPropertyNonNull("glom.document.password");
69         }
70
71         public String getGlobalLocale() {
72                 return getPropertyNonNull("glom.document.locale");
73         }
74
75         public String getDocumentsDirectory() {
76                 return getPropertyNonNull("glom.document.directory");
77         }
78         
79         /** Get the key for any *.*.filename = thefilename line.
80          *
81          * @param value
82          * @return
83          */
84         private String getKey(final String filename) {
85                 
86                 for (final String key : stringPropertyNames()) {
87                         
88                         //Split the line at the . separators,
89                         final String[] keyArray = key.split("\\.");
90                         if (keyArray.length != 3)
91                                 continue;
92                         if(!("filename".equals(keyArray[2]))) {
93                                 continue;
94                         }
95
96                         if (getPropertyNonNull(key).trim().equals(filename)) {
97                                 return key;
98                         }
99                 }
100
101                 return null;
102         }
103         
104         private String getPropertyNonNull(final String key) {
105                 return StringUtils.defaultString(getProperty(key));
106         }
107 }