1 17 package org.apache.servicemix.components.util; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 24 import javax.activation.DataHandler ; 25 import javax.activation.DataSource ; 26 import javax.jbi.JBIException; 27 import javax.jbi.messaging.MessageExchange; 28 import javax.jbi.messaging.MessagingException; 29 import javax.jbi.messaging.NormalizedMessage; 30 31 import org.apache.servicemix.jbi.util.FileUtil; 32 import org.apache.servicemix.jbi.util.StreamDataSource; 33 34 40 public class BinaryFileMarshaler extends DefaultFileMarshaler { 41 42 private String attachment = "content"; 43 private String contentType = null; 44 45 public String getAttachment() { 46 return attachment; 47 } 48 49 public void setAttachment(String attachment) { 50 this.attachment = attachment; 51 } 52 53 public String getContentType() { 54 return contentType; 55 } 56 57 public void setContentType(String contentType) { 58 this.contentType = contentType; 59 } 60 61 public void readMessage(MessageExchange exchange, NormalizedMessage message, InputStream in, String path) throws IOException , JBIException { 62 DataSource ds = new StreamDataSource(in, contentType); 63 DataHandler handler = new DataHandler (ds); 64 message.addAttachment(attachment, handler); 65 message.setProperty(FILE_NAME_PROPERTY, new File (path).getName()); 66 message.setProperty(FILE_PATH_PROPERTY, path); 67 } 68 69 public void writeMessage(MessageExchange exchange, NormalizedMessage message, OutputStream out, String path) throws IOException , JBIException { 70 DataHandler handler = message.getAttachment(attachment); 71 if (handler == null) { 72 throw new MessagingException("Could not find attachment: " + attachment); 73 } 74 InputStream is = handler.getInputStream(); 75 FileUtil.copyInputStream(is, out); 76 } 77 78 } 79 | Popular Tags |