View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.services.webpage;
18  
19  import java.io.OutputStream;
20  import java.io.FileOutputStream;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.FileNotFoundException;
24  import java.util.Properties;
25  import javax.servlet.ServletException;
26  
27  import org.apache.log4j.Logger;
28  
29  /*
30   * Mutable Configuration settings for the WebPage Service
31   * Provides the singleton interface to the WebPage Service configuration state.
32   * The state of the configuration is serialized to persistence store in standard
33   * java <code>java.util.Parameter</code> format (name/value/pair)
34   *
35   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
36   * @version $Id: Configuration.java,v 1.2 2004/02/23 03:46:26 jford Exp $ 
37   */
38  public class Configuration
39  {  
40      public static final String WPS_SERVLET = "/wps";
41  
42      public static final int STATUS_NOT_CONFIGURED = 0;
43      public static final int STATUS_ONLINE = 1;
44      public static final int STATUS_OFFLINE = -1;
45  
46      static Logger log = Logger.getLogger(Configuration.class);
47  
48      // Key and default value for SID Query Request parameter
49      public static final String KEY_WPS_SID  = "wps.sid";
50      public static final String WPS_SID = "wpsid";
51      private String sid = WPS_SID; // cached value
52  
53      // Key and default value for PATH Query Request parameter
54      public static final String KEY_WPS_PATH     = "wps.path";
55      public static final String WPS_PATH    = "wpspath";
56      private String path = WPS_PATH; // cached value
57  
58      // Key and default value for URL Query Request parameter
59      public static final String KEY_WPS_URL  = "wps.url";
60      public static final String WPS_URL = "wps.url";
61      private String url = WPS_URL; // cached value
62  
63      // Key and default value for DEBUG setting
64      public static final String KEY_WPS_DEBUG     = "wps.debug";    
65      private boolean debug = false; // cached value
66  
67      // Key and default value for Parser setting
68      public static final String KEY_PARSER     = "parser.default";
69      public static final String PARSER_SWING     = "swing";
70      public static final String PARSER_OPENXML     = "openxml";
71      private String parser = PARSER_SWING; // cached value
72  
73      // Key and default value for Content Log Enabling/Disabling
74      public static final String KEY_LOG_ENABLE     = "log.content.enable";
75      private boolean enableContentLog = false;
76  
77      // Key and default value for ContentLog Location
78      public static final String KEY_LOG_LOCATION        = "log.content.location";
79      public static final String WPS_LOG_LOCATION     = "/WEB-INF/logs/wps-content.log";
80      private String logLocation = WPS_LOG_LOCATION; // cached value
81  
82      // Key and default value for Content Log Reset on Startup
83      public static final String KEY_LOG_RESET     = "log.content.reset";
84      private boolean resetContentLog = false;
85      
86      // Key and default value for Webapp name
87      public static final String KEY_WEBAPP         = "wps.webapp.name";
88      public static final String WPS_WEBAPP         = "/jetspeed";
89      private String webapp = WPS_WEBAPP; // cached value
90  
91      // Key and default value for Login Path web application
92      public static final String KEY_LOGIN   = "ne.webapp.login";
93      public static final String WPS_LOGIN = "/jetspeed/loginController.php";
94      private String login = WPS_LOGIN; // cached value
95  
96      // Key and default value for Logout Path web application
97      public static final String KEY_LOGOUT   = "ne.webapp.logout";
98      public static final String WPS_LOGOUT = "/jetspeed/logoutController.php";
99      private String logout = WPS_LOGOUT; // cached value
100 
101     // Key and default value for User Session Key
102     public static final String KEY_USER_SESSION    = "user.session.key";
103     public static final String WPS_USER_SESSION = "turbine.user";
104     private String userSessionKey = WPS_USER_SESSION; // cached value
105 
106     // Key and default value for Default User
107     public static final String KEY_USER_DEFAULT    = "user.default";
108     public static final String WPS_USER_DEFAULT = "joe";
109     private String userDefault = WPS_USER_DEFAULT; // cached value
110 
111     // Key and default value for Web Interface User Parameter
112     public static final String KEY_PARAM_USER = "ne.webapp.param.username";
113     public static final String WPS_PARAM_USER = "da_username";
114     private String paramUser = WPS_PARAM_USER; // cached value
115 
116     // Key and default value for Web Interface Password Parameter
117     public static final String KEY_PARAM_PASSWORD = "webapp.param.password";
118     public static final String WPS_PARAM_PASSWORD = "da_password";
119     private String paramPassword = WPS_PARAM_PASSWORD; // cached value
120 
121     // Key and default value for Web Interface Permission Parameter
122     public static final String KEY_PARAM_PERMISSIONS = "webapp.param.permissions";
123     public static final String WPS_PARAM_PERMISSIONS = "serialized_permissions";
124     private String paramPermissions = WPS_PARAM_PERMISSIONS; // cached value
125 
126     // Key and default value for Web Interface Login Failure String
127     public static final String KEY_LOGIN_FAILURE = "webapp.login.failure";
128     public static final String WPS_LOGIN_FAILURE = "LOGIN FAIL";
129     private String loginFailureString = WPS_LOGIN_FAILURE; // cached value
130     
131     // Key and default value for Web Interface Login Success String
132     public static final String KEY_LOGIN_SUCCESS = "webapp.login.success";
133     public static final String WPS_LOGIN_SUCCESS = "LOGIN SUCCESS";
134     private String loginSuccessString = WPS_LOGIN_SUCCESS; // cached value
135     
136 
137     // keys that don't need to be cached for performance
138     public static final String KEY_CONTENT_INFO = "content.info";
139     public static final String KEY_CONTENT_ERROR = "content.error";
140 
141     // constants
142     public static final String TRUE_VALUE     = "true";
143     public static final String FALSE_VALUE      = "false";
144 
145     // singleton instance
146     private static Configuration instance = null;
147 
148     // the properties are contained in memory here
149     private Properties properties;
150 
151     // path to the properties NVP (name/value/pair) file
152     private String ppath;
153 
154     // text placed into the first line of the configuration file.
155     public static final String HEADER = "Jetspeed Web Page Service Configuration Properties";
156 
157 
158     /*
159      * Singleton instance factory.     
160      *
161      * @return the Configuration singleton
162      *
163      */
164     public static Configuration getInstance() 
165     {
166         return instance;
167     }
168 
169     /*
170      * Singleton instance factory. Call this to get the initial instance, loaded
171      * the state from persistent store.
172      *
173      * @return the Configuration singleton
174      * @throws ServletException
175      */
176     public static Configuration getInitialInstance(String ppath) throws ServletException
177     {
178         if (instance == null)
179         {    
180             Properties properties = new Properties();
181     
182             try
183             {
184                 properties.load(new FileInputStream(ppath));        
185                 instance = new Configuration(properties, ppath);
186                 instance.refresh();
187             }
188             catch (Exception e)
189             {
190                 log.debug(e.toString());            
191                 instance = null;
192             }
193         }
194         return instance;
195     }
196 
197     /*
198      * Private constructor for singleton configuration.
199      *
200      * @param     properties    the properties collection for this configuration
201      * @param     path         the path to the properties file
202      *
203      */
204     private Configuration(Properties properties, String ppath)
205     {
206         this.properties = properties;
207         this.ppath = ppath;
208     }
209 
210     /*
211      * Used to obtain a configuration property.
212      *
213      * @param    name - the property name
214      * @return    the property value or <code>null</code> if the property isn't found
215      *
216      */
217     public String getProperty(String name)
218     {
219         return properties.getProperty(name);
220     }
221 
222     /*
223      * Inserts a new property or updates an existing one.
224      * @param    name - the property name
225      * @param    value - the property value
226      *
227      */
228     public void setProperty(String name, String value) 
229     {                            
230         properties.setProperty(name, value);
231         OutputStream out;
232 
233         try
234         {
235             out = new FileOutputStream(ppath);
236             properties.store(out, HEADER);
237             out.close();
238         }
239         catch (FileNotFoundException e)
240         {
241             String error = "Unable to update configuration file " + ppath + e.toString();
242             log.debug(error);
243         }
244         catch (SecurityException e)
245         {
246             String error = "Unable to update configuration file " + ppath + e.toString();
247             log.debug(error);
248         }
249         catch (IOException e)
250         {
251             String error = "Error updating configuration file " + ppath + e.toString();
252             log.debug(error);
253         }
254         out = null;
255     }
256 
257     /*
258      * Refresh the configuration internal state from the properties persistent store.
259      *
260      */
261     public void refresh()
262     {
263         String result = getInstance().getProperty(Configuration.KEY_WPS_DEBUG);
264         if (null == result) 
265         {
266             debug = false;
267         }
268         else
269         {
270             debug = (result.equalsIgnoreCase(TRUE_VALUE));
271         }
272 
273         sid = getInstance().getProperty(Configuration.KEY_WPS_SID);
274         if (null == sid) 
275         {
276             sid = WPS_SID;
277         }
278 
279         path = getInstance().getProperty(Configuration.KEY_WPS_PATH);
280         if (null == path) 
281             path = WPS_PATH;
282 
283         url = getInstance().getProperty(Configuration.KEY_WPS_URL);
284         if (null == url) 
285             url = WPS_URL;
286 
287         parser = getInstance().getProperty(Configuration.KEY_PARSER);
288         if (null == parser) 
289             parser = PARSER_SWING;
290 
291         result = getInstance().getProperty(Configuration.KEY_LOG_ENABLE);
292         if (null == result) 
293             enableContentLog = false;
294         else
295             enableContentLog = (result.equalsIgnoreCase(TRUE_VALUE));
296     
297         logLocation = getInstance().getProperty(Configuration.KEY_LOG_LOCATION);
298         if (null == logLocation) 
299             logLocation = WPS_LOG_LOCATION;
300     
301         result = getInstance().getProperty(Configuration.KEY_LOG_RESET);
302         if (null == result) 
303             resetContentLog = false;
304         else
305             resetContentLog = (result.equalsIgnoreCase(TRUE_VALUE));
306     
307         webapp = getInstance().getProperty(Configuration.KEY_WEBAPP);
308         if (null == webapp) 
309             webapp = WPS_WEBAPP;
310     
311         login = getInstance().getProperty(Configuration.KEY_LOGIN);
312         if (null == login) 
313              login = WPS_LOGIN;
314     
315         logout = getInstance().getProperty(Configuration.KEY_LOGOUT);
316         if (null == logout) 
317              login = WPS_LOGOUT;
318     
319         userSessionKey = getInstance().getProperty(Configuration.KEY_USER_SESSION);
320         if (null == userSessionKey) 
321              userSessionKey = WPS_USER_SESSION;
322 
323         userDefault = getInstance().getProperty(Configuration.KEY_USER_DEFAULT);
324         if (null == userDefault) 
325             userDefault = WPS_USER_DEFAULT;
326 
327         paramUser = getInstance().getProperty(Configuration.KEY_PARAM_USER);
328         if (null == paramUser) 
329             paramUser = WPS_PARAM_USER;
330 
331         paramPassword = getInstance().getProperty(Configuration.KEY_PARAM_PASSWORD);
332         if (null == paramPassword) 
333             paramPassword = WPS_PARAM_PASSWORD;
334 
335         paramPermissions = getInstance().getProperty(Configuration.KEY_PARAM_PERMISSIONS);
336         if (null == paramPermissions) 
337             paramPermissions = WPS_PARAM_PERMISSIONS;
338 
339         loginFailureString = getInstance().getProperty(Configuration.KEY_LOGIN_FAILURE);
340         if (null == loginFailureString) 
341             loginFailureString = WPS_LOGIN_FAILURE;
342 
343         loginSuccessString = getInstance().getProperty(Configuration.KEY_LOGIN_SUCCESS);
344         if (null == loginSuccessString) 
345             loginSuccessString = WPS_LOGIN_SUCCESS;
346 
347     }
348 
349     /*
350      * Static accessors for convenient access to configuration state.
351      *
352      */
353     public boolean getDebug()
354     {
355         return debug;
356     }
357     
358     public String getSID()
359     {
360         return sid;
361     }
362 
363     public String getPath()
364     {
365         return path;
366     }
367 
368     public String getURL()
369     {
370         return url;
371     }
372 
373     public String getParser()
374     {
375         return parser;
376     }
377 
378     public boolean getEnableContentLog()
379     {
380         return enableContentLog;
381     }
382 
383     public String getLogLocation()
384     {
385         return logLocation;
386     }
387     
388     public boolean getResetContentLog()
389     {
390         return resetContentLog;
391     }
392 
393     public String getWebapp()
394     {
395          return webapp;
396     }
397 
398     public String getLogin()
399     {
400         return login;
401     }
402 
403     public String getLogout()
404     {
405         return logout;
406     }
407 
408     public String getUserSessionKey()
409     {
410         return userSessionKey;
411     }
412 
413     public String getDefaultUser()
414     {
415         return userDefault;
416     }
417 
418     public String getParamUser()
419     {
420         return paramUser;
421     }
422 
423     public String getParamPassword()
424     {
425         return paramPassword;
426     }
427 
428     public String getParamPermissions()
429     {
430         return paramPermissions;
431     }
432 
433     public String getLoginFailureString()
434     {
435         return loginFailureString;
436     }
437 
438     public String getLoginSuccessString()
439     {
440         return loginSuccessString;
441     }
442 
443     /*
444      * Create a go-between String using the format expected by the WPS.
445      *
446      * Example:
447      *
448      *  http://<ProxyHostAddess>/jetProxy?jaid=1.Element?japath=/somestuff/Controller.php
449      *
450      * @param proxyHost The base URL of the proxy server's host, i.e (http://localhost).
451      * @param neid The unique network element id.
452      * @param resource The resource to be proxied. This can be a combination of the relative
453      *        path + the resource, or just solely the resource.
454      * @param relativePath The relative path to the resource. Necessary for request-relative
455      *        resources. Specify as null to disable this parameter.
456      */
457     public static String createProxyString(String proxyHost, 
458                                            String neid,
459                                            String resource,
460                                            String relativePath)
461     {
462         String base = WebPageHelper.concatURLs(proxyHost, WPS_SERVLET);
463         StringBuffer buffer = new StringBuffer(base);                    
464         //
465         // build the request string in proxy server expected format
466         //
467         // Example:
468         //
469         // http://<ProxyHostAddess>/jetProxy?jaid=1.Element?japath=/somestuff/Controller.php
470         //
471         buffer.append("?");
472         buffer.append(getInstance().getSID());
473         buffer.append("=");
474         buffer.append(neid);  
475         buffer.append("&");
476         buffer.append(getInstance().getPath());
477         buffer.append("=");
478 
479         //
480         // Is it a request-relative or webapp-relative resource?
481         // When the target resource starts with "/", then it is webapp-relative
482         // otherwise it is request-relative. When request relative, use the
483         // path from the original request to find the resource, otherwise take
484         // the resource as is.
485         //
486         if (null != relativePath && !resource.startsWith("/"))
487         {
488             // its request-relative, use path from request
489             buffer.append(relativePath);          
490         }
491 
492         buffer.append(resource.replace('&', '@'));
493         String proxiedPath = buffer.toString();                
494         return proxiedPath;            
495     }
496 }