1 22 package org.objectweb.petals.component.common.util; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.text.SimpleDateFormat ; 29 import java.util.Date ; 30 import java.util.Set ; 31 32 import javax.activation.DataHandler ; 33 import javax.jbi.messaging.MessagingException; 34 import javax.jbi.messaging.NormalizedMessage; 35 36 42 public class NormalizedMessageUtil { 43 44 47 private static final String DATE_FORMAT = "ddMMyyHHmmssSSS"; 48 49 56 public static void copyAttachments(final NormalizedMessage from, 57 NormalizedMessage to) throws MessagingException { 58 Set attachmentIds = from.getAttachmentNames(); 59 for (Object id : attachmentIds) { 60 String attachName = (String ) id; 61 DataHandler dh = from.getAttachment(attachName); 62 to.addAttachment(attachName, dh); 63 } 64 } 65 66 72 public static void copyProperties(final NormalizedMessage from, 73 NormalizedMessage to) { 74 Set propertiesNames = from.getPropertyNames(); 75 for (Object propertyName : propertiesNames) { 76 String name = (String ) propertyName; 77 Object value = from.getProperty(name); 78 to.setProperty(name, value); 79 } 80 } 81 82 88 public static void copyContent(final NormalizedMessage from, 89 NormalizedMessage to) throws MessagingException { 90 to.setContent(from.getContent()); 91 } 92 93 102 public static void writeAttachmentToFile(final DataHandler handler, 103 final File destDir) { 104 FileOutputStream fos = null; 105 106 try { 107 if (!destDir.exists()) 108 destDir.mkdirs(); 109 110 File outFile = new File (destDir.getAbsolutePath(), handler 112 .getName()); 113 114 String ext = ""; 116 if (outFile.exists()) { 117 SimpleDateFormat sdf = new SimpleDateFormat (DATE_FORMAT); 118 ext = sdf.format(new Date (System.currentTimeMillis())); 119 } 120 121 fos = new FileOutputStream ( 122 new File (outFile.getAbsoluteFile() + ext)); 123 124 handler.writeTo(fos); 125 fos.flush(); 126 127 } catch (FileNotFoundException e) { 128 } catch (IOException e) { 129 } finally { 130 try { 131 fos.close(); 132 } catch (IOException e) { 133 } 134 } 135 } 136 137 144 public static void writeAttachmentsToFiles(final NormalizedMessage nm, 145 final File destDir) { 146 147 Set attachments = nm.getAttachmentNames(); 148 for (Object attachmentId : attachments) { 149 writeAttachmentToFile(nm.getAttachment((String ) attachmentId), 150 destDir); 151 } 152 } 153 154 } 155 | Popular Tags |