1 22 package org.objectweb.petals.component.common.util; 23 24 import java.net.URI ; 25 import java.util.HashSet ; 26 import java.util.Map ; 27 import java.util.Set ; 28 29 import javax.activation.DataHandler ; 30 import javax.jbi.messaging.ExchangeStatus; 31 import javax.jbi.messaging.Fault; 32 import javax.jbi.messaging.MessageExchange; 33 import javax.jbi.messaging.MessagingException; 34 import javax.jbi.messaging.NormalizedMessage; 35 import javax.jbi.servicedesc.ServiceEndpoint; 36 import javax.xml.namespace.QName ; 37 import javax.xml.transform.Source ; 38 39 import org.objectweb.petals.component.common.HandlingException; 40 import org.objectweb.petals.component.common.MEPConstants; 41 import org.objectweb.petals.component.common.PEtALSComponentSDKException; 42 43 49 public class MessageExchangeWrapperImpl implements MessageExchangeWrapper { 50 51 private MessageExchange exchange; 52 53 private static final String IN_MESSAGE_NAME = "in"; 54 55 private static final String OUT_MESSAGE_NAME = "out"; 56 57 public MessageExchangeWrapperImpl(MessageExchange exchange) { 58 super(); 59 this.exchange = exchange; 60 } 61 62 public MessageExchange getMessageExchange() { 63 return exchange; 64 } 65 66 public Fault createFault() throws MessagingException { 67 return exchange.createFault(); 68 } 69 70 public NormalizedMessage createMessage() throws MessagingException { 71 return exchange.createMessage(); 72 } 73 74 public ServiceEndpoint getEndpoint() { 75 return exchange.getEndpoint(); 76 } 77 78 public Exception getError() { 79 return exchange.getError(); 80 } 81 82 public String getExchangeId() { 83 return exchange.getExchangeId(); 84 } 85 86 public Fault getFault() { 87 return exchange.getFault(); 88 } 89 90 public QName getInterfaceName() { 91 return exchange.getInterfaceName(); 92 } 93 94 public NormalizedMessage getMessage(String name) { 95 return exchange.getMessage(name); 96 } 97 98 public QName getOperation() { 99 return exchange.getOperation(); 100 } 101 102 public URI getPattern() { 103 return exchange.getPattern(); 104 } 105 106 public Object getProperty(String name) { 107 return exchange.getProperty(name); 108 } 109 110 public Set getPropertyNames() { 111 return exchange.getPropertyNames(); 112 } 113 114 public Role getRole() { 115 return exchange.getRole(); 116 } 117 118 public QName getService() { 119 return exchange.getService(); 120 } 121 122 public ExchangeStatus getStatus() { 123 return exchange.getStatus(); 124 } 125 126 public boolean isTransacted() { 127 return exchange.isTransacted(); 128 } 129 130 public void setEndpoint(ServiceEndpoint endpoint) { 131 exchange.setEndpoint(endpoint); 132 } 133 134 public void setError(Exception error) { 135 exchange.setError(error); 136 } 137 138 public void setFault(Fault fault) throws MessagingException { 139 exchange.setFault(fault); 140 } 141 142 public void setInterfaceName(QName interfaceName) { 143 exchange.setInterfaceName(interfaceName); 144 } 145 146 public void setMessage(NormalizedMessage msg, String name) 147 throws MessagingException { 148 exchange.setMessage(msg, name); 149 } 150 151 public void setOperation(QName name) { 152 exchange.setOperation(name); 153 } 154 155 public void setProperty(String name, Object obj) { 156 exchange.setProperty(name, obj); 157 } 158 159 public void setService(QName service) { 160 exchange.setService(service); 161 } 162 163 public void setStatus(ExchangeStatus status) throws MessagingException { 164 exchange.setStatus(status); 165 } 166 167 public void setOutMessageContent(String outContent) 168 throws HandlingException { 169 try { 170 NormalizedMessage outMess = getOutMessage(); 171 if (outMess == null) { 172 outMess = exchange.createMessage(); 173 } 174 outMess.setContent(SourceHelper.createSource(outContent)); 175 setOutMessage(outMess); 176 } catch (MessagingException e) { 177 throw new HandlingException("Error creating response message"); 178 } catch (PEtALSComponentSDKException e) { 179 throw new HandlingException("Error creating response message"); 180 } 181 } 182 183 public void setOutMessageAttachements(Map <String , DataHandler > attachements) 184 throws HandlingException { 185 try { 186 NormalizedMessage outMess = getOutMessage(); 187 if (outMess == null) { 188 outMess = exchange.createMessage(); 189 } 190 for (String attachementId : attachements.keySet()) { 191 outMess.addAttachment(attachementId, attachements 192 .get(attachementId)); 193 } 194 setOutMessage(outMess); 195 } catch (MessagingException e) { 196 throw new HandlingException("Error creating response message"); 197 } 198 } 199 200 public String getInMessageContent() throws HandlingException { 201 NormalizedMessage inMessage = getInMessage(); 202 if (inMessage == null) { 203 throw new HandlingException("In message must not be null"); 204 } 205 Source content = inMessage.getContent(); 206 String stringContent = null; 207 try { 208 stringContent = SourceHelper.createString(content); 209 } catch (PEtALSComponentSDKException e) { 210 throw new HandlingException("Error creating string from source", e); 211 } 212 return stringContent; 213 } 214 215 public Set <DataHandler > getInMessageAttachments() throws HandlingException { 216 Set <DataHandler > attachments = new HashSet <DataHandler >(); 217 218 NormalizedMessage inMessage = getInMessage(); 219 if (inMessage == null) { 220 throw new HandlingException("In message must not be null"); 221 } 222 223 Set names = inMessage.getAttachmentNames(); 224 for (Object attachId : names) { 225 DataHandler dh = inMessage.getAttachment((String ) attachId); 226 if (dh != null) 227 attachments.add(dh); 228 } 229 return attachments; 230 } 231 232 public URI getExchangePattern() throws HandlingException { 233 URI pattern = exchange.getPattern(); 234 if (pattern == null) { 235 throw new HandlingException( 236 "You must specify a message exchange pattern in the message exchange"); 237 } 238 return pattern; 239 } 240 241 public String getOperationName() throws HandlingException { 242 QName operation = exchange.getOperation(); 243 if (operation == null) { 244 throw new HandlingException( 245 "You must specify an operation the message exchange"); 246 } 247 return operation.getLocalPart(); 248 } 249 250 public String getEndpointName() throws HandlingException { 251 ServiceEndpoint ep = exchange.getEndpoint(); 252 if (ep == null) { 253 throw new HandlingException( 254 "You must specify an endpoint in the message exchange"); 255 } 256 return ep.getEndpointName(); 257 } 258 259 public NormalizedMessage getInMessage() { 260 return exchange.getMessage(IN_MESSAGE_NAME); 261 } 262 263 public void setInMessage(final NormalizedMessage msg) 264 throws MessagingException { 265 exchange.setMessage(msg, IN_MESSAGE_NAME); 266 } 267 268 public NormalizedMessage getOutMessage() { 269 return exchange.getMessage(OUT_MESSAGE_NAME); 270 } 271 272 public void setOutMessage(final NormalizedMessage msg) 273 throws MessagingException { 274 exchange.setMessage(msg, OUT_MESSAGE_NAME); 275 } 276 277 278 279 public void setDoneStatus() throws MessagingException { 280 setStatus(ExchangeStatus.DONE); 281 } 282 283 public boolean isDoneStatus() { 284 return (ExchangeStatus.DONE.equals(getStatus())); 285 } 286 287 public void setErrorStatus() throws MessagingException { 288 setStatus(ExchangeStatus.ERROR); 289 } 290 291 public boolean isErrorStatus() { 292 return (ExchangeStatus.ERROR.equals(getStatus())); 293 } 294 295 public void setActiveStatus() throws MessagingException { 296 setStatus(ExchangeStatus.ACTIVE); 297 } 298 299 public boolean isActiveStatus() { 300 return (ExchangeStatus.ACTIVE.equals(getStatus())); 301 } 302 303 304 305 public boolean isProviderRole() { 306 return (MessageExchange.Role.PROVIDER.equals(getRole())); 307 } 308 309 public boolean isConsumerRole() { 310 return (MessageExchange.Role.CONSUMER.equals(getRole())); 311 } 312 313 314 315 public boolean isInOnlyPattern() { 316 return (MEPConstants.IN_ONLY_PATTERN.value().equals( 317 exchange.getPattern())); 318 } 319 320 public boolean isRobustInOnlyPattern() { 321 return (MEPConstants.ROBUST_IN_ONLY_PATTERN.value().equals( 322 exchange.getPattern())); 323 } 324 325 public boolean isInOutPattern() { 326 return (MEPConstants.IN_OUT_PATTERN.value().equals( 327 exchange.getPattern())); 328 } 329 330 public boolean isInOptionalOutPattern() { 331 return (MEPConstants.IN_OPTIONAL_OUT_PATTERN.value().equals( 332 exchange.getPattern())); 333 } 334 } 335 | Popular Tags |