1 17 package org.alfresco.repo.exporter; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.zip.ZipEntry ; 27 import java.util.zip.ZipOutputStream ; 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 42 public class ACPExportPackageHandler 43 implements ExportPackageHandler 44 { 45 46 public final static String ACP_EXTENSION = "acp"; 47 48 protected MimetypeService mimetypeService; 49 protected OutputStream outputStream; 50 protected File dataFile; 51 protected File contentDir; 52 protected File tempDataFile; 53 protected OutputStream tempDataFileStream; 54 protected ZipOutputStream zipStream; 55 protected int iFileCnt = 0; 56 57 58 66 public ACPExportPackageHandler(File destDir, File zipFile, File dataFile, File contentDir, boolean overwrite, MimetypeService mimetypeService) 67 { 68 try 69 { 70 String zipFilePath = zipFile.getPath(); 72 if (!zipFilePath.endsWith("." + ACP_EXTENSION)) 73 { 74 zipFilePath += "." + ACP_EXTENSION; 75 } 76 77 File absZipFile = new File (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 (absZipFile); 90 this.dataFile = dataFile; 91 this.contentDir = contentDir; 92 this.mimetypeService = mimetypeService; 93 } 94 catch (FileNotFoundException e) 95 { 96 throw new ExporterException("Failed to create zip file", e); 97 } 98 } 99 100 107 public ACPExportPackageHandler(OutputStream outputStream, File dataFile, File contentDir, MimetypeService mimetypeService) 108 { 109 this.outputStream = outputStream; 110 this.dataFile = dataFile; 111 this.contentDir = contentDir; 112 this.mimetypeService = mimetypeService; 113 } 114 115 118 public void startExport() 119 { 120 zipStream = new ZipOutputStream (outputStream); 121 } 122 123 126 public OutputStream createDataStream() 127 { 128 tempDataFile = TempFileProvider.createTempFile("exportDataStream", ".xml"); 129 try 130 { 131 tempDataFileStream = new FileOutputStream (tempDataFile); 132 return tempDataFileStream; 133 } 134 catch (FileNotFoundException e) 135 { 136 throw new ExporterException("Failed to create data file stream", e); 137 } 138 } 139 140 143 public ContentData exportContent(InputStream content, ContentData contentData) 144 { 145 String contentDirPath = contentDir.getPath(); 147 if (contentDirPath.indexOf(".") != -1) 148 { 149 contentDirPath = contentDirPath.substring(0, contentDirPath.indexOf(".")); 150 } 151 String extension = "bin"; 152 if (mimetypeService != null) 153 { 154 String 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 } 165 } 166 } 167 File file = new File (contentDirPath, "content" + iFileCnt++ + "." + extension); 168 169 try 170 { 171 ZipEntry zipEntry = new ZipEntry (file.getPath()); 172 zipStream.putNextEntry(zipEntry); 173 174 copyStream(zipStream, content); 176 } 177 catch (IOException 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 188 public void endExport() 189 { 190 String dataFilePath = dataFile.getPath(); 192 if (!dataFilePath.endsWith(".xml")) 193 { 194 dataFilePath += ".xml"; 195 } 196 197 ZipEntry zipEntry = new ZipEntry (dataFilePath); 199 200 try 201 { 202 tempDataFileStream.close(); 204 zipStream.putNextEntry(zipEntry); 205 InputStream dataFileStream = new FileInputStream (tempDataFile); 206 copyStream(zipStream, dataFileStream); 207 dataFileStream.close(); 208 } 209 catch (IOException e) 210 { 211 throw new ExporterException("Failed to zip data stream file", e); 212 } 213 214 try 215 { 216 zipStream.close(); 218 } 219 catch(IOException e) 220 { 221 throw new ExporterException("Failed to close zip package stream", e); 222 } 223 } 224 225 230 protected void log(String message) 231 { 232 } 233 234 241 private void copyStream(OutputStream output, InputStream in) 242 throws IOException 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 |