1 13 package info.magnolia.cms.core.ie; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ItemType; 17 import info.magnolia.cms.core.NodeData; 18 import info.magnolia.cms.core.Path; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipOutputStream ; 31 32 import javax.jcr.PropertyType; 33 import javax.jcr.RepositoryException; 34 35 import org.apache.log4j.Logger; 36 import org.doomdark.uuid.UUIDGenerator; 37 38 39 43 public class PackageExport implements ExportHandler { 44 45 48 protected static Logger log = Logger.getLogger(PackageExport.class); 49 50 private static final String START_DIRECTORY = "data"; 52 public static final String DATA_FILE_NAME = "data.xml"; 54 57 private boolean binaryAsLink = true; 58 59 private Map params = new Hashtable (); 60 61 64 65 public void setBinaryAsLink(boolean binaryAsLink) { 66 this.binaryAsLink = binaryAsLink; 67 } 68 69 public boolean getBinaryAsLink() { 70 return this.binaryAsLink; 71 } 72 73 public Object exportContent(Content content) throws RepositoryException { 74 String message = "export to object not supported by PackageExport"; log.error(message); 76 throw new UnsupportedOperationException (message); 77 } 78 79 public void exportContent(Content content, OutputStream outStream) throws RepositoryException, IOException { 80 ContentZipper zipper = new ContentZipper(content); 81 zipper.run(); 82 FileInputStream is = new FileInputStream (zipper.getZipFile()); 84 byte[] buffer = new byte[8192]; 85 int read = 0; 86 while ((read = is.read(buffer)) > 0) { 87 outStream.write(buffer, 0, read); 88 } 89 outStream.flush(); 90 outStream.close(); 91 is.close(); 92 zipper.getZipFile().delete(); 93 } 94 95 public void setParameter(String key, Object value) { 96 this.params.put(key, value); 97 } 98 99 public Object getParameter(String key) { 100 return this.params.get(key); 101 } 102 103 106 class ContentZipper { 107 108 private String zipFileName; 109 110 private File zipFile; 111 112 private ZipOutputStream outputStream; 113 114 private Content content; 115 116 ContentZipper(Content content) { 117 this.content = content; 118 } 119 120 public void run() throws IOException , RepositoryException { 121 try { 122 this.createTargetFile(); 123 this.outputStream = new ZipOutputStream (new FileOutputStream (this.zipFile)); 124 this.outputStream.setMethod(ZipOutputStream.DEFLATED); 126 this.outputStream.setLevel(9); 127 this.addTextContent(); 128 this.addBinaryContent(this.content); 129 this.outputStream.close(); 130 } 131 catch (IOException e) { 132 log.error(e.getMessage()); 133 log.error("failed to pack content, deleteting temp file " + this.getZipFileName()); if (this.zipFile.exists()) { 135 this.zipFile.delete(); 136 } 137 throw e; 138 } 139 } 140 141 public String getZipFileName() { 142 return zipFileName; 143 } 144 145 public void setZipFileName(String zipFileName) { 146 this.zipFileName = zipFileName; 147 } 148 149 public File getZipFile() { 150 return zipFile; 151 } 152 153 public void setZipFile(File zipFile) { 154 this.zipFile = zipFile; 155 } 156 157 public ZipOutputStream getOutputStream() { 158 return outputStream; 159 } 160 161 public void setOutputStream(ZipOutputStream outputStream) { 162 this.outputStream = outputStream; 163 } 164 165 private void createTargetFile() throws IOException { 166 this.zipFileName = UUIDGenerator.getInstance().generateTimeBasedUUID().toString() + ".zip"; if (log.isDebugEnabled()) { 168 log.debug("Generating zip file " + this.zipFileName); } 170 this.zipFile = new File (Path.getTempDirectoryPath() + "/" + this.zipFileName); this.zipFile.createNewFile(); 172 } 173 174 private void addTextContent() throws IOException , RepositoryException { 175 XmlExport xmlExport = new XmlExport(); 176 xmlExport.setBinaryAsLink(true); 177 if (log.isDebugEnabled()) { 178 log.debug("adding a new zip file entry " + START_DIRECTORY + "/" + DATA_FILE_NAME); } 180 this.outputStream.putNextEntry(new ZipEntry (START_DIRECTORY + "/" + DATA_FILE_NAME)); xmlExport.exportContent(this.content, this.outputStream); 182 this.outputStream.closeEntry(); 183 } 184 185 private void addBinaryContent(Content content) throws IOException , RepositoryException { 186 Iterator dataNodes = content.getNodeDataCollection().iterator(); 187 while (dataNodes.hasNext()) { 188 NodeData nodeData = (NodeData) dataNodes.next(); 189 if (nodeData.getType() == PropertyType.BINARY) { 190 if (log.isDebugEnabled()) { 191 log.debug("adding a new zip file entry " + START_DIRECTORY + nodeData.getHandle()); } 193 this.outputStream.putNextEntry(new ZipEntry (START_DIRECTORY + nodeData.getHandle())); 194 InputStream is = nodeData.getStream(); 195 byte[] buffer = new byte[8192]; 196 int read = 0; 197 while ((read = is.read(buffer)) > 0) { 198 this.outputStream.write(buffer, 0, read); 199 } 200 this.outputStream.closeEntry(); 201 } 202 } 203 Iterator subNodes = content.getChildren(ItemType.NT_BASE).iterator(); 204 while (subNodes.hasNext()) { 205 this.addBinaryContent((Content) subNodes.next()); 206 } 207 } 208 } 209 210 } 211 | Popular Tags |