Source Code Cross Referenced for FileManagerUtils.java in  » Rule-Engine » drolls-Rule-Engine » org » drools » brms » server » files » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Rule Engine » drolls Rule Engine » org.drools.brms.server.files 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.