1 57 58 package org.apache.wsif.providers.soap.apachesoap; 59 60 import java.io.BufferedReader ; 61 import java.io.IOException ; 62 import java.io.Serializable ; 63 import java.io.StringReader ; 64 import java.io.StringWriter ; 65 import java.net.URL ; 66 import java.util.Hashtable ; 67 import java.util.HashMap ; 68 69 import javax.mail.MessagingException ; 70 import org.apache.soap.Envelope; 71 import org.apache.soap.SOAPException; 72 import org.apache.soap.encoding.SOAPMappingRegistry; 73 import org.apache.soap.rpc.SOAPContext; 74 import org.apache.soap.transport.SOAPTransport; 75 import org.apache.wsif.WSIFCorrelationId; 76 import org.apache.wsif.WSIFCorrelationService; 77 import org.apache.wsif.WSIFException; 78 import org.apache.wsif.logging.Trc; 79 import org.apache.wsif.util.WSIFCorrelationServiceLocator; 80 import org.apache.wsif.util.WSIFProperties; 81 import org.apache.wsif.util.jms.WSIFJMSCorrelationId; 82 import org.apache.wsif.util.jms.WSIFJMSDestination; 83 import org.apache.wsif.util.jms.WSIFJMSFinder; 84 import org.apache.wsif.wsdl.extensions.jms.JMSAddress; 85 86 91 public class SOAPJMSConnection implements SOAPTransport { 92 private BufferedReader responseReader = null; 93 private SOAPContext responseSOAPContext = null; 94 private WSIFJMSDestination destination = null; 95 96 private boolean asyncOperation = false; 98 private WSIFOperation_ApacheSOAP wsifOperation = null; 99 private long syncTimeout; 100 private long asyncTimeout; 101 102 private static final String DUMMY_RESPONSE = 103 "<?xml version='1.0' encoding='UTF-8'?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">\n<SOAP-ENV:Body>\n<ns1:addEntryResponse xmlns:ns1=\"http://wsifservice.addressbook/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n</ns1:addEntryResponse>\n\n</SOAP-ENV:Body>\n</SOAP-ENV:Envelope>"; 104 105 public SOAPJMSConnection(JMSAddress ja, String portName) throws WSIFException { 106 Trc.entry(this, ja, portName); 107 destination = 108 new WSIFJMSDestination( 109 WSIFJMSFinder.newFinder(ja,portName), 110 ja.getJmsProvDestName(), 111 WSIFProperties.getSyncTimeout()); 112 Trc.exit(ja); 113 } 114 115 public Hashtable getHeaders() { 116 Trc.entry(this); 117 Trc.exit(null); 118 return null; 119 } 120 121 public SOAPContext getResponseSOAPContext() { 122 Trc.entry(this); 123 Trc.exit(responseSOAPContext); 124 return responseSOAPContext; 125 } 126 127 public long getSyncTimeout() { 128 Trc.entry(this); 129 Trc.exit(new Long (syncTimeout)); 130 return syncTimeout; 131 } 132 133 public long getAsyncTimeout() { 134 Trc.entry(this); 135 Trc.exit(new Long (asyncTimeout)); 136 return asyncTimeout; 137 } 138 139 public BufferedReader receive() { 140 Trc.entry(this); 141 Trc.exit(); 142 return responseReader; 143 } 144 145 150 public void send( 151 URL sendTo, 152 String action, 153 Hashtable headers, 154 Envelope env, 155 SOAPMappingRegistry smr, 156 SOAPContext ctx) 157 throws SOAPException { 158 Trc.entry(this, sendTo, action, headers, env, smr, ctx); 159 160 try { 161 if (isAsyncOperation()) { 162 performAsyncSend(sendTo, action, headers, env, smr, ctx); 163 } else { 164 StringWriter payloadSW = new StringWriter (); 165 env.marshall(payloadSW, smr, ctx); 166 String id = destination.send(payloadSW.toString(), null); 167 String response = destination.receiveString(id, syncTimeout); 168 responseSOAPContext = new SOAPContext(); 169 responseSOAPContext.setRootPart(response, "text/xml"); 170 responseReader = new BufferedReader (new StringReader (response)); 171 } 172 } catch (IOException ioe) { 173 Trc.exception(ioe); 174 throw new SOAPException("WSIF SOAPJMSConnection ", ioe.toString()); 176 } catch (MessagingException me) { 177 Trc.exception(me); 178 throw new SOAPException("WSIF SOAPJMSConnection ", me.toString()); 180 } 181 Trc.exit(); 182 } 183 184 191 private void performAsyncSend( 192 URL sendTo, 193 String action, 194 Hashtable headers, 195 Envelope env, 196 SOAPMappingRegistry smr, 197 SOAPContext ctx) 198 throws IOException , SOAPException, MessagingException { 199 String msgID; 200 StringWriter payloadSW = new StringWriter (); 201 env.marshall(payloadSW, smr, ctx); 202 WSIFOperation_ApacheSOAP wsifOp = (WSIFOperation_ApacheSOAP) getWsifOperation(); 203 204 WSIFCorrelationId cid; 205 206 if ( wsifOp.getResponseHandler() == null ) { 208 msgID = destination.send(payloadSW.toString(), null); 209 cid = new WSIFJMSCorrelationId( msgID ); 210 } else { 211 WSIFCorrelationService correlator = 212 WSIFCorrelationServiceLocator.getCorrelationService(); 213 synchronized( correlator ) { 214 msgID = destination.send(payloadSW.toString(), null); 215 cid = new WSIFJMSCorrelationId( msgID ); 216 if ( correlator != null ) { 217 correlator.put( 218 cid, 219 (Serializable )getWsifOperation(), 220 asyncTimeout ); 221 } 222 } 223 } 224 225 226 wsifOp.setAsyncRequestID(new WSIFJMSCorrelationId(msgID)); 227 228 responseSOAPContext = new SOAPContext(); 230 responseSOAPContext.setRootPart(DUMMY_RESPONSE, "text/xml"); 231 responseReader = new BufferedReader (new StringReader (DUMMY_RESPONSE)); 232 233 } 234 235 public void setAsyncOperation(boolean b) { 236 Trc.entry(this, b); 237 asyncOperation = b; 238 Trc.exit(); 239 } 240 241 public boolean isAsyncOperation() { 242 Trc.entry(this); 243 Trc.exit(asyncOperation); 244 return asyncOperation; 245 } 246 247 public WSIFOperation_ApacheSOAP getWsifOperation() { 248 Trc.entry(this); 249 Trc.exit(wsifOperation); 250 return wsifOperation; 251 } 252 253 public void setWsifOperation(WSIFOperation_ApacheSOAP op) { 254 Trc.entry(this, op); 255 wsifOperation = op; 256 Trc.exit(); 257 } 258 259 public void setJmsProperty(String name, Object value) throws WSIFException { 260 Trc.entry(this, name, value); 261 destination.setProperty(name, value); 262 Trc.exit(); 263 } 264 265 public void setJmsProperties(HashMap hm) { 266 Trc.entry(this,hm); 267 destination.setProperties(hm); 268 Trc.exit(); 269 } 270 271 public HashMap getJmsProperties() throws WSIFException { 272 Trc.entry(this); 273 HashMap hm = destination.getProperties(); 274 Trc.exit(hm); 275 return hm; 276 } 277 278 void close() throws WSIFException { 279 Trc.entry(this); 280 destination.close(); 281 Trc.exit(); 282 } 283 284 public void setSyncTimeout(long timeout) { 285 Trc.entry(this, new Long (timeout)); 286 syncTimeout = timeout; 287 Trc.exit(); 288 } 289 290 public void setAsyncTimeout(long timeout) { 291 Trc.entry(this, new Long (timeout)); 292 asyncTimeout = timeout; 293 Trc.exit(); 294 } 295 } | Popular Tags |