001
014
015 package com.liferay.portal.webserver;
016
017 import com.liferay.portal.NoSuchGroupException;
018 import com.liferay.portal.freemarker.FreeMarkerUtil;
019 import com.liferay.portal.kernel.freemarker.FreeMarkerContext;
020 import com.liferay.portal.kernel.freemarker.FreeMarkerEngineUtil;
021 import com.liferay.portal.kernel.util.ContentTypes;
022 import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
023 import com.liferay.portal.kernel.util.FileUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.HttpUtil;
026 import com.liferay.portal.kernel.util.MimeTypesUtil;
027 import com.liferay.portal.kernel.util.ParamUtil;
028 import com.liferay.portal.kernel.util.ReleaseInfo;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.util.Validator_IW;
033 import com.liferay.portal.kernel.webdav.WebDAVUtil;
034 import com.liferay.portal.model.Company;
035 import com.liferay.portal.model.Group;
036 import com.liferay.portal.model.User;
037 import com.liferay.portal.security.auth.PrincipalException;
038 import com.liferay.portal.security.auth.PrincipalThreadLocal;
039 import com.liferay.portal.security.permission.PermissionChecker;
040 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
041 import com.liferay.portal.security.permission.PermissionThreadLocal;
042 import com.liferay.portal.service.CompanyLocalServiceUtil;
043 import com.liferay.portal.service.GroupLocalServiceUtil;
044 import com.liferay.portal.service.UserLocalServiceUtil;
045 import com.liferay.portal.util.PortalUtil;
046 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
047 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
048 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
049 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
050 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
051 import com.liferay.portlet.documentlibrary.model.DLFolder;
052 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
053 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
054 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
055 import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
056 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
057 import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
058 import com.liferay.portlet.documentlibrary.util.DLUtil;
059 import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
060 import com.liferay.util.servlet.ServletResponseUtil;
061
062 import java.io.IOException;
063 import java.io.InputStream;
064
065 import java.text.Format;
066
067 import java.util.ArrayList;
068 import java.util.List;
069
070 import javax.servlet.ServletException;
071 import javax.servlet.http.HttpServlet;
072 import javax.servlet.http.HttpServletRequest;
073 import javax.servlet.http.HttpServletResponse;
074
075
079 public class WebServerServlet extends HttpServlet {
080
081 public void service(
082 HttpServletRequest request, HttpServletResponse response)
083 throws IOException, ServletException {
084
085 try {
086 long companyId = PortalUtil.getCompanyId(request);
087
088 User user = PortalUtil.getUser(request);
089
090 if (user == null) {
091 Company company = CompanyLocalServiceUtil.getCompany(companyId);
092
093 user = company.getDefaultUser();
094 }
095
096 PrincipalThreadLocal.setName(user.getUserId());
097
098 PermissionChecker permissionChecker =
099 PermissionCheckerFactoryUtil.create(user, true);
100
101 PermissionThreadLocal.setPermissionChecker(permissionChecker);
102
103 String path = HttpUtil.fixPath(request.getPathInfo());
104 String[] pathArray = StringUtil.split(path, StringPool.SLASH);
105
106 if (pathArray.length == 0) {
107 sendGroups(
108 response, user,
109 request.getServletPath() + StringPool.SLASH + path);
110 }
111 else {
112 if (Validator.isNumber(pathArray[0])) {
113 sendFile(request, response, user, pathArray);
114 }
115 else {
116 sendDocumentLibrary(
117 request, response, user,
118 request.getServletPath() + StringPool.SLASH + path,
119 pathArray);
120 }
121 }
122 }
123 catch (NoSuchFileEntryException nsfee) {
124 PortalUtil.sendError(
125 HttpServletResponse.SC_NOT_FOUND, nsfee, request, response);
126 }
127 catch (Exception e) {
128 PortalUtil.sendError(e, request, response);
129 }
130 }
131
132 protected long getGroupId(long companyId, String name) throws Exception {
133 try {
134 Group group = GroupLocalServiceUtil.getFriendlyURLGroup(
135 companyId, StringPool.SLASH + name);
136
137 return group.getGroupId();
138 }
139 catch (NoSuchGroupException nsge) {
140 }
141
142 User user = UserLocalServiceUtil.getUserByScreenName(companyId, name);
143
144 Group group = user.getGroup();
145
146 return group.getGroupId();
147 }
148
149 protected void sendDocumentLibrary(
150 HttpServletRequest request, HttpServletResponse response, User user,
151 String path, String[] pathArray)
152 throws Exception {
153
154 long groupId = getGroupId(user.getCompanyId(), pathArray[0]);
155 long dlFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
156
157 for (int i = 1; i < pathArray.length; i++) {
158 String name = pathArray[i];
159
160 try {
161 DLFolder folder = DLFolderServiceUtil.getFolder(
162 groupId, dlFolderId, name);
163
164 dlFolderId = folder.getFolderId();
165 }
166 catch (NoSuchFolderException nsfe) {
167 if (i != pathArray.length - 1) {
168 throw nsfe;
169 }
170
171 String title = name;
172
173 sendFile(response, user, groupId, dlFolderId, title);
174
175 return;
176 }
177 }
178
179 try {
180 sendFile(response, user, groupId, dlFolderId, "index.html");
181
182 return;
183 }
184 catch (Exception e) {
185 if ((e instanceof NoSuchFileEntryException) ||
186 (e instanceof PrincipalException)) {
187
188 try {
189 sendFile(response, user, groupId, dlFolderId, "index.htm");
190
191 return;
192 }
193 catch (NoSuchFileEntryException nsfee) {
194 }
195 catch (PrincipalException pe) {
196 }
197 }
198 else {
199 throw e;
200 }
201 }
202
203 List<WebServerEntry> webServerEntries = new ArrayList<WebServerEntry>();
204
205 webServerEntries.add(new WebServerEntry(path, "../"));
206
207 List<DLFolder> dlFolders = DLFolderServiceUtil.getFolders(
208 groupId, dlFolderId);
209
210 for (DLFolder dlFolder : dlFolders) {
211 webServerEntries.add(
212 new WebServerEntry(
213 path, dlFolder.getName() + StringPool.SLASH,
214 dlFolder.getCreateDate(), dlFolder.getModifiedDate(),
215 dlFolder.getDescription(), 0));
216 }
217
218 List<DLFileEntry> dlFileEntries = DLFileEntryServiceUtil.getFileEntries(
219 groupId, dlFolderId);
220
221 for (DLFileEntry dlFileEntry : dlFileEntries) {
222 webServerEntries.add(
223 new WebServerEntry(
224 path, dlFileEntry.getTitle(),
225 dlFileEntry.getCreateDate(), dlFileEntry.getModifiedDate(),
226 dlFileEntry.getDescription(), dlFileEntry.getSize()));
227 }
228
229 sendHTML(response, path, webServerEntries);
230 }
231
232 protected void sendFile(
233 HttpServletRequest request, HttpServletResponse response,
234 User user, String[] pathArray)
235 throws Exception {
236
237 long groupId = 0;
238 long folderId = 0;
239 String name = null;
240 String fileName = null;
241
242 DLFileEntry dlFileEntry = null;
243
244 if (pathArray.length == 1) {
245 long fileShortcutId = GetterUtil.getLong(pathArray[0]);
246
247 DLFileShortcut fileShortcut =
248 DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
249
250 groupId = fileShortcut.getGroupId();
251 folderId = fileShortcut.getToFolderId();
252 name = fileShortcut.getToName();
253
254 dlFileEntry = DLFileEntryServiceUtil.getFileEntry(
255 groupId, folderId, name);
256
257 fileName = dlFileEntry.getTitle();
258 }
259 else if (pathArray.length == 2) {
260 groupId = GetterUtil.getLong(pathArray[0]);
261
262 dlFileEntry = DLFileEntryServiceUtil.getFileEntryByUuidAndGroupId(
263 pathArray[1], groupId);
264
265 folderId = dlFileEntry.getFolderId();
266 fileName = dlFileEntry.getTitle();
267 name = dlFileEntry.getName();
268 }
269 else {
270 groupId = GetterUtil.getLong(pathArray[0]);
271 folderId = GetterUtil.getLong(pathArray[1]);
272 fileName = HttpUtil.decodeURL(pathArray[2], true);
273
274 dlFileEntry = DLFileEntryServiceUtil.getFileEntryByTitle(
275 groupId, folderId, fileName);
276
277 name = dlFileEntry.getName();
278 }
279
280 if (dlFileEntry == null) {
281 throw new NoSuchFileEntryException();
282 }
283
284 String version = ParamUtil.getString(request, "version");
285
286 String targetExtension = ParamUtil.getString(
287 request, "targetExtension");
288
289 if (Validator.isNull(version)) {
290 if (Validator.isNotNull(dlFileEntry.getVersion())) {
291 version = dlFileEntry.getVersion();
292 }
293 else {
294 throw new NoSuchFileEntryException();
295 }
296 }
297
298 DLFileVersion fileVersion =
299 DLFileVersionLocalServiceUtil.getFileVersion(
300 groupId, folderId, name, version);
301
302 fileName = fileVersion.getTitle();
303
304 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
305 user.getCompanyId(), user.getUserId(), groupId, folderId, name,
306 version);
307
308 boolean converted = false;
309
310 if (Validator.isNotNull(targetExtension)) {
311 String id = DocumentConversionUtil.getTempFileId(
312 dlFileEntry.getFileEntryId(), version);
313
314 String sourceExtension = FileUtil.getExtension(fileName);
315
316 InputStream convertedIS = DocumentConversionUtil.convert(
317 id, is, sourceExtension, targetExtension);
318
319 if ((convertedIS != null) && (convertedIS != is)) {
320 fileName = FileUtil.stripExtension(fileName).concat(
321 StringPool.PERIOD).concat(targetExtension);
322
323 is = convertedIS;
324
325 converted = true;
326 }
327 }
328
329 int contentLength = 0;
330
331 if (!converted) {
332 if (DLUtil.compareVersions(version, dlFileEntry.getVersion()) >=
333 0) {
334
335 contentLength = (int)dlFileEntry.getSize();
336 }
337 else {
338 contentLength = (int)fileVersion.getSize();
339 }
340 }
341
342 String contentType = MimeTypesUtil.getContentType(fileName);
343
344 ServletResponseUtil.sendFile(
345 request, response, fileName, is, contentLength, contentType);
346 }
347
348 protected void sendFile(
349 HttpServletResponse response, User user, long groupId,
350 long folderId, String title)
351 throws Exception {
352
353 DLFileEntry dlFileEntry = DLFileEntryServiceUtil.getFileEntryByTitle(
354 groupId, folderId, title);
355
356 String contentType = MimeTypesUtil.getContentType(
357 dlFileEntry.getTitle());
358
359 InputStream inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(
360 user.getCompanyId(), user.getUserId(), groupId, folderId,
361 dlFileEntry.getName());
362
363 response.setContentType(contentType);
364
365 ServletResponseUtil.write(response, inputStream);
366 }
367
368 protected void sendGroups(
369 HttpServletResponse response, User user, String path)
370 throws Exception {
371
372 List<WebServerEntry> webServerEntries = new ArrayList<WebServerEntry>();
373
374 List<Group> groups = WebDAVUtil.getGroups(user);
375
376 for (Group group : groups) {
377 String name = HttpUtil.fixPath(group.getFriendlyURL());
378
379 webServerEntries.add(
380 new WebServerEntry(
381 path, name + StringPool.SLASH, null, null,
382 group.getDescription(), 0));
383 }
384
385 sendHTML(response, path, webServerEntries);
386 }
387
388 protected void sendHTML(
389 HttpServletResponse response, String path,
390 List<WebServerEntry> webServerEntries)
391 throws Exception {
392
393 FreeMarkerContext freeMarkerContext =
394 FreeMarkerEngineUtil.getWrappedRestrictedToolsContext();
395
396 freeMarkerContext.put("dateFormat", _dateFormat);
397 freeMarkerContext.put("entries", webServerEntries);
398 freeMarkerContext.put("path", HttpUtil.encodePath(path));
399 freeMarkerContext.put("serverInfo", ReleaseInfo.getServerInfo());
400 freeMarkerContext.put("validator", Validator_IW.getInstance());
401
402 String html = FreeMarkerUtil.process(_TPL_TEMPLATE, freeMarkerContext);
403
404 response.setContentType(ContentTypes.TEXT_HTML_UTF8);
405
406 ServletResponseUtil.write(response, html);
407 }
408
409 private static final String _DATE_FORMAT_PATTERN = "d MMM yyyy HH:mm z";
410
411 private static final String _TPL_TEMPLATE =
412 "com/liferay/portal/webserver/dependencies/template.ftl";
413
414 private static Format _dateFormat =
415 FastDateFormatFactoryUtil.getSimpleDateFormat(_DATE_FORMAT_PATTERN);
416
417 }