1 57 58 package org.apache.wsif.providers.soap.soaprmi; 59 60 import java.io.BufferedWriter ; 61 import java.io.IOException ; 62 import java.io.Reader ; 63 import java.io.StringWriter ; 64 import java.io.Writer ; 65 import java.net.URL ; 66 import java.util.HashMap ; 67 import java.util.Iterator ; 68 import java.util.List ; 69 import java.util.Map ; 70 import java.util.Vector ; 71 72 import javax.wsdl.Definition; 73 import javax.wsdl.Input; 74 import javax.wsdl.Operation; 75 import javax.wsdl.Output; 76 import javax.wsdl.Part; 77 import javax.xml.namespace.QName ; 78 79 import org.apache.wsif.WSIFException; 80 import org.apache.wsif.WSIFMessage; 81 import org.apache.wsif.WSIFOperation; 82 import org.apache.wsif.WSIFPort; 83 import org.apache.wsif.base.WSIFDefaultOperation; 84 import org.apache.wsif.logging.Trc; 85 import org.apache.wsif.providers.WSIFDynamicTypeMap; 86 import org.apache.wsif.providers.WSIFDynamicTypeMapping; 87 import soaprmi.mapping.XmlJavaMapping; 88 import soaprmi.mapping.XmlJavaTypeMap; 89 import soaprmi.mapping.XmlMapException; 90 import soaprmi.soap.Soap; 91 import soaprmi.soaprpc.MethodInvoker; 92 import soaprmi.util.HTTPUtils; 93 94 100 public class WSIFOperation_SoapRMI 101 extends WSIFDefaultOperation 102 implements WSIFOperation { 103 104 private static final long serialVersionUID = 1L; 105 106 protected WSIFPort_SoapRMI portInstance; 107 protected Operation operation; 108 protected Definition definition; 109 110 protected List partNames; 112 protected String [] names; 113 protected Class [] types; 114 protected String inputEncodingStyle = Soap.SOAP_ENC_NS; 115 protected String inputNamespace; 116 117 protected String returnName; 118 protected Class returnType; 119 protected String outputEncodingStyle = Soap.SOAP_ENC_NS; 120 protected String actionUri; 121 protected WSIFDynamicTypeMap typeMap; 122 protected XmlJavaMapping soaprmiMapping; 123 124 128 public WSIFOperation_SoapRMI( 129 WSIFPort_SoapRMI pi, 130 Operation op, 131 WSIFDynamicTypeMap typeMap) 132 throws WSIFException { 133 this.typeMap = typeMap; 134 setDynamicWSIFPort(pi); 135 setOperation(op); 136 setDefintion(pi.getDefinition()); 137 prepare(); 138 } 139 140 144 public WSIFOperation_SoapRMI copy() throws WSIFException { 145 146 WSIFOperation_SoapRMI op = 147 new WSIFOperation_SoapRMI(portInstance, operation, typeMap); 148 149 op.setSoapActionURI(getSoapActionURI()); 150 op.setInputNamespace(getInputNamespace()); 151 op.setInputEncodingStyle(getInputEncodingStyle()); 152 op.setOutputEncodingStyle(getOutputEncodingStyle()); 153 op.setPartNames(getPartNames()); 154 op.setReturnName(getReturnName()); 155 156 return op; 157 } 158 159 163 private void prepare() throws WSIFException { 164 soaprmiMapping = new XmlJavaMapping(); 165 try { 166 XmlJavaMapping defaultMapping = soaprmi.soap.Soap.getDefault().getMapping(); 167 soaprmiMapping.connectTo(defaultMapping); 168 soaprmiMapping.setDefaultStructNsPrefix(null); 170 171 for (Iterator i = typeMap.iterator(); i.hasNext();) { 172 WSIFDynamicTypeMapping mapping = (WSIFDynamicTypeMapping) i.next(); 173 174 soaprmiMapping.mapStruct( 176 "http://schemas.xmlsoap.org/soap/encoding/", 177 mapping.getXmlType().getNamespaceURI(), 178 mapping.getXmlType().getLocalPart(), 179 mapping.getJavaType()); 180 181 } 182 } catch (XmlMapException ex) { 183 throw new WSIFException("Could not initialize mapping.", ex); 184 } 185 186 Input input = operation.getInput(); 188 if (input != null) { 189 List parts; 190 if (partNames != null) { 191 parts = new Vector (); 192 for (Iterator i = partNames.iterator(); i.hasNext();) { 193 String partName = (String ) i.next(); 194 Part part = input.getMessage().getPart(partName); 195 if (part == null) { 196 throw new WSIFException( 197 "no input part named " + partName + " for bining operation " + getName()); 198 } 199 parts.add(part); 200 } 201 } else { 202 parts = input.getMessage().getOrderedParts(null); 203 } 204 int count = parts.size(); 205 names = new String [count]; 206 types = new Class [count]; 207 208 for (int i = 0; i < count; ++i) { 210 Part part = (Part) parts.get(i); 211 names[i] = part.getName(); 212 QName partType = part.getTypeName(); 213 if (partType == null) { 214 throw new WSIFException("part " + names[i] + " must have type name declared"); 215 } 216 try { 217 XmlJavaTypeMap typeMap = 218 soaprmiMapping.queryTypeMap( 219 inputEncodingStyle, 220 partType.getNamespaceURI(), 221 partType.getLocalPart()); 222 types[i] = typeMap.javaClass(); 223 } catch (XmlMapException ex) { 224 throw new WSIFException( 225 "Could not determine local java type for " 226 + partType.getNamespaceURI() 227 + ":" 228 + partType.getLocalPart(), 229 ex); 230 } 231 232 } 233 } else { 234 names = new String [0]; 235 types = new Class [0]; 236 } 237 238 Output output = operation.getOutput(); 240 if (output != null) { 241 Part returnPart = null; 242 if (returnName != null) { 243 returnPart = output.getMessage().getPart(returnName); 244 if (returnPart == null) { 245 throw new WSIFException( 246 "no output part named " + returnName + " for bining operation " + getName()); 247 } 248 } else { 249 List parts = output.getMessage().getOrderedParts(null); 250 if (parts.size() > 0) { 251 returnPart = (Part) parts.get(0); 252 returnName = returnPart.getName(); 253 } 254 } 255 if (returnPart != null) { 256 QName partType = returnPart.getTypeName(); 257 try { 258 XmlJavaTypeMap typeMap = 259 soaprmiMapping.queryTypeMap( 260 outputEncodingStyle, 261 partType.getNamespaceURI(), 262 partType.getLocalPart()); 263 returnType = typeMap.javaClass(); 264 } catch (XmlMapException ex) { 265 throw new WSIFException( 266 "Could not determine local java type for " 267 + partType.getNamespaceURI() 268 + ":" 269 + partType.getLocalPart(), 270 ex); 271 } 272 } else { 273 returnType = Void.TYPE; 274 } 275 } 276 277 } 278 279 public boolean executeRequestResponseOperation( 280 WSIFMessage input, 281 WSIFMessage output, 282 WSIFMessage fault) 283 throws WSIFException { 284 return invokeRequestResponseOperation(input, output, fault); 285 } 286 287 public void executeInputOnlyOperation(WSIFMessage input) throws WSIFException { 288 invokeInputOnlyOperation(input); 289 } 290 291 294 public boolean invokeRequestResponseOperation( 295 WSIFMessage input, 296 WSIFMessage output, 297 WSIFMessage fault) 298 throws WSIFException { 299 if (names == null) 300 prepare(); 301 302 try { 303 304 306 Object [] params = new Object [types.length]; 307 308 Object partInst; 309 for (int i = 0; i < names.length; ++i) { 310 partInst = input.getObjectPart(names[i]); 311 if (partInst == null) { 312 boolean foundInputParameter = false; 313 String paramName = names[i]; 314 Iterator partsIterator = input.getPartNames(); 315 while (partsIterator.hasNext()) { 316 String partName = (String ) partsIterator.next(); 317 if (partName == null || paramName == null) 318 break; 319 if (partName.equals(paramName)) { 320 foundInputParameter = true; 321 } 322 } 323 if (!foundInputParameter) 324 throw new WSIFException( 325 "expected input message to have part with name '" + names[i] + "'"); 326 } 327 Object value = partInst; 328 if (value != null 330 && !types[i].isPrimitive() 331 && !(types[i].isAssignableFrom(value.getClass()))) { 332 throw new WSIFException( 333 "value " 334 + value 335 + " has unexpected type " 336 + value.getClass() 337 + " instead of " 338 + types[i]); 339 } 340 params[i] = value; 341 } 342 343 MethodInvoker mi = null; mi = 347 MethodInvoker.makeMethodInvoker( 348 getInputNamespace(), 349 returnType, 350 getName(), 351 types, 352 names, 353 getSoapActionURI(), 354 soaprmiMapping); 355 360 Map requestHeaders = new HashMap (); 361 362 requestHeaders.put("SOAPAction", getSoapActionURI()); 363 364 StringWriter sw = new StringWriter (); 365 Writer writer = new BufferedWriter (sw); 366 367 String locationUri = portInstance.getLocation(); 368 369 if (Trc.ON) 370 Trc.event( 371 this, 372 "invoking SoapRMI operation " 373 + getName() 374 + " on " 375 + locationUri); 376 377 mi.sendRequest(params, writer); 378 379 String requestContent = sw.toString(); 380 381 String httpProxyHost = null; 382 int httpProxyPort = -1; 383 384 URL url = new URL (locationUri); 385 386 Reader reader = 387 HTTPUtils.post(url, requestContent, requestHeaders, "text/xml; charset=utf-8", 388 60 * 1000, httpProxyHost, httpProxyPort); 391 392 Object result = mi.receiveResponse(reader); 393 394 if (returnType != null) { 395 if (result != null 396 && !returnType.isPrimitive() 397 && !(returnType.isAssignableFrom(result.getClass()))) { 398 throw new WSIFException( 399 "return value " 400 + result 401 + " has unexpected type " 402 + result.getClass() 403 + " instead of " 404 + returnType); 405 } 406 output.setObjectPart(returnName, result); 407 } 408 409 412 } catch (IOException ex) { 413 throw new WSIFException("IO Exception", ex); 415 } catch (soaprmi.RemoteException ex) { 416 ex.printStackTrace(); 417 throw new WSIFException("SoapRMI exception", ex); 418 } catch (soaprmi.soap.SoapException ex) { 419 throw new WSIFException("SOAP exception", ex); 420 } catch (xpp.XmlPullParserException ex) { 421 throw new WSIFException("SOAP exception", ex); 422 } 423 424 return true; 425 } 426 427 430 public void invokeInputOnlyOperation(WSIFMessage input) throws WSIFException { 431 throw new WSIFException("not implemented"); 432 } 433 434 437 public String getName() { 438 return operation.getName(); 439 } 440 441 public String getSoapActionURI() { 442 return actionUri; 443 } 444 public void setSoapActionURI(String value) { 445 actionUri = value; 446 } 447 448 public String getInputNamespace() { 449 return inputNamespace; 450 } 451 public void setInputNamespace(String value) { 452 inputNamespace = value; 453 } 454 455 public String getInputEncodingStyle() { 456 return inputEncodingStyle; 457 } 458 459 public void setInputEncodingStyle(String value) { 460 inputEncodingStyle = value; 461 } 462 463 public String getOutputEncodingStyle() { 464 return outputEncodingStyle; 465 } 466 467 public void setOutputEncodingStyle(String value) { 468 outputEncodingStyle = value; 469 } 470 471 public List getPartNames() { 472 return partNames; 473 } 474 475 public void setPartNames(List value) { 476 partNames = value; 477 } 478 479 public String getReturnName() { 480 return returnName; 481 } 482 483 public void setReturnName(String value) { 484 returnName = value; 485 } 486 487 public Operation getOperation() { 489 return operation; 490 } 491 492 public void setOperation(Operation value) { 493 operation = value; 494 } 495 496 public Definition getDefinition() { 497 return definition; 498 } 499 500 public void setDefintion(Definition value) { 501 definition = value; 502 } 503 504 public WSIFPort_SoapRMI getDynamicWSIFPort() { 506 return portInstance; 507 } 508 509 public void setDynamicWSIFPort(WSIFPort_SoapRMI value) { 510 portInstance = value; 511 } 512 513 public WSIFPort getWSIFPort() { 514 Trc.entry(this); 515 Trc.exit(portInstance); 516 return portInstance; 517 } 518 519 } | Popular Tags |