1 17 package org.apache.servicemix.components.util; 18 19 import org.apache.servicemix.jbi.jaxp.BytesSource; 20 import org.apache.servicemix.jbi.jaxp.ResourceSource; 21 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 22 import org.apache.servicemix.jbi.jaxp.StringSource; 23 import org.apache.servicemix.jbi.messaging.PojoMarshaler; 24 import org.xml.sax.SAXException ; 25 26 import javax.activation.DataHandler ; 27 import javax.jbi.messaging.MessageExchange; 28 import javax.jbi.messaging.MessagingException; 29 import javax.jbi.messaging.NormalizedMessage; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.TransformerException ; 33 import javax.xml.transform.sax.SAXSource ; 34 import javax.xml.transform.stream.StreamSource ; 35 36 import java.io.IOException ; 37 import java.util.Iterator ; 38 39 44 public class CopyTransformer implements MessageTransformer { 45 46 private static final CopyTransformer instance = new CopyTransformer(); 47 48 private SourceTransformer sourceTransformer = new SourceTransformer(); 49 50 private boolean copyProperties = true; 51 private boolean copyAttachments = true; 52 private boolean copySecuritySubject = true; 53 54 57 public boolean isCopyAttachments() { 58 return copyAttachments; 59 } 60 61 64 public void setCopyAttachments(boolean copyAttachments) { 65 this.copyAttachments = copyAttachments; 66 } 67 68 71 public boolean isCopyProperties() { 72 return copyProperties; 73 } 74 75 78 public void setCopyProperties(boolean copyProperties) { 79 this.copyProperties = copyProperties; 80 } 81 82 85 public boolean isCopySecuritySubject() { 86 return copySecuritySubject; 87 } 88 89 92 public void setCopySecuritySubject(boolean copySecuritySubject) { 93 this.copySecuritySubject = copySecuritySubject; 94 } 95 96 101 public static CopyTransformer getInstance() { 102 return instance; 103 } 104 105 public boolean transform(MessageExchange exchange, NormalizedMessage from, NormalizedMessage to) throws MessagingException { 106 if (copyProperties) { 107 copyProperties(from, to); 108 } 109 110 Source content = from.getContent(); 111 if ((content instanceof StreamSource || 112 content instanceof SAXSource ) && 113 !(content instanceof StringSource) && 114 !(content instanceof BytesSource) && 115 !(content instanceof ResourceSource)) { 116 try { 118 content = sourceTransformer.toDOMSource(from); 119 } 120 catch (TransformerException e) { 121 throw new MessagingException(e); 122 } 123 catch (ParserConfigurationException e) { 124 throw new MessagingException(e); 125 } 126 catch (IOException e) { 127 throw new MessagingException(e); 128 } 129 catch (SAXException e) { 130 throw new MessagingException(e); 131 } 132 } 133 to.setContent(content); 134 135 if (copyAttachments) { 136 copyAttachments(from, to); 137 } 138 139 if (copySecuritySubject) { 140 copySecuritySubject(from, to); 141 } 142 143 return true; 144 } 145 146 152 public static void copyProperties(NormalizedMessage from, NormalizedMessage to) { 153 for (Iterator iter = from.getPropertyNames().iterator(); iter.hasNext();) { 154 String name = (String ) iter.next(); 155 if (!SourceTransformer.CONTENT_DOCUMENT_PROPERTY.equals(name) && 157 !PojoMarshaler.BODY.equals(name)) 158 { 159 Object value = from.getProperty(name); 160 to.setProperty(name, value); 161 } 162 } 163 } 164 165 172 public static void copyAttachments(NormalizedMessage from, NormalizedMessage to) throws MessagingException { 173 for (Iterator iter = from.getAttachmentNames().iterator(); iter.hasNext();) { 174 String name = (String ) iter.next(); 175 DataHandler value = from.getAttachment(name); 176 to.addAttachment(name, value); 177 } 178 } 179 180 187 public static void copySecuritySubject(NormalizedMessage from, NormalizedMessage to) throws MessagingException { 188 to.setSecuritySubject(from.getSecuritySubject()); 189 } 190 191 } 192 | Popular Tags |