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.requestcycle;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import net.fckeditor.connector.Dispatcher;
26  import net.fckeditor.handlers.Command;
27  import net.fckeditor.handlers.ResourceType;
28  import net.fckeditor.tool.Utils;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Maintains base/common request parameters from the File Browser. The gain of
35   * this class is to provide abstracted and pre-processed access to common
36   * request parameters.
37   * <p>
38   * In the regular case, you will use an already created instance of this class.
39   * </p>
40   * 
41   * @version $Id: Context.java 3695 2009-06-18 20:18:38Z mosipov $
42   */
43  public class Context {
44  	// This is just a helper class which has no relevance for the logger
45  	private static final Logger logger = LoggerFactory.getLogger(Dispatcher.class);
46  	
47  	private String typeStr;
48  	private String commandStr;
49  	private String currentFolderStr;
50  	
51  	/**
52  	 * Sole class constructor. Takes in a request instance, processes parameters
53  	 * and populates private fields which can be accessed through getters.<br />
54  	 * This class will only be instantiated in {@link ThreadLocalData}.
55  	 * 
56  	 * @param request
57  	 *            current user request instance
58  	 */
59  	protected Context(final HttpServletRequest request) {
60  		commandStr = request.getParameter("Command");
61  		typeStr = request.getParameter("Type");
62  		currentFolderStr = request.getParameter("CurrentFolder");
63  		
64  		// if this is a QuickUpload request, 'commandStr', 'currentFolderStr'
65  		// are empty and 'typeStr' maybe empty too
66  		if (Utils.isEmpty(commandStr) && Utils.isEmpty(currentFolderStr)) {
67  			commandStr = "QuickUpload";
68  			currentFolderStr = "/";
69  			if (Utils.isEmpty(typeStr))
70  				typeStr = "File";
71  		}
72  		
73  		// checks to meet specs in http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/Server_Side_Integration#File_Browser_Requests
74  		if (currentFolderStr != null && !currentFolderStr.startsWith("/"))
75  			currentFolderStr = "/".concat(currentFolderStr);
76  	}
77  	
78  	/**
79  	 * Returns the type parameter of this context.
80  	 * 
81  	 * @return the type parameter of this context
82  	 */
83  	public String getTypeStr() {
84  		return typeStr;
85  	}
86  	
87  	/**
88  	 * Returns a default resource type instance for the type parameter.
89  	 * 
90  	 * @see ResourceType#getDefaultResourceType(String)
91  	 * @return default resource type instance
92  	 */
93  	public ResourceType getDefaultResourceType() {
94  		return ResourceType.getDefaultResourceType(typeStr);
95  	}
96  
97  	/**
98  	 * Returns a resource type instance for the type parameter.
99  	 * 
100 	 * @see ResourceType#getResourceType(String)
101 	 * @return resource type instance
102 	 */
103 	public ResourceType getResourceType() {
104 		return ResourceType.getResourceType(typeStr);
105 	}
106 
107 	/**
108 	 * Returns the command parameter of this context.
109 	 * 
110 	 * @return the command parameter of this context
111 	 */
112 	public String getCommandStr() {
113 		return commandStr;
114 	}
115 
116 	/**
117 	 * Returns a command instance for the command parameter.
118 	 * 
119 	 * @see Command#getCommand(String)
120 	 * @return command instance
121 	 */
122 	public Command getCommand() {
123 		return Command.getCommand(commandStr);
124 	}
125 	
126 	/**
127 	 * Returns the current folder parameter of this context.
128 	 * 
129 	 * @return the current folder parameter of this context
130 	 */
131 	public String getCurrentFolderStr() {
132 		return currentFolderStr;
133 	}
134 	
135 	/** Logs base/common request parameters. */
136 	public void logBaseParameters() {
137 		logger.debug("Parameter Command: {}", commandStr);
138 		logger.debug("Parameter Type: {}", typeStr);
139 		logger.debug("Parameter CurrentFolder: {}", currentFolderStr);
140 	}
141 
142 }