001
014
015 package com.liferay.portal.webserver;
016
017 import com.liferay.document.library.kernel.exception.NoSuchFileEntryException;
018 import com.liferay.document.library.kernel.exception.NoSuchFileException;
019 import com.liferay.document.library.kernel.exception.NoSuchFolderException;
020 import com.liferay.document.library.kernel.model.DLFolderConstants;
021 import com.liferay.document.library.kernel.service.DLAppLocalServiceUtil;
022 import com.liferay.document.library.kernel.service.DLAppServiceUtil;
023 import com.liferay.document.library.kernel.util.AudioProcessorUtil;
024 import com.liferay.document.library.kernel.util.DLUtil;
025 import com.liferay.document.library.kernel.util.ImageProcessorUtil;
026 import com.liferay.document.library.kernel.util.PDFProcessor;
027 import com.liferay.document.library.kernel.util.PDFProcessorUtil;
028 import com.liferay.document.library.kernel.util.VideoProcessor;
029 import com.liferay.document.library.kernel.util.VideoProcessorUtil;
030 import com.liferay.portal.kernel.exception.PortalException;
031 import com.liferay.portal.kernel.exception.SystemException;
032 import com.liferay.portal.kernel.flash.FlashMagicBytesUtil;
033 import com.liferay.portal.kernel.image.ImageBag;
034 import com.liferay.portal.kernel.image.ImageToolUtil;
035 import com.liferay.portal.kernel.log.Log;
036 import com.liferay.portal.kernel.log.LogFactoryUtil;
037 import com.liferay.portal.kernel.model.Company;
038 import com.liferay.portal.kernel.model.Group;
039 import com.liferay.portal.kernel.model.Image;
040 import com.liferay.portal.kernel.model.ImageConstants;
041 import com.liferay.portal.kernel.model.User;
042 import com.liferay.portal.kernel.portlet.PortletProvider;
043 import com.liferay.portal.kernel.portlet.PortletProviderUtil;
044 import com.liferay.portal.kernel.portletfilerepository.PortletFileRepositoryUtil;
045 import com.liferay.portal.kernel.repository.Repository;
046 import com.liferay.portal.kernel.repository.RepositoryException;
047 import com.liferay.portal.kernel.repository.RepositoryProviderUtil;
048 import com.liferay.portal.kernel.repository.capabilities.ThumbnailCapability;
049 import com.liferay.portal.kernel.repository.model.FileEntry;
050 import com.liferay.portal.kernel.repository.model.FileShortcut;
051 import com.liferay.portal.kernel.repository.model.FileVersion;
052 import com.liferay.portal.kernel.repository.model.Folder;
053 import com.liferay.portal.kernel.security.auth.PrincipalException;
054 import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
055 import com.liferay.portal.kernel.security.permission.ActionKeys;
056 import com.liferay.portal.kernel.security.permission.PermissionChecker;
057 import com.liferay.portal.kernel.security.permission.PermissionCheckerFactoryUtil;
058 import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
059 import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
060 import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
061 import com.liferay.portal.kernel.service.ImageLocalServiceUtil;
062 import com.liferay.portal.kernel.service.ImageServiceUtil;
063 import com.liferay.portal.kernel.service.UserLocalServiceUtil;
064 import com.liferay.portal.kernel.service.permission.PortletPermissionUtil;
065 import com.liferay.portal.kernel.servlet.HttpHeaders;
066 import com.liferay.portal.kernel.servlet.PortalSessionThreadLocal;
067 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
068 import com.liferay.portal.kernel.template.Template;
069 import com.liferay.portal.kernel.template.TemplateConstants;
070 import com.liferay.portal.kernel.template.TemplateManagerUtil;
071 import com.liferay.portal.kernel.template.TemplateResource;
072 import com.liferay.portal.kernel.template.URLTemplateResource;
073 import com.liferay.portal.kernel.theme.ThemeDisplay;
074 import com.liferay.portal.kernel.util.CharPool;
075 import com.liferay.portal.kernel.util.ContentTypes;
076 import com.liferay.portal.kernel.util.DigesterUtil;
077 import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
078 import com.liferay.portal.kernel.util.FileUtil;
079 import com.liferay.portal.kernel.util.GetterUtil;
080 import com.liferay.portal.kernel.util.HtmlUtil;
081 import com.liferay.portal.kernel.util.HttpUtil;
082 import com.liferay.portal.kernel.util.MimeTypesUtil;
083 import com.liferay.portal.kernel.util.ParamUtil;
084 import com.liferay.portal.kernel.util.PortalUtil;
085 import com.liferay.portal.kernel.util.ReleaseInfo;
086 import com.liferay.portal.kernel.util.SetUtil;
087 import com.liferay.portal.kernel.util.StringPool;
088 import com.liferay.portal.kernel.util.StringUtil;
089 import com.liferay.portal.kernel.util.UnicodeProperties;
090 import com.liferay.portal.kernel.util.Validator;
091 import com.liferay.portal.kernel.util.Validator_IW;
092 import com.liferay.portal.kernel.util.WebKeys;
093 import com.liferay.portal.kernel.webdav.WebDAVUtil;
094 import com.liferay.portal.kernel.workflow.WorkflowConstants;
095 import com.liferay.portal.model.impl.ImageImpl;
096 import com.liferay.portal.util.PropsValues;
097 import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
098 import com.liferay.trash.kernel.model.TrashEntry;
099 import com.liferay.trash.kernel.util.TrashUtil;
100
101 import java.awt.image.RenderedImage;
102
103 import java.io.File;
104 import java.io.FileInputStream;
105 import java.io.IOException;
106 import java.io.InputStream;
107
108 import java.net.URL;
109
110 import java.text.Format;
111
112 import java.util.ArrayList;
113 import java.util.Date;
114 import java.util.List;
115 import java.util.Set;
116
117 import javax.servlet.ServletConfig;
118 import javax.servlet.ServletException;
119 import javax.servlet.http.HttpServlet;
120 import javax.servlet.http.HttpServletRequest;
121 import javax.servlet.http.HttpServletResponse;
122 import javax.servlet.http.HttpSession;
123
124
128 public class WebServerServlet extends HttpServlet {
129
130 public static final String PATH_PORTLET_FILE_ENTRY = "portlet_file_entry";
131
132
135 public static boolean hasFiles(HttpServletRequest request) {
136 String name = PrincipalThreadLocal.getName();
137 String password = PrincipalThreadLocal.getPassword();
138
139 try {
140
141
142
143
144 User user = _getUser(request);
145
146 if (!user.isDefaultUser()) {
147 PrincipalThreadLocal.setName(user.getUserId());
148 PrincipalThreadLocal.setPassword(
149 PortalUtil.getUserPassword(request));
150 }
151
152 String path = HttpUtil.fixPath(request.getPathInfo());
153
154 String[] pathArray = StringUtil.split(path, CharPool.SLASH);
155
156 if (pathArray.length == 0) {
157 return true;
158 }
159 else if (Validator.isNumber(pathArray[0])) {
160 _checkFileEntry(pathArray);
161 }
162 else if (PATH_PORTLET_FILE_ENTRY.equals(pathArray[0])) {
163 FileEntry fileEntry = getPortletFileEntry(request, pathArray);
164
165 if (fileEntry != null) {
166 return true;
167 }
168 }
169 else {
170 long groupId = _getGroupId(user.getCompanyId(), pathArray[0]);
171 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
172
173 for (int i = 1; i < pathArray.length; i++) {
174 try {
175 Folder folder = DLAppLocalServiceUtil.getFolder(
176 groupId, folderId, pathArray[i]);
177
178 folderId = folder.getFolderId();
179 }
180 catch (NoSuchFolderException nsfe) {
181 if (i != (pathArray.length - 1)) {
182 return false;
183 }
184
185 pathArray = new String[] {
186 String.valueOf(groupId), String.valueOf(folderId),
187 pathArray[i]
188 };
189
190 _checkFileEntry(pathArray);
191 }
192 }
193 }
194 }
195 catch (Exception e) {
196 return false;
197 }
198 finally {
199 PrincipalThreadLocal.setName(name);
200 PrincipalThreadLocal.setPassword(password);
201 }
202
203 return true;
204 }
205
206 @Override
207 public void init(ServletConfig servletConfig) throws ServletException {
208 super.init(servletConfig);
209
210 _lastModified = GetterUtil.getBoolean(
211 servletConfig.getInitParameter("last_modified"), true);
212
213 Class<?> clazz = getClass();
214
215 ClassLoader classLoader = clazz.getClassLoader();
216
217 String templateId =
218 "com/liferay/portal/webserver/dependencies/template.ftl";
219
220 URL url = classLoader.getResource(templateId);
221
222 _templateResource = new URLTemplateResource(templateId, url);
223 }
224
225 @Override
226 public void service(
227 HttpServletRequest request, HttpServletResponse response)
228 throws IOException, ServletException {
229
230 User user = null;
231
232 try {
233 user = _getUser(request);
234
235 PrincipalThreadLocal.setName(user.getUserId());
236 PrincipalThreadLocal.setPassword(
237 PortalUtil.getUserPassword(request));
238
239 PermissionChecker permissionChecker =
240 PermissionCheckerFactoryUtil.create(user);
241
242 PermissionThreadLocal.setPermissionChecker(permissionChecker);
243
244 if (_lastModified) {
245 long lastModified = getLastModified(request);
246
247 if (lastModified > 0) {
248 long ifModifiedSince = request.getDateHeader(
249 HttpHeaders.IF_MODIFIED_SINCE);
250
251 if ((ifModifiedSince > 0) &&
252 (ifModifiedSince == lastModified)) {
253
254 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
255
256 return;
257 }
258 }
259
260 if (lastModified > 0) {
261 response.setDateHeader(
262 HttpHeaders.LAST_MODIFIED, lastModified);
263 }
264 }
265
266 String path = HttpUtil.fixPath(request.getPathInfo());
267 String[] pathArray = StringUtil.split(path, CharPool.SLASH);
268
269 if (pathArray.length == 0) {
270 sendGroups(
271 response, user,
272 request.getServletPath() + StringPool.SLASH + path);
273 }
274 else {
275 if (Validator.isNumber(pathArray[0])) {
276 sendFile(request, response, user, pathArray);
277 }
278 else if (PATH_PORTLET_FILE_ENTRY.equals(pathArray[0])) {
279 sendPortletFileEntry(request, response, pathArray);
280 }
281 else {
282 if (PropsValues.WEB_SERVER_SERVLET_CHECK_IMAGE_GALLERY) {
283 if (isLegacyImageGalleryImageId(request, response)) {
284 return;
285 }
286 }
287
288 Image image = getImage(request, true);
289
290 if (image != null) {
291 writeImage(image, request, response);
292 }
293 else {
294 sendDocumentLibrary(
295 request, response, user,
296 request.getServletPath() + StringPool.SLASH + path,
297 pathArray);
298 }
299 }
300 }
301 }
302 catch (NoSuchFileEntryException nsfee) {
303 PortalUtil.sendError(
304 HttpServletResponse.SC_NOT_FOUND, nsfee, request, response);
305 }
306 catch (NoSuchFolderException nsfe) {
307 PortalUtil.sendError(
308 HttpServletResponse.SC_NOT_FOUND, nsfe, request, response);
309 }
310 catch (PrincipalException pe) {
311 processPrincipalException(pe, user, request, response);
312 }
313 catch (Exception e) {
314 PortalUtil.sendError(e, request, response);
315 }
316 }
317
318 protected static FileEntry getPortletFileEntry(
319 HttpServletRequest request, String[] pathArray)
320 throws Exception {
321
322 long groupId = GetterUtil.getLong(pathArray[1]);
323 String uuid = pathArray[3];
324
325 FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
326 uuid, groupId);
327
328 int status = ParamUtil.getInteger(
329 request, "status", WorkflowConstants.STATUS_APPROVED);
330
331 if ((status != WorkflowConstants.STATUS_IN_TRASH) &&
332 fileEntry.isInTrash()) {
333
334 return null;
335 }
336
337 return fileEntry;
338 }
339
340 protected Image convertFileEntry(boolean smallImage, FileEntry fileEntry)
341 throws PortalException {
342
343 try {
344 Image image = new ImageImpl();
345
346 image.setModifiedDate(fileEntry.getModifiedDate());
347
348 InputStream is = null;
349
350 if (smallImage) {
351 is = ImageProcessorUtil.getThumbnailAsStream(
352 fileEntry.getFileVersion(), 0);
353 }
354 else {
355 is = fileEntry.getContentStream();
356 }
357
358 byte[] bytes = FileUtil.getBytes(is);
359
360 image.setTextObj(bytes);
361
362 image.setType(fileEntry.getExtension());
363
364 return image;
365 }
366 catch (PortalException pe) {
367 throw pe;
368 }
369 catch (SystemException se) {
370 throw se;
371 }
372 catch (Exception e) {
373 throw new SystemException(e);
374 }
375 }
376
377 protected Image getDefaultImage(HttpServletRequest request, long imageId) {
378 String path = GetterUtil.getString(request.getPathInfo());
379
380 if (path.startsWith("/company_logo") ||
381 path.startsWith("/layout_set_logo") || path.startsWith("/logo")) {
382
383 return ImageToolUtil.getDefaultCompanyLogo();
384 }
385 else if (path.startsWith("/organization_logo")) {
386 return ImageToolUtil.getDefaultOrganizationLogo();
387 }
388 else if (path.startsWith("/user_female_portrait")) {
389 return ImageToolUtil.getDefaultUserFemalePortrait();
390 }
391 else if (path.startsWith("/user_male_portrait")) {
392 return ImageToolUtil.getDefaultUserMalePortrait();
393 }
394 else if (path.startsWith("/user_portrait")) {
395 return ImageToolUtil.getDefaultUserMalePortrait();
396 }
397 else {
398 return null;
399 }
400 }
401
402 protected FileEntry getFileEntry(String[] pathArray) throws Exception {
403 if (pathArray.length == 1) {
404 long fileShortcutId = GetterUtil.getLong(pathArray[0]);
405
406 FileShortcut dlFileShortcut = DLAppServiceUtil.getFileShortcut(
407 fileShortcutId);
408
409 return DLAppServiceUtil.getFileEntry(
410 dlFileShortcut.getToFileEntryId());
411 }
412 else if (pathArray.length == 2) {
413 long groupId = GetterUtil.getLong(pathArray[0]);
414
415 return DLAppServiceUtil.getFileEntryByUuidAndGroupId(
416 pathArray[1], groupId);
417 }
418 else if (pathArray.length == 3) {
419 long groupId = GetterUtil.getLong(pathArray[0]);
420 long folderId = GetterUtil.getLong(pathArray[1]);
421 String fileName = HttpUtil.decodeURL(pathArray[2]);
422
423 if (fileName.contains(StringPool.QUESTION)) {
424 fileName = fileName.substring(
425 0, fileName.indexOf(StringPool.QUESTION));
426 }
427
428 return DLAppServiceUtil.getFileEntry(groupId, folderId, fileName);
429 }
430 else {
431 long groupId = GetterUtil.getLong(pathArray[0]);
432
433 String uuid = pathArray[3];
434
435 return DLAppServiceUtil.getFileEntryByUuidAndGroupId(uuid, groupId);
436 }
437 }
438
439 protected Image getImage(HttpServletRequest request, boolean getDefault)
440 throws PortalException {
441
442 Image image = null;
443
444 long imageId = getImageId(request);
445
446 if (imageId > 0) {
447 image = ImageServiceUtil.getImage(imageId);
448
449 String path = GetterUtil.getString(request.getPathInfo());
450
451 if (path.startsWith("/user_female_portrait") ||
452 path.startsWith("/user_male_portrait") ||
453 path.startsWith("/user_portrait")) {
454
455 image = getUserPortraitImageResized(image, imageId);
456 }
457 }
458 else {
459 String uuid = ParamUtil.getString(request, "uuid");
460 long groupId = ParamUtil.getLong(request, "groupId");
461 boolean igSmallImage = ParamUtil.getBoolean(
462 request, "igSmallImage");
463
464 if (Validator.isNotNull(uuid) && (groupId > 0)) {
465 try {
466 FileEntry fileEntry =
467 DLAppServiceUtil.getFileEntryByUuidAndGroupId(
468 uuid, groupId);
469
470 image = convertFileEntry(igSmallImage, fileEntry);
471 }
472 catch (Exception e) {
473 }
474 }
475 }
476
477 if (getDefault) {
478 if (image == null) {
479 if (_log.isWarnEnabled()) {
480 _log.warn("Get a default image for " + imageId);
481 }
482
483 image = getDefaultImage(request, imageId);
484 }
485 }
486
487 return image;
488 }
489
490 protected byte[] getImageBytes(HttpServletRequest request, Image image)
491 throws PortalException {
492
493 byte[] textObj = image.getTextObj();
494
495 if ((textObj == null) || (textObj.length == 0)) {
496 throw new NoSuchFileException();
497 }
498
499 try {
500 if (!PropsValues.IMAGE_AUTO_SCALE) {
501 return textObj;
502 }
503
504 ImageBag imageBag = null;
505
506 if (image.getImageId() == 0) {
507 imageBag = ImageToolUtil.read(textObj);
508
509 RenderedImage renderedImage = imageBag.getRenderedImage();
510
511 image.setHeight(renderedImage.getHeight());
512 image.setWidth(renderedImage.getWidth());
513 }
514
515 int height = ParamUtil.getInteger(
516 request, "height", image.getHeight());
517 int width = ParamUtil.getInteger(
518 request, "width", image.getWidth());
519
520 if ((height >= image.getHeight()) && (width >= image.getWidth())) {
521 return textObj;
522 }
523
524 if (image.getImageId() != 0) {
525 imageBag = ImageToolUtil.read(textObj);
526 }
527
528 RenderedImage renderedImage = ImageToolUtil.scale(
529 imageBag.getRenderedImage(), height, width);
530
531 return ImageToolUtil.getBytes(renderedImage, imageBag.getType());
532 }
533 catch (Exception e) {
534 if (_log.isWarnEnabled()) {
535 _log.warn("Error scaling image " + image.getImageId(), e);
536 }
537 }
538
539 return textObj;
540 }
541
542 protected long getImageId(HttpServletRequest request) {
543
544
545
546 long imageId = ParamUtil.getLong(request, "image_id");
547
548 if (imageId <= 0) {
549 imageId = ParamUtil.getLong(request, "img_id");
550 }
551
552 if (imageId <= 0) {
553 imageId = ParamUtil.getLong(request, "i_id");
554 }
555
556 User user = null;
557
558 if (imageId <= 0) {
559 long companyId = ParamUtil.getLong(request, "companyId");
560 String screenName = ParamUtil.getString(request, "screenName");
561
562 if ((companyId > 0) && Validator.isNotNull(screenName)) {
563 user = UserLocalServiceUtil.fetchUserByScreenName(
564 companyId, screenName);
565
566 if (user != null) {
567 imageId = user.getPortraitId();
568 }
569 }
570 }
571
572 if (PropsValues.USERS_IMAGE_CHECK_TOKEN && (imageId > 0)) {
573 String imageIdToken = ParamUtil.getString(request, "img_id_token");
574
575 if (user == null) {
576 user = UserLocalServiceUtil.fetchUserByPortraitId(imageId);
577 }
578
579 if ((user != null) &&
580 !imageIdToken.equals(DigesterUtil.digest(user.getUserUuid()))) {
581
582 return 0;
583 }
584 }
585
586 return imageId;
587 }
588
589 @Override
590 protected long getLastModified(HttpServletRequest request) {
591 try {
592 Date modifiedDate = null;
593
594 Image image = getImage(request, true);
595
596 if (image != null) {
597 modifiedDate = image.getModifiedDate();
598 }
599 else {
600 String path = HttpUtil.fixPath(request.getPathInfo());
601
602 String[] pathArray = StringUtil.split(path, CharPool.SLASH);
603
604 if (pathArray.length == 0) {
605 return -1;
606 }
607
608 if (pathArray[0].equals("language")) {
609 return -1;
610 }
611
612 FileEntry fileEntry = null;
613
614 try {
615 fileEntry = getFileEntry(pathArray);
616 }
617 catch (Exception e) {
618 }
619
620 if (fileEntry == null) {
621 return -1;
622 }
623
624 String version = ParamUtil.getString(request, "version");
625
626 if (Validator.isNotNull(version)) {
627 FileVersion fileVersion = fileEntry.getFileVersion(version);
628
629 modifiedDate = fileVersion.getModifiedDate();
630 }
631 else {
632 modifiedDate = fileEntry.getModifiedDate();
633 }
634 }
635
636 if (modifiedDate == null) {
637 modifiedDate = PortalUtil.getUptime();
638 }
639
640
641
642 return (modifiedDate.getTime() / 1000) * 1000;
643 }
644 catch (PrincipalException pe) {
645 if (_log.isWarnEnabled()) {
646 _log.warn(pe, pe);
647 }
648 }
649 catch (Exception e) {
650 _log.error(e, e);
651 }
652
653 return -1;
654 }
655
656 protected Image getUserPortraitImageResized(Image image, long imageId)
657 throws PortalException {
658
659 if (image == null) {
660 return null;
661 }
662
663 if (((PropsValues.USERS_IMAGE_MAX_HEIGHT > 0) &&
664 (image.getHeight() > PropsValues.USERS_IMAGE_MAX_HEIGHT)) ||
665 ((PropsValues.USERS_IMAGE_MAX_WIDTH > 0) &&
666 (image.getWidth() > PropsValues.USERS_IMAGE_MAX_WIDTH))) {
667
668 User user = UserLocalServiceUtil.getUserByPortraitId(imageId);
669
670 UserLocalServiceUtil.updatePortrait(
671 user.getUserId(), image.getTextObj());
672
673 return ImageLocalServiceUtil.getImage(imageId);
674 }
675
676 return image;
677 }
678
679 protected boolean isLegacyImageGalleryImageId(
680 HttpServletRequest request, HttpServletResponse response) {
681
682 try {
683 long imageId = getImageId(request);
684
685 if (imageId == 0) {
686 return false;
687 }
688
689 Repository repository = RepositoryProviderUtil.getImageRepository(
690 imageId);
691
692 if (!repository.isCapabilityProvided(ThumbnailCapability.class)) {
693 return false;
694 }
695
696 ThumbnailCapability thumbnailCapability = repository.getCapability(
697 ThumbnailCapability.class);
698
699 FileEntry fileEntry = thumbnailCapability.fetchImageFileEntry(
700 imageId);
701
702 if (fileEntry == null) {
703 return false;
704 }
705
706 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
707 WebKeys.THEME_DISPLAY);
708
709 String queryString = StringPool.BLANK;
710
711 if (imageId == thumbnailCapability.getSmallImageId(fileEntry)) {
712 queryString = "&imageThumbnail=1";
713 }
714 else if (imageId ==
715 thumbnailCapability.getCustom1ImageId(fileEntry)) {
716
717 queryString = "&imageThumbnail=2";
718 }
719 else if (imageId ==
720 thumbnailCapability.getCustom2ImageId(fileEntry)) {
721
722 queryString = "&imageThumbnail=3";
723 }
724
725 FileVersion fileVersion = fileEntry.getFileVersion();
726
727 if (PropsValues.DL_FILE_ENTRY_IG_THUMBNAIL_GENERATION &&
728 Validator.isNotNull(queryString)) {
729
730 ImageProcessorUtil.hasImages(fileVersion);
731 }
732
733 String url = DLUtil.getPreviewURL(
734 fileEntry, fileVersion, themeDisplay, queryString);
735
736 response.setHeader(HttpHeaders.LOCATION, url);
737 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
738
739 return true;
740 }
741 catch (Exception e) {
742 }
743
744 return false;
745 }
746
747 protected boolean isSupportsRangeHeader(String contentType) {
748 return _acceptRangesMimeTypes.contains(contentType);
749 }
750
751 protected void processPrincipalException(
752 Throwable t, User user, HttpServletRequest request,
753 HttpServletResponse response)
754 throws IOException, ServletException {
755
756 if (!user.isDefaultUser()) {
757 PortalUtil.sendError(
758 HttpServletResponse.SC_UNAUTHORIZED, (Exception)t, request,
759 response);
760
761 return;
762 }
763
764 String redirect = PortalUtil.getPathMain() + "/portal/login";
765
766 String currentURL = PortalUtil.getCurrentURL(request);
767
768 redirect = HttpUtil.addParameter(redirect, "redirect", currentURL);
769
770 response.sendRedirect(redirect);
771 }
772
773 protected void sendDocumentLibrary(
774 HttpServletRequest request, HttpServletResponse response, User user,
775 String path, String[] pathArray)
776 throws Exception {
777
778 long groupId = _getGroupId(user.getCompanyId(), pathArray[0]);
779
780 Group group = GroupLocalServiceUtil.getGroup(groupId);
781
782 UnicodeProperties typeSettingsProperties =
783 group.getTypeSettingsProperties();
784
785 boolean directoryIndexingEnabled = GetterUtil.getBoolean(
786 typeSettingsProperties.getProperty("directoryIndexingEnabled"),
787 PropsValues.WEB_SERVER_SERVLET_DIRECTORY_INDEXING_ENABLED);
788
789 if (!directoryIndexingEnabled) {
790 throw new NoSuchFolderException();
791 }
792
793 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
794
795 for (int i = 1; i < pathArray.length; i++) {
796 String name = pathArray[i];
797
798 try {
799 Folder folder = DLAppServiceUtil.getFolder(
800 groupId, folderId, name);
801
802 folderId = folder.getFolderId();
803 }
804 catch (NoSuchFolderException nsfe) {
805 if (i != (pathArray.length - 1)) {
806 throw nsfe;
807 }
808
809 String title = name;
810
811 sendFile(response, user, groupId, folderId, title);
812
813 return;
814 }
815 }
816
817 try {
818 sendFile(response, user, groupId, folderId, "index.html");
819
820 return;
821 }
822 catch (Exception e) {
823 if ((e instanceof NoSuchFileEntryException) ||
824 (e instanceof PrincipalException)) {
825
826 try {
827 sendFile(response, user, groupId, folderId, "index.htm");
828
829 return;
830 }
831 catch (NoSuchFileEntryException nsfee) {
832 }
833 catch (PrincipalException pe) {
834 }
835 }
836 else {
837 throw e;
838 }
839 }
840
841 List<WebServerEntry> webServerEntries = new ArrayList<>();
842
843 webServerEntries.add(new WebServerEntry(path, "../"));
844
845 List<Folder> folders = DLAppServiceUtil.getFolders(groupId, folderId);
846
847 for (Folder folder : folders) {
848 WebServerEntry webServerEntry = new WebServerEntry(
849 path, folder.getName() + StringPool.SLASH,
850 folder.getCreateDate(), folder.getModifiedDate(),
851 folder.getDescription(), 0);
852
853 webServerEntries.add(webServerEntry);
854 }
855
856 List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
857 groupId, folderId);
858
859 for (FileEntry fileEntry : fileEntries) {
860 WebServerEntry webServerEntry = new WebServerEntry(
861 path, fileEntry.getTitle(), fileEntry.getCreateDate(),
862 fileEntry.getModifiedDate(), fileEntry.getDescription(),
863 fileEntry.getSize());
864
865 webServerEntries.add(webServerEntry);
866 }
867
868 sendHTML(response, path, webServerEntries);
869 }
870
871 protected void sendFile(
872 HttpServletRequest request, HttpServletResponse response, User user,
873 String[] pathArray)
874 throws Exception {
875
876
877
878 FileEntry fileEntry = getFileEntry(pathArray);
879
880 if (fileEntry == null) {
881 throw new NoSuchFileEntryException();
882 }
883
884 String version = ParamUtil.getString(request, "version");
885
886 if (Validator.isNull(version)) {
887 if (Validator.isNotNull(fileEntry.getVersion())) {
888 version = fileEntry.getVersion();
889 }
890 }
891
892 String tempFileId = DLUtil.getTempFileId(
893 fileEntry.getFileEntryId(), version);
894
895 if (fileEntry.isInTrash()) {
896 int status = ParamUtil.getInteger(
897 request, "status", WorkflowConstants.STATUS_APPROVED);
898
899 if (status != WorkflowConstants.STATUS_IN_TRASH) {
900 throw new NoSuchFileEntryException();
901 }
902
903 PermissionChecker permissionChecker =
904 PermissionThreadLocal.getPermissionChecker();
905
906 String portletId = PortletProviderUtil.getPortletId(
907 TrashEntry.class.getName(), PortletProvider.Action.VIEW);
908
909 if (!PortletPermissionUtil.hasControlPanelAccessPermission(
910 permissionChecker, fileEntry.getGroupId(), portletId)) {
911
912 throw new PrincipalException.MustHavePermission(
913 permissionChecker, FileEntry.class.getName(),
914 fileEntry.getFileEntryId(),
915 ActionKeys.ACCESS_IN_CONTROL_PANEL);
916 }
917 }
918
919 FileVersion fileVersion = fileEntry.getFileVersion(version);
920
921 if ((ParamUtil.getInteger(request, "height") > 0) ||
922 (ParamUtil.getInteger(request, "width") > 0)) {
923
924 InputStream inputStream = fileVersion.getContentStream(true);
925
926 Image image = ImageToolUtil.getImage(inputStream);
927
928 writeImage(image, request, response);
929
930 return;
931 }
932
933 String fileName = fileVersion.getFileName();
934
935
936
937 boolean converted = false;
938
939 String targetExtension = ParamUtil.getString(
940 request, "targetExtension");
941 int imageThumbnail = ParamUtil.getInteger(request, "imageThumbnail");
942 int documentThumbnail = ParamUtil.getInteger(
943 request, "documentThumbnail");
944 int previewFileIndex = ParamUtil.getInteger(
945 request, "previewFileIndex");
946 boolean audioPreview = ParamUtil.getBoolean(request, "audioPreview");
947 boolean imagePreview = ParamUtil.getBoolean(request, "imagePreview");
948 boolean videoPreview = ParamUtil.getBoolean(request, "videoPreview");
949 int videoThumbnail = ParamUtil.getInteger(request, "videoThumbnail");
950
951 InputStream inputStream = null;
952 long contentLength = 0;
953
954 if ((imageThumbnail > 0) && (imageThumbnail <= 3)) {
955 fileName = FileUtil.stripExtension(fileName).concat(
956 StringPool.PERIOD).concat(
957 ImageProcessorUtil.getThumbnailType(fileVersion));
958
959 int thumbnailIndex = imageThumbnail - 1;
960
961 inputStream = ImageProcessorUtil.getThumbnailAsStream(
962 fileVersion, thumbnailIndex);
963 contentLength = ImageProcessorUtil.getThumbnailFileSize(
964 fileVersion, thumbnailIndex);
965
966 converted = true;
967 }
968 else if ((documentThumbnail > 0) && (documentThumbnail <= 3)) {
969 fileName = FileUtil.stripExtension(fileName).concat(
970 StringPool.PERIOD).concat(PDFProcessor.THUMBNAIL_TYPE);
971
972 int thumbnailIndex = documentThumbnail - 1;
973
974 inputStream = PDFProcessorUtil.getThumbnailAsStream(
975 fileVersion, thumbnailIndex);
976 contentLength = PDFProcessorUtil.getThumbnailFileSize(
977 fileVersion, thumbnailIndex);
978
979 converted = true;
980 }
981 else if (previewFileIndex > 0) {
982 fileName = FileUtil.stripExtension(fileName).concat(
983 StringPool.PERIOD).concat(PDFProcessor.PREVIEW_TYPE);
984 inputStream = PDFProcessorUtil.getPreviewAsStream(
985 fileVersion, previewFileIndex);
986 contentLength = PDFProcessorUtil.getPreviewFileSize(
987 fileVersion, previewFileIndex);
988
989 converted = true;
990 }
991 else if (audioPreview || videoPreview) {
992 String type = ParamUtil.getString(request, "type");
993
994 fileName = FileUtil.stripExtension(fileName).concat(
995 StringPool.PERIOD).concat(type);
996
997 if (audioPreview) {
998 inputStream = AudioProcessorUtil.getPreviewAsStream(
999 fileVersion, type);
1000 contentLength = AudioProcessorUtil.getPreviewFileSize(
1001 fileVersion, type);
1002 }
1003 else {
1004 inputStream = VideoProcessorUtil.getPreviewAsStream(
1005 fileVersion, type);
1006 contentLength = VideoProcessorUtil.getPreviewFileSize(
1007 fileVersion, type);
1008 }
1009
1010 converted = true;
1011 }
1012 else if (imagePreview) {
1013 String type = ImageProcessorUtil.getPreviewType(fileVersion);
1014
1015 fileName = FileUtil.stripExtension(fileName).concat(
1016 StringPool.PERIOD).concat(type);
1017
1018 inputStream = ImageProcessorUtil.getPreviewAsStream(fileVersion);
1019
1020 contentLength = ImageProcessorUtil.getPreviewFileSize(fileVersion);
1021
1022 converted = true;
1023 }
1024 else if ((videoThumbnail > 0) && (videoThumbnail <= 3)) {
1025 fileName = FileUtil.stripExtension(fileName).concat(
1026 StringPool.PERIOD).concat(VideoProcessor.THUMBNAIL_TYPE);
1027
1028 int thumbnailIndex = videoThumbnail - 1;
1029
1030 inputStream = VideoProcessorUtil.getThumbnailAsStream(
1031 fileVersion, thumbnailIndex);
1032 contentLength = VideoProcessorUtil.getThumbnailFileSize(
1033 fileVersion, thumbnailIndex);
1034
1035 converted = true;
1036 }
1037 else {
1038 inputStream = fileVersion.getContentStream(true);
1039 contentLength = fileVersion.getSize();
1040
1041 if (Validator.isNotNull(targetExtension)) {
1042 File convertedFile = DocumentConversionUtil.convert(
1043 tempFileId, inputStream, fileVersion.getExtension(),
1044 targetExtension);
1045
1046 if (convertedFile != null) {
1047 fileName = FileUtil.stripExtension(fileName).concat(
1048 StringPool.PERIOD).concat(targetExtension);
1049 inputStream = new FileInputStream(convertedFile);
1050 contentLength = convertedFile.length();
1051
1052 converted = true;
1053 }
1054 }
1055 }
1056
1057 FlashMagicBytesUtil.Result flashMagicBytesUtilResult =
1058 FlashMagicBytesUtil.check(inputStream);
1059
1060 if (flashMagicBytesUtilResult.isFlash()) {
1061 fileName = FileUtil.stripExtension(fileName) + ".swf";
1062 }
1063
1064 inputStream = flashMagicBytesUtilResult.getInputStream();
1065
1066
1067
1068 String contentType = null;
1069
1070 if (converted) {
1071 contentType = MimeTypesUtil.getContentType(fileName);
1072 }
1073 else {
1074 contentType = fileVersion.getMimeType();
1075 }
1076
1077 if (_log.isDebugEnabled()) {
1078 _log.debug("Content type set to " + contentType);
1079 }
1080
1081
1082
1083 if (isSupportsRangeHeader(contentType)) {
1084 ServletResponseUtil.sendFileWithRangeHeader(
1085 request, response, fileName, inputStream, contentLength,
1086 contentType);
1087 }
1088 else {
1089 boolean download = ParamUtil.getBoolean(request, "download");
1090
1091 if (download) {
1092 ServletResponseUtil.sendFile(
1093 request, response, fileName, inputStream, contentLength,
1094 contentType, HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT);
1095 }
1096 else {
1097 ServletResponseUtil.sendFile(
1098 request, response, fileName, inputStream, contentLength,
1099 contentType);
1100 }
1101 }
1102 }
1103
1104 protected void sendFile(
1105 HttpServletResponse response, User user, long groupId,
1106 long folderId, String title)
1107 throws Exception {
1108
1109 FileEntry fileEntry = DLAppServiceUtil.getFileEntry(
1110 groupId, folderId, title);
1111
1112 String contentType = fileEntry.getMimeType();
1113
1114 response.setContentType(contentType);
1115
1116 InputStream inputStream = fileEntry.getContentStream();
1117
1118 ServletResponseUtil.write(response, inputStream, fileEntry.getSize());
1119 }
1120
1121 protected void sendGroups(
1122 HttpServletResponse response, User user, String path)
1123 throws Exception {
1124
1125 if (!PropsValues.WEB_SERVER_SERVLET_DIRECTORY_INDEXING_ENABLED) {
1126 response.setStatus(HttpServletResponse.SC_FORBIDDEN);
1127
1128 throw new PrincipalException();
1129 }
1130
1131 List<WebServerEntry> webServerEntries = new ArrayList<>();
1132
1133 List<Group> groups = WebDAVUtil.getGroups(user);
1134
1135 for (Group group : groups) {
1136 String name = HttpUtil.fixPath(group.getFriendlyURL());
1137
1138 WebServerEntry webServerEntry = new WebServerEntry(
1139 path, name + StringPool.SLASH, null, null,
1140 group.getDescription(), 0);
1141
1142 webServerEntries.add(webServerEntry);
1143 }
1144
1145 sendHTML(response, path, webServerEntries);
1146 }
1147
1148 protected void sendHTML(
1149 HttpServletResponse response, String path,
1150 List<WebServerEntry> webServerEntries)
1151 throws Exception {
1152
1153 Template template = TemplateManagerUtil.getTemplate(
1154 TemplateConstants.LANG_TYPE_FTL, _templateResource, true);
1155
1156 template.put("dateFormat", _dateFormat);
1157 template.put("entries", webServerEntries);
1158 template.put("path", HttpUtil.encodePath(path));
1159
1160 if (_WEB_SERVER_SERVLET_VERSION_VERBOSITY_DEFAULT) {
1161 }
1162 else if (_WEB_SERVER_SERVLET_VERSION_VERBOSITY_PARTIAL) {
1163 template.put("releaseInfo", ReleaseInfo.getName());
1164 }
1165 else {
1166 template.put("releaseInfo", ReleaseInfo.getReleaseInfo());
1167 }
1168
1169 template.put("validator", Validator_IW.getInstance());
1170
1171 response.setContentType(ContentTypes.TEXT_HTML_UTF8);
1172
1173 template.processTemplate(response.getWriter());
1174 }
1175
1176 protected void sendPortletFileEntry(
1177 HttpServletRequest request, HttpServletResponse response,
1178 String[] pathArray)
1179 throws Exception {
1180
1181 FileEntry fileEntry = getPortletFileEntry(request, pathArray);
1182
1183 if (fileEntry == null) {
1184 return;
1185 }
1186
1187 String fileName = HttpUtil.decodeURL(HtmlUtil.escape(pathArray[2]));
1188
1189 if (fileEntry.isInTrash()) {
1190 fileName = TrashUtil.getOriginalTitle(fileName);
1191 }
1192
1193 boolean download = ParamUtil.getBoolean(request, "download");
1194
1195 if (download) {
1196 ServletResponseUtil.sendFile(
1197 request, response, fileName, fileEntry.getContentStream(),
1198 fileEntry.getSize(), fileEntry.getMimeType(),
1199 HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT);
1200 }
1201 else {
1202 InputStream is = fileEntry.getContentStream();
1203
1204 FlashMagicBytesUtil.Result flashMagicBytesUtilResult =
1205 FlashMagicBytesUtil.check(is);
1206
1207 is = flashMagicBytesUtilResult.getInputStream();
1208
1209 if (flashMagicBytesUtilResult.isFlash()) {
1210 fileName = FileUtil.stripExtension(fileName) + ".swf";
1211 }
1212
1213 ServletResponseUtil.sendFile(
1214 request, response, fileName, is, fileEntry.getSize(),
1215 fileEntry.getMimeType());
1216 }
1217 }
1218
1219 protected void writeImage(
1220 Image image, HttpServletRequest request,
1221 HttpServletResponse response)
1222 throws PortalException {
1223
1224 if (image == null) {
1225 return;
1226 }
1227
1228 String contentType = null;
1229
1230 String type = image.getType();
1231
1232 if (!type.equals(ImageConstants.TYPE_NOT_AVAILABLE)) {
1233 contentType = MimeTypesUtil.getExtensionContentType(type);
1234
1235 response.setContentType(contentType);
1236 }
1237
1238 String fileName = ParamUtil.getString(request, "fileName");
1239
1240 byte[] bytes = getImageBytes(request, image);
1241
1242 try {
1243 if (Validator.isNotNull(fileName)) {
1244 ServletResponseUtil.sendFile(
1245 request, response, fileName, bytes, contentType);
1246 }
1247 else {
1248 ServletResponseUtil.write(response, bytes);
1249 }
1250 }
1251 catch (Exception e) {
1252 if (_log.isWarnEnabled()) {
1253 _log.warn(e, e);
1254 }
1255 }
1256 }
1257
1258 private static void _checkFileEntry(String[] pathArray) throws Exception {
1259 if (pathArray.length == 1) {
1260 long fileShortcutId = GetterUtil.getLong(pathArray[0]);
1261
1262 FileShortcut fileShortcut = DLAppLocalServiceUtil.getFileShortcut(
1263 fileShortcutId);
1264
1265 DLAppLocalServiceUtil.getFileEntry(fileShortcut.getToFileEntryId());
1266 }
1267 else if (pathArray.length == 2) {
1268
1269
1270
1271 }
1272 else if (pathArray.length == 3) {
1273 long groupId = GetterUtil.getLong(pathArray[0]);
1274 long folderId = GetterUtil.getLong(pathArray[1]);
1275 String fileName = HttpUtil.decodeURL(pathArray[2]);
1276
1277 try {
1278 DLAppLocalServiceUtil.getFileEntry(groupId, folderId, fileName);
1279 }
1280 catch (RepositoryException re) {
1281 }
1282 }
1283 else {
1284 long groupId = GetterUtil.getLong(pathArray[0]);
1285
1286 String uuid = pathArray[3];
1287
1288 try {
1289 DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
1290 uuid, groupId);
1291 }
1292 catch (RepositoryException re) {
1293 }
1294 }
1295 }
1296
1297 private static long _getGroupId(long companyId, String name)
1298 throws Exception {
1299
1300 Group group = GroupLocalServiceUtil.fetchFriendlyURLGroup(
1301 companyId, StringPool.SLASH + name);
1302
1303 if (group != null) {
1304 return group.getGroupId();
1305 }
1306
1307 User user = UserLocalServiceUtil.getUserByScreenName(companyId, name);
1308
1309 group = user.getGroup();
1310
1311 return group.getGroupId();
1312 }
1313
1314 private static User _getUser(HttpServletRequest request) throws Exception {
1315 HttpSession session = request.getSession();
1316
1317 if (PortalSessionThreadLocal.getHttpSession() == null) {
1318 PortalSessionThreadLocal.setHttpSession(session);
1319 }
1320
1321 User user = PortalUtil.getUser(request);
1322
1323 if (user != null) {
1324 return user;
1325 }
1326
1327 String userIdString = (String)session.getAttribute("j_username");
1328 String password = (String)session.getAttribute("j_password");
1329
1330 if ((userIdString != null) && (password != null)) {
1331 long userId = GetterUtil.getLong(userIdString);
1332
1333 user = UserLocalServiceUtil.getUser(userId);
1334 }
1335 else {
1336 long companyId = PortalUtil.getCompanyId(request);
1337
1338 Company company = CompanyLocalServiceUtil.getCompany(companyId);
1339
1340 user = company.getDefaultUser();
1341 }
1342
1343 return user;
1344 }
1345
1346 private static final boolean _WEB_SERVER_SERVLET_VERSION_VERBOSITY_DEFAULT =
1347 StringUtil.equalsIgnoreCase(
1348 PropsValues.WEB_SERVER_SERVLET_VERSION_VERBOSITY,
1349 ReleaseInfo.getName());
1350
1351 private static final boolean _WEB_SERVER_SERVLET_VERSION_VERBOSITY_PARTIAL =
1352 StringUtil.equalsIgnoreCase(
1353 PropsValues.WEB_SERVER_SERVLET_VERSION_VERBOSITY, "partial");
1354
1355 private static final Log _log = LogFactoryUtil.getLog(
1356 WebServerServlet.class);
1357
1358 private static final Set<String> _acceptRangesMimeTypes = SetUtil.fromArray(
1359 PropsValues.WEB_SERVER_SERVLET_ACCEPT_RANGES_MIME_TYPES);
1360
1361 private final Format _dateFormat =
1362 FastDateFormatFactoryUtil.getSimpleDateFormat("d MMM yyyy HH:mm z");
1363 private boolean _lastModified = true;
1364 private TemplateResource _templateResource;
1365
1366 }