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.portlet.documentlibrary.util;
016    
017    import com.liferay.portal.kernel.util.FileUtil;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.UnicodeFormatter;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.PrefsPropsUtil;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portlet.documentlibrary.FileExtensionException;
026    import com.liferay.portlet.documentlibrary.FileNameException;
027    import com.liferay.portlet.documentlibrary.FileSizeException;
028    import com.liferay.portlet.documentlibrary.FolderNameException;
029    import com.liferay.portlet.documentlibrary.InvalidFileVersionException;
030    import com.liferay.portlet.documentlibrary.SourceFileNameException;
031    
032    import java.io.File;
033    import java.io.IOException;
034    import java.io.InputStream;
035    
036    /**
037     * @author Adolfo P??rez
038     */
039    public final class DLValidatorImpl implements DLValidator {
040    
041            @Override
042            public boolean isValidName(String name) {
043                    if (Validator.isNull(name)) {
044                            return false;
045                    }
046    
047                    for (String blacklistChar : PropsValues.DL_CHAR_BLACKLIST) {
048                            if (name.contains(blacklistChar)) {
049                                    return false;
050                            }
051                    }
052    
053                    for (String blacklistLastChar : PropsValues.DL_CHAR_LAST_BLACKLIST) {
054                            if (blacklistLastChar.startsWith(_UNICODE_PREFIX)) {
055                                    blacklistLastChar = UnicodeFormatter.parseString(
056                                            blacklistLastChar);
057                            }
058    
059                            if (name.endsWith(blacklistLastChar)) {
060                                    return false;
061                            }
062                    }
063    
064                    String nameWithoutExtension = name;
065    
066                    if (name.contains(StringPool.PERIOD)) {
067                            int index = name.lastIndexOf(StringPool.PERIOD);
068    
069                            nameWithoutExtension = name.substring(0, index);
070                    }
071    
072                    for (String blacklistName : PropsValues.DL_NAME_BLACKLIST) {
073                            if (StringUtil.equalsIgnoreCase(
074                                            nameWithoutExtension, blacklistName)) {
075    
076                                    return false;
077                            }
078                    }
079    
080                    return true;
081            }
082    
083            @Override
084            public void validateDirectoryName(String directoryName)
085                    throws FolderNameException {
086    
087                    if (!isValidName(directoryName)) {
088                            throw new FolderNameException(directoryName);
089                    }
090            }
091    
092            @Override
093            public void validateFileExtension(String fileName)
094                    throws FileExtensionException {
095    
096                    boolean validFileExtension = false;
097    
098                    String[] fileExtensions = PrefsPropsUtil.getStringArray(
099                            PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
100    
101                    for (String fileExtension : fileExtensions) {
102                            if (StringPool.STAR.equals(fileExtension) ||
103                                    StringUtil.endsWith(fileName, fileExtension)) {
104    
105                                    validFileExtension = true;
106    
107                                    break;
108                            }
109                    }
110    
111                    if (!validFileExtension) {
112                            throw new FileExtensionException(fileName);
113                    }
114            }
115    
116            @Override
117            public void validateFileName(String fileName) throws FileNameException {
118                    if (!isValidName(fileName)) {
119                            throw new FileNameException(fileName);
120                    }
121            }
122    
123            @Override
124            public void validateFileSize(String fileName, byte[] bytes)
125                    throws FileSizeException {
126    
127                    if (bytes == null) {
128                            throw new FileSizeException(fileName);
129                    }
130    
131                    validateFileSize(fileName, bytes.length);
132            }
133    
134            @Override
135            public void validateFileSize(String fileName, File file)
136                    throws FileSizeException {
137    
138                    if (file == null) {
139                            throw new FileSizeException(fileName);
140                    }
141    
142                    validateFileSize(fileName, file.length());
143            }
144    
145            @Override
146            public void validateFileSize(String fileName, InputStream is)
147                    throws FileSizeException {
148    
149                    try {
150                            if (is == null) {
151                                    throw new FileSizeException(fileName);
152                            }
153    
154                            validateFileSize(fileName, is.available());
155                    }
156                    catch (IOException ioe) {
157                            new FileSizeException(ioe);
158                    }
159            }
160    
161            @Override
162            public void validateFileSize(String fileName, long size)
163                    throws FileSizeException {
164    
165                    long maxSize = PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE);
166    
167                    if ((maxSize > 0) && (size > maxSize)) {
168                            throw new FileSizeException(fileName);
169                    }
170            }
171    
172            @Override
173            public void validateSourceFileExtension(
174                            String fileExtension, String sourceFileName)
175                    throws SourceFileNameException {
176    
177                    String sourceFileExtension = FileUtil.getExtension(sourceFileName);
178    
179                    if (Validator.isNotNull(sourceFileName) &&
180                            PropsValues.DL_FILE_EXTENSIONS_STRICT_CHECK &&
181                            !fileExtension.equals(sourceFileExtension)) {
182    
183                            throw new SourceFileNameException(sourceFileExtension);
184                    }
185            }
186    
187            @Override
188            public void validateVersionLabel(String versionLabel)
189                    throws InvalidFileVersionException {
190    
191                    if (Validator.isNull(versionLabel)) {
192                            return;
193                    }
194    
195                    if (!DLUtil.isValidVersion(versionLabel)) {
196                            throw new InvalidFileVersionException();
197                    }
198            }
199    
200            private static final String _UNICODE_PREFIX = "\\u";
201    
202    }