001    /**
002     * Copyright (c) 2000-2013 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.store;
016    
017    import com.liferay.portal.kernel.bean.BeanReference;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.io.ByteArrayFileInputStream;
021    import com.liferay.portal.kernel.search.BooleanClauseOccur;
022    import com.liferay.portal.kernel.search.BooleanQuery;
023    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
024    import com.liferay.portal.kernel.search.Field;
025    import com.liferay.portal.kernel.search.Hits;
026    import com.liferay.portal.kernel.search.Indexer;
027    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
028    import com.liferay.portal.kernel.search.SearchContext;
029    import com.liferay.portal.kernel.search.SearchEngineUtil;
030    import com.liferay.portal.kernel.search.TermQuery;
031    import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
032    import com.liferay.portal.kernel.util.ArrayUtil;
033    import com.liferay.portal.kernel.util.FileUtil;
034    import com.liferay.portal.kernel.util.PropsKeys;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.kernel.util.UnicodeFormatter;
038    import com.liferay.portal.kernel.util.Validator;
039    import com.liferay.portal.model.Group;
040    import com.liferay.portal.portletfilerepository.PortletFileRepositoryThreadLocal;
041    import com.liferay.portal.security.permission.ActionKeys;
042    import com.liferay.portal.security.permission.PermissionChecker;
043    import com.liferay.portal.security.permission.PermissionThreadLocal;
044    import com.liferay.portal.service.GroupLocalService;
045    import com.liferay.portal.util.PrefsPropsUtil;
046    import com.liferay.portal.util.PropsValues;
047    import com.liferay.portlet.documentlibrary.DirectoryNameException;
048    import com.liferay.portlet.documentlibrary.FileExtensionException;
049    import com.liferay.portlet.documentlibrary.FileNameException;
050    import com.liferay.portlet.documentlibrary.FileSizeException;
051    import com.liferay.portlet.documentlibrary.FolderNameException;
052    import com.liferay.portlet.documentlibrary.InvalidFileVersionException;
053    import com.liferay.portlet.documentlibrary.SourceFileNameException;
054    import com.liferay.portlet.documentlibrary.antivirus.AntivirusScannerUtil;
055    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
056    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
057    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
058    import com.liferay.portlet.documentlibrary.util.DLUtil;
059    
060    import java.io.File;
061    import java.io.IOException;
062    import java.io.InputStream;
063    
064    /**
065     * @author Brian Wing Shun Chan
066     * @author Alexander Chow
067     * @author Edward Han
068     */
069    public class DLStoreImpl implements DLStore {
070    
071            @Override
072            public void addDirectory(long companyId, long repositoryId, String dirName)
073                    throws PortalException, SystemException {
074    
075                    if (!isValidName(dirName) || dirName.equals("/")) {
076                            throw new DirectoryNameException(dirName);
077                    }
078    
079                    store.addDirectory(companyId, repositoryId, dirName);
080            }
081    
082            @Override
083            public void addFile(
084                            long companyId, long repositoryId, String fileName,
085                            boolean validateFileExtension, byte[] bytes)
086                    throws PortalException, SystemException {
087    
088                    validate(fileName, validateFileExtension, bytes);
089    
090                    if (PropsValues.DL_STORE_ANTIVIRUS_ENABLED) {
091                            AntivirusScannerUtil.scan(bytes);
092                    }
093    
094                    store.addFile(companyId, repositoryId, fileName, bytes);
095            }
096    
097            @Override
098            public void addFile(
099                            long companyId, long repositoryId, String fileName,
100                            boolean validateFileExtension, File file)
101                    throws PortalException, SystemException {
102    
103                    validate(fileName, validateFileExtension, file);
104    
105                    if (PropsValues.DL_STORE_ANTIVIRUS_ENABLED) {
106                            AntivirusScannerUtil.scan(file);
107                    }
108    
109                    store.addFile(companyId, repositoryId, fileName, file);
110            }
111    
112            @Override
113            public void addFile(
114                            long companyId, long repositoryId, String fileName,
115                            boolean validateFileExtension, InputStream is)
116                    throws PortalException, SystemException {
117    
118                    if (is instanceof ByteArrayFileInputStream) {
119                            ByteArrayFileInputStream byteArrayFileInputStream =
120                                    (ByteArrayFileInputStream)is;
121    
122                            File file = byteArrayFileInputStream.getFile();
123    
124                            addFile(
125                                    companyId, repositoryId, fileName, validateFileExtension, file);
126    
127                            return;
128                    }
129    
130                    validate(fileName, validateFileExtension, is);
131    
132                    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED ||
133                            !AntivirusScannerUtil.isActive()) {
134    
135                            store.addFile(companyId, repositoryId, fileName, is);
136                    }
137                    else {
138                            File tempFile = null;
139    
140                            try {
141                                    if (is.markSupported()) {
142                                            is.mark(is.available() + 1);
143    
144                                            AntivirusScannerUtil.scan(is);
145    
146                                            is.reset();
147    
148                                            store.addFile(companyId, repositoryId, fileName, is);
149                                    }
150                                    else {
151                                            tempFile = FileUtil.createTempFile();
152    
153                                            FileUtil.write(tempFile, is);
154    
155                                            AntivirusScannerUtil.scan(tempFile);
156    
157                                            store.addFile(companyId, repositoryId, fileName, tempFile);
158                                    }
159                            }
160                            catch (IOException ioe) {
161                                    throw new SystemException(
162                                            "Unable to scan file " + fileName, ioe);
163                            }
164                            finally {
165                                    if (tempFile != null) {
166                                            tempFile.delete();
167                                    }
168                            }
169                    }
170            }
171    
172            @Override
173            public void addFile(
174                            long companyId, long repositoryId, String fileName, byte[] bytes)
175                    throws PortalException, SystemException {
176    
177                    addFile(companyId, repositoryId, fileName, true, bytes);
178            }
179    
180            @Override
181            public void addFile(
182                            long companyId, long repositoryId, String fileName, File file)
183                    throws PortalException, SystemException {
184    
185                    addFile(companyId, repositoryId, fileName, true, file);
186            }
187    
188            @Override
189            public void addFile(
190                            long companyId, long repositoryId, String fileName, InputStream is)
191                    throws PortalException, SystemException {
192    
193                    addFile(companyId, repositoryId, fileName, true, is);
194            }
195    
196            @Override
197            public void checkRoot(long companyId) throws SystemException {
198                    store.checkRoot(companyId);
199            }
200    
201            @Override
202            public void copyFileVersion(
203                            long companyId, long repositoryId, String fileName,
204                            String fromVersionLabel, String toVersionLabel)
205                    throws PortalException, SystemException {
206    
207                    store.copyFileVersion(
208                            companyId, repositoryId, fileName, fromVersionLabel,
209                            toVersionLabel);
210            }
211    
212            @Override
213            public void deleteDirectory(
214                            long companyId, long repositoryId, String dirName)
215                    throws PortalException, SystemException {
216    
217                    store.deleteDirectory(companyId, repositoryId, dirName);
218            }
219    
220            @Override
221            public void deleteFile(long companyId, long repositoryId, String fileName)
222                    throws PortalException, SystemException {
223    
224                    validate(fileName, false);
225    
226                    store.deleteFile(companyId, repositoryId, fileName);
227            }
228    
229            @Override
230            public void deleteFile(
231                            long companyId, long repositoryId, String fileName,
232                            String versionLabel)
233                    throws PortalException, SystemException {
234    
235                    validate(fileName, false, versionLabel);
236    
237                    store.deleteFile(companyId, repositoryId, fileName, versionLabel);
238            }
239    
240            @Override
241            public File getFile(long companyId, long repositoryId, String fileName)
242                    throws PortalException, SystemException {
243    
244                    validate(fileName, false);
245    
246                    return store.getFile(companyId, repositoryId, fileName);
247            }
248    
249            @Override
250            public File getFile(
251                            long companyId, long repositoryId, String fileName,
252                            String versionLabel)
253                    throws PortalException, SystemException {
254    
255                    validate(fileName, false, versionLabel);
256    
257                    return store.getFile(companyId, repositoryId, fileName, versionLabel);
258            }
259    
260            @Override
261            public byte[] getFileAsBytes(
262                            long companyId, long repositoryId, String fileName)
263                    throws PortalException, SystemException {
264    
265                    validate(fileName, false);
266    
267                    return store.getFileAsBytes(companyId, repositoryId, fileName);
268            }
269    
270            @Override
271            public byte[] getFileAsBytes(
272                            long companyId, long repositoryId, String fileName,
273                            String versionLabel)
274                    throws PortalException, SystemException {
275    
276                    validate(fileName, false, versionLabel);
277    
278                    return store.getFileAsBytes(
279                            companyId, repositoryId, fileName, versionLabel);
280            }
281    
282            @Override
283            public InputStream getFileAsStream(
284                            long companyId, long repositoryId, String fileName)
285                    throws PortalException, SystemException {
286    
287                    validate(fileName, false);
288    
289                    return store.getFileAsStream(companyId, repositoryId, fileName);
290            }
291    
292            @Override
293            public InputStream getFileAsStream(
294                            long companyId, long repositoryId, String fileName,
295                            String versionLabel)
296                    throws PortalException, SystemException {
297    
298                    validate(fileName, false, versionLabel);
299    
300                    return store.getFileAsStream(
301                            companyId, repositoryId, fileName, versionLabel);
302            }
303    
304            @Override
305            public String[] getFileNames(
306                            long companyId, long repositoryId, String dirName)
307                    throws PortalException, SystemException {
308    
309                    if (!isValidName(dirName)) {
310                            throw new DirectoryNameException(dirName);
311                    }
312    
313                    return store.getFileNames(companyId, repositoryId, dirName);
314            }
315    
316            @Override
317            public long getFileSize(long companyId, long repositoryId, String fileName)
318                    throws PortalException, SystemException {
319    
320                    validate(fileName, false);
321    
322                    return store.getFileSize(companyId, repositoryId, fileName);
323            }
324    
325            @Override
326            public boolean hasDirectory(
327                            long companyId, long repositoryId, String dirName)
328                    throws PortalException, SystemException {
329    
330                    if (!isValidName(dirName)) {
331                            throw new DirectoryNameException(dirName);
332                    }
333    
334                    return store.hasDirectory(companyId, repositoryId, dirName);
335            }
336    
337            @Override
338            public boolean hasFile(long companyId, long repositoryId, String fileName)
339                    throws PortalException, SystemException {
340    
341                    validate(fileName, false);
342    
343                    return store.hasFile(companyId, repositoryId, fileName);
344            }
345    
346            @Override
347            public boolean hasFile(
348                            long companyId, long repositoryId, String fileName,
349                            String versionLabel)
350                    throws PortalException, SystemException {
351    
352                    validate(fileName, false, versionLabel);
353    
354                    return store.hasFile(companyId, repositoryId, fileName, versionLabel);
355            }
356    
357            @Override
358            public boolean isValidName(String name) {
359                    if (Validator.isNull(name)) {
360                            return false;
361                    }
362    
363                    for (String blacklistChar : PropsValues.DL_CHAR_BLACKLIST) {
364                            if (name.contains(blacklistChar)) {
365                                    return false;
366                            }
367                    }
368    
369                    for (String blacklistLastChar : PropsValues.DL_CHAR_LAST_BLACKLIST) {
370                            if (blacklistLastChar.startsWith(_UNICODE_PREFIX)) {
371                                    blacklistLastChar = UnicodeFormatter.parseString(
372                                            blacklistLastChar);
373                            }
374    
375                            if (name.endsWith(blacklistLastChar)) {
376                                    return false;
377                            }
378                    }
379    
380                    String nameWithoutExtension = name;
381    
382                    if (name.contains(StringPool.PERIOD)) {
383                            int index = name.lastIndexOf(StringPool.PERIOD);
384    
385                            nameWithoutExtension = name.substring(0, index);
386                    }
387    
388                    for (String blacklistName : PropsValues.DL_NAME_BLACKLIST) {
389                            if (StringUtil.equalsIgnoreCase(
390                                            nameWithoutExtension, blacklistName)) {
391    
392                                    return false;
393                            }
394                    }
395    
396                    return true;
397            }
398    
399            @Override
400            public void move(String srcDir, String destDir) throws SystemException {
401                    store.move(srcDir, destDir);
402            }
403    
404            public Hits search(
405                            long companyId, long userId, String portletId, long groupId,
406                            long[] repositoryIds, String keywords, int start, int end)
407                    throws SystemException {
408    
409                    try {
410                            SearchContext searchContext = new SearchContext();
411    
412                            searchContext.setCompanyId(companyId);
413                            searchContext.setEnd(end);
414                            searchContext.setEntryClassNames(
415                                    new String[] {DLFileEntryConstants.getClassName()});
416                            searchContext.setGroupIds(new long[] {groupId});
417    
418                            Indexer indexer = IndexerRegistryUtil.getIndexer(
419                                    DLFileEntryConstants.getClassName());
420    
421                            searchContext.setSearchEngineId(indexer.getSearchEngineId());
422    
423                            searchContext.setStart(start);
424                            searchContext.setUserId(userId);
425    
426                            BooleanQuery contextQuery = BooleanQueryFactoryUtil.create(
427                                    searchContext);
428    
429                            contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
430    
431                            if (groupId > 0) {
432                                    Group group = groupLocalService.getGroup(groupId);
433    
434                                    if (group.isLayout()) {
435                                            contextQuery.addRequiredTerm(Field.SCOPE_GROUP_ID, groupId);
436    
437                                            groupId = group.getParentGroupId();
438                                    }
439    
440                                    contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
441                            }
442    
443                            if (ArrayUtil.isNotEmpty(repositoryIds)) {
444                                    BooleanQuery repositoryIdsQuery =
445                                            BooleanQueryFactoryUtil.create(searchContext);
446    
447                                    for (long repositoryId : repositoryIds) {
448                                            try {
449                                                    if (userId > 0) {
450                                                            PermissionChecker permissionChecker =
451                                                                    PermissionThreadLocal.getPermissionChecker();
452    
453                                                            DLFolderPermission.check(
454                                                                    permissionChecker, groupId, repositoryId,
455                                                                    ActionKeys.VIEW);
456                                                    }
457    
458                                                    if (repositoryId ==
459                                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
460    
461                                                            repositoryId = groupId;
462                                                    }
463    
464                                                    TermQuery termQuery = TermQueryFactoryUtil.create(
465                                                            searchContext, "repositoryId", repositoryId);
466    
467                                                    repositoryIdsQuery.add(
468                                                            termQuery, BooleanClauseOccur.SHOULD);
469                                            }
470                                            catch (Exception e) {
471                                            }
472                                    }
473    
474                                    contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
475                            }
476    
477                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
478                                    searchContext);
479    
480                            searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);
481    
482                            BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(
483                                    searchContext);
484    
485                            fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
486    
487                            if (searchQuery.clauses().size() > 0) {
488                                    fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
489                            }
490    
491                            return SearchEngineUtil.search(searchContext, fullQuery);
492                    }
493                    catch (Exception e) {
494                            throw new SystemException(e);
495                    }
496            }
497    
498            @Override
499            public void updateFile(
500                            long companyId, long repositoryId, long newRepositoryId,
501                            String fileName)
502                    throws PortalException, SystemException {
503    
504                    store.updateFile(companyId, repositoryId, newRepositoryId, fileName);
505            }
506    
507            @Override
508            public void updateFile(
509                            long companyId, long repositoryId, String fileName,
510                            String newFileName)
511                    throws PortalException, SystemException {
512    
513                    store.updateFile(companyId, repositoryId, fileName, newFileName);
514            }
515    
516            @Override
517            public void updateFile(
518                            long companyId, long repositoryId, String fileName,
519                            String fileExtension, boolean validateFileExtension,
520                            String versionLabel, String sourceFileName, File file)
521                    throws PortalException, SystemException {
522    
523                    validate(
524                            fileName, fileExtension, sourceFileName, validateFileExtension,
525                            file, versionLabel);
526    
527                    if (PropsValues.DL_STORE_ANTIVIRUS_ENABLED) {
528                            AntivirusScannerUtil.scan(file);
529                    }
530    
531                    store.updateFile(companyId, repositoryId, fileName, versionLabel, file);
532            }
533    
534            @Override
535            public void updateFile(
536                            long companyId, long repositoryId, String fileName,
537                            String fileExtension, boolean validateFileExtension,
538                            String versionLabel, String sourceFileName, InputStream is)
539                    throws PortalException, SystemException {
540    
541                    if (is instanceof ByteArrayFileInputStream) {
542                            ByteArrayFileInputStream byteArrayFileInputStream =
543                                    (ByteArrayFileInputStream)is;
544    
545                            File file = byteArrayFileInputStream.getFile();
546    
547                            updateFile(
548                                    companyId, repositoryId, fileName, fileExtension,
549                                    validateFileExtension, versionLabel, sourceFileName, file);
550    
551                            return;
552                    }
553    
554                    validate(
555                            fileName, fileExtension, sourceFileName, validateFileExtension, is,
556                            versionLabel);
557    
558                    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED ||
559                            !AntivirusScannerUtil.isActive()) {
560    
561                            store.updateFile(
562                                    companyId, repositoryId, fileName, versionLabel, is);
563                    }
564                    else {
565                            File tempFile = null;
566    
567                            try {
568                                    if (is.markSupported()) {
569                                            is.mark(is.available() + 1);
570    
571                                            AntivirusScannerUtil.scan(is);
572    
573                                            is.reset();
574    
575                                            store.updateFile(
576                                                    companyId, repositoryId, fileName, versionLabel, is);
577                                    }
578                                    else {
579                                            tempFile = FileUtil.createTempFile();
580    
581                                            FileUtil.write(tempFile, is);
582    
583                                            AntivirusScannerUtil.scan(tempFile);
584    
585                                            store.updateFile(
586                                                    companyId, repositoryId, fileName, versionLabel,
587                                                    tempFile);
588                                    }
589                            }
590                            catch (IOException ioe) {
591                                    throw new SystemException(
592                                            "Unable to scan file " + fileName, ioe);
593                            }
594                            finally {
595                                    if (tempFile != null) {
596                                            tempFile.delete();
597                                    }
598                            }
599                    }
600            }
601    
602            @Override
603            public void updateFileVersion(
604                            long companyId, long repositoryId, String fileName,
605                            String fromVersionLabel, String toVersionLabel)
606                    throws PortalException, SystemException {
607    
608                    store.updateFileVersion(
609                            companyId, repositoryId, fileName, fromVersionLabel,
610                            toVersionLabel);
611            }
612    
613            @Override
614            public void validate(String fileName, boolean validateFileExtension)
615                    throws PortalException, SystemException {
616    
617                    if (!isValidName(fileName)) {
618                            throw new FileNameException(fileName);
619                    }
620    
621                    if (!validateFileExtension) {
622                            return;
623                    }
624    
625                    Thread currentThread = Thread.currentThread();
626    
627                    StackTraceElement[] stackTraceElements = currentThread.getStackTrace();
628    
629                    for (StackTraceElement stackTraceElement : stackTraceElements) {
630                            String className = stackTraceElement.getClassName();
631    
632                            if (className.startsWith("com.liferay.") &&
633                                    (className.endsWith("EditLayoutSetAction") ||
634                                     className.endsWith("EditUserPortraitAction") ||
635                                     className.endsWith("LogoAction") ||
636                                     className.endsWith("PortletFileRepositoryImpl")||
637                                     className.endsWith("TempFileUtil"))) {
638    
639                                    return;
640                            }
641                    }
642    
643                    boolean validFileExtension = false;
644    
645                    String[] fileExtensions = PrefsPropsUtil.getStringArray(
646                            PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
647    
648                    for (String fileExtension : fileExtensions) {
649                            if (StringPool.STAR.equals(fileExtension) ||
650                                    StringUtil.endsWith(fileName, fileExtension)) {
651    
652                                    validFileExtension = true;
653    
654                                    break;
655                            }
656                    }
657    
658                    if (!validFileExtension) {
659                            throw new FileExtensionException(fileName);
660                    }
661            }
662    
663            @Override
664            public void validate(
665                            String fileName, boolean validateFileExtension, byte[] bytes)
666                    throws PortalException, SystemException {
667    
668                    validate(fileName, validateFileExtension);
669    
670                    if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
671                            (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
672                            ((bytes == null) ||
673                             (bytes.length >
674                                     PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
675    
676                            throw new FileSizeException(fileName);
677                    }
678            }
679    
680            @Override
681            public void validate(
682                            String fileName, boolean validateFileExtension, File file)
683                    throws PortalException, SystemException {
684    
685                    validate(fileName, validateFileExtension);
686    
687                    if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
688                            (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
689                            ((file == null) ||
690                             (file.length() >
691                                    PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
692    
693                            throw new FileSizeException(fileName);
694                    }
695            }
696    
697            @Override
698            public void validate(
699                            String fileName, boolean validateFileExtension, InputStream is)
700                    throws PortalException, SystemException {
701    
702                    validate(fileName, validateFileExtension);
703    
704                    // LEP-4851
705    
706                    try {
707                            if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
708                                    ((is == null) ||
709                                     ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
710                                      (is.available() >
711                                            PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE))))) {
712    
713                                    throw new FileSizeException(fileName);
714                            }
715                    }
716                    catch (IOException ioe) {
717                            throw new FileSizeException(ioe.getMessage());
718                    }
719            }
720    
721            @Override
722            public void validate(
723                            String fileName, String fileExtension, String sourceFileName,
724                            boolean validateFileExtension, File file)
725                    throws PortalException, SystemException {
726    
727                    validate(
728                            fileName, fileExtension, sourceFileName, validateFileExtension,
729                            StringPool.BLANK);
730    
731                    if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
732                            (file != null) &&
733                            (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
734                            (file.length() >
735                                    PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE))) {
736    
737                            throw new FileSizeException(fileName);
738                    }
739            }
740    
741            @Override
742            public void validate(
743                            String fileName, String fileExtension, String sourceFileName,
744                            boolean validateFileExtension, InputStream is)
745                    throws PortalException, SystemException {
746    
747                    validate(
748                            fileName, fileExtension, sourceFileName, validateFileExtension,
749                            StringPool.BLANK);
750    
751                    try {
752                            if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
753                                    (is != null) &&
754                                    (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
755                                    (is.available() >
756                                            PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE))) {
757    
758                                    throw new FileSizeException(fileName);
759                            }
760                    }
761                    catch (IOException ioe) {
762                            throw new FileSizeException(ioe.getMessage());
763                    }
764            }
765    
766            @Override
767            public void validateDirectoryName(String directoryName)
768                    throws PortalException {
769    
770                    if (!isValidName(directoryName)) {
771                            throw new FolderNameException(directoryName);
772                    }
773            }
774    
775            protected void isValidVersion(String versionLabel) throws PortalException {
776                    if (Validator.isNull(versionLabel)) {
777                            return;
778                    }
779    
780                    if (!DLUtil.isValidVersion(versionLabel)) {
781                            throw new InvalidFileVersionException();
782                    }
783            }
784    
785            protected void validate(
786                            String fileName, boolean validateFileExtension, String versionLabel)
787                    throws PortalException, SystemException {
788    
789                    isValidVersion(versionLabel);
790    
791                    validate(fileName, validateFileExtension);
792            }
793    
794            protected void validate(
795                            String fileName, String fileExtension, String sourceFileName,
796                            boolean validateFileExtension, File file, String versionLabel)
797                    throws PortalException, SystemException {
798    
799                    isValidVersion(versionLabel);
800    
801                    validate(
802                            fileName, fileExtension, sourceFileName, validateFileExtension,
803                            file);
804            }
805    
806            protected void validate(
807                            String fileName, String fileExtension, String sourceFileName,
808                            boolean validateFileExtension, InputStream is, String versionLabel)
809                    throws PortalException, SystemException {
810    
811                    isValidVersion(versionLabel);
812    
813                    validate(
814                            fileName, fileExtension, sourceFileName, validateFileExtension, is);
815            }
816    
817            protected void validate(
818                            String fileName, String fileExtension, String sourceFileName,
819                            boolean validateFileExtension, String versionLabel)
820                    throws PortalException, SystemException {
821    
822                    String sourceFileExtension = FileUtil.getExtension(sourceFileName);
823    
824                    if (Validator.isNotNull(sourceFileName) &&
825                            PropsValues.DL_FILE_EXTENSIONS_STRICT_CHECK &&
826                            !fileExtension.equals(sourceFileExtension)) {
827    
828                            throw new SourceFileNameException(sourceFileExtension);
829                    }
830    
831                    validate(fileName, validateFileExtension, versionLabel);
832            }
833    
834            @BeanReference(type = GroupLocalService.class)
835            protected GroupLocalService groupLocalService;
836    
837            @BeanReference(type = Store.class)
838            protected Store store;
839    
840            private static final String[] _KEYWORDS_FIELDS = {
841                    Field.ASSET_TAG_NAMES, Field.CONTENT, Field.PROPERTIES
842            };
843    
844            private static final String _UNICODE_PREFIX = "\\u";
845    
846    }