1 57 58 package org.apache.soap.messaging; 59 60 import java.io.*; 61 import java.util.*; 62 import java.net.URL ; 63 import javax.xml.parsers.*; 64 import org.w3c.dom.*; 65 import org.xml.sax.*; 66 import org.apache.soap.util.*; 67 import org.apache.soap.util.xml.*; 68 import org.apache.soap.rpc.SOAPContext; 69 import org.apache.soap.rpc.Call; 70 import org.apache.soap.*; 71 import org.apache.soap.transport.*; 72 import org.apache.soap.transport.http.*; 73 import javax.mail.*; 74 import javax.mail.internet.*; 75 import javax.activation.*; 76 77 88 public class Message { 89 SOAPTransport st; 90 SOAPContext reqCtx = new SOAPContext (), resCtx = null; 91 DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder(); 92 93 public Message () { 94 } 95 96 public void setSOAPTransport (SOAPTransport st) { 97 this.st = st; 98 } 99 100 public SOAPTransport getSOAPTransport () { 101 return st; 102 } 103 104 115 public void send (URL url, String actionURI, Envelope env) 116 throws SOAPException { 117 if (st == null) { 119 st = new SOAPHTTPConnection (); 120 } 121 122 st.send (url, actionURI, null, env, null, reqCtx); 124 } 125 126 136 public Envelope receiveEnvelope () throws SOAPException { 137 if (st == null) { 138 throw new SOAPException (Constants.FAULT_CODE_CLIENT, 139 "Unable to receive without sending on an " + 140 "appropriate transport."); 141 } 142 try { 143 resCtx = st.getResponseSOAPContext (); 144 String payloadStr = Call.getEnvelopeString (st); 145 146 Document doc = 147 xdb.parse(new InputSource(new StringReader(payloadStr))); 148 149 if (doc == null) { 150 throw new SOAPException (Constants.FAULT_CODE_CLIENT, 151 "Parsing error, response was:\n" + 152 payloadStr); 153 } 154 155 return Envelope.unmarshall (doc.getDocumentElement (), resCtx); 156 } catch (MessagingException me) { 157 throw new SOAPException (Constants.FAULT_CODE_CLIENT, me.getMessage (), 158 me); 159 } catch (SAXException ioe) { 160 throw new SOAPException (Constants.FAULT_CODE_CLIENT, 161 "Parsing error, response was:\n" + 162 ioe.getMessage(), ioe); 163 } catch (IOException ioe) { 164 throw new SOAPException (Constants.FAULT_CODE_CLIENT, ioe.getMessage (), 165 ioe); 166 } catch (IllegalArgumentException e) { 167 throw new SOAPException (Constants.FAULT_CODE_CLIENT, e.getMessage (), 168 e); 169 } 170 } 171 172 179 public DataHandler receive() throws SOAPException, MessagingException { 180 if (st == null) { 181 throw new SOAPException(Constants.FAULT_CODE_CLIENT, 182 "Unable to receive without sending on an " + 183 "appropriate transport."); 184 } 185 st.receive(); 186 resCtx = st.getResponseSOAPContext(); 187 return resCtx.getRootPart().getDataHandler(); 188 } 189 190 194 public SOAPContext getRequestSOAPContext() { 195 return reqCtx; 196 } 197 198 204 public void addBodyPart(MimeBodyPart part) throws MessagingException { 205 reqCtx.addBodyPart(part); 206 } 207 208 212 public SOAPContext getResponseSOAPContext() { 213 return resCtx; 214 } 215 216 232 public MimeBodyPart findBodyPart(String uri) { 233 if (resCtx == null) 234 return null; 235 else 236 return resCtx.findBodyPart(uri); 237 } 238 239 246 public int getPartCount() throws MessagingException { 247 if (resCtx == null) 248 return 0; 249 else 250 return resCtx.getCount(); 251 } 252 253 261 public MimeBodyPart getBodyPart(int index) throws IndexOutOfBoundsException { 262 if (resCtx == null) 263 return null; 264 else 265 return resCtx.getBodyPart(index); 266 } 267 268 274 public MimeBodyPart getRootPart() throws MessagingException { 275 if (resCtx == null) 276 return null; 277 else 278 return resCtx.getRootPart(); 279 } 280 } 281 | Popular Tags |