001: package org.drools.brms.server.files;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.BufferedInputStream;
020: import java.io.BufferedReader;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.io.OutputStream;
025: import java.net.URLEncoder;
026: import java.util.Date;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import javax.jcr.RepositoryException;
031: import javax.servlet.http.HttpServletRequest;
032:
033: import org.apache.commons.fileupload.FileItem;
034: import org.apache.commons.fileupload.FileItemFactory;
035: import org.apache.commons.fileupload.FileUploadException;
036: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
037: import org.apache.commons.fileupload.servlet.ServletFileUpload;
038: import org.drools.brms.client.common.AssetFormats;
039: import org.drools.brms.client.common.HTMLFileManagerFields;
040: import org.drools.brms.server.util.ClassicDRLImporter;
041: import org.drools.brms.server.util.FormData;
042: import org.drools.brms.server.util.ClassicDRLImporter.Rule;
043: import org.drools.compiler.DroolsParserException;
044: import org.drools.repository.AssetItem;
045: import org.drools.repository.CategoryItem;
046: import org.drools.repository.PackageItem;
047: import org.drools.repository.RulesRepository;
048: import org.drools.repository.RulesRepositoryException;
049: import org.jboss.seam.ScopeType;
050: import org.jboss.seam.annotations.AutoCreate;
051: import org.jboss.seam.annotations.In;
052: import org.jboss.seam.annotations.Name;
053: import org.jboss.seam.annotations.Scope;
054: import org.jboss.seam.annotations.security.Restrict;
055:
056: /**
057: * This assists the file manager servlets.
058: * @author Fernando Meyer
059: */
060: @Name("fileManager")
061: @Scope(ScopeType.EVENT)
062: @AutoCreate
063: public class FileManagerUtils {
064:
065: @In
066: public RulesRepository repository;
067:
068: /**
069: * This attach a file to an asset.
070: */
071: @Restrict("#{identity.loggedIn}")
072: public void attachFile(FormData uploadItem) throws IOException {
073:
074: String uuid = uploadItem.getUuid();
075: InputStream fileData = uploadItem.getFile().getInputStream();
076: String fileName = uploadItem.getFile().getName();
077:
078: attachFileToAsset(uuid, fileData, fileName);
079: uploadItem.getFile().getInputStream().close();
080: }
081:
082: /**
083: * This utility method attaches a file to an asset.
084: */
085: @Restrict("#{identity.loggedIn}")
086: public void attachFileToAsset(String uuid, InputStream fileData,
087: String fileName) {
088:
089: AssetItem item = repository.loadAssetByUUID(uuid);
090: item.updateBinaryContentAttachment(fileData);
091: item.updateBinaryContentAttachmentFileName(fileName);
092: item.checkin("Attached file: " + fileName);
093: }
094:
095: /**
096: * The get returns files based on UUID of an asset.
097: */
098: @Restrict("#{identity.loggedIn}")
099: public String loadFileAttachmentByUUID(String uuid, OutputStream out)
100: throws IOException {
101:
102: AssetItem item = repository.loadAssetByUUID(uuid);
103:
104: byte[] data = item.getBinaryContentAsBytes();
105:
106: out.write(data);
107: out.flush();
108:
109: return item.getBinaryContentAttachmentFileName();
110: }
111:
112: /**
113: * Get the form data from the inbound request.
114: */
115: public static FormData getFormData(HttpServletRequest request) {
116: FileItemFactory factory = new DiskFileItemFactory();
117: ServletFileUpload upload = new ServletFileUpload(factory);
118:
119: FormData data = new FormData();
120: try {
121: List items = upload.parseRequest(request);
122: Iterator it = items.iterator();
123: while (it.hasNext()) {
124: FileItem item = (FileItem) it.next();
125: if (item.isFormField()
126: && item.getFieldName().equals(
127: HTMLFileManagerFields.FORM_FIELD_UUID)) {
128: data.setUuid(item.getString());
129: }
130: data.setFile(item);
131: }
132: return data;
133: } catch (FileUploadException e) {
134: throw new RulesRepositoryException(e);
135: }
136: }
137:
138: /**
139: * Load up the approproate package version.
140: * @param packageName The name of the package.
141: * @param packageVersion The version (if it is a snapshot).
142: * @param isLatest true if the latest package binary will be used (ie NOT a snapshot).
143: * @return The filename if its all good.
144: */
145: public String loadBinaryPackage(String packageName,
146: String packageVersion, boolean isLatest, OutputStream out)
147: throws IOException {
148: PackageItem item = null;
149: if (isLatest) {
150: item = repository.loadPackage(packageName);
151: byte[] data = item.getCompiledPackageBytes();
152: out.write(data);
153: out.flush();
154: return packageName + ".pkg";
155: } else {
156: item = repository.loadPackageSnapshot(packageName,
157: packageVersion);
158: byte[] data = item.getCompiledPackageBytes();
159: out.write(data);
160: out.flush();
161: return packageName + "_"
162: + URLEncoder.encode(packageVersion, "UTF-8")
163: + ".pkg";
164: }
165:
166: }
167:
168: public byte[] exportRulesRepository() {
169: try {
170: return this .repository.exportRulesRepository();
171: } catch (RepositoryException e) {
172: throw new RulesRepositoryException(e);
173: } catch (IOException e) {
174: throw new RulesRepositoryException(e);
175: }
176: }
177:
178: @Restrict("#{identity.loggedIn}")
179: public void importRulesRepository(byte[] data) {
180: repository.importRulesRepository(data);
181: }
182:
183: /**
184: * This will import DRL from a drl file into a more normalised structure.
185: * If the package does not exist, it will be created.
186: * If it does, it will be "merged" in the sense that any new rules in the drl
187: * will be created as new assets in the repo, everything else will stay as it was
188: * in the repo.
189: */
190: @Restrict("#{identity.loggedIn}")
191: public void importClassicDRL(InputStream drlStream)
192: throws IOException, DroolsParserException {
193:
194: ClassicDRLImporter imp = new ClassicDRLImporter(drlStream);
195: PackageItem pkg = null;
196: boolean existing = false;
197: if (repository.containsPackage(imp.getPackageName())) {
198: pkg = repository.loadPackage(imp.getPackageName());
199: existing = true;
200: } else {
201: pkg = repository.createPackage(imp.getPackageName(),
202: "<imported>");
203: pkg.updateHeader(imp.getPackageHeader());
204: }
205:
206: for (Rule rule : imp.getRules()) {
207:
208: if (existing && pkg.containsAsset(rule.name)) {
209: //skip it
210: } else {
211:
212: AssetItem asset = pkg.addAsset(rule.name, "<imported>");
213:
214: if (imp.isDSLEnabled()) {
215: asset.updateFormat(AssetFormats.DSL_TEMPLATE_RULE);
216: } else {
217: asset.updateFormat(AssetFormats.DRL);
218: }
219: asset.updateContent(rule.content);
220: asset
221: .updateExternalSource("Imported from external DRL");
222: }
223: }
224:
225: repository.save();
226: }
227:
228: /**
229: * This will return the last time the package was built.
230: */
231: public long getLastModified(String name, String version) {
232: PackageItem item = null;
233: if (version.equals("LATEST")) {
234: item = repository.loadPackage(name);
235: } else {
236: item = repository.loadPackageSnapshot(name, version);
237: }
238: return item.getLastModified().getTimeInMillis();
239: }
240:
241: }
|