KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > exporter > ACPExportPackageHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.exporter;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.zip.ZipEntry JavaDoc;
27 import java.util.zip.ZipOutputStream JavaDoc;
28
29 import org.alfresco.error.AlfrescoRuntimeException;
30 import org.alfresco.service.cmr.repository.ContentData;
31 import org.alfresco.service.cmr.repository.MimetypeService;
32 import org.alfresco.service.cmr.view.ExportPackageHandler;
33 import org.alfresco.service.cmr.view.ExporterException;
34 import org.alfresco.util.TempFileProvider;
35
36
37 /**
38  * Handler for exporting Repository to ACP (Alfresco Content Package) file
39  *
40  * @author David Caruana
41  */

42 public class ACPExportPackageHandler
43     implements ExportPackageHandler
44 {
45     /** ACP File Extension */
46     public final static String JavaDoc ACP_EXTENSION = "acp";
47     
48     protected MimetypeService mimetypeService;
49     protected OutputStream JavaDoc outputStream;
50     protected File JavaDoc dataFile;
51     protected File JavaDoc contentDir;
52     protected File JavaDoc tempDataFile;
53     protected OutputStream JavaDoc tempDataFileStream;
54     protected ZipOutputStream JavaDoc zipStream;
55     protected int iFileCnt = 0;
56
57     
58     /**
59      * Construct
60      *
61      * @param destDir
62      * @param zipFile
63      * @param dataFile
64      * @param contentDir
65      */

66     public ACPExportPackageHandler(File JavaDoc destDir, File JavaDoc zipFile, File JavaDoc dataFile, File JavaDoc contentDir, boolean overwrite, MimetypeService mimetypeService)
67     {
68         try
69         {
70             // Ensure ACP file has appropriate ACP extension
71
String JavaDoc zipFilePath = zipFile.getPath();
72             if (!zipFilePath.endsWith("." + ACP_EXTENSION))
73             {
74                 zipFilePath += "." + ACP_EXTENSION;
75             }
76
77             File JavaDoc absZipFile = new File JavaDoc(destDir, zipFilePath);
78             log("Exporting to package zip file " + absZipFile.getAbsolutePath());
79
80             if (absZipFile.exists())
81             {
82                 if (overwrite == false)
83                 {
84                     throw new ExporterException("Package zip file " + absZipFile.getAbsolutePath() + " already exists.");
85                 }
86                 log("Warning: Overwriting existing package zip file " + absZipFile.getAbsolutePath());
87             }
88             
89             this.outputStream = new FileOutputStream JavaDoc(absZipFile);
90             this.dataFile = dataFile;
91             this.contentDir = contentDir;
92             this.mimetypeService = mimetypeService;
93         }
94         catch (FileNotFoundException JavaDoc e)
95         {
96             throw new ExporterException("Failed to create zip file", e);
97         }
98     }
99
100     /**
101      * Construct
102      *
103      * @param outputStream
104      * @param dataFile
105      * @param contentDir
106      */

107     public ACPExportPackageHandler(OutputStream JavaDoc outputStream, File JavaDoc dataFile, File JavaDoc contentDir, MimetypeService mimetypeService)
108     {
109         this.outputStream = outputStream;
110         this.dataFile = dataFile;
111         this.contentDir = contentDir;
112         this.mimetypeService = mimetypeService;
113     }
114
115     /* (non-Javadoc)
116      * @see org.alfresco.service.cmr.view.ExportPackageHandler#startExport()
117      */

118     public void startExport()
119     {
120         zipStream = new ZipOutputStream JavaDoc(outputStream);
121     }
122
123     /* (non-Javadoc)
124      * @see org.alfresco.service.cmr.view.ExportPackageHandler#createDataStream()
125      */

126     public OutputStream JavaDoc createDataStream()
127     {
128         tempDataFile = TempFileProvider.createTempFile("exportDataStream", ".xml");
129         try
130         {
131             tempDataFileStream = new FileOutputStream JavaDoc(tempDataFile);
132             return tempDataFileStream;
133         }
134         catch (FileNotFoundException JavaDoc e)
135         {
136             throw new ExporterException("Failed to create data file stream", e);
137         }
138     }
139
140     /* (non-Javadoc)
141      * @see org.alfresco.service.cmr.view.ExportStreamHandler#exportStream(java.io.InputStream)
142      */

143     public ContentData exportContent(InputStream JavaDoc content, ContentData contentData)
144     {
145         // create zip entry for stream to export
146
String JavaDoc contentDirPath = contentDir.getPath();
147         if (contentDirPath.indexOf(".") != -1)
148         {
149             contentDirPath = contentDirPath.substring(0, contentDirPath.indexOf("."));
150         }
151         String JavaDoc extension = "bin";
152         if (mimetypeService != null)
153         {
154             String JavaDoc mimetype = contentData.getMimetype();
155             if (mimetype != null && mimetype.length() > 0)
156             {
157                 try
158                 {
159                     extension = mimetypeService.getExtension(mimetype);
160                 }
161                 catch(AlfrescoRuntimeException e)
162                 {
163                     // use default extension
164
}
165             }
166         }
167         File JavaDoc file = new File JavaDoc(contentDirPath, "content" + iFileCnt++ + "." + extension);
168         
169         try
170         {
171             ZipEntry JavaDoc zipEntry = new ZipEntry JavaDoc(file.getPath());
172             zipStream.putNextEntry(zipEntry);
173             
174             // copy export stream to zip
175
copyStream(zipStream, content);
176         }
177         catch (IOException JavaDoc e)
178         {
179             throw new ExporterException("Failed to zip export stream", e);
180         }
181         
182         return new ContentData(file.getPath(), contentData.getMimetype(), contentData.getSize(), contentData.getEncoding());
183     }
184
185     /* (non-Javadoc)
186      * @see org.alfresco.service.cmr.view.ExportPackageHandler#endExport()
187      */

188     public void endExport()
189     {
190         // ensure data file has .xml extension
191
String JavaDoc dataFilePath = dataFile.getPath();
192         if (!dataFilePath.endsWith(".xml"))
193         {
194             dataFilePath += ".xml";
195         }
196         
197         // add data file to zip stream
198
ZipEntry JavaDoc zipEntry = new ZipEntry JavaDoc(dataFilePath);
199         
200         try
201         {
202             // close data file stream and place temp data file into zip output stream
203
tempDataFileStream.close();
204             zipStream.putNextEntry(zipEntry);
205             InputStream JavaDoc dataFileStream = new FileInputStream JavaDoc(tempDataFile);
206             copyStream(zipStream, dataFileStream);
207             dataFileStream.close();
208         }
209         catch (IOException JavaDoc e)
210         {
211             throw new ExporterException("Failed to zip data stream file", e);
212         }
213         
214         try
215         {
216             // close zip stream
217
zipStream.close();
218         }
219         catch(IOException JavaDoc e)
220         {
221             throw new ExporterException("Failed to close zip package stream", e);
222         }
223     }
224     
225     /**
226      * Log Export Message
227      *
228      * @param message message to log
229      */

230     protected void log(String JavaDoc message)
231     {
232     }
233
234     /**
235      * Copy input stream to output stream
236      *
237      * @param output output stream
238      * @param in input stream
239      * @throws IOException
240      */

241     private void copyStream(OutputStream JavaDoc output, InputStream JavaDoc in)
242         throws IOException JavaDoc
243     {
244         byte[] buffer = new byte[2048 * 10];
245         int read = in.read(buffer, 0, 2048 *10);
246         while (read != -1)
247         {
248             output.write(buffer, 0, read);
249             read = in.read(buffer, 0, 2048 *10);
250         }
251     }
252     
253 }
254
Popular Tags