1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor.connector;
22
23 import java.io.IOException;
24 import java.util.List;
25
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28
29 import net.fckeditor.connector.exception.FolderAlreadyExistsException;
30 import net.fckeditor.connector.exception.InvalidCurrentFolderException;
31 import net.fckeditor.connector.exception.InvalidNewFolderNameException;
32 import net.fckeditor.connector.exception.ReadException;
33 import net.fckeditor.connector.exception.WriteException;
34 import net.fckeditor.handlers.Command;
35 import net.fckeditor.handlers.PropertiesLoader;
36 import net.fckeditor.handlers.RequestCycleHandler;
37 import net.fckeditor.handlers.ResourceType;
38 import net.fckeditor.requestcycle.Context;
39 import net.fckeditor.requestcycle.ThreadLocalData;
40 import net.fckeditor.response.GetResponse;
41 import net.fckeditor.response.UploadResponse;
42 import net.fckeditor.tool.Utils;
43 import net.fckeditor.tool.UtilsFile;
44 import net.fckeditor.tool.UtilsResponse;
45
46 import org.apache.commons.fileupload.FileItem;
47 import org.apache.commons.fileupload.FileItemFactory;
48 import org.apache.commons.fileupload.FileUploadException;
49 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
50 import org.apache.commons.fileupload.servlet.ServletFileUpload;
51 import org.apache.commons.io.FilenameUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public class Dispatcher {
69 private static final Logger logger = LoggerFactory.getLogger(Dispatcher.class);
70 private Connector connector;
71
72
73
74
75
76
77
78
79
80
81
82
83 Dispatcher(final ServletContext servletContext) throws Exception {
84
85 String className = PropertiesLoader.getConnectorImpl();
86 if (Utils.isEmpty(className))
87 logger.error("Empty Connector implementation class name provided");
88 else {
89 try {
90 Class<?> clazz = Class.forName(className);
91 connector = (Connector) clazz.newInstance();
92 logger.info("Connector initialized to {}", className);
93 } catch (Throwable e) {
94 logger.error("Connector implementation {} could not be instantiated", className);
95 throw new RuntimeException("Connector implementation " + className + " could not be instantiated", e);
96 }
97 }
98 connector.init(servletContext);
99 }
100
101
102
103
104
105
106
107
108
109
110
111 GetResponse doGet(final HttpServletRequest request) {
112 logger.debug("Entering Dispatcher#doGet");
113
114 Context context = ThreadLocalData.getContext();
115 context.logBaseParameters();
116
117 GetResponse getResponse = null;
118
119 if (!Command.isValidForGet(context.getCommandStr()))
120 getResponse = GetResponse.getInvalidCommandError();
121 else if (!ResourceType.isValidType(context.getTypeStr()))
122 getResponse = GetResponse.getInvalidResourceTypeError();
123 else if (!UtilsFile.isValidPath(context.getCurrentFolderStr()))
124 getResponse = GetResponse.getInvalidCurrentFolderError();
125 else {
126
127
128 ResourceType type = context.getResourceType();
129 Command command = context.getCommand();
130
131
132 if ((command.equals(Command.GET_FOLDERS) || command.equals(Command.GET_FOLDERS_AND_FILES))
133 && !RequestCycleHandler.isGetResourcesEnabled(request))
134 getResponse = GetResponse.getGetResourcesDisabledError();
135 else if (command.equals(Command.CREATE_FOLDER) && !RequestCycleHandler.isCreateFolderEnabled(request))
136 getResponse = GetResponse.getCreateFolderDisabledError();
137 else {
138
139
140 try {
141 if (command.equals(Command.CREATE_FOLDER)) {
142 String newFolderNameStr = request
143 .getParameter("NewFolderName");
144 logger.debug("Parameter NewFolderName: {}",
145 newFolderNameStr);
146 String sanitizedNewFolderNameStr = UtilsFile
147 .sanitizeFolderName(newFolderNameStr);
148 if (Utils.isEmpty(sanitizedNewFolderNameStr))
149 getResponse = GetResponse
150 .getInvalidNewFolderNameError();
151 else {
152 logger.debug(
153 "Parameter NewFolderName (sanitized): {}",
154 sanitizedNewFolderNameStr);
155 connector.createFolder(type, context
156 .getCurrentFolderStr(),
157 sanitizedNewFolderNameStr);
158 getResponse = GetResponse.getOK();
159 }
160 } else if (command.equals(Command.GET_FOLDERS)
161 || command
162 .equals(Command.GET_FOLDERS_AND_FILES)) {
163 String url = UtilsResponse.getUrl(RequestCycleHandler
164 .getUserFilesPath(request), type, context
165 .getCurrentFolderStr());
166 getResponse = getFoldersAndOrFiles(command, type, context
167 .getCurrentFolderStr(), url);
168 }
169 } catch (InvalidCurrentFolderException e) {
170 getResponse = GetResponse.getInvalidCurrentFolderError();
171 } catch (InvalidNewFolderNameException e) {
172 getResponse = GetResponse.getInvalidNewFolderNameError();
173 } catch (FolderAlreadyExistsException e) {
174 getResponse = GetResponse.getFolderAlreadyExistsError();
175 } catch (WriteException e) {
176 getResponse = GetResponse.getCreateFolderWriteError();
177 } catch (ReadException e) {
178 getResponse = GetResponse.getGetResourcesReadError();
179 }
180 }
181 }
182
183 logger.debug("Exiting Dispatcher#doGet");
184 return getResponse;
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 private GetResponse getFoldersAndOrFiles(final Command command,
209 final ResourceType type, final String currentFolder,
210 final String constructedUrl) throws InvalidCurrentFolderException,
211 ReadException {
212 GetResponse getResponse = new GetResponse(command, type,
213 currentFolder, constructedUrl);
214 getResponse.setFolders(connector.getFolders(type, currentFolder));
215 if (command.equals(Command.GET_FOLDERS_AND_FILES))
216 getResponse.setFiles(connector.getFiles(type, currentFolder));
217 return getResponse;
218 }
219
220
221
222
223
224
225
226
227
228
229 UploadResponse doPost(final HttpServletRequest request) {
230 logger.debug("Entering Dispatcher#doPost");
231
232 Context context = ThreadLocalData.getContext();
233 context.logBaseParameters();
234
235 UploadResponse uploadResponse = null;
236
237 if (!RequestCycleHandler.isFileUploadEnabled(request))
238 uploadResponse = UploadResponse.getFileUploadDisabledError();
239
240 else if (!Command.isValidForPost(context.getCommandStr()))
241 uploadResponse = UploadResponse.getInvalidCommandError();
242 else if (!ResourceType.isValidType(context.getTypeStr()))
243 uploadResponse = UploadResponse.getInvalidResourceTypeError();
244 else if (!UtilsFile.isValidPath(context.getCurrentFolderStr()))
245 uploadResponse = UploadResponse.getInvalidCurrentFolderError();
246 else {
247
248
249 ResourceType type = context.getDefaultResourceType();
250 FileItemFactory factory = new DiskFileItemFactory();
251 ServletFileUpload upload = new ServletFileUpload(factory);
252 try {
253 List<FileItem> items = upload.parseRequest(request);
254
255 FileItem uplFile = items.get(0);
256
257
258 String fileName = FilenameUtils.getName(uplFile.getName());
259 logger.debug("Parameter NewFile: {}", fileName);
260
261 if (type.isDeniedExtension(FilenameUtils.getExtension(fileName)))
262 uploadResponse = UploadResponse.getInvalidFileTypeError();
263
264 else if (type.equals(ResourceType.IMAGE)
265 && PropertiesLoader.isSecureImageUploads()
266 && !UtilsFile.isImage(uplFile.getInputStream())) {
267 uploadResponse = UploadResponse.getInvalidFileTypeError();
268 } else {
269 String sanitizedFileName = UtilsFile
270 .sanitizeFileName(fileName);
271 logger.debug("Parameter NewFile (sanitized): {}",
272 sanitizedFileName);
273 String newFileName = connector.fileUpload(type, context
274 .getCurrentFolderStr(), sanitizedFileName, uplFile
275 .getInputStream());
276 String fileUrl = UtilsResponse.fileUrl(RequestCycleHandler
277 .getUserFilesPath(request), type, context
278 .getCurrentFolderStr(), newFileName);
279
280 if (sanitizedFileName.equals(newFileName))
281 uploadResponse = UploadResponse.getOK(fileUrl);
282 else {
283 uploadResponse = UploadResponse.getFileRenamedWarning(fileUrl, newFileName);
284 logger.debug("Parameter NewFile (renamed): {}",
285 newFileName);
286 }
287 }
288
289 uplFile.delete();
290 } catch (InvalidCurrentFolderException e) {
291 uploadResponse = UploadResponse.getInvalidCurrentFolderError();
292 } catch (WriteException e) {
293 uploadResponse = UploadResponse.getFileUploadWriteError();
294 } catch (IOException e) {
295 uploadResponse = UploadResponse.getFileUploadWriteError();
296 } catch (FileUploadException e) {
297 uploadResponse = UploadResponse.getFileUploadWriteError();
298 }
299 }
300
301 logger.debug("Exiting Dispatcher#doPost");
302 return uploadResponse;
303 }
304
305 }