View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2009 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 package
33   * {@link net.fckeditor.requestcycle}.
34   * 
35   * @version $Id: RequestCycleHandler.java 3840 2009-07-08 20:29:46Z mosipov $
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 will be disabled!");
49  		else {
50  			try {
51  				Class<?> clazz = Class.forName(fqcn);
52  				userAction = (UserAction) clazz.newInstance();
53  				logger.info("UserAction implementation successfully instantiated!");
54  			} catch (Exception e) {
55  				logger.error("Couldn't instantiate class [".concat(fqcn).concat(
56  				        "], any user action will 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 'DefaultUserFilesPath' 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 successfully instantiated!");
70  			} catch (Exception e) {
71  				logger.error("Couldn't instantiate class [".concat(fqcn)
72  				        .concat("], The 'DefaultUserFilesPath' will be used in the ConnectorServlet!"), e);
73  			}
74  		}
75  	}
76  
77  	/**
78  	 * Just a wrapper to
79  	 * {@link UserAction#isEnabledForFileBrowsing(HttpServletRequest)}.
80  	 * 
81  	 * @param request
82  	 * @return {@link UserAction#isEnabledForFileBrowsing(HttpServletRequest)}
83  	 *         or false if <code>userAction</code> isn't set.
84  	 */
85  	public static boolean isEnabledForFileBrowsing(final HttpServletRequest request) {
86  		return (userAction != null && userAction.isEnabledForFileBrowsing(request));
87  	}
88  
89  	/**
90  	 * Just a wrapper to
91  	 * {@link UserAction#isEnabledForFileUpload(HttpServletRequest)}.
92  	 * 
93  	 * @param request
94  	 * @return {@link UserAction#isEnabledForFileUpload(HttpServletRequest)} or
95  	 *         false if <code>userAction</code> isn't set.
96  	 */
97  	public static boolean isEnabledForFileUpload(final HttpServletRequest request) {
98  		return (userAction != null && userAction.isEnabledForFileUpload(request));
99  	}
100 
101 	/**
102 	 * Getter for the <code>UserFilesPath</code>.<br />
103 	 * Method is used by other handlers only!
104 	 * 
105 	 * @param request
106 	 * @return {@link UserPathBuilder#getUserFilesPath(HttpServletRequest)} or
107 	 *         <code>null</code> if <code>userPathBuilder</code> is
108 	 *         <code>null</code>.
109 	 */
110 	protected static String getUserFilePath(final HttpServletRequest request) {
111 		return (userPathBuilder != null) ? userPathBuilder.getUserFilesPath(request) : null;
112 	}
113 }