View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 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 4785 2009-12-21 20:10:28Z 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  	 * Returns <code>true</code> if user is allowed to list resources. The
83  	 * behavior is specified by the current UserAction instance.
84  	 * 
85  	 * @param request
86  	 *            current user request instance
87  	 * @return true if user is allowed to list resources, false otherwise
88  	 * @see UserAction#isEnabledForFileBrowsing(HttpServletRequest)
89  	 */
90  	public static boolean isGetResourcesEnabled(final HttpServletRequest request) {
91  		return userAction.isEnabledForFileBrowsing(request);
92  	}
93  	
94  	/**
95  	 * Returns <code>true</code> if user is allowed to upload files. The
96  	 * behavior is specified by the current UserAction instance.
97  	 * 
98  	 * @param request
99  	 *            current user request instance
100 	 * @return true if user is allowed to upload files, false otherwise
101 	 * @see UserAction#isEnabledForFileUpload(HttpServletRequest)
102 	 */
103 	public static boolean isFileUploadEnabled(HttpServletRequest request) {
104 		return userAction.isEnabledForFileUpload(request); 
105 	}
106 
107 	/**
108 	 * Returns <code>true</code> if user is allowed to create folders. The
109 	 * behavior is specified by the current UserAction instance.
110 	 * 
111 	 * @param request
112 	 *            current user request instance
113 	 * @return true if user is allowed to create folders, false otherwise
114 	 * @see UserAction#isEnabledForFileBrowsing(HttpServletRequest)
115 	 */
116 	public static boolean isCreateFolderEnabled(final HttpServletRequest request) {
117 		return userAction.isCreateFolderEnabled(request);
118 	}
119 
120 	/**
121 	 * Returns the current userfiles path. The path is specified by the current
122 	 * UserPathBuilder instance.
123 	 * 
124 	 * @param request
125 	 *            current user request instance
126 	 * @return current userfiles path
127 	 * @see UserPathBuilder#getUserFilesPath(HttpServletRequest)
128 	 */
129 	public static String getUserFilesPath(final HttpServletRequest request) {
130 		return userPathBuilder.getUserFilesPath(request);
131 	}
132 
133 	/**
134 	 * Returns the current absolute userfiles path. The path is specified by the
135 	 * current UserPathBuilder instance. <br />
136 	 * Note that the path is absolute to the underlying system of the current
137 	 * {@link Connector} instance.
138 	 * 
139 	 * @param request
140 	 *            current user request instance
141 	 * @return current absolute userfiles path
142 	 * @see UserPathBuilder#getUserFilesAbsolutePath(HttpServletRequest)
143 	 */
144 	public static String getUserFilesAbsolutePath(final HttpServletRequest request) {
145 		return userPathBuilder.getUserFilesAbsolutePath(request);
146 	}
147 }