1 17 package org.alfresco.repo.exporter; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 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 39 public class FileExportPackageHandler 40 implements ExportPackageHandler 41 { 42 protected MimetypeService mimetypeService = null; 43 protected File contentDir; 44 protected File absContentDir; 45 protected File absDataFile; 46 protected boolean overwrite; 47 protected OutputStream absDataStream = null; 48 49 58 public FileExportPackageHandler(File destDir, File dataFile, File contentDir, boolean overwrite, MimetypeService mimetypeService) 59 { 60 this.contentDir = contentDir; 61 this.absContentDir = new File (destDir, contentDir.getPath()); 62 this.absDataFile = new File (destDir, dataFile.getPath()); 63 this.overwrite = overwrite; 64 this.mimetypeService = mimetypeService; 65 } 66 67 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 87 public OutputStream 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 (absDataFile); 103 return absDataStream; 104 } 105 catch(IOException e) 106 { 107 throw new ExporterException("Failed to create package file " + absDataFile.getAbsolutePath() + " due to " + e.getMessage()); 108 } 109 } 110 111 114 public ContentData exportContent(InputStream content, ContentData contentData) 115 { 116 try 118 { 119 absContentDir.mkdirs(); 120 } 121 catch(SecurityException e) 122 { 123 throw new ExporterException("Failed to create package dir " + absContentDir.getAbsolutePath() + " due to " + e.getMessage()); 124 } 125 126 String extension = "bin"; 128 if (mimetypeService != null) 129 { 130 String 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 } 141 } 142 } 143 File outputFile = TempFileProvider.createTempFile("export", "." + extension, absContentDir); 144 145 try 146 { 147 FileOutputStream outputStream = new FileOutputStream (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 e) 159 { 160 throw new ExporterException("Failed to create export package file due to " + e.getMessage()); 161 } 162 catch(IOException e) 163 { 164 throw new ExporterException("Failed to export content due to " + e.getMessage()); 165 } 166 167 File url = new File (contentDir, outputFile.getName()); 169 return new ContentData(url.getPath(), contentData.getMimetype(), contentData.getSize(), contentData.getEncoding()); 170 } 171 172 175 public void endExport() 176 { 177 if (absDataStream != null) 179 { 180 try 181 { 182 absDataStream.close(); 183 } 184 catch(IOException e) 185 { 186 throw new ExporterException("Failed to close package data file " + absDataFile + " due to" + e.getMessage()); 187 } 188 } 189 } 190 191 196 protected void log(String message) 197 { 198 } 199 } 200 | Popular Tags |