KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileNotFoundException JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import org.alfresco.error.AlfrescoRuntimeException;
27 import org.alfresco.service.cmr.repository.ContentData;
28 import org.alfresco.service.cmr.repository.MimetypeService;
29 import org.alfresco.service.cmr.view.ExportPackageHandler;
30 import org.alfresco.service.cmr.view.ExporterException;
31 import org.alfresco.util.TempFileProvider;
32
33
34 /**
35  * Handler for exporting Repository to file system files
36  *
37  * @author David Caruana
38  */

39 public class FileExportPackageHandler
40     implements ExportPackageHandler
41 {
42     protected MimetypeService mimetypeService = null;
43     protected File JavaDoc contentDir;
44     protected File JavaDoc absContentDir;
45     protected File JavaDoc absDataFile;
46     protected boolean overwrite;
47     protected OutputStream JavaDoc absDataStream = null;
48
49     /**
50      * Constuct Handler
51      *
52      * @param destDir destination directory
53      * @param dataFile filename of data file (relative to destDir)
54      * @param packageDir directory for content (relative to destDir)
55      * @param overwrite force overwrite of existing package directory
56      * @param mimetypeService (optional) mimetype service
57      */

58     public FileExportPackageHandler(File JavaDoc destDir, File JavaDoc dataFile, File JavaDoc contentDir, boolean overwrite, MimetypeService mimetypeService)
59     {
60         this.contentDir = contentDir;
61         this.absContentDir = new File JavaDoc(destDir, contentDir.getPath());
62         this.absDataFile = new File JavaDoc(destDir, dataFile.getPath());
63         this.overwrite = overwrite;
64         this.mimetypeService = mimetypeService;
65     }
66
67     /* (non-Javadoc)
68      * @see org.alfresco.service.cmr.view.ExportPackageHandler#startExport()
69      */

70     public void startExport()
71     {
72         log("Exporting to package " + absDataFile.getAbsolutePath());
73         
74         if (absContentDir.exists())
75         {
76             if (overwrite == false)
77             {
78                 throw new ExporterException("Package content dir " + absContentDir.getAbsolutePath() + " already exists.");
79             }
80             log("Warning: Overwriting existing package dir " + absContentDir.getAbsolutePath());
81         }
82     }
83
84     /* (non-Javadoc)
85      * @see org.alfresco.service.cmr.view.ExportPackageHandler#createDataStream()
86      */

87     public OutputStream JavaDoc createDataStream()
88     {
89         if (absDataFile.exists())
90         {
91             if (overwrite == false)
92             {
93                 throw new ExporterException("Package data file " + absDataFile.getAbsolutePath() + " already exists.");
94             }
95             log("Warning: Overwriting existing package file " + absDataFile.getAbsolutePath());
96             absDataFile.delete();
97         }
98
99         try
100         {
101             absDataFile.createNewFile();
102             absDataStream = new FileOutputStream JavaDoc(absDataFile);
103             return absDataStream;
104         }
105         catch(IOException JavaDoc e)
106         {
107             throw new ExporterException("Failed to create package file " + absDataFile.getAbsolutePath() + " due to " + e.getMessage());
108         }
109     }
110     
111     /* (non-Javadoc)
112      * @see org.alfresco.service.cmr.view.ExportStreamHandler#exportStream(java.io.InputStream)
113      */

114     public ContentData exportContent(InputStream JavaDoc content, ContentData contentData)
115     {
116         // Lazily create package directory
117
try
118         {
119             absContentDir.mkdirs();
120         }
121         catch(SecurityException JavaDoc e)
122         {
123             throw new ExporterException("Failed to create package dir " + absContentDir.getAbsolutePath() + " due to " + e.getMessage());
124         }
125         
126         // Create file in package directory to hold exported content
127
String JavaDoc extension = "bin";
128         if (mimetypeService != null)
129         {
130             String JavaDoc mimetype = contentData.getMimetype();
131             if (mimetype != null && mimetype.length() > 0)
132             {
133                 try
134                 {
135                     extension = mimetypeService.getExtension(mimetype);
136                 }
137                 catch(AlfrescoRuntimeException e)
138                 {
139                     // use default extension
140
}
141             }
142         }
143         File JavaDoc outputFile = TempFileProvider.createTempFile("export", "." + extension, absContentDir);
144         
145         try
146         {
147             // Copy exported content from repository to exported file
148
FileOutputStream JavaDoc outputStream = new FileOutputStream JavaDoc(outputFile);
149             byte[] buffer = new byte[2048 * 10];
150             int read = content.read(buffer, 0, 2048 *10);
151             while (read != -1)
152             {
153                 outputStream.write(buffer, 0, read);
154                 read = content.read(buffer, 0, 2048 *10);
155             }
156             outputStream.close();
157         }
158         catch(FileNotFoundException JavaDoc e)
159         {
160             throw new ExporterException("Failed to create export package file due to " + e.getMessage());
161         }
162         catch(IOException JavaDoc e)
163         {
164             throw new ExporterException("Failed to export content due to " + e.getMessage());
165         }
166         
167         // return relative path to exported content file (relative to xml export file)
168
File JavaDoc url = new File JavaDoc(contentDir, outputFile.getName());
169         return new ContentData(url.getPath(), contentData.getMimetype(), contentData.getSize(), contentData.getEncoding());
170     }
171
172     /* (non-Javadoc)
173      * @see org.alfresco.service.cmr.view.ExportPackageHandler#endExport()
174      */

175     public void endExport()
176     {
177         // close Export File
178
if (absDataStream != null)
179         {
180             try
181             {
182                 absDataStream.close();
183             }
184             catch(IOException JavaDoc e)
185             {
186                 throw new ExporterException("Failed to close package data file " + absDataFile + " due to" + e.getMessage());
187             }
188         }
189     }
190     
191     /**
192      * Log Export Message
193      *
194      * @param message message to log
195      */

196     protected void log(String JavaDoc message)
197     {
198     }
199 }
200
Popular Tags