@Override public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { long groupId = ParamUtil.getLong(request, "groupId"); String articleId = ParamUtil.getString(request, "articleId"); String languageId = LanguageUtil.getLanguageId(request); JournalArticle article = JournalArticleServiceUtil.getLatestArticle( groupId, articleId, WorkflowConstants.STATUS_APPROVED); String fileName = "content.xml"; byte[] bytes = article.getContentByLocale(languageId).getBytes(); ServletResponseUtil.sendFile(request, response, fileName, bytes, ContentTypes.TEXT_XML_UTF8); return null; } catch (Exception e) { PortalUtil.sendError(e, request, response); return null; } }
protected void writeImage(Image image, HttpServletRequest request, HttpServletResponse response) { if (image == null) { return; } String contentType = null; String type = image.getType(); if (!type.equals(ImageConstants.TYPE_NOT_AVAILABLE)) { contentType = MimeTypesUtil.getContentType("A." + type); response.setContentType(contentType); } String fileName = ParamUtil.getString(request, "fileName"); try { byte[] bytes = getImageBytes(request, image); if (Validator.isNotNull(fileName)) { ServletResponseUtil.sendFile(request, response, fileName, bytes, contentType); } else { ServletResponseUtil.write(response, bytes); } } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn(e, e); } } }
protected void sendResponse( ActionRequest actionRequest, ActionResponse actionResponse, String msg, boolean success) throws Exception { StringBundler sb = new StringBundler(7); sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sb.append("<response>"); if (success) { sb.append("<error>0</error>"); } else { sb.append("<error>1</error>"); sb.append("<message>"); sb.append(msg); sb.append("</message>"); } sb.append("</response>"); HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse); ServletResponseUtil.sendFile( request, response, null, sb.toString().getBytes(StringPool.UTF8), ContentTypes.TEXT_XML_UTF8); }
@Override public void processAction( ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { try { String csv = getUsersCSV(actionRequest, actionResponse); String fileName = "users.csv"; byte[] bytes = csv.getBytes(); HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse); ServletResponseUtil.sendFile(request, response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8); setForward(actionRequest, ActionConstants.COMMON_NULL); } catch (Exception e) { SessionErrors.add(actionRequest, e.getClass()); setForward(actionRequest, "portlet.users_admin.error"); } }
@Override public void serveResource( StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ResourceRequest resourceRequest, ResourceResponse resourceResponse) { long entryId = ParamUtil.getLong(resourceRequest, "entryId"); ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY); BlogsEntry entry; try { entry = BlogsEntryServiceUtil.getEntry(entryId); StringBundler sb = new StringBundler(); sb.append("<html>"); sb.append("<head>"); sb.append("<meta content=\""); sb.append(ContentTypes.TEXT_HTML_UTF8); sb.append("\" http-equiv=\"content-type\" />"); sb.append("<base href=\""); sb.append(themeDisplay.getPortalURL()); sb.append("\" />"); sb.append("</head>"); sb.append("<body>"); sb.append(entry.getContent()); sb.append("</body>"); sb.append("</html>"); InputStream is = new UnsyncByteArrayInputStream(sb.toString().getBytes(StringPool.UTF8)); File destinationFile = null; destinationFile = DocumentConversionUtil.convert(entry.getEntryId() + "", is, "html", "pdf"); OutputStream out = resourceResponse.getPortletOutputStream(); InputStream in = new FileInputStream(destinationFile); HttpServletResponse httpRes = PortalUtil.getHttpServletResponse(resourceResponse); HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(resourceRequest); String contentType = MimeTypesUtil.getContentType(destinationFile); ServletResponseUtil.sendFile(httpReq, httpRes, destinationFile.getName(), in, contentType); out.close(); } catch (PortalException e) { System.out.println("PortalException : " + getClass().getName() + "\n" + e); // e.printStackTrace(); } catch (SystemException e) { System.out.println("SystemException : " + getClass().getName() + "\n" + e); // e.printStackTrace(); } catch (UnsupportedEncodingException e) { System.out.println("UnsupportedEncodingException : " + getClass().getName() + "\n" + e); // e.printStackTrace(); } catch (Exception e) { System.out.println("Exception : " + getClass().getName() + "\n" + e); // e.printStackTrace(); } }
@Override public ActionForward strutsExecute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { ServletResponseUtil.sendFile( request, response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8); return null; } catch (Exception e) { PortalUtil.sendError(e, request, response); return null; } }
@Override public void processAction( ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { File file = null; try { ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); long groupId = ParamUtil.getLong(actionRequest, "groupId"); boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout"); long[] layoutIds = ParamUtil.getLongValues(actionRequest, "layoutIds"); String fileName = ParamUtil.getString(actionRequest, "exportFileName"); String range = ParamUtil.getString(actionRequest, "range"); Date startDate = null; Date endDate = null; if (range.equals("dateRange")) { int startDateMonth = ParamUtil.getInteger(actionRequest, "startDateMonth"); int startDateDay = ParamUtil.getInteger(actionRequest, "startDateDay"); int startDateYear = ParamUtil.getInteger(actionRequest, "startDateYear"); int startDateHour = ParamUtil.getInteger(actionRequest, "startDateHour"); int startDateMinute = ParamUtil.getInteger(actionRequest, "startDateMinute"); int startDateAmPm = ParamUtil.getInteger(actionRequest, "startDateAmPm"); if (startDateAmPm == Calendar.PM) { startDateHour += 12; } startDate = PortalUtil.getDate( startDateMonth, startDateDay, startDateYear, startDateHour, startDateMinute, themeDisplay.getTimeZone(), new PortalException()); int endDateMonth = ParamUtil.getInteger(actionRequest, "endDateMonth"); int endDateDay = ParamUtil.getInteger(actionRequest, "endDateDay"); int endDateYear = ParamUtil.getInteger(actionRequest, "endDateYear"); int endDateHour = ParamUtil.getInteger(actionRequest, "endDateHour"); int endDateMinute = ParamUtil.getInteger(actionRequest, "endDateMinute"); int endDateAmPm = ParamUtil.getInteger(actionRequest, "endDateAmPm"); if (endDateAmPm == Calendar.PM) { endDateHour += 12; } endDate = PortalUtil.getDate( endDateMonth, endDateDay, endDateYear, endDateHour, endDateMinute, themeDisplay.getTimeZone(), new PortalException()); } else if (range.equals("fromLastPublishDate")) { LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(groupId, privateLayout); UnicodeProperties settingsProperties = layoutSet.getSettingsProperties(); long lastPublishDate = GetterUtil.getLong(settingsProperties.getProperty("last-publish-date")); if (lastPublishDate > 0) { Calendar cal = Calendar.getInstance(themeDisplay.getTimeZone(), themeDisplay.getLocale()); endDate = cal.getTime(); cal.setTimeInMillis(lastPublishDate); startDate = cal.getTime(); } } else if (range.equals("last")) { int rangeLast = ParamUtil.getInteger(actionRequest, "last"); Date now = new Date(); startDate = new Date(now.getTime() - (rangeLast * Time.HOUR)); endDate = now; } file = LayoutServiceUtil.exportLayoutsAsFile( groupId, privateLayout, layoutIds, actionRequest.getParameterMap(), startDate, endDate); HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse); ServletResponseUtil.sendFile( request, response, fileName, new FileInputStream(file), ContentTypes.APPLICATION_ZIP); setForward(actionRequest, ActionConstants.COMMON_NULL); } catch (Exception e) { _log.error(e, e); SessionErrors.add(actionRequest, e.getClass().getName()); String pagesRedirect = ParamUtil.getString(actionRequest, "pagesRedirect"); sendRedirect(actionRequest, actionResponse, pagesRedirect); } finally { FileUtil.delete(file); } }
@Override public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { String resourceID = GetterUtil.getString(resourceRequest.getResourceID()); HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest); HttpServletResponse response = PortalUtil.getHttpServletResponse(resourceResponse); if (resourceID.equals("compareVersions")) { ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY); long groupId = ParamUtil.getLong(resourceRequest, "groupId"); String articleId = ParamUtil.getString(resourceRequest, "articleId"); double sourceVersion = ParamUtil.getDouble(resourceRequest, "filterSourceVersion"); double targetVersion = ParamUtil.getDouble(resourceRequest, "filterTargetVersion"); String languageId = ParamUtil.getString(resourceRequest, "languageId"); String diffHtmlResults = null; try { diffHtmlResults = JournalUtil.diffHtml( groupId, articleId, sourceVersion, targetVersion, languageId, new PortletRequestModel(resourceRequest, resourceResponse), themeDisplay); } catch (CompareVersionsException cve) { resourceRequest.setAttribute(WebKeys.DIFF_VERSION, cve.getVersion()); } catch (Exception e) { try { PortalUtil.sendError(e, request, response); } catch (ServletException se) { } } resourceRequest.setAttribute(WebKeys.DIFF_HTML_RESULTS, diffHtmlResults); PortletSession portletSession = resourceRequest.getPortletSession(); PortletContext portletContext = portletSession.getPortletContext(); PortletRequestDispatcher portletRequestDispatcher = portletContext.getRequestDispatcher( "/html/taglib/ui/diff_version_comparator/diff_html.jsp"); portletRequestDispatcher.include(resourceRequest, resourceResponse); } else if (resourceID.equals("rss")) { try { byte[] xml = JournalRSSUtil.getRSS(resourceRequest, resourceResponse); ServletResponseUtil.sendFile(request, response, null, xml, ContentTypes.TEXT_XML_UTF8); } catch (Exception e) { try { PortalUtil.sendError(e, request, response); } catch (ServletException se) { } } } else { super.serveResource(resourceRequest, resourceResponse); } }
protected void sendFile( HttpServletRequest request, HttpServletResponse response, User user, String[] pathArray) throws Exception { // Retrieve file details FileEntry fileEntry = getFileEntry(pathArray); if (fileEntry == null) { throw new NoSuchFileEntryException(); } String version = ParamUtil.getString(request, "version"); if (Validator.isNull(version)) { if (Validator.isNotNull(fileEntry.getVersion())) { version = fileEntry.getVersion(); } } String tempFileId = DLUtil.getTempFileId(fileEntry.getFileEntryId(), version); FileVersion fileVersion = fileEntry.getFileVersion(version); String fileName = fileVersion.getTitle(); String extension = fileVersion.getExtension(); if (Validator.isNotNull(extension) && !fileName.endsWith(StringPool.PERIOD + extension)) { fileName += StringPool.PERIOD + extension; } // Handle requested conversion boolean converted = false; String targetExtension = ParamUtil.getString(request, "targetExtension"); int imageThumbnail = ParamUtil.getInteger(request, "imageThumbnail"); int documentThumbnail = ParamUtil.getInteger(request, "documentThumbnail"); int previewFileIndex = ParamUtil.getInteger(request, "previewFileIndex"); boolean audioPreview = ParamUtil.getBoolean(request, "audioPreview"); boolean imagePreview = ParamUtil.getBoolean(request, "imagePreview"); boolean videoPreview = ParamUtil.getBoolean(request, "videoPreview"); int videoThumbnail = ParamUtil.getInteger(request, "videoThumbnail"); InputStream inputStream = null; long contentLength = 0; if ((imageThumbnail > 0) && (imageThumbnail <= 3)) { fileName = FileUtil.stripExtension(fileName) .concat(StringPool.PERIOD) .concat(ImageProcessorUtil.getThumbnailType(fileVersion)); int thumbnailIndex = imageThumbnail - 1; inputStream = ImageProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex); contentLength = ImageProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex); converted = true; } else if ((documentThumbnail > 0) && (documentThumbnail <= 3)) { fileName = FileUtil.stripExtension(fileName) .concat(StringPool.PERIOD) .concat(PDFProcessor.THUMBNAIL_TYPE); int thumbnailIndex = documentThumbnail - 1; inputStream = PDFProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex); contentLength = PDFProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex); converted = true; } else if (previewFileIndex > 0) { fileName = FileUtil.stripExtension(fileName) .concat(StringPool.PERIOD) .concat(PDFProcessor.PREVIEW_TYPE); inputStream = PDFProcessorUtil.getPreviewAsStream(fileVersion, previewFileIndex); contentLength = PDFProcessorUtil.getPreviewFileSize(fileVersion, previewFileIndex); converted = true; } else if (audioPreview || videoPreview) { String type = ParamUtil.getString(request, "type"); fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(type); if (audioPreview) { inputStream = AudioProcessorUtil.getPreviewAsStream(fileVersion, type); contentLength = AudioProcessorUtil.getPreviewFileSize(fileVersion, type); } else { inputStream = VideoProcessorUtil.getPreviewAsStream(fileVersion, type); contentLength = VideoProcessorUtil.getPreviewFileSize(fileVersion, type); } converted = true; } else if (imagePreview) { String type = ImageProcessorUtil.getPreviewType(fileVersion); fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(type); inputStream = ImageProcessorUtil.getPreviewAsStream(fileVersion); contentLength = ImageProcessorUtil.getPreviewFileSize(fileVersion); converted = true; } else if ((videoThumbnail > 0) && (videoThumbnail <= 3)) { fileName = FileUtil.stripExtension(fileName) .concat(StringPool.PERIOD) .concat(VideoProcessor.THUMBNAIL_TYPE); int thumbnailIndex = videoThumbnail - 1; inputStream = VideoProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex); contentLength = VideoProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex); converted = true; } else { inputStream = fileVersion.getContentStream(true); contentLength = fileVersion.getSize(); if (Validator.isNotNull(targetExtension)) { File convertedFile = DocumentConversionUtil.convert(tempFileId, inputStream, extension, targetExtension); if (convertedFile != null) { fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(targetExtension); inputStream = new FileInputStream(convertedFile); contentLength = convertedFile.length(); converted = true; } } } // Determine proper content type String contentType = null; if (converted) { contentType = MimeTypesUtil.getContentType(fileName); } else { contentType = fileVersion.getMimeType(); } // Support range HTTP header response.setHeader(HttpHeaders.ACCEPT_RANGES, HttpHeaders.ACCEPT_RANGES_BYTES_VALUE); List<Range> ranges = null; try { ranges = ServletResponseUtil.getRanges(request, response, contentLength); } catch (IOException ioe) { if (_log.isErrorEnabled()) { _log.error(ioe); } response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + contentLength); response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); return; } if ((ranges == null) || ranges.isEmpty()) { ServletResponseUtil.sendFile( request, response, fileName, inputStream, contentLength, contentType); } else { if (_log.isDebugEnabled()) { _log.debug("Request has range header " + request.getHeader(HttpHeaders.RANGE)); } ServletResponseUtil.write( request, response, fileName, ranges, inputStream, contentLength, contentType); } }
@Override public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { String servletPath = request.getServletPath(); String pathInfo = URLDecoder.decode(request.getPathInfo(), StringPool.UTF8); String path = servletPath.concat(pathInfo); if (!isAllowedPath(path)) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } File file = new File(_tempDir, path); String canonicalPath = file.getCanonicalPath(); if (!file.exists() || file.isDirectory() || !file.canRead() || file.isHidden() || !canonicalPath.startsWith(_tempDir.getCanonicalPath())) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } long lastModified = file.lastModified(); if (lastModified > 0) { long ifModifiedSince = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); if ((ifModifiedSince > 0) && (ifModifiedSince == lastModified)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } } if (lastModified > 0) { response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified); } String fileName = file.getName(); // Determine proper content type String contentType = MimeTypesUtil.getContentType(fileName); // Send file if (isSupportsRangeHeader(contentType)) { sendFileWithRangeHeader( request, response, fileName, new FileInputStream(file), file.length(), contentType); } else { ServletResponseUtil.sendFile( request, response, fileName, new FileInputStream(file), file.length(), contentType); } }
protected void getFile( long fileEntryId, long folderId, String name, String title, String version, long fileShortcutId, String uuid, long groupId, String targetExtension, HttpServletRequest request, HttpServletResponse response) throws Exception { if (name.startsWith("DLFE-")) { name = name.substring(5); } name = FileUtil.stripExtension(name); FileEntry fileEntry = null; if (Validator.isNotNull(uuid) && (groupId > 0)) { fileEntry = DLAppServiceUtil.getFileEntryByUuidAndGroupId(uuid, groupId); folderId = fileEntry.getFolderId(); } if (fileEntryId > 0) { fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId); } else if (fileShortcutId <= 0) { if (Validator.isNotNull(title)) { fileEntry = DLAppServiceUtil.getFileEntry(groupId, folderId, title); } else if (Validator.isNotNull(name)) { DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.fetchFileEntryByName(groupId, folderId, name); if (dlFileEntry == null) { // LPS-30374 List<DLFileEntry> dlFileEntries = DLFileEntryLocalServiceUtil.getFileEntries(folderId, name); if (!dlFileEntries.isEmpty()) { dlFileEntry = dlFileEntries.get(0); } } if (dlFileEntry != null) { ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); PermissionChecker permissionChecker = themeDisplay.getPermissionChecker(); DLFileEntryPermission.check(permissionChecker, dlFileEntry, ActionKeys.VIEW); fileEntry = new LiferayFileEntry(dlFileEntry); } } } else { DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(fileShortcutId); fileEntryId = fileShortcut.getToFileEntryId(); fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId); } if (Validator.isNull(version)) { if ((fileEntry != null) && Validator.isNotNull(fileEntry.getVersion())) { version = fileEntry.getVersion(); } else { throw new NoSuchFileEntryException(); } } FileVersion fileVersion = fileEntry.getFileVersion(version); InputStream is = fileVersion.getContentStream(true); String fileName = fileVersion.getTitle(); long contentLength = fileVersion.getSize(); String contentType = fileVersion.getMimeType(); if (Validator.isNotNull(targetExtension)) { String id = DLUtil.getTempFileId(fileEntry.getFileEntryId(), version); String sourceExtension = fileVersion.getExtension(); if (!fileName.endsWith(StringPool.PERIOD + sourceExtension)) { fileName += StringPool.PERIOD + sourceExtension; } File convertedFile = DocumentConversionUtil.convert(id, is, sourceExtension, targetExtension); if (convertedFile != null) { fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(targetExtension); is = new FileInputStream(convertedFile); contentLength = convertedFile.length(); contentType = MimeTypesUtil.getContentType(fileName); } } ServletResponseUtil.sendFile(request, response, fileName, is, contentLength, contentType); }
public static void sendFile(PortletRequest portletRequest, PortletResponse portletResponse) throws IOException { long groupId = ParamUtil.getLong(portletRequest, "groupId"); String articleId = ParamUtil.getString(portletRequest, "articleId"); String targetExtension = ParamUtil.getString(portletRequest, "targetExtension"); PortletPreferences portletPreferences = portletRequest.getPreferences(); String[] allowedExtensions = StringUtil.split(portletPreferences.getValue("extensions", null)); String languageId = LanguageUtil.getLanguageId(portletRequest); PortletRequestModel portletRequestModel = new PortletRequestModel(portletRequest, portletResponse); ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY); HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest); HttpServletResponse response = PortalUtil.getHttpServletResponse(portletResponse); JournalArticleDisplay articleDisplay = JournalContentUtil.getDisplay( groupId, articleId, null, "export", languageId, 1, portletRequestModel, themeDisplay); int pages = articleDisplay.getNumberOfPages(); StringBundler sb = new StringBundler(pages + 12); sb.append("<html>"); sb.append("<head>"); sb.append("<meta content=\""); sb.append(ContentTypes.TEXT_HTML_UTF8); sb.append("\" http-equiv=\"content-type\" />"); sb.append("<base href=\""); sb.append(themeDisplay.getPortalURL()); sb.append("\" />"); sb.append("</head>"); sb.append("<body>"); sb.append(articleDisplay.getContent()); for (int i = 2; i <= pages; i++) { articleDisplay = JournalContentUtil.getDisplay(groupId, articleId, "export", languageId, i, themeDisplay); sb.append(articleDisplay.getContent()); } sb.append("</body>"); sb.append("</html>"); InputStream is = new UnsyncByteArrayInputStream(sb.toString().getBytes(StringPool.UTF8)); String title = articleDisplay.getTitle(); String sourceExtension = "html"; String fileName = title.concat(StringPool.PERIOD).concat(sourceExtension); String contentType = MimeTypesUtil.getContentType(fileName); if (Validator.isNull(targetExtension) || !ArrayUtil.contains(allowedExtensions, targetExtension)) { ServletResponseUtil.sendFile(request, response, fileName, is, contentType); return; } String id = DLUtil.getTempFileId( articleDisplay.getId(), String.valueOf(articleDisplay.getVersion()), languageId); File convertedFile = DocumentConversionUtil.convert(id, is, sourceExtension, targetExtension); if (convertedFile != null) { fileName = title.concat(StringPool.PERIOD).concat(targetExtension); is = new FileInputStream(convertedFile); } ServletResponseUtil.sendFile(request, response, fileName, is, contentType); }