001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public void setStore(Store store) {
499                    this.store = store;
500            }
501    
502            @Override
503            public void updateFile(
504                            long companyId, long repositoryId, long newRepositoryId,
505                            String fileName)
506                    throws PortalException, SystemException {
507    
508                    store.updateFile(companyId, repositoryId, newRepositoryId, fileName);
509            }
510    
511            @Override
512            public void updateFile(
513                            long companyId, long repositoryId, String fileName,
514                            String newFileName)
515                    throws PortalException, SystemException {
516    
517                    store.updateFile(companyId, repositoryId, fileName, newFileName);
518            }
519    
520            @Override
521            public void updateFile(
522                            long companyId, long repositoryId, String fileName,
523                            String fileExtension, boolean validateFileExtension,
524                            String versionLabel, String sourceFileName, File file)
525                    throws PortalException, SystemException {
526    
527                    validate(
528                            fileName, fileExtension, sourceFileName, validateFileExtension,
529                            file, versionLabel);
530    
531                    if (PropsValues.DL_STORE_ANTIVIRUS_ENABLED) {
532                            AntivirusScannerUtil.scan(file);
533                    }
534    
535                    store.updateFile(companyId, repositoryId, fileName, versionLabel, file);
536            }
537    
538            @Override
539            public void updateFile(
540                            long companyId, long repositoryId, String fileName,
541                            String fileExtension, boolean validateFileExtension,
542                            String versionLabel, String sourceFileName, InputStream is)
543                    throws PortalException, SystemException {
544    
545                    if (is instanceof ByteArrayFileInputStream) {
546                            ByteArrayFileInputStream byteArrayFileInputStream =
547                                    (ByteArrayFileInputStream)is;
548    
549                            File file = byteArrayFileInputStream.getFile();
550    
551                            updateFile(
552                                    companyId, repositoryId, fileName, fileExtension,
553                                    validateFileExtension, versionLabel, sourceFileName, file);
554    
555                            return;
556                    }
557    
558                    validate(
559                            fileName, fileExtension, sourceFileName, validateFileExtension, is,
560                            versionLabel);
561    
562                    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED ||
563                            !AntivirusScannerUtil.isActive()) {
564    
565                            store.updateFile(
566                                    companyId, repositoryId, fileName, versionLabel, is);
567                    }
568                    else {
569                            File tempFile = null;
570    
571                            try {
572                                    if (is.markSupported()) {
573                                            is.mark(is.available() + 1);
574    
575                                            AntivirusScannerUtil.scan(is);
576    
577                                            is.reset();
578    
579                                            store.updateFile(
580                                                    companyId, repositoryId, fileName, versionLabel, is);
581                                    }
582                                    else {
583                                            tempFile = FileUtil.createTempFile();
584    
585                                            FileUtil.write(tempFile, is);
586    
587                                            AntivirusScannerUtil.scan(tempFile);
588    
589                                            store.updateFile(
590                                                    companyId, repositoryId, fileName, versionLabel,
591                                                    tempFile);
592                                    }
593                            }
594                            catch (IOException ioe) {
595                                    throw new SystemException(
596                                            "Unable to scan file " + fileName, ioe);
597                            }
598                            finally {
599                                    if (tempFile != null) {
600                                            tempFile.delete();
601                                    }
602                            }
603                    }
604            }
605    
606            @Override
607            public void updateFileVersion(
608                            long companyId, long repositoryId, String fileName,
609                            String fromVersionLabel, String toVersionLabel)
610                    throws PortalException, SystemException {
611    
612                    store.updateFileVersion(
613                            companyId, repositoryId, fileName, fromVersionLabel,
614                            toVersionLabel);
615            }
616    
617            @Override
618            public void validate(String fileName, boolean validateFileExtension)
619                    throws PortalException, SystemException {
620    
621                    if (!isValidName(fileName)) {
622                            throw new FileNameException(fileName);
623                    }
624    
625                    if (!validateFileExtension) {
626                            return;
627                    }
628    
629                    Thread currentThread = Thread.currentThread();
630    
631                    StackTraceElement[] stackTraceElements = currentThread.getStackTrace();
632    
633                    for (StackTraceElement stackTraceElement : stackTraceElements) {
634                            String className = stackTraceElement.getClassName();
635    
636                            if (className.startsWith("com.liferay.") &&
637                                    (className.endsWith("EditLayoutSetAction") ||
638                                     className.endsWith("EditUserPortraitAction") ||
639                                     className.endsWith("LogoAction") ||
640                                     className.endsWith("PortletFileRepositoryImpl")||
641                                     className.endsWith("TempFileUtil"))) {
642    
643                                    return;
644                            }
645                    }
646    
647                    boolean validFileExtension = false;
648    
649                    String[] fileExtensions = PrefsPropsUtil.getStringArray(
650                            PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
651    
652                    for (String fileExtension : fileExtensions) {
653                            if (StringPool.STAR.equals(fileExtension) ||
654                                    StringUtil.endsWith(fileName, fileExtension)) {
655    
656                                    validFileExtension = true;
657    
658                                    break;
659                            }
660                    }
661    
662                    if (!validFileExtension) {
663                            throw new FileExtensionException(fileName);
664                    }
665            }
666    
667            @Override
668            public void validate(
669                            String fileName, boolean validateFileExtension, byte[] bytes)
670                    throws PortalException, SystemException {
671    
672                    validate(fileName, validateFileExtension);
673    
674                    if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
675                            (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
676                            ((bytes == null) ||
677                             (bytes.length >
678                                     PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
679    
680                            throw new FileSizeException(fileName);
681                    }
682            }
683    
684            @Override
685            public void validate(
686                            String fileName, boolean validateFileExtension, File file)
687                    throws PortalException, SystemException {
688    
689                    validate(fileName, validateFileExtension);
690    
691                    if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
692                            (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
693                            ((file == null) ||
694                             (file.length() >
695                                    PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
696    
697                            throw new FileSizeException(fileName);
698                    }
699            }
700    
701            @Override
702            public void validate(
703                            String fileName, boolean validateFileExtension, InputStream is)
704                    throws PortalException, SystemException {
705    
706                    validate(fileName, validateFileExtension);
707    
708                    // LEP-4851
709    
710                    try {
711                            if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
712                                    ((is == null) ||
713                                     ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
714                                      (is.available() >
715                                            PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE))))) {
716    
717                                    throw new FileSizeException(fileName);
718                            }
719                    }
720                    catch (IOException ioe) {
721                            throw new FileSizeException(ioe.getMessage());
722                    }
723            }
724    
725            @Override
726            public void validate(
727                            String fileName, String fileExtension, String sourceFileName,
728                            boolean validateFileExtension, File file)
729                    throws PortalException, SystemException {
730    
731                    validate(
732                            fileName, fileExtension, sourceFileName, validateFileExtension,
733                            StringPool.BLANK);
734    
735                    if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
736                            (file != null) &&
737                            (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
738                            (file.length() >
739                                    PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE))) {
740    
741                            throw new FileSizeException(fileName);
742                    }
743            }
744    
745            @Override
746            public void validate(
747                            String fileName, String fileExtension, String sourceFileName,
748                            boolean validateFileExtension, InputStream is)
749                    throws PortalException, SystemException {
750    
751                    validate(
752                            fileName, fileExtension, sourceFileName, validateFileExtension,
753                            StringPool.BLANK);
754    
755                    try {
756                            if (PortletFileRepositoryThreadLocal.isFileMaxSizeCheckEnabled() &&
757                                    (is != null) &&
758                                    (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
759                                    (is.available() >
760                                            PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE))) {
761    
762                                    throw new FileSizeException(fileName);
763                            }
764                    }
765                    catch (IOException ioe) {
766                            throw new FileSizeException(ioe.getMessage());
767                    }
768            }
769    
770            @Override
771            public void validateDirectoryName(String directoryName)
772                    throws PortalException {
773    
774                    if (!isValidName(directoryName)) {
775                            throw new FolderNameException(directoryName);
776                    }
777            }
778    
779            protected void isValidVersion(String versionLabel) throws PortalException {
780                    if (Validator.isNull(versionLabel)) {
781                            return;
782                    }
783    
784                    if (!DLUtil.isValidVersion(versionLabel)) {
785                            throw new InvalidFileVersionException();
786                    }
787            }
788    
789            protected void validate(
790                            String fileName, boolean validateFileExtension, String versionLabel)
791                    throws PortalException, SystemException {
792    
793                    isValidVersion(versionLabel);
794    
795                    validate(fileName, validateFileExtension);
796            }
797    
798            protected void validate(
799                            String fileName, String fileExtension, String sourceFileName,
800                            boolean validateFileExtension, File file, String versionLabel)
801                    throws PortalException, SystemException {
802    
803                    isValidVersion(versionLabel);
804    
805                    validate(
806                            fileName, fileExtension, sourceFileName, validateFileExtension,
807                            file);
808            }
809    
810            protected void validate(
811                            String fileName, String fileExtension, String sourceFileName,
812                            boolean validateFileExtension, InputStream is, String versionLabel)
813                    throws PortalException, SystemException {
814    
815                    isValidVersion(versionLabel);
816    
817                    validate(
818                            fileName, fileExtension, sourceFileName, validateFileExtension, is);
819            }
820    
821            protected void validate(
822                            String fileName, String fileExtension, String sourceFileName,
823                            boolean validateFileExtension, String versionLabel)
824                    throws PortalException, SystemException {
825    
826                    String sourceFileExtension = FileUtil.getExtension(sourceFileName);
827    
828                    if (Validator.isNotNull(sourceFileName) &&
829                            PropsValues.DL_FILE_EXTENSIONS_STRICT_CHECK &&
830                            !fileExtension.equals(sourceFileExtension)) {
831    
832                            throw new SourceFileNameException(sourceFileExtension);
833                    }
834    
835                    validate(fileName, validateFileExtension, versionLabel);
836            }
837    
838            @BeanReference(type = GroupLocalService.class)
839            protected GroupLocalService groupLocalService;
840    
841            protected Store store;
842    
843            private static final String[] _KEYWORDS_FIELDS = {
844                    Field.ASSET_TAG_NAMES, Field.CONTENT, Field.PROPERTIES
845            };
846    
847            private static final String _UNICODE_PREFIX = "\\u";
848    
849    }