1 17 package org.apache.servicemix.jbi.util; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.Serializable ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.activation.DataHandler ; 27 import javax.activation.DataSource ; 28 import javax.jbi.messaging.Fault; 29 import javax.jbi.messaging.MessageExchange; 30 import javax.jbi.messaging.MessagingException; 31 import javax.jbi.messaging.NormalizedMessage; 32 import javax.security.auth.Subject ; 33 import javax.xml.transform.Source ; 34 35 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 36 import org.apache.servicemix.jbi.jaxp.StringSource; 37 import org.apache.servicemix.jbi.util.ByteArrayDataSource; 38 import org.apache.servicemix.jbi.util.FileUtil; 39 40 44 public class MessageUtil { 45 46 public static void transfer(NormalizedMessage source, NormalizedMessage dest) throws Exception { 47 dest.setContent(source.getContent()); 48 for (Iterator it = source.getPropertyNames().iterator(); it.hasNext();) { 49 String name = (String ) it.next(); 50 dest.setProperty(name, source.getProperty(name)); 51 } 52 for (Iterator it = source.getAttachmentNames().iterator(); it.hasNext();) { 53 String name = (String ) it.next(); 54 dest.addAttachment(name, source.getAttachment(name)); 55 } 56 dest.setSecuritySubject(source.getSecuritySubject()); 57 } 58 59 public static NormalizedMessage copy(NormalizedMessage source) throws Exception { 60 if (source instanceof Fault) { 61 return new FaultImpl((Fault) source); 62 } else { 63 return new NormalizedMessageImpl(source); 64 } 65 } 66 67 public static NormalizedMessage copyIn(MessageExchange exchange) throws Exception { 68 return copy(exchange.getMessage("in")); 69 } 70 71 public static NormalizedMessage copyOut(MessageExchange exchange) throws Exception { 72 return copy(exchange.getMessage("out")); 73 } 74 75 public static Fault copyFault(MessageExchange exchange) throws Exception { 76 return (Fault) copy(exchange.getMessage("fault")); 77 } 78 79 public static void transferInToIn(MessageExchange source, MessageExchange dest) throws Exception { 80 transferToIn(source.getMessage("in"), dest); 81 } 82 83 public static void transferOutToIn(MessageExchange source, MessageExchange dest) throws Exception { 84 transferToIn(source.getMessage("out"), dest); 85 } 86 87 public static void transferToIn(NormalizedMessage sourceMsg, MessageExchange dest) throws Exception { 88 transferTo(sourceMsg, dest, "in"); 89 } 90 91 public static void transferOutToOut(MessageExchange source, MessageExchange dest) throws Exception { 92 transferToOut(source.getMessage("out"), dest); 93 } 94 95 public static void transferInToOut(MessageExchange source, MessageExchange dest) throws Exception { 96 transferToOut(source.getMessage("in"), dest); 97 } 98 99 public static void transferToOut(NormalizedMessage sourceMsg, MessageExchange dest) throws Exception { 100 transferTo(sourceMsg, dest, "out"); 101 } 102 103 public static void transferFaultToFault(MessageExchange source, MessageExchange dest) throws Exception { 104 transferToFault(source.getFault(), dest); 105 } 106 107 public static void transferToFault(Fault fault, MessageExchange dest) throws Exception { 108 transferTo(fault, dest, "fault"); 109 } 110 111 public static void transferTo(NormalizedMessage sourceMsg, MessageExchange dest, String name) throws Exception { 112 NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest.createFault() : dest.createMessage(); 113 transfer(sourceMsg, destMsg); 114 dest.setMessage(destMsg, name); 115 } 116 117 public static void transferTo(MessageExchange source, MessageExchange dest, String name) throws Exception { 118 NormalizedMessage sourceMsg = source.getMessage(name); 119 NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest.createFault() : dest.createMessage(); 120 transfer(sourceMsg, destMsg); 121 dest.setMessage(destMsg, name); 122 } 123 124 private static class NormalizedMessageImpl implements NormalizedMessage, Serializable { 125 126 private static final long serialVersionUID = -5813947566001096708L; 127 128 private Subject subject; 129 private Source content; 130 private Map properties = new HashMap (); 131 private Map attachments = new HashMap (); 132 133 public NormalizedMessageImpl(NormalizedMessage message) throws Exception { 134 String str = new SourceTransformer().contentToString(message); 135 if (str != null) { 136 this.content = new StringSource(str); 137 } 138 for (Iterator it = message.getPropertyNames().iterator(); it.hasNext();) { 139 String name = (String ) it.next(); 140 this.properties.put(name, message.getProperty(name)); 141 } 142 for (Iterator it = message.getAttachmentNames().iterator(); it.hasNext();) { 143 String name = (String ) it.next(); 144 DataHandler dh = message.getAttachment(name); 145 DataSource ds = dh.getDataSource(); 146 if (ds instanceof ByteArrayDataSource == false) { 147 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 148 FileUtil.copyInputStream(ds.getInputStream(), baos); 149 ByteArrayDataSource bads = new ByteArrayDataSource(baos.toByteArray(), ds.getContentType()); 150 bads.setName(ds.getName()); 151 dh = new DataHandler (bads); 152 } 153 this.attachments.put(name, dh); 154 } 155 this.subject = message.getSecuritySubject(); 156 } 157 158 public void addAttachment(String id, DataHandler content) throws MessagingException { 159 this.attachments.put(id, content); 160 } 161 162 public Source getContent() { 163 return content; 164 } 165 166 public DataHandler getAttachment(String id) { 167 return (DataHandler ) this.attachments.get(id); 168 } 169 170 public Set getAttachmentNames() { 171 return this.attachments.keySet(); 172 } 173 174 public void removeAttachment(String id) throws MessagingException { 175 this.attachments.remove(id); 176 } 177 178 public void setContent(Source content) throws MessagingException { 179 this.content = content; 180 } 181 182 public void setProperty(String name, Object value) { 183 this.properties.put(name, value); 184 } 185 186 public void setSecuritySubject(Subject subject) { 187 this.subject = subject; 188 } 189 190 public Set getPropertyNames() { 191 return this.properties.keySet(); 192 } 193 194 public Object getProperty(String name) { 195 return this.properties.get(name); 196 } 197 198 public Subject getSecuritySubject() { 199 return this.subject; 200 } 201 202 } 203 204 private static class FaultImpl extends NormalizedMessageImpl implements Fault { 205 private static final long serialVersionUID = -6076815664102825860L; 206 207 public FaultImpl(Fault fault) throws Exception { 208 super(fault); 209 } 210 } 211 212 } 213 | Popular Tags |