001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.documentlibrary.service.impl;
022:
023: import com.liferay.documentlibrary.FileNameException;
024: import com.liferay.documentlibrary.FileSizeException;
025: import com.liferay.documentlibrary.SourceFileNameException;
026: import com.liferay.documentlibrary.service.DLLocalService;
027: import com.liferay.documentlibrary.util.Hook;
028: import com.liferay.documentlibrary.util.HookFactory;
029: import com.liferay.portal.PortalException;
030: import com.liferay.portal.SystemException;
031: import com.liferay.portal.kernel.search.Hits;
032: import com.liferay.portal.kernel.util.StringPool;
033: import com.liferay.portal.kernel.util.StringUtil;
034: import com.liferay.portal.kernel.util.Validator;
035: import com.liferay.portal.lucene.LuceneFields;
036: import com.liferay.portal.lucene.LuceneUtil;
037: import com.liferay.portal.util.PropsValues;
038: import com.liferay.util.FileUtil;
039: import com.liferay.util.lucene.HitsImpl;
040:
041: import java.io.File;
042: import java.io.InputStream;
043:
044: import org.apache.lucene.index.Term;
045: import org.apache.lucene.search.BooleanClause;
046: import org.apache.lucene.search.BooleanQuery;
047: import org.apache.lucene.search.Searcher;
048: import org.apache.lucene.search.TermQuery;
049:
050: /**
051: * <a href="DLLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class DLLocalServiceImpl implements DLLocalService {
057:
058: public void addFile(long companyId, String portletId, long groupId,
059: long repositoryId, String fileName, String properties,
060: String[] tagsEntries, InputStream is)
061: throws PortalException, SystemException {
062:
063: validate(fileName, is);
064:
065: Hook hook = HookFactory.getInstance();
066:
067: hook.addFile(companyId, portletId, groupId, repositoryId,
068: fileName, properties, tagsEntries, is);
069: }
070:
071: public void checkRoot(long companyId) throws SystemException {
072: Hook hook = HookFactory.getInstance();
073:
074: hook.checkRoot(companyId);
075: }
076:
077: public InputStream getFileAsStream(long companyId,
078: long repositoryId, String fileName) throws PortalException,
079: SystemException {
080:
081: Hook hook = HookFactory.getInstance();
082:
083: return hook.getFileAsStream(companyId, repositoryId, fileName);
084: }
085:
086: public InputStream getFileAsStream(long companyId,
087: long repositoryId, String fileName, double versionNumber)
088: throws PortalException, SystemException {
089:
090: Hook hook = HookFactory.getInstance();
091:
092: return hook.getFileAsStream(companyId, repositoryId, fileName,
093: versionNumber);
094: }
095:
096: public boolean hasFile(long companyId, long repositoryId,
097: String fileName, double versionNumber)
098: throws PortalException, SystemException {
099:
100: Hook hook = HookFactory.getInstance();
101:
102: return hook.hasFile(companyId, repositoryId, fileName,
103: versionNumber);
104: }
105:
106: public void move(String srcDir, String destDir)
107: throws SystemException {
108: Hook hook = HookFactory.getInstance();
109:
110: hook.move(srcDir, destDir);
111: }
112:
113: public Hits search(long companyId, String portletId, long groupId,
114: long[] repositoryIds, String keywords)
115: throws SystemException {
116:
117: Searcher searcher = null;
118:
119: try {
120: HitsImpl hits = new HitsImpl();
121:
122: BooleanQuery contextQuery = new BooleanQuery();
123:
124: LuceneUtil.addRequiredTerm(contextQuery,
125: LuceneFields.PORTLET_ID, portletId);
126:
127: if (groupId > 0) {
128: LuceneUtil.addRequiredTerm(contextQuery,
129: LuceneFields.GROUP_ID, groupId);
130: }
131:
132: if ((repositoryIds != null) && (repositoryIds.length > 0)) {
133: BooleanQuery repositoryIdsQuery = new BooleanQuery();
134:
135: for (int i = 0; i < repositoryIds.length; i++) {
136: Term term = new Term("repositoryId", String
137: .valueOf(repositoryIds[i]));
138: TermQuery termQuery = new TermQuery(term);
139:
140: repositoryIdsQuery.add(termQuery,
141: BooleanClause.Occur.SHOULD);
142: }
143:
144: contextQuery.add(repositoryIdsQuery,
145: BooleanClause.Occur.MUST);
146: }
147:
148: BooleanQuery searchQuery = new BooleanQuery();
149:
150: if (Validator.isNotNull(keywords)) {
151: LuceneUtil.addTerm(searchQuery, LuceneFields.CONTENT,
152: keywords);
153: LuceneUtil.addTerm(searchQuery,
154: LuceneFields.PROPERTIES, keywords);
155: LuceneUtil.addTerm(searchQuery, LuceneFields.TAG_ENTRY,
156: keywords);
157: }
158:
159: BooleanQuery fullQuery = new BooleanQuery();
160:
161: fullQuery.add(contextQuery, BooleanClause.Occur.MUST);
162:
163: if (searchQuery.clauses().size() > 0) {
164: fullQuery.add(searchQuery, BooleanClause.Occur.MUST);
165: }
166:
167: searcher = LuceneUtil.getSearcher(companyId);
168:
169: hits.recordHits(searcher.search(fullQuery), searcher);
170:
171: return hits;
172: } catch (Exception e) {
173: return LuceneUtil.closeSearcher(searcher, keywords, e);
174: }
175: }
176:
177: public void updateFile(long companyId, String portletId,
178: long groupId, long repositoryId, String fileName,
179: double versionNumber, String sourceFileName,
180: String properties, String[] tagsEntries, InputStream is)
181: throws PortalException, SystemException {
182:
183: validate(fileName, sourceFileName, is);
184:
185: Hook hook = HookFactory.getInstance();
186:
187: hook.updateFile(companyId, portletId, groupId, repositoryId,
188: fileName, versionNumber, sourceFileName, properties,
189: tagsEntries, is);
190: }
191:
192: public void validate(String fileName, File file)
193: throws PortalException {
194: validate(fileName);
195:
196: if ((PropsValues.DL_FILE_MAX_SIZE > 0)
197: && ((file == null) || (file.length() > PropsValues.DL_FILE_MAX_SIZE))) {
198:
199: throw new FileSizeException(fileName);
200: }
201: }
202:
203: public void validate(String fileName, byte[] byteArray)
204: throws PortalException {
205:
206: validate(fileName);
207:
208: if ((PropsValues.DL_FILE_MAX_SIZE > 0)
209: && ((byteArray == null) || (byteArray.length > PropsValues.DL_FILE_MAX_SIZE))) {
210:
211: throw new FileSizeException(fileName);
212: }
213: }
214:
215: public void validate(String fileName, InputStream is)
216: throws PortalException {
217:
218: validate(fileName);
219:
220: if (is == null) {
221: throw new FileSizeException(fileName);
222: }
223: }
224:
225: public void validate(String fileName) throws PortalException {
226: if ((fileName.indexOf("\\\\") != -1)
227: || (fileName.indexOf("//") != -1)
228: || (fileName.indexOf(":") != -1)
229: || (fileName.indexOf("*") != -1)
230: || (fileName.indexOf("?") != -1)
231: || (fileName.indexOf("\"") != -1)
232: || (fileName.indexOf("<") != -1)
233: || (fileName.indexOf(">") != -1)
234: || (fileName.indexOf("|") != -1)
235: || (fileName.indexOf("&") != -1)
236: || (fileName.indexOf("[") != -1)
237: || (fileName.indexOf("]") != -1)
238: || (fileName.indexOf("'") != -1)) {
239:
240: throw new FileNameException(fileName);
241: }
242:
243: boolean validFileExtension = false;
244:
245: String[] fileExtensions = PropsValues.DL_FILE_EXTENSIONS;
246:
247: if (!PropsValues.WEBDAV_LITMUS) {
248: for (int i = 0; i < fileExtensions.length; i++) {
249: if (StringPool.STAR.equals(fileExtensions[i])
250: || StringUtil.endsWith(fileName,
251: fileExtensions[i])) {
252:
253: validFileExtension = true;
254:
255: break;
256: }
257: }
258:
259: if (!validFileExtension) {
260: throw new FileNameException(fileName);
261: }
262: }
263: }
264:
265: public void validate(String fileName, String sourceFileName,
266: InputStream is) throws PortalException {
267:
268: String fileNameExtension = FileUtil.getExtension(fileName);
269: String sourceFileNameExtension = FileUtil
270: .getExtension(sourceFileName);
271:
272: if (!PropsValues.WEBDAV_LITMUS) {
273: if (Validator.isNull(fileNameExtension)
274: || !fileNameExtension
275: .equalsIgnoreCase(sourceFileNameExtension)) {
276:
277: throw new SourceFileNameException(sourceFileName);
278: }
279: }
280:
281: if (is == null) {
282: throw new FileSizeException(fileName);
283: }
284: }
285:
286: }
|