1 17 package org.apache.servicemix.components.saaj; 18 19 import java.io.ByteArrayOutputStream ; 20 21 import javax.jbi.messaging.MessageExchange; 22 import javax.jbi.messaging.NormalizedMessage; 23 import javax.xml.soap.MimeHeaders ; 24 import javax.xml.soap.SOAPConnection ; 25 import javax.xml.soap.SOAPConnectionFactory ; 26 import javax.xml.soap.SOAPException ; 27 import javax.xml.soap.SOAPMessage ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.servicemix.MessageExchangeListener; 32 import org.apache.servicemix.components.util.TransformComponentSupport; 33 34 41 public class SaajBinding extends TransformComponentSupport implements MessageExchangeListener { 42 43 private static final transient Log log = LogFactory.getLog(SaajBinding.class); 44 45 private SaajMarshaler marshaler = new SaajMarshaler(); 46 private SOAPConnectionFactory connectionFactory; 47 private Object soapEndpoint; 48 private String soapAction; 49 50 51 public SOAPConnectionFactory getConnectionFactory() throws SOAPException { 52 if (connectionFactory == null) { 53 connectionFactory = createConnectionFactory(); 54 } 55 return connectionFactory; 56 } 57 58 public void setConnectionFactory(SOAPConnectionFactory connectionFactory) { 59 this.connectionFactory = connectionFactory; 60 } 61 62 public Object getSoapEndpoint() { 63 return soapEndpoint; 64 } 65 66 public void setSoapEndpoint(Object soapEndpoint) { 67 this.soapEndpoint = soapEndpoint; 68 } 69 70 73 public SaajMarshaler getMarshaller() { 74 return marshaler; 75 } 76 77 80 public void setMarshaller(SaajMarshaler marshaler) { 81 this.marshaler = marshaler; 82 } 83 84 87 public SaajMarshaler getMarshaler() { 88 return marshaler; 89 } 90 91 94 public void setMarshaler(SaajMarshaler marshaler) { 95 this.marshaler = marshaler; 96 } 97 98 protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception { 99 SOAPConnection connection = getConnectionFactory().createConnection(); 100 try { 101 SOAPMessage inMessage = marshaler.createSOAPMessage(in); 102 MimeHeaders mh = inMessage.getMimeHeaders(); 103 if (mh.getHeader("SOAPAction") == null) { 104 if (soapAction != null && soapAction.length() > 0) { 105 mh.addHeader("SOAPAction", soapAction); 106 } else { 107 mh.addHeader("SOAPAction", "\"\""); 108 } 109 inMessage.saveChanges(); 110 } 111 112 if (log.isDebugEnabled()) { 113 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 114 inMessage.writeTo(buffer); 115 log.debug(new String (buffer.toByteArray())); 116 } 117 118 SOAPMessage response = connection.call(inMessage, soapEndpoint); 119 if (response != null) { 120 marshaler.toNMS(out, response); 121 return true; 122 } else { 123 return false; 124 } 125 } 126 finally { 127 try { 128 connection.close(); 129 } 130 catch (SOAPException e) { 131 log.warn("Failed to close connection: " + e, e); 132 } 133 } 134 } 135 136 protected SOAPConnectionFactory createConnectionFactory() throws SOAPException { 137 return SOAPConnectionFactory.newInstance(); 138 } 139 140 141 protected SOAPConnection createConnection() throws SOAPException { 142 return getConnectionFactory().createConnection(); 143 } 144 145 148 public String getSoapAction() { 149 return soapAction; 150 } 151 152 156 public void setSoapAction(String soapAction) { 157 this.soapAction = soapAction; 158 } 159 } 160 | Popular Tags |