1 57 58 package org.apache.soap.rpc; 59 60 import java.io.*; 61 import java.net.*; 62 import java.util.*; 63 import java.lang.reflect.*; 64 import javax.xml.parsers.*; 65 import org.w3c.dom.*; 66 import org.xml.sax.*; 67 import org.apache.soap.util.xml.*; 68 import org.apache.soap.util.IOUtils; 69 import org.apache.soap.*; 70 import org.apache.soap.encoding.*; 71 import org.apache.soap.transport.*; 72 import org.apache.soap.transport.http.*; 73 import org.apache.soap.server.*; 74 import javax.mail.MessagingException ; 75 import javax.mail.internet.MimeBodyPart ; 76 77 85 public class Call extends RPCMessage 86 { 87 private DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder(); 88 private SOAPMappingRegistry smr = null; 89 private SOAPTransport st = null;; 90 private int to = 0; 91 92 public Call() 93 { 94 this(null, null, null, null, null); 95 } 96 97 public Call(String targetObjectURI, String methodName, Vector params, 98 Header header, String encodingStyleURI) 99 { 100 this(targetObjectURI, methodName, params, header, encodingStyleURI, 101 new SOAPContext()); 102 } 103 104 public Call(String targetObjectURI, String methodName, Vector params, 105 Header header, String encodingStyleURI, SOAPContext ctx) 106 { 107 super(targetObjectURI, methodName, params, header, encodingStyleURI, ctx); 108 } 109 110 public void setSOAPMappingRegistry(SOAPMappingRegistry smr) 111 { 112 this.smr = smr; 113 } 114 115 public SOAPMappingRegistry getSOAPMappingRegistry() 116 { 117 if (smr == null) 119 { 120 smr = new SOAPMappingRegistry(); 121 } 122 123 return smr; 124 } 125 126 public void setSOAPTransport(SOAPTransport st) 127 { 128 this.st = st; 129 } 130 131 public SOAPTransport getSOAPTransport() 132 { 133 return st; 134 } 135 136 141 public void setTimeout (int value) { 142 to = value; 143 } 144 145 150 public int getTimeout () { 151 return to; 152 } 153 154 160 public void addBodyPart(MimeBodyPart part) throws MessagingException 161 { 162 ctx.addBodyPart(part); 163 } 164 165 168 public void removeBodyPart(MimeBodyPart part) throws MessagingException 169 { 170 ctx.removeBodyPart(part); 171 } 172 173 public Envelope buildEnvelope() 174 { 175 return super.buildEnvelope(false); 176 } 177 178 public static Call extractFromEnvelope(Envelope env, ServiceManager svcMgr, 179 SOAPContext ctx) 180 throws IllegalArgumentException 181 { 182 return (Call)RPCMessage.extractFromEnvelope(env, svcMgr, false, null, ctx); 183 } 184 185 192 public static String getEnvelopeString(SOAPTransport st) 193 throws SOAPException, MessagingException , IOException { 194 SOAPContext respCtx = st.getResponseSOAPContext(); 195 BufferedReader in = null; 196 String payloadStr = null; 197 198 MimeBodyPart rootPart = respCtx.getRootPart(); 199 if (rootPart.isMimeType("text/*")) { 200 in = st.receive(); 202 payloadStr = IOUtils.getStringFromReader(in); 203 } 204 205 if (!rootPart.isMimeType(Constants.HEADERVAL_CONTENT_TYPE)) { 208 throw new SOAPException(Constants.FAULT_CODE_PROTOCOL, 209 "Unsupported response content type \"" + 210 rootPart.getContentType() + "\", must be: \"" + 211 Constants.HEADERVAL_CONTENT_TYPE + "\"." + 212 (payloadStr == null ? "" : " Response was:\n" + payloadStr)); 213 } 214 215 return payloadStr; 216 } 217 218 221 public Response invoke(URL url, String SOAPActionURI) throws SOAPException 222 { 223 if (SOAPActionURI == null) 224 { 225 SOAPActionURI = ""; 226 } 227 228 if (smr == null) 230 { 231 smr = new SOAPMappingRegistry(); 232 } 233 234 try 235 { 236 Envelope callEnv = buildEnvelope(); 238 239 if (st == null) 241 st = new SOAPHTTPConnection(); 242 243 if (to != 0 && st instanceof SOAPHTTPConnection) 245 ((SOAPHTTPConnection)st).setTimeout(to); 246 247 st.send(url, SOAPActionURI, null, callEnv, smr, ctx); 249 250 SOAPContext respCtx = st.getResponseSOAPContext(); 252 253 String payloadStr = getEnvelopeString(st); 256 257 Document respDoc = 259 xdb.parse(new InputSource(new StringReader(payloadStr))); 260 Element payload = null; 261 262 if (respDoc != null) 263 { 264 payload = respDoc.getDocumentElement(); 265 } 266 else { 268 throw new SOAPException (Constants.FAULT_CODE_CLIENT, 269 "Parsing error, response was:\n" + payloadStr); 270 } 271 272 Envelope respEnv = Envelope.unmarshall(payload, respCtx); 274 275 Response resp = Response.extractFromEnvelope(respEnv, smr, respCtx); 277 278 String fullTargetObjectURI = resp.getFullTargetObjectURI(); 280 281 if (fullTargetObjectURI != null) 282 { 283 setTargetObjectURI(fullTargetObjectURI); 284 } 285 286 return resp; 287 } 288 catch (MessagingException me) 289 { 290 throw new SOAPException(Constants.FAULT_CODE_CLIENT, me.getMessage(), me); 291 } 292 catch (IllegalArgumentException e) 293 { 294 throw new SOAPException(Constants.FAULT_CODE_CLIENT, e.getMessage(), e); 295 } 296 catch (SAXException e) 297 { 298 throw new SOAPException(Constants.FAULT_CODE_CLIENT, 299 "Parsing error, response was:\n" +e.getMessage(), 300 e); 301 } 302 catch (IOException e) 303 { 304 throw new SOAPException(Constants.FAULT_CODE_PROTOCOL, 305 e.getMessage(), e); 306 } 307 } 308 } 309 | Popular Tags |