001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.repository.liferayrepository;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.Repository;
019    import com.liferay.portal.kernel.repository.model.FileContentReference;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.ModelValidator;
022    import com.liferay.portal.repository.util.RepositoryWrapper;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.documentlibrary.util.DLAppUtil;
025    
026    import java.io.File;
027    import java.io.InputStream;
028    
029    /**
030     * @author Adolfo P??rez
031     */
032    public class ModelValidatorRepositoryWrapper extends RepositoryWrapper {
033    
034            public ModelValidatorRepositoryWrapper(
035                    Repository repository,
036                    ModelValidator<FileContentReference> modelValidator) {
037    
038                    super(repository);
039    
040                    _modelValidator = modelValidator;
041            }
042    
043            @Override
044            public FileEntry addFileEntry(
045                            long userId, long folderId, String sourceFileName, String mimeType,
046                            String title, String description, String changeLog, File file,
047                            ServiceContext serviceContext)
048                    throws PortalException {
049    
050                    FileContentReference fileContentReference =
051                            FileContentReference.fromFile(
052                                    sourceFileName, DLAppUtil.getExtension(title, sourceFileName),
053                                    mimeType, file);
054    
055                    _modelValidator.validate(fileContentReference);
056    
057                    return super.addFileEntry(
058                            userId, folderId, sourceFileName, mimeType, title, description,
059                            changeLog, file, serviceContext);
060            }
061    
062            @Override
063            public FileEntry addFileEntry(
064                            long userId, long folderId, String sourceFileName, String mimeType,
065                            String title, String description, String changeLog, InputStream is,
066                            long size, ServiceContext serviceContext)
067                    throws PortalException {
068    
069                    FileContentReference fileContentReference =
070                            FileContentReference.fromInputStream(
071                                    sourceFileName, DLAppUtil.getExtension(title, sourceFileName),
072                                    mimeType, is, size);
073    
074                    _modelValidator.validate(fileContentReference);
075    
076                    return super.addFileEntry(
077                            userId, folderId, sourceFileName, mimeType, title, description,
078                            changeLog, is, size, serviceContext);
079            }
080    
081            @Override
082            public FileEntry updateFileEntry(
083                            long userId, long fileEntryId, String sourceFileName,
084                            String mimeType, String title, String description, String changeLog,
085                            boolean majorVersion, File file, ServiceContext serviceContext)
086                    throws PortalException {
087    
088                    FileContentReference fileContentReference =
089                            FileContentReference.fromFile(
090                                    sourceFileName, DLAppUtil.getExtension(title, sourceFileName),
091                                    mimeType, file);
092    
093                    _modelValidator.validate(fileContentReference);
094    
095                    return super.updateFileEntry(
096                            userId, fileEntryId, sourceFileName, mimeType, title, description,
097                            changeLog, majorVersion, file, serviceContext);
098            }
099    
100            @Override
101            public FileEntry updateFileEntry(
102                            long userId, long fileEntryId, String sourceFileName,
103                            String mimeType, String title, String description, String changeLog,
104                            boolean majorVersion, InputStream is, long size,
105                            ServiceContext serviceContext)
106                    throws PortalException {
107    
108                    FileContentReference fileContentReference =
109                            FileContentReference.fromInputStream(
110                                    sourceFileName, DLAppUtil.getExtension(title, sourceFileName),
111                                    mimeType, is, size);
112    
113                    _modelValidator.validate(fileContentReference);
114    
115                    return super.updateFileEntry(
116                            userId, fileEntryId, sourceFileName, mimeType, title, description,
117                            changeLog, majorVersion, is, size, serviceContext);
118            }
119    
120            private final ModelValidator<FileContentReference> _modelValidator;
121    
122    }