1
14
15 package com.liferay.portal.webserver;
16
17 import com.liferay.portal.NoSuchGroupException;
18 import com.liferay.portal.freemarker.FreeMarkerUtil;
19 import com.liferay.portal.kernel.dao.orm.QueryUtil;
20 import com.liferay.portal.kernel.freemarker.FreeMarkerContext;
21 import com.liferay.portal.kernel.freemarker.FreeMarkerEngineUtil;
22 import com.liferay.portal.kernel.util.ContentTypes;
23 import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
24 import com.liferay.portal.kernel.util.FileUtil;
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.HttpUtil;
27 import com.liferay.portal.kernel.util.MimeTypesUtil;
28 import com.liferay.portal.kernel.util.OrderByComparator;
29 import com.liferay.portal.kernel.util.ParamUtil;
30 import com.liferay.portal.kernel.util.ReleaseInfo;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.kernel.util.Validator_IW;
35 import com.liferay.portal.model.Company;
36 import com.liferay.portal.model.Group;
37 import com.liferay.portal.model.GroupConstants;
38 import com.liferay.portal.model.User;
39 import com.liferay.portal.security.auth.PrincipalException;
40 import com.liferay.portal.security.auth.PrincipalThreadLocal;
41 import com.liferay.portal.security.permission.PermissionChecker;
42 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
43 import com.liferay.portal.security.permission.PermissionThreadLocal;
44 import com.liferay.portal.service.CompanyLocalServiceUtil;
45 import com.liferay.portal.service.GroupLocalServiceUtil;
46 import com.liferay.portal.service.UserLocalServiceUtil;
47 import com.liferay.portal.util.PortalUtil;
48 import com.liferay.portal.util.comparator.GroupFriendlyURLComparator;
49 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
50 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
51 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
52 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
53 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
54 import com.liferay.portlet.documentlibrary.model.DLFolder;
55 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
56 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
57 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
58 import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
59 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
60 import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
61 import com.liferay.portlet.documentlibrary.util.DLUtil;
62 import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
63 import com.liferay.util.servlet.ServletResponseUtil;
64
65 import java.io.IOException;
66 import java.io.InputStream;
67
68 import java.text.Format;
69
70 import java.util.ArrayList;
71 import java.util.Collections;
72 import java.util.LinkedHashMap;
73 import java.util.List;
74
75 import javax.servlet.ServletException;
76 import javax.servlet.http.HttpServlet;
77 import javax.servlet.http.HttpServletRequest;
78 import javax.servlet.http.HttpServletResponse;
79
80
86 public class WebServerServlet extends HttpServlet {
87
88 public void service(
89 HttpServletRequest request, HttpServletResponse response)
90 throws IOException, ServletException {
91
92 try {
93 long companyId = PortalUtil.getCompanyId(request);
94
95 User user = PortalUtil.getUser(request);
96
97 if (user == null) {
98 Company company = CompanyLocalServiceUtil.getCompany(companyId);
99
100 user = company.getDefaultUser();
101 }
102
103 PrincipalThreadLocal.setName(user.getUserId());
104
105 PermissionChecker permissionChecker =
106 PermissionCheckerFactoryUtil.create(user, true);
107
108 PermissionThreadLocal.setPermissionChecker(permissionChecker);
109
110 String path = HttpUtil.fixPath(request.getPathInfo());
111 String[] pathArray = StringUtil.split(path, StringPool.SLASH);
112
113 if (pathArray.length == 0) {
114 sendGroups(
115 response, user,
116 request.getServletPath() + StringPool.SLASH + path);
117 }
118 else {
119 if (Validator.isNumber(pathArray[0])) {
120 sendFile(request, response, user, pathArray);
121 }
122 else {
123 sendDocumentLibrary(
124 request, response, user,
125 request.getServletPath() + StringPool.SLASH + path,
126 pathArray);
127 }
128 }
129 }
130 catch (NoSuchFileEntryException nsfee) {
131 PortalUtil.sendError(
132 HttpServletResponse.SC_NOT_FOUND, nsfee, request, response);
133 }
134 catch (Exception e) {
135 PortalUtil.sendError(e, request, response);
136 }
137 }
138
139 protected long getGroupId(long companyId, String name) throws Exception {
140 try {
141 Group group = GroupLocalServiceUtil.getFriendlyURLGroup(
142 companyId, StringPool.SLASH + name);
143
144 return group.getGroupId();
145 }
146 catch (NoSuchGroupException nsge) {
147 }
148
149 User user = UserLocalServiceUtil.getUserByScreenName(companyId, name);
150
151 Group group = user.getGroup();
152
153 return group.getGroupId();
154 }
155
156 protected List<Group> getGroups(User user) throws Exception {
157
158
160 if (user.isDefaultUser()) {
161 List<Group> groups = new ArrayList<Group>();
162
163 Group group = GroupLocalServiceUtil.getGroup(
164 user.getCompanyId(), GroupConstants.GUEST);
165
166 groups.add(group);
167
168 return groups;
169 }
170
171
173 LinkedHashMap<String, Object> params =
174 new LinkedHashMap<String, Object>();
175
176 params.put("usersGroups", user.getUserId());
177
178 OrderByComparator orderByComparator = new GroupFriendlyURLComparator(
179 true);
180
181 List<Group> groups = GroupLocalServiceUtil.search(
182 user.getCompanyId(), null, null, params, QueryUtil.ALL_POS,
183 QueryUtil.ALL_POS, orderByComparator);
184
185
187 groups.addAll(
188 GroupLocalServiceUtil.getUserOrganizationsGroups(
189 user.getUserId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS));
190
191
193 if (!user.isDefaultUser()) {
194 groups.add(user.getGroup());
195 }
196
197 Collections.sort(groups, orderByComparator);
198
199 return groups;
200 }
201
202 protected void sendDocumentLibrary(
203 HttpServletRequest request, HttpServletResponse response, User user,
204 String path, String[] pathArray)
205 throws Exception {
206
207 long groupId = getGroupId(user.getCompanyId(), pathArray[0]);
208 long dlFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
209
210 for (int i = 1; i < pathArray.length; i++) {
211 String name = pathArray[i];
212
213 try {
214 DLFolder folder = DLFolderServiceUtil.getFolder(
215 groupId, dlFolderId, name);
216
217 dlFolderId = folder.getFolderId();
218 }
219 catch (NoSuchFolderException nsfe) {
220 if (i != pathArray.length - 1) {
221 throw nsfe;
222 }
223
224 String title = name;
225
226 sendFile(response, user, groupId, dlFolderId, title);
227
228 return;
229 }
230 }
231
232 try {
233 sendFile(response, user, groupId, dlFolderId, "index.html");
234
235 return;
236 }
237 catch (Exception e) {
238 if ((e instanceof NoSuchFileEntryException) ||
239 (e instanceof PrincipalException)) {
240
241 try {
242 sendFile(response, user, groupId, dlFolderId, "index.htm");
243
244 return;
245 }
246 catch (NoSuchFileEntryException nsfee) {
247 }
248 catch (PrincipalException pe) {
249 }
250 }
251 else {
252 throw e;
253 }
254 }
255
256 List<WebServerEntry> webServerEntries = new ArrayList<WebServerEntry>();
257
258 webServerEntries.add(new WebServerEntry(path, "../"));
259
260 List<DLFolder> dlFolders = DLFolderServiceUtil.getFolders(
261 groupId, dlFolderId);
262
263 for (DLFolder dlFolder : dlFolders) {
264 webServerEntries.add(
265 new WebServerEntry(
266 path, dlFolder.getName() + StringPool.SLASH,
267 dlFolder.getCreateDate(), dlFolder.getModifiedDate(),
268 dlFolder.getDescription(), 0));
269 }
270
271 List<DLFileEntry> dlFileEntries = DLFileEntryServiceUtil.getFileEntries(
272 groupId, dlFolderId);
273
274 for (DLFileEntry dlFileEntry : dlFileEntries) {
275 webServerEntries.add(
276 new WebServerEntry(
277 path, dlFileEntry.getTitle(),
278 dlFileEntry.getCreateDate(), dlFileEntry.getModifiedDate(),
279 dlFileEntry.getDescription(), dlFileEntry.getSize()));
280 }
281
282 sendHTML(response, path, webServerEntries);
283 }
284
285 protected void sendFile(
286 HttpServletRequest request, HttpServletResponse response,
287 User user, String[] pathArray)
288 throws Exception {
289
290 long groupId = 0;
291 long folderId = 0;
292 String name = null;
293 String fileName = null;
294
295 DLFileEntry dlFileEntry = null;
296
297 if (pathArray.length == 1) {
298 long fileShortcutId = GetterUtil.getLong(pathArray[0]);
299
300 DLFileShortcut fileShortcut =
301 DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
302
303 groupId = fileShortcut.getGroupId();
304 folderId = fileShortcut.getToFolderId();
305 name = fileShortcut.getToName();
306
307 dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
308 groupId, folderId, name);
309
310 fileName = dlFileEntry.getTitle();
311 }
312 else if (pathArray.length == 2) {
313 groupId = GetterUtil.getLong(pathArray[0]);
314
315 dlFileEntry =
316 DLFileEntryLocalServiceUtil.getFileEntryByUuidAndGroupId(
317 pathArray[1], groupId);
318
319 folderId = dlFileEntry.getFolderId();
320 fileName = dlFileEntry.getTitle();
321 name = dlFileEntry.getName();
322 }
323 else {
324 groupId = GetterUtil.getLong(pathArray[0]);
325 folderId = GetterUtil.getLong(pathArray[1]);
326 fileName = HttpUtil.decodeURL(pathArray[2], true);
327
328 dlFileEntry = DLFileEntryServiceUtil.getFileEntryByTitle(
329 groupId, folderId, fileName);
330
331 name = dlFileEntry.getName();
332 }
333
334 if (dlFileEntry == null) {
335 throw new NoSuchFileEntryException();
336 }
337
338 String version = ParamUtil.getString(request, "version");
339
340 String targetExtension = ParamUtil.getString(
341 request, "targetExtension");
342
343 if (Validator.isNull(version)) {
344 if (Validator.isNotNull(dlFileEntry.getVersion())) {
345 version = dlFileEntry.getVersion();
346 }
347 else {
348 throw new NoSuchFileEntryException();
349 }
350 }
351
352 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
353 user.getCompanyId(), user.getUserId(), groupId, folderId, name,
354 version);
355
356 boolean converted = false;
357
358 if (Validator.isNotNull(targetExtension)) {
359 String id = DocumentConversionUtil.getTempFileId(
360 dlFileEntry.getFileEntryId(), version);
361
362 String sourceExtension = FileUtil.getExtension(fileName);
363
364 InputStream convertedIS = DocumentConversionUtil.convert(
365 id, is, sourceExtension, targetExtension);
366
367 if ((convertedIS != null) && (convertedIS != is)) {
368 fileName = FileUtil.stripExtension(fileName).concat(
369 StringPool.PERIOD).concat(targetExtension);
370
371 is = convertedIS;
372
373 converted = true;
374 }
375 }
376
377 int contentLength = 0;
378
379 if (!converted) {
380 if (DLUtil.compareVersions(version, dlFileEntry.getVersion()) >=
381 0) {
382
383 contentLength = (int)dlFileEntry.getSize();
384 }
385 else {
386 DLFileVersion fileVersion =
387 DLFileVersionLocalServiceUtil.getFileVersion(
388 groupId, folderId, name, version);
389
390 contentLength = (int)fileVersion.getSize();
391 }
392 }
393
394 String contentType = MimeTypesUtil.getContentType(fileName);
395
396 ServletResponseUtil.sendFile(
397 request, response, fileName, is, contentLength, contentType);
398 }
399
400 protected void sendFile(
401 HttpServletResponse response, User user, long groupId,
402 long folderId, String title)
403 throws Exception {
404
405 DLFileEntry dlFileEntry = DLFileEntryServiceUtil.getFileEntryByTitle(
406 groupId, folderId, title);
407
408 String contentType = MimeTypesUtil.getContentType(
409 dlFileEntry.getTitle());
410
411 InputStream inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(
412 user.getCompanyId(), user.getUserId(), groupId, folderId,
413 dlFileEntry.getName());
414
415 response.setContentType(contentType);
416
417 ServletResponseUtil.write(response, inputStream);
418 }
419
420 protected void sendGroups(
421 HttpServletResponse response, User user, String path)
422 throws Exception {
423
424 List<WebServerEntry> webServerEntries = new ArrayList<WebServerEntry>();
425
426 List<Group> groups = getGroups(user);
427
428 for (Group group : groups) {
429 String name = HttpUtil.fixPath(group.getFriendlyURL());
430
431 webServerEntries.add(
432 new WebServerEntry(
433 path, name + StringPool.SLASH, null, null,
434 group.getDescription(), 0));
435 }
436
437 sendHTML(response, path, webServerEntries);
438 }
439
440 protected void sendHTML(
441 HttpServletResponse response, String path,
442 List<WebServerEntry> webServerEntries)
443 throws Exception {
444
445 FreeMarkerContext freeMarkerContext =
446 FreeMarkerEngineUtil.getWrappedRestrictedToolsContext();
447
448 freeMarkerContext.put("dateFormat", _dateFormat);
449 freeMarkerContext.put("entries", webServerEntries);
450 freeMarkerContext.put("path", HttpUtil.encodePath(path));
451 freeMarkerContext.put("serverInfo", ReleaseInfo.getServerInfo());
452 freeMarkerContext.put("validator", Validator_IW.getInstance());
453
454 String html = FreeMarkerUtil.process(_TPL_TEMPLATE, freeMarkerContext);
455
456 response.setContentType(ContentTypes.TEXT_HTML_UTF8);
457
458 ServletResponseUtil.write(response, html);
459 }
460
461 private static final String _DATE_FORMAT_PATTERN = "d MMM yyyy HH:mm z";
462
463 private static final String _TPL_TEMPLATE =
464 "com/liferay/portal/webserver/dependencies/template.ftl";
465
466 private static Format _dateFormat =
467 FastDateFormatFactoryUtil.getSimpleDateFormat(_DATE_FORMAT_PATTERN);
468
469 }