View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2003-2008 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.handlers;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import net.fckeditor.requestcycle.UserAction;
26  import net.fckeditor.requestcycle.UserPathBuilder;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Handler for classes which implement the interfaces from the package
33   * {@link net.fckeditor.requestcycle}.
34   * 
35   * @version $Id: RequestCycleHandler.java 1961 2008-05-08 08:08:16Z th-schwarz $
36   */
37  public class RequestCycleHandler {
38  	private static Logger logger = LoggerFactory.getLogger(RequestCycleHandler.class);
39  	private static UserAction userAction = null;
40  	private static UserPathBuilder userPathBuilder = null;
41  
42  	static {
43  		// If there are more objects to instantiate in future, we could solve the following by reflection!
44  		
45  		// 1. try to instantiate the UserAction object
46  		String fqcn = PropertiesLoader.getProperty("connector.userActionImpl");
47  		if (fqcn == null)
48  			logger.warn("No property found for UserAction implementation, any user action is disabled!");
49  		else {
50  			try {
51  				Class<?> clazz = Class.forName(fqcn);
52  				userAction = (UserAction) clazz.newInstance();
53  				logger.info("UserAction object successful instanciated!");
54  			} catch (Exception e) {
55  				logger.error("Couldn't instanciate the class [".concat(fqcn).concat(
56  				        "], any user action of the ConnectorServlet is disabled!"), e);
57  			}
58  		}
59  
60  		// 2. try to instantiate the UserPathBuilder object
61  		fqcn = PropertiesLoader.getProperty("connector.userPathBuilderImpl");
62  		if (fqcn == null)
63  			logger.warn("No property found for UserPathBuilder implementation! "
64  					.concat("The default of users's 'BaseDir' will be used in the ConnectorServlet!"));
65  		else {
66  			try {
67  				Class<?> clazz = Class.forName(fqcn);
68  				userPathBuilder = (UserPathBuilder) clazz.newInstance();
69  				logger.info("UserPathBuilder object successfull instanciated!");
70  			} catch (Exception e) {
71  				logger.error("Couldn't instanciate the class [".concat(fqcn)
72  				        .concat("], The default of users's 'BaseDir' will be used in the ConnectorServlet!"), e);
73  			}
74  		}
75  	}
76  
77  	/**
78  	 * Just a wrapper to {@link UserAction#isEnabledForFileBrowsing(HttpServletRequest)}.
79  	 * 
80  	 * @param servletRequest
81  	 * @return {@link UserAction#isEnabledForFileBrowsing(HttpServletRequest)} or false, if
82  	 *         sessionData isn't set.
83  	 */
84  	public static boolean isEnabledForFileBrowsing(final HttpServletRequest servletRequest) {
85  		return (userAction != null && userAction.isEnabledForFileBrowsing(servletRequest));
86  	}
87  
88  	/**
89  	 * Just a wrapper to {@link UserAction#isEnabledForFileUpload(HttpServletRequest)}.
90  	 * 
91  	 * @param request
92  	 * @return {@link UserAction#isEnabledForFileUpload(HttpServletRequest)} or false, if userAction
93  	 *         isn't set.
94  	 */
95  	public static boolean isEnabledForFileUpload(final HttpServletRequest request) {
96  		return (userAction != null && userAction.isEnabledForFileUpload(request));
97  	}
98  
99  	/**
100 	 * Getter for the user's file path.<br>
101 	 * Method is used by other handlers only!
102 	 * 
103 	 * @param request
104 	 * @return {@link UserPathBuilder#getUserFilesPath(HttpServletRequest)} or null, if
105 	 *         userPathBuilder is null.
106 	 */
107 	protected static String getUserFilePath(final HttpServletRequest request) {
108 		return (userPathBuilder != null) ? userPathBuilder.getUserFilesPath(request) : null;
109 	}
110 }