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.connector.Connector;
26  import net.fckeditor.requestcycle.UserAction;
27  import net.fckeditor.requestcycle.UserPathBuilder;
28  import net.fckeditor.tool.Utils;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Handles the {@link UserAction} and {@link UserPathBuilder} interfaces. <br />
35   * This class instantiates the the chosen implementations and acts as a proxy
36   * for their methods/access.
37   * 
38   * @version $Id: RequestCycleHandler.java 3759 2009-06-22 20:02:26Z mosipov $
39   */
40  public class RequestCycleHandler {
41  	private static final Logger logger = LoggerFactory
42  			.getLogger(RequestCycleHandler.class);
43  	private static UserAction userAction = null;
44  	private static UserPathBuilder userPathBuilder = null;
45  
46  	static {
47  		// If there are more objects to instantiate in future, we could solve
48  		// the following by reflection!
49  
50  		// 1. try to instantiate the UserAction object
51  		String className = PropertiesLoader.getUserActionImpl();
52  		if (Utils.isEmpty(className))
53  			logger.error("Empty UserAction implementation class name provided");
54  		else {
55  			try {
56  				Class<?> clazz = Class.forName(className);
57  				userAction = (UserAction) clazz.newInstance();
58  				logger.info("UserAction initialized to {}", className);
59  			} catch (Throwable e) {
60  				logger.error("UserAction implementation {} could not be instantiated", className);
61  				throw new RuntimeException("UserAction implementation " + className + " could not be instantiated", e); //$NON-NLS-1$
62  			}
63  		}
64  
65  		// 2. try to instantiate the UserPathBuilder object
66  		className = PropertiesLoader.getUserPathBuilderImpl();
67  		if (Utils.isEmpty(className))
68  			logger.error("Empty UserPathBuilder implementation class name provided");
69  		else {
70  			try {
71  				Class<?> clazz = Class.forName(className);
72  				userPathBuilder = (UserPathBuilder) clazz.newInstance();
73  				logger.info("UserPathBuilder initialized to {}", className);
74  			} catch (Throwable e) {
75  				logger.error("UserPathBuilder implementation {} could not be instantiated", className);
76  				throw new RuntimeException("UserPathBuilder implementation " + className + " could not be instantiated", e); //$NON-NLS-1$
77  			}
78  		}
79  	}
80  
81  	/**
82  	 * Just a wrapper to
83  	 * {@link UserAction#isEnabledForFileBrowsing(HttpServletRequest)}.
84  	 * 
85  	 * @return {@link UserAction#isEnabledForFileBrowsing(HttpServletRequest)}
86  	 *         or false if {@link UserAction} isn't set.
87  	 * @see #isGetResourcesEnabled(HttpServletRequest)
88  	 * @deprecated Method will be removed in FCKeditor.Java 2.6, user
89  	 *             {@link #isGetResourcesEnabled(HttpServletRequest)}.
90  	 */
91  	@Deprecated
92  	public static boolean isEnabledForFileBrowsing(
93  			final HttpServletRequest request) {
94  		return (userAction != null && userAction
95  				.isEnabledForFileBrowsing(request));
96  	}
97  	
98  	/**
99  	 * Returns <code>true</code> if user is allowed to list resources. The
100 	 * behavior is specified by the current UserAction instance.
101 	 * 
102 	 * @param request
103 	 *            current user request instance
104 	 * @return true if user is allowed to list resources, false otherwise
105 	 * @see UserAction#isEnabledForFileBrowsing(HttpServletRequest)
106 	 */
107 	public static boolean isGetResourcesEnabled(final HttpServletRequest request) {
108 		return userAction.isEnabledForFileBrowsing(request);
109 	}
110 
111 	/**
112 	 * Just a wrapper to
113 	 * {@link UserAction#isEnabledForFileUpload(HttpServletRequest)}.
114 	 * 
115 	 * @return {@link UserAction#isEnabledForFileUpload(HttpServletRequest)} or
116 	 *         false if {@link UserAction} isn't set.
117 	 * @see #isFileUploadEnabled(HttpServletRequest)
118 	 * @deprecated Method will be removed in FCKeditor.Java 2.6,
119 	 *             {@link #isFileUploadEnabled(HttpServletRequest)}.
120 	 */
121 	public static boolean isEnabledForFileUpload(
122 			final HttpServletRequest request) {
123 		return (userAction != null && userAction
124 				.isEnabledForFileUpload(request));
125 	}
126 	
127 	/**
128 	 * Returns <code>true</code> if user is allowed to upload files. The
129 	 * behavior is specified by the current UserAction instance.
130 	 * 
131 	 * @param request
132 	 *            current user request instance
133 	 * @return true if user is allowed to upload files, false otherwise
134 	 * @see UserAction#isEnabledForFileUpload(HttpServletRequest)
135 	 */
136 	public static boolean isFileUploadEnabled(HttpServletRequest request) {
137 		return userAction.isEnabledForFileUpload(request);
138 	}
139 
140 	/**
141 	 * Returns <code>true</code> if user is allowed to create folders. The
142 	 * behavior is specified by the current UserAction instance.
143 	 * 
144 	 * @param request
145 	 *            current user request instance
146 	 * @return true if user is allowed to create folders, false otherwise
147 	 * @see UserAction#isEnabledForFileBrowsing(HttpServletRequest)
148 	 */
149 	public static boolean isCreateFolderEnabled(final HttpServletRequest request) {
150 		return userAction.isCreateFolderEnabled(request);
151 	}
152 
153 	/**
154 	 * Returns the current userfiles path. The path is specified by the current
155 	 * UserPathBuilder instance.
156 	 * 
157 	 * @param request
158 	 *            current user request instance
159 	 * @return current userfiles path
160 	 * @see UserPathBuilder#getUserFilesPath(HttpServletRequest)
161 	 */
162 	public static String getUserFilesPath(final HttpServletRequest request) {
163 		return userPathBuilder.getUserFilesPath(request);
164 	}
165 
166 	/**
167 	 * Returns the current absolute userfiles path. The path is specified by the
168 	 * current UserPathBuilder instance. <br />
169 	 * Note that the path is absolute to the underlying system of the current
170 	 * {@link Connector} instance.
171 	 * 
172 	 * @param request
173 	 *            current user request instance
174 	 * @return current absolute userfiles path
175 	 * @see UserPathBuilder#getUserFilesAbsolutePath(HttpServletRequest)
176 	 */
177 	public static String getUserFilesAbsolutePath(final HttpServletRequest request) {
178 		return userPathBuilder.getUserFilesAbsolutePath(request);
179 	}
180 }