1 17 package org.apache.servicemix.components.util; 18 19 import org.apache.servicemix.expression.Expression; 20 import org.apache.servicemix.expression.PropertyExpression; 21 import org.apache.servicemix.jbi.NoMessageContentAvailableException; 22 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 23 import org.w3c.dom.Node ; 24 25 import javax.jbi.JBIException; 26 import javax.jbi.messaging.MessageExchange; 27 import javax.jbi.messaging.MessagingException; 28 import javax.jbi.messaging.NormalizedMessage; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.TransformerException ; 31 import javax.xml.transform.dom.DOMSource ; 32 import javax.xml.transform.stream.StreamResult ; 33 import javax.xml.transform.stream.StreamSource ; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.io.ObjectOutputStream ; 39 import java.io.OutputStream ; 40 import java.io.OutputStreamWriter ; 41 42 48 public class DefaultFileMarshaler extends MarshalerSupport implements FileMarshaler { 49 50 public static final String FILE_NAME_PROPERTY = "org.apache.servicemix.file.name"; 51 public static final String FILE_PATH_PROPERTY = "org.apache.servicemix.file.path"; 52 public static final String FILE_CONTENT = "org.apache.servicemix.file.content"; 53 54 protected static final PropertyExpression FILE_NAME_EXPRESSION = new PropertyExpression(FILE_NAME_PROPERTY); 55 protected static final PropertyExpression FILE_CONTENT_EXPRESSION = new PropertyExpression(FILE_CONTENT); 56 57 private Expression fileName = FILE_NAME_EXPRESSION; 58 private Expression content = FILE_CONTENT_EXPRESSION; 59 60 public void readMessage(MessageExchange exchange, NormalizedMessage message, InputStream in, String path) throws IOException , JBIException { 61 message.setContent(new StreamSource (in, path)); 62 message.setProperty(FILE_NAME_PROPERTY, new File (path).getName()); 63 message.setProperty(FILE_PATH_PROPERTY, path); 64 } 65 66 public String getOutputName(MessageExchange exchange, NormalizedMessage message) throws MessagingException { 67 return asString(fileName.evaluate(exchange, message)); 68 } 69 70 public void writeMessage(MessageExchange exchange, NormalizedMessage message, OutputStream out, String path) throws IOException , JBIException { 71 try { 72 Object value = content.evaluate(exchange, message); 73 if (value != null) { 74 writeValue(value, out); 75 } 76 else { 77 writeMessageContent(exchange, message, out, path); 78 } 79 } 80 catch (IOException e) { 81 throw new MessagingException(e); 82 } 83 } 84 85 public Expression getContent() { 88 return content; 89 } 90 91 public void setContent(Expression content) { 92 this.content = content; 93 } 94 95 public Expression getFileName() { 96 return fileName; 97 } 98 99 public void setFileName(Expression fileName) { 100 this.fileName = fileName; 101 } 102 103 106 112 protected void writeValue(Object value, OutputStream out) throws IOException , MessagingException { 113 if (value instanceof String ) { 114 OutputStreamWriter writer = new OutputStreamWriter (out); 115 writer.write((String ) value); 116 writer.flush(); 117 } 118 else if (value instanceof byte[]) { 119 out.write((byte[]) value); 120 } 121 else { 122 ObjectOutputStream objectOut = new ObjectOutputStream (out); 123 objectOut.writeObject(value); 124 } 125 } 126 127 134 protected void writeMessageContent(MessageExchange exchange, NormalizedMessage message, OutputStream out, String path) throws MessagingException { 135 Source content = null; 136 Node document = (Node ) message.getProperty(SourceTransformer.CONTENT_DOCUMENT_PROPERTY); 137 if (document != null) { 138 content = new DOMSource (document); 139 } 140 else { 141 content = message.getContent(); 142 } 143 if (content == null) { 144 throw new NoMessageContentAvailableException(exchange); 145 } 146 try { 147 getTransformer().toResult(content, new StreamResult (out)); 148 } catch (TransformerException e) { 149 throw new MessagingException(e); 150 } 151 } 152 } 153 | Popular Tags |