Search in sources :

Example 1 with NoSuchFileEntryException

use of com.liferay.portlet.documentlibrary.NoSuchFileEntryException in project liferay-ide by liferay.

the class KBArticleImporterUtil method addImageFileEntry.

protected static FileEntry addImageFileEntry(long userId, KBArticle kbArticle, String imageFileName, InputStream inputStream, Map<String, FileEntry> fileEntriesMap) throws PortalException, SystemException {
    FileEntry fileEntry = fileEntriesMap.get(imageFileName);
    if (fileEntry != null) {
        return fileEntry;
    }
    String mimeType = MimeTypesUtil.getContentType(imageFileName);
    try {
        PortletFileRepositoryUtil.getPortletFileEntry(kbArticle.getGroupId(), kbArticle.getAttachmentsFolderId(), imageFileName);
        PortletFileRepositoryUtil.deletePortletFileEntry(kbArticle.getGroupId(), kbArticle.getAttachmentsFolderId(), imageFileName);
    } catch (NoSuchFileEntryException nsfee) {
    }
    fileEntry = PortletFileRepositoryUtil.addPortletFileEntry(kbArticle.getGroupId(), userId, KBArticle.class.getName(), kbArticle.getClassPK(), PortletKeys.KNOWLEDGE_BASE_ARTICLE, kbArticle.getAttachmentsFolderId(), inputStream, imageFileName, mimeType, false);
    fileEntriesMap.put(imageFileName, fileEntry);
    return fileEntry;
}
Also used : NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry)

Example 2 with NoSuchFileEntryException

use of com.liferay.portlet.documentlibrary.NoSuchFileEntryException in project liferay-ide by liferay.

the class WebServerServlet method sendDocumentLibrary.

protected void sendDocumentLibrary(HttpServletRequest request, HttpServletResponse response, User user, String path, String[] pathArray) throws Exception {
    if (!PropsValues.WEB_SERVER_SERVLET_DIRECTORY_INDEXING_ENABLED) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    long groupId = _getGroupId(user.getCompanyId(), pathArray[0]);
    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
    for (int i = 1; i < pathArray.length; i++) {
        String name = pathArray[i];
        try {
            Folder folder = DLAppServiceUtil.getFolder(groupId, folderId, name);
            folderId = folder.getFolderId();
        } catch (NoSuchFolderException nsfe) {
            if (i != pathArray.length - 1) {
                throw nsfe;
            }
            String title = name;
            sendFile(response, user, groupId, folderId, title);
            return;
        }
    }
    try {
        sendFile(response, user, groupId, folderId, "index.html");
        return;
    } catch (Exception e) {
        if ((e instanceof NoSuchFileEntryException) || (e instanceof PrincipalException)) {
            try {
                sendFile(response, user, groupId, folderId, "index.htm");
                return;
            } catch (NoSuchFileEntryException nsfee) {
            } catch (PrincipalException pe) {
            }
        } else {
            throw e;
        }
    }
    List<WebServerEntry> webServerEntries = new ArrayList<WebServerEntry>();
    webServerEntries.add(new WebServerEntry(path, "../"));
    List<Folder> folders = DLAppServiceUtil.getFolders(groupId, folderId);
    for (Folder folder : folders) {
        WebServerEntry webServerEntry = new WebServerEntry(path, folder.getName() + StringPool.SLASH, folder.getCreateDate(), folder.getModifiedDate(), folder.getDescription(), 0);
        webServerEntries.add(webServerEntry);
    }
    List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(groupId, folderId);
    for (FileEntry fileEntry : fileEntries) {
        WebServerEntry webServerEntry = new WebServerEntry(path, fileEntry.getTitle(), fileEntry.getCreateDate(), fileEntry.getModifiedDate(), fileEntry.getDescription(), fileEntry.getSize());
        webServerEntries.add(webServerEntry);
    }
    sendHTML(response, path, webServerEntries);
}
Also used : ArrayList(java.util.ArrayList) Folder(com.liferay.portal.kernel.repository.model.Folder) ServletException(javax.servlet.ServletException) RepositoryException(com.liferay.portal.kernel.repository.RepositoryException) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) NoSuchFolderException(com.liferay.portlet.documentlibrary.NoSuchFolderException) PrincipalException(com.liferay.portal.security.auth.PrincipalException) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) IOException(java.io.IOException) NoSuchGroupException(com.liferay.portal.NoSuchGroupException) NoSuchFolderException(com.liferay.portlet.documentlibrary.NoSuchFolderException) PrincipalException(com.liferay.portal.security.auth.PrincipalException) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) DLFileEntry(com.liferay.portlet.documentlibrary.model.DLFileEntry)

Example 3 with NoSuchFileEntryException

use of com.liferay.portlet.documentlibrary.NoSuchFileEntryException in project liferay-ide by liferay.

the class WebServerServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    User user = null;
    try {
        user = _getUser(request);
        PrincipalThreadLocal.setName(user.getUserId());
        PrincipalThreadLocal.setPassword(PortalUtil.getUserPassword(request));
        PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user, true);
        PermissionThreadLocal.setPermissionChecker(permissionChecker);
        if (_lastModified) {
            long lastModified = getLastModified(request);
            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 path = HttpUtil.fixPath(request.getPathInfo());
        String[] pathArray = StringUtil.split(path, CharPool.SLASH);
        if (pathArray.length == 0) {
            sendGroups(response, user, request.getServletPath() + StringPool.SLASH + path);
        } else {
            if (_PATH_DDL.equals(pathArray[0])) {
                sendDDLRecordFile(request, response, pathArray);
            } else if (Validator.isNumber(pathArray[0])) {
                sendFile(request, response, user, pathArray);
            } else {
                if (isLegacyImageGalleryImageId(request, response)) {
                    return;
                }
                Image image = getImage(request, true);
                if (image != null) {
                    writeImage(image, request, response);
                } else {
                    sendDocumentLibrary(request, response, user, request.getServletPath() + StringPool.SLASH + path, pathArray);
                }
            }
        }
    } catch (NoSuchFileEntryException nsfee) {
        PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND, nsfee, request, response);
    } catch (PrincipalException pe) {
        processPrincipalException(pe, user, request, response);
    } catch (Exception e) {
        PortalUtil.sendError(e, request, response);
    }
}
Also used : User(com.liferay.portal.model.User) PrincipalException(com.liferay.portal.security.auth.PrincipalException) PermissionChecker(com.liferay.portal.security.permission.PermissionChecker) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) Image(com.liferay.portal.model.Image) RenderedImage(java.awt.image.RenderedImage) ServletException(javax.servlet.ServletException) RepositoryException(com.liferay.portal.kernel.repository.RepositoryException) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) NoSuchFolderException(com.liferay.portlet.documentlibrary.NoSuchFolderException) PrincipalException(com.liferay.portal.security.auth.PrincipalException) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) IOException(java.io.IOException) NoSuchGroupException(com.liferay.portal.NoSuchGroupException)

Example 4 with NoSuchFileEntryException

use of com.liferay.portlet.documentlibrary.NoSuchFileEntryException in project liferay-ide by liferay.

the class WebServerServlet method sendFile.

protected void sendFile(HttpServletRequest request, HttpServletResponse response, User user, String[] pathArray) throws Exception {
    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;
    }
    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 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(fileVersion.getExtension());
        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(PDFProcessorImpl.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(PDFProcessorImpl.PREVIEW_TYPE);
        inputStream = PDFProcessorUtil.getPreviewAsStream(fileVersion, previewFileIndex);
        contentLength = PDFProcessorUtil.getPreviewFileSize(fileVersion, previewFileIndex);
        converted = true;
    } else if (audioPreview) {
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(AudioProcessor.PREVIEW_TYPE);
        inputStream = AudioProcessorUtil.getPreviewAsStream(fileVersion);
        contentLength = AudioProcessorUtil.getPreviewFileSize(fileVersion);
        converted = true;
    } else if (videoPreview) {
        String type = ParamUtil.getString(request, "type");
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(type);
        inputStream = VideoProcessorUtil.getPreviewAsStream(fileVersion, type);
        contentLength = VideoProcessorUtil.getPreviewFileSize(fileVersion, type);
        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.size() > 0)) {
            if (_log.isDebugEnabled()) {
                _log.debug("Video range requested");
            }
            String contentType = MimeTypesUtil.getContentType(fileName);
            ServletResponseUtil.write(request, response, fileName, ranges, inputStream, contentLength, contentType);
            return;
        }
        converted = true;
    } else if ((videoThumbnail > 0) && (videoThumbnail <= 3)) {
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(VideoProcessorImpl.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 = Files.newInputStream(convertedFile.toPath());
                contentLength = convertedFile.length();
                converted = true;
            }
        }
    }
    String contentType = null;
    if (converted) {
        contentType = MimeTypesUtil.getContentType(fileName);
    } else {
        contentType = fileVersion.getMimeType();
    }
    ServletResponseUtil.sendFile(request, response, fileName, inputStream, contentLength, contentType);
}
Also used : InputStream(java.io.InputStream) FileVersion(com.liferay.portal.kernel.repository.model.FileVersion) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) DLFileEntry(com.liferay.portlet.documentlibrary.model.DLFileEntry) IOException(java.io.IOException) Range(com.liferay.portal.kernel.servlet.Range) File(java.io.File)

Aggregations

NoSuchFileEntryException (com.liferay.portlet.documentlibrary.NoSuchFileEntryException)4 FileEntry (com.liferay.portal.kernel.repository.model.FileEntry)3 IOException (java.io.IOException)3 NoSuchGroupException (com.liferay.portal.NoSuchGroupException)2 PortalException (com.liferay.portal.kernel.exception.PortalException)2 SystemException (com.liferay.portal.kernel.exception.SystemException)2 RepositoryException (com.liferay.portal.kernel.repository.RepositoryException)2 PrincipalException (com.liferay.portal.security.auth.PrincipalException)2 NoSuchFolderException (com.liferay.portlet.documentlibrary.NoSuchFolderException)2 DLFileEntry (com.liferay.portlet.documentlibrary.model.DLFileEntry)2 ServletException (javax.servlet.ServletException)2 FileVersion (com.liferay.portal.kernel.repository.model.FileVersion)1 Folder (com.liferay.portal.kernel.repository.model.Folder)1 Range (com.liferay.portal.kernel.servlet.Range)1 Image (com.liferay.portal.model.Image)1 User (com.liferay.portal.model.User)1 PermissionChecker (com.liferay.portal.security.permission.PermissionChecker)1 RenderedImage (java.awt.image.RenderedImage)1 File (java.io.File)1 InputStream (java.io.InputStream)1