1 17 package org.apache.servicemix.jbi.messaging; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.Externalizable ; 21 import java.io.IOException ; 22 import java.io.ObjectInput ; 23 import java.io.ObjectOutput ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import javax.activation.DataHandler ; 31 import javax.activation.DataSource ; 32 import javax.jbi.messaging.Fault; 33 import javax.jbi.messaging.MessageExchange; 34 import javax.jbi.messaging.MessagingException; 35 import javax.jbi.messaging.NormalizedMessage; 36 import javax.security.auth.Subject ; 37 import javax.xml.transform.Source ; 38 import javax.xml.transform.TransformerException ; 39 import javax.xml.transform.sax.SAXSource ; 40 import javax.xml.transform.stream.StreamSource ; 41 42 import org.apache.servicemix.client.Message; 43 import org.apache.servicemix.jbi.RuntimeJBIException; 44 import org.apache.servicemix.jbi.jaxp.BytesSource; 45 import org.apache.servicemix.jbi.jaxp.ResourceSource; 46 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 47 import org.apache.servicemix.jbi.jaxp.StringSource; 48 import org.apache.servicemix.jbi.util.ByteArrayDataSource; 49 import org.apache.servicemix.jbi.util.FileUtil; 50 51 56 public class NormalizedMessageImpl implements NormalizedMessage, Externalizable , Message { 57 58 private static final long serialVersionUID = 9179194301410526549L; 59 60 protected transient MessageExchangeImpl exchange; 61 private transient Source content; 62 private transient Object body; 63 private Subject securitySubject; 64 private Map properties; 65 private Map attachments; 66 67 private static SourceTransformer transformer = new SourceTransformer(); 68 69 73 public NormalizedMessageImpl() { 74 } 75 76 77 81 public NormalizedMessageImpl(MessageExchangeImpl exchange) { 82 this.exchange = exchange; 83 } 84 85 86 87 88 91 public Source getContent() { 92 if (content == null && body != null) { 93 try { 94 getMarshaler().marshal(exchange, this, body); 95 } 96 catch (MessagingException e) { 97 throw new RuntimeJBIException(e); 98 } 99 } 100 return content; 101 } 102 103 108 public void setContent(Source source) { 109 this.content = source; 110 } 111 112 115 public Subject getSecuritySubject() { 116 return securitySubject; 117 } 118 119 124 public void setSecuritySubject(Subject securitySubject) { 125 this.securitySubject = securitySubject; 126 } 127 128 134 public Object getProperty(String name) { 135 if (properties != null) { 136 return properties.get(name); 137 } 138 return null; 139 } 140 141 144 public Set getPropertyNames() { 145 if (properties != null) { 146 return Collections.unmodifiableSet(properties.keySet()); 147 } 148 return Collections.EMPTY_SET; 149 } 150 151 157 public void setProperty(String name, Object value) { 158 if (value == null) { 159 if (properties != null) { 160 properties.remove(name); 161 } 162 } else { 163 getProperties().put(name, value); 164 } 165 } 166 167 173 public void addAttachment(String id, DataHandler content) { 174 getAttachments().put(id, content.getDataSource()); 175 } 176 177 183 public DataHandler getAttachment(String id) { 184 if (attachments != null) { 185 return new DataHandler ((DataSource ) attachments.get(id)); 186 } 187 return null; 188 } 189 190 193 public Iterator listAttachments() { 194 if (attachments != null) { 195 return attachments.keySet().iterator(); 196 } 197 return Collections.EMPTY_LIST.iterator(); 198 } 199 200 205 public void removeAttachment(String id) { 206 if (attachments != null) { 207 attachments.remove(id); 208 } 209 } 210 211 214 public Set getAttachmentNames(){ 215 if (attachments != null){ 216 return Collections.unmodifiableSet(attachments.keySet()); 217 } 218 return Collections.EMPTY_SET; 219 } 220 221 222 public String toString() { 223 return super.toString() + "{properties: " + getProperties() + "}"; 224 } 225 226 230 public Object getBody() throws MessagingException { 231 if (body == null) { 232 body = getMarshaler().unmarshal(exchange, this); 233 } 234 return body; 235 } 236 237 public void setBody(Object body) throws MessagingException { 238 this.body = body; 239 } 240 241 public String getBodyText() throws TransformerException { 242 return transformer.toString(getContent()); 243 } 244 245 public void setBodyText(String xml) { 246 setContent(new StringSource(xml)); 247 } 248 249 public PojoMarshaler getMarshaler() { 250 return exchange.getMarshaler(); 251 } 252 253 public MessageExchange getExchange() { 254 return exchange; 255 } 256 257 public Fault createFault() throws MessagingException { 258 return getExchange().createFault(); 259 } 260 261 262 protected Map getProperties() { 265 if (properties == null) { 266 properties = createPropertiesMap(); 267 } 268 return properties; 269 } 270 271 protected Map getAttachments() { 272 if (attachments == null) { 273 attachments = createAttachmentsMap(); 274 } 275 return attachments; 276 } 277 278 protected void setAttachments(Map attachments) { 279 this.attachments = attachments; 280 } 281 282 protected void setProperties(Map properties) { 283 this.properties = properties; 284 } 285 286 protected Map createPropertiesMap() { 287 return new HashMap (); 289 } 290 291 protected Map createAttachmentsMap() { 292 return new HashMap (); 294 } 295 296 301 public void writeExternal(ObjectOutput out) throws IOException { 302 try { 303 convertAttachments(); 304 out.writeObject(attachments); 305 out.writeObject(properties); 306 String src = transformer.toString(content); 307 out.writeObject(src); 308 if ((content instanceof StreamSource || 311 content instanceof SAXSource ) && 312 !(content instanceof StringSource) && 313 !(content instanceof BytesSource) && 314 !(content instanceof ResourceSource)) { 315 content = new StringSource(src); 316 } 317 } catch (TransformerException e) { 318 throw (IOException ) new IOException ("Could not transform content to string").initCause(e); 319 } 320 } 321 322 private void convertAttachments() throws IOException { 323 if (attachments != null) { 324 Map newAttachments = createAttachmentsMap(); 325 for (Iterator it = attachments.keySet().iterator(); it.hasNext();) { 326 String name = (String ) it.next(); 327 DataSource ds = (DataSource ) attachments.get(name); 328 if (ds instanceof ByteArrayDataSource) { 329 newAttachments.put(name, ds); 330 } else { 331 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 332 FileUtil.copyInputStream(ds.getInputStream(), baos); 333 ByteArrayDataSource bads = new ByteArrayDataSource(baos.toByteArray(), ds.getContentType()); 334 bads.setName(ds.getName()); 335 newAttachments.put(name, bads); 336 } 337 } 338 attachments = newAttachments; 339 } 340 } 341 342 349 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 350 attachments = (Map ) in.readObject(); 351 properties = (Map ) in.readObject(); 352 String src = (String ) in.readObject(); 353 if (src != null) { 354 content = new StringSource(src); 355 } 356 } 357 358 } 359 360 | Popular Tags |