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                            ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
129                             (bytes.length >
130                                            PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
131    
132                            throw new FileSizeException(fileName);
133                    }
134            }
135    
136            @Override
137            public void validateFileSize(String fileName, File file)
138                    throws FileSizeException {
139    
140                    if ((file == null) ||
141                            ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
142                             (file.length() >
143                                            PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
144    
145                            throw new FileSizeException(fileName);
146                    }
147            }
148    
149            @Override
150            public void validateFileSize(String fileName, InputStream is)
151                    throws FileSizeException {
152    
153                    try {
154                            if ((is == null) ||
155                                    ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
156                                     (is.available() >
157                                                    PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
158    
159                                    throw new FileSizeException(fileName);
160                            }
161                    }
162                    catch (IOException ioe) {
163                            throw new FileSizeException(ioe.getMessage());
164                    }
165            }
166    
167            @Override
168            public void validateSourceFileExtension(
169                            String fileExtension, String sourceFileName)
170                    throws SourceFileNameException {
171    
172                    String sourceFileExtension = FileUtil.getExtension(sourceFileName);
173    
174                    if (Validator.isNotNull(sourceFileName) &&
175                            PropsValues.DL_FILE_EXTENSIONS_STRICT_CHECK &&
176                            !fileExtension.equals(sourceFileExtension)) {
177    
178                            throw new SourceFileNameException(sourceFileExtension);
179                    }
180            }
181    
182            @Override
183            public void validateVersionLabel(String versionLabel)
184                    throws InvalidFileVersionException {
185    
186                    if (Validator.isNull(versionLabel)) {
187                            return;
188                    }
189    
190                    if (!DLUtil.isValidVersion(versionLabel)) {
191                            throw new InvalidFileVersionException();
192                    }
193            }
194    
195            private static final String _UNICODE_PREFIX = "\\u";
196    
197    }