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.exception.FileExtensionException;
026    import com.liferay.portlet.documentlibrary.exception.FileNameException;
027    import com.liferay.portlet.documentlibrary.exception.FileSizeException;
028    import com.liferay.portlet.documentlibrary.exception.FolderNameException;
029    import com.liferay.portlet.documentlibrary.exception.InvalidFileVersionException;
030    import com.liferay.portlet.documentlibrary.exception.SourceFileNameException;
031    import com.liferay.portlet.documentlibrary.webdav.DLWebDAVUtil;
032    
033    import java.io.File;
034    import java.io.IOException;
035    import java.io.InputStream;
036    
037    /**
038     * @author Adolfo P??rez
039     */
040    public final class DLValidatorImpl implements DLValidator {
041    
042            @Override
043            public String fixName(String name) {
044                    if (Validator.isNull(name)) {
045                            return StringPool.UNDERLINE;
046                    }
047    
048                    for (String blacklistChar : PropsValues.DL_CHAR_BLACKLIST) {
049                            name = StringUtil.replace(
050                                    name, blacklistChar, StringPool.UNDERLINE);
051                    }
052    
053                    name = replaceDLCharLastBlacklist(name);
054    
055                    return replaceDLNameBlacklist(name);
056            }
057    
058            @Override
059            public boolean isValidName(String name) {
060                    if (Validator.isNull(name)) {
061                            return false;
062                    }
063    
064                    for (String blacklistChar : PropsValues.DL_CHAR_BLACKLIST) {
065                            if (name.contains(blacklistChar)) {
066                                    return false;
067                            }
068                    }
069    
070                    for (String blacklistLastChar : PropsValues.DL_CHAR_LAST_BLACKLIST) {
071                            if (blacklistLastChar.startsWith(UnicodeFormatter.UNICODE_PREFIX)) {
072                                    blacklistLastChar = UnicodeFormatter.parseString(
073                                            blacklistLastChar);
074                            }
075    
076                            if (name.endsWith(blacklistLastChar)) {
077                                    return false;
078                            }
079                    }
080    
081                    String nameWithoutExtension = FileUtil.stripExtension(name);
082    
083                    for (String blacklistName : PropsValues.DL_NAME_BLACKLIST) {
084                            if (StringUtil.equalsIgnoreCase(
085                                            nameWithoutExtension, blacklistName)) {
086    
087                                    return false;
088                            }
089                    }
090    
091                    return true;
092            }
093    
094            @Override
095            public void validateDirectoryName(String directoryName)
096                    throws FolderNameException {
097    
098                    if (!isValidName(directoryName)) {
099                            throw new FolderNameException(directoryName);
100                    }
101            }
102    
103            @Override
104            public void validateFileExtension(String fileName)
105                    throws FileExtensionException {
106    
107                    boolean validFileExtension = false;
108    
109                    String[] fileExtensions = PrefsPropsUtil.getStringArray(
110                            PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
111    
112                    for (String fileExtension : fileExtensions) {
113                            if (StringPool.STAR.equals(fileExtension) ||
114                                    StringUtil.endsWith(fileName, fileExtension)) {
115    
116                                    validFileExtension = true;
117    
118                                    break;
119                            }
120                    }
121    
122                    if (!validFileExtension) {
123                            throw new FileExtensionException(fileName);
124                    }
125            }
126    
127            @Override
128            public void validateFileName(String fileName) throws FileNameException {
129                    if (!isValidName(fileName)) {
130                            throw new FileNameException(fileName);
131                    }
132    
133                    if (!DLWebDAVUtil.isRepresentableTitle(fileName)) {
134                            throw new FileNameException(
135                                    "Unrepresentable WebDAV title for file name " + fileName);
136                    }
137            }
138    
139            @Override
140            public void validateFileSize(String fileName, byte[] bytes)
141                    throws FileSizeException {
142    
143                    if (bytes == null) {
144                            throw new FileSizeException(fileName);
145                    }
146    
147                    validateFileSize(fileName, bytes.length);
148            }
149    
150            @Override
151            public void validateFileSize(String fileName, File file)
152                    throws FileSizeException {
153    
154                    if (file == null) {
155                            throw new FileSizeException(fileName);
156                    }
157    
158                    validateFileSize(fileName, file.length());
159            }
160    
161            @Override
162            public void validateFileSize(String fileName, InputStream is)
163                    throws FileSizeException {
164    
165                    try {
166                            if (is == null) {
167                                    throw new FileSizeException(fileName);
168                            }
169    
170                            validateFileSize(fileName, is.available());
171                    }
172                    catch (IOException ioe) {
173                            new FileSizeException(ioe);
174                    }
175            }
176    
177            @Override
178            public void validateFileSize(String fileName, long size)
179                    throws FileSizeException {
180    
181                    long maxSize = PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE);
182    
183                    if ((maxSize > 0) && (size > maxSize)) {
184                            throw new FileSizeException(fileName);
185                    }
186            }
187    
188            @Override
189            public void validateSourceFileExtension(
190                            String fileExtension, String sourceFileName)
191                    throws SourceFileNameException {
192    
193                    String sourceFileExtension = FileUtil.getExtension(sourceFileName);
194    
195                    if (Validator.isNotNull(sourceFileName) &&
196                            PropsValues.DL_FILE_EXTENSIONS_STRICT_CHECK &&
197                            !fileExtension.equals(sourceFileExtension)) {
198    
199                            throw new SourceFileNameException(sourceFileExtension);
200                    }
201            }
202    
203            @Override
204            public void validateVersionLabel(String versionLabel)
205                    throws InvalidFileVersionException {
206    
207                    if (Validator.isNull(versionLabel)) {
208                            return;
209                    }
210    
211                    if (!DLUtil.isValidVersion(versionLabel)) {
212                            throw new InvalidFileVersionException(
213                                    "Invalid version label " + versionLabel);
214                    }
215            }
216    
217            protected String replaceDLCharLastBlacklist(String title) {
218                    String previousTitle = null;
219    
220                    while (!title.equals(previousTitle)) {
221                            previousTitle = title;
222    
223                            for (String blacklistLastChar :
224                                            PropsValues.DL_CHAR_LAST_BLACKLIST) {
225    
226                                    if (blacklistLastChar.startsWith(
227                                                    UnicodeFormatter.UNICODE_PREFIX)) {
228    
229                                            blacklistLastChar = UnicodeFormatter.parseString(
230                                                    blacklistLastChar);
231                                    }
232    
233                                    if (title.endsWith(blacklistLastChar)) {
234                                            title = StringUtil.replaceLast(
235                                                    title, blacklistLastChar, StringPool.BLANK);
236                                    }
237                            }
238                    }
239    
240                    return title;
241            }
242    
243            protected String replaceDLNameBlacklist(String title) {
244                    String extension = FileUtil.getExtension(title);
245                    String nameWithoutExtension = FileUtil.stripExtension(title);
246    
247                    for (String blacklistName : PropsValues.DL_NAME_BLACKLIST) {
248                            if (StringUtil.equalsIgnoreCase(
249                                            nameWithoutExtension, blacklistName)) {
250    
251                                    if (Validator.isNull(extension)) {
252                                            return nameWithoutExtension + StringPool.UNDERLINE;
253                                    }
254    
255                                    return nameWithoutExtension + StringPool.UNDERLINE +
256                                            StringPool.PERIOD + extension;
257                            }
258                    }
259    
260                    return title;
261            }
262    
263    }