1 16 17 package org.apache.axis.providers.java; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Constants; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.components.logger.LogFactory; 23 import org.apache.axis.description.OperationDesc; 24 import org.apache.axis.description.ParameterDesc; 25 import org.apache.axis.description.ServiceDesc; 26 import org.apache.axis.constants.Style; 27 import org.apache.axis.handlers.soap.SOAPService; 28 import org.apache.axis.message.RPCElement; 29 import org.apache.axis.message.RPCHeaderParam; 30 import org.apache.axis.message.RPCParam; 31 import org.apache.axis.message.SOAPBodyElement; 32 import org.apache.axis.message.SOAPEnvelope; 33 import org.apache.axis.soap.SOAPConstants; 34 import org.apache.axis.utils.JavaUtils; 35 import org.apache.axis.utils.Messages; 36 import org.apache.commons.logging.Log; 37 import org.xml.sax.SAXException ; 38 39 import javax.xml.namespace.QName ; 40 import javax.xml.rpc.holders.Holder ; 41 import javax.wsdl.OperationType; 42 import java.lang.reflect.Method ; 43 import java.util.ArrayList ; 44 import java.util.Iterator ; 45 import java.util.Vector ; 46 47 53 public class RPCProvider extends JavaProvider { 54 protected static Log log = 55 LogFactory.getLog(RPCProvider.class.getName()); 56 57 66 public void processMessage(MessageContext msgContext, 67 SOAPEnvelope reqEnv, 68 SOAPEnvelope resEnv, 69 Object obj) 70 throws Exception { 71 if (log.isDebugEnabled()) { 72 log.debug("Enter: RPCProvider.processMessage()"); 73 } 74 75 SOAPService service = msgContext.getService(); 76 ServiceDesc serviceDesc = service.getServiceDescription(); 77 OperationDesc operation = msgContext.getOperation(); 78 79 Vector bodies = reqEnv.getBodyElements(); 80 if (log.isDebugEnabled()) { 81 log.debug(Messages.getMessage("bodyElems00", "" + bodies.size())); 82 if(bodies.size()>0){ 83 log.debug(Messages.getMessage("bodyIs00", "" + bodies.get(0))); 84 } 85 } 86 87 RPCElement body = null; 88 89 for (int bNum = 0; body == null && bNum < bodies.size(); bNum++) { 91 if (!(bodies.get(bNum) instanceof RPCElement)) { 97 SOAPBodyElement bodyEl = (SOAPBodyElement) bodies.get(bNum); 98 if (bodyEl.isRoot() && operation != null && bodyEl.getID() == null) { 102 ParameterDesc param = operation.getParameter(bNum); 103 if (param != null) { 105 Object val = bodyEl.getValueAsType(param.getTypeQName()); 106 body = new RPCElement("", 107 operation.getName(), 108 new Object []{val}); 109 } 110 } 111 } else { 112 body = (RPCElement) bodies.get(bNum); 113 } 114 } 115 116 if (body == null) { 119 if (!(serviceDesc.getStyle().equals(Style.DOCUMENT))) { 121 throw new Exception (Messages.getMessage("noBody00")); 122 } 123 124 ArrayList ops = serviceDesc.getOperations(); 127 for (Iterator iterator = ops.iterator(); iterator.hasNext();) { 128 OperationDesc desc = (OperationDesc) iterator.next(); 129 if (desc.getNumInParams() == 0) { 130 msgContext.setOperation(desc); 132 body = new RPCElement(desc.getName()); 134 break; 136 } 137 } 138 139 if (body == null) { 141 throw new Exception (Messages.getMessage("noBody00")); 142 } 143 } 144 145 String methodName = body.getMethodName(); 146 Vector args = null; 147 try { 148 args = body.getParams(); 149 } catch (SAXException e) { 150 if(e.getException() != null) 151 throw e.getException(); 152 throw e; 153 } 154 int numArgs = args.size(); 155 156 operation = msgContext.getOperation(); 159 160 if (operation == null) { 161 QName qname = new QName (body.getNamespaceURI(), 162 body.getName()); 163 operation = serviceDesc.getOperationByElementQName(qname); 164 165 if (operation == null) { 166 SOAPConstants soapConstants = msgContext == null ? 167 SOAPConstants.SOAP11_CONSTANTS : 168 msgContext.getSOAPConstants(); 169 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { 170 AxisFault fault = 171 new AxisFault(Constants.FAULT_SOAP12_SENDER, 172 Messages.getMessage("noSuchOperation", 173 methodName), 174 null, 175 null); 176 fault.addFaultSubCode(Constants.FAULT_SUBCODE_PROC_NOT_PRESENT); 177 throw new SAXException (fault); 178 } else { 179 throw new AxisFault(Constants.FAULT_CLIENT, Messages.getMessage("noSuchOperation", methodName), 180 null, null); 181 } 182 } else { 183 msgContext.setOperation(operation); 184 } 185 } 186 187 Object [] argValues = new Object [operation.getNumParams()]; 190 191 ArrayList outs = new ArrayList (); 193 194 for (int i = 0; i < numArgs; i++) { 200 RPCParam rpcParam = (RPCParam) args.get(i); 201 Object value = rpcParam.getObjectValue(); 202 203 ParameterDesc paramDesc = rpcParam.getParamDesc(); 205 206 if (paramDesc != null && paramDesc.getJavaType() != null) { 210 211 Class sigType = paramDesc.getJavaType(); 213 214 value = JavaUtils.convert(value, sigType); 216 217 rpcParam.setObjectValue(value); 218 if (paramDesc.getMode() == ParameterDesc.INOUT) { 219 outs.add(rpcParam); 220 } 221 } 222 223 if (paramDesc == null || paramDesc.getOrder() == -1) { 226 argValues[i] = value; 227 } else { 228 argValues[paramDesc.getOrder()] = value; 229 } 230 231 if (log.isDebugEnabled()) { 232 log.debug(" " + Messages.getMessage("value00", 233 "" + argValues[i])); 234 } 235 } 236 237 String allowedMethods = (String ) service.getOption("allowedMethods"); 240 checkMethodName(msgContext, allowedMethods, operation.getName()); 241 242 int count = numArgs; 244 for (int i = 0; i < argValues.length; i++) { 245 246 ParameterDesc param = operation.getParameter(i); 248 if(param.getMode() == ParameterDesc.IN) 249 continue; 250 251 Class holderClass = param.getJavaType(); 252 if (holderClass != null && 253 Holder .class.isAssignableFrom(holderClass)) { 254 int index = count; 255 if (param.getOrder() != -1) { 257 index = param.getOrder(); 258 } else { 259 count++; 260 } 261 if (argValues[index] != null) { 263 continue; 264 } 265 argValues[index] = holderClass.newInstance(); 266 RPCParam p = new RPCParam(param.getQName(), 270 argValues[index]); 271 p.setParamDesc(param); 272 outs.add(p); 273 } else { 274 throw new AxisFault(Messages.getMessage("badOutParameter00", 275 "" + param.getQName(), 276 operation.getName())); 277 } 278 } 279 280 Object objRes = null; 282 try { 283 objRes = invokeMethod(msgContext, 284 operation.getMethod(), 285 obj, argValues); 286 } catch (IllegalArgumentException e) { 287 String methodSig = operation.getMethod().toString(); 288 String argClasses = ""; 289 for (int i = 0; i < argValues.length; i++) { 290 if (argValues[i] == null) { 291 argClasses += "null"; 292 } else { 293 argClasses += argValues[i].getClass().getName(); 294 } 295 if (i + 1 < argValues.length) { 296 argClasses += ","; 297 } 298 } 299 log.info(Messages.getMessage("dispatchIAE00", 300 new String []{methodSig, argClasses}), 301 e); 302 throw new AxisFault(Messages.getMessage("dispatchIAE00", 303 new String []{methodSig, argClasses}), 304 e); 305 } 306 307 309 if (OperationType.ONE_WAY.equals(operation.getMep())) 310 311 return; 312 313 314 315 RPCElement resBody = new RPCElement(methodName + "Response"); 316 resBody.setPrefix(body.getPrefix()); 317 resBody.setNamespaceURI(body.getNamespaceURI()); 318 resBody.setEncodingStyle(msgContext.getEncodingStyle()); 319 320 try { 321 if (operation.getMethod().getReturnType() != Void.TYPE) { 323 QName returnQName = operation.getReturnQName(); 324 if (returnQName == null) { 325 String nsp = body.getNamespaceURI(); 326 if(nsp == null || nsp.length()==0) { 327 nsp = serviceDesc.getDefaultNamespace(); 328 } 329 returnQName = new QName (msgContext.isEncoded() ? "" : 330 nsp, 331 methodName + "Return"); 332 } 333 334 RPCParam param = new RPCParam(returnQName, objRes); 335 param.setParamDesc(operation.getReturnParamDesc()); 336 337 if (!operation.isReturnHeader()) { 338 if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS && 340 (serviceDesc.getStyle().equals(Style.RPC))) { 341 RPCParam resultParam = new RPCParam(Constants.QNAME_RPC_RESULT, returnQName); 342 resultParam.setXSITypeGeneration(Boolean.FALSE); 343 resBody.addParam(resultParam); 344 } 345 resBody.addParam(param); 346 } else { 347 resEnv.addHeader(new RPCHeaderParam(param)); 348 } 349 350 } 351 352 if (!outs.isEmpty()) { 354 for (Iterator i = outs.iterator(); i.hasNext();) { 355 RPCParam param = (RPCParam) i.next(); 357 Holder holder = (Holder ) param.getObjectValue(); 358 Object value = JavaUtils.getHolderValue(holder); 359 ParameterDesc paramDesc = param.getParamDesc(); 360 361 param.setObjectValue(value); 362 if (paramDesc != null && paramDesc.isOutHeader()) { 363 resEnv.addHeader(new RPCHeaderParam(param)); 364 } else { 365 resBody.addParam(param); 366 } 367 } 368 } 369 } catch (Exception e) { 370 throw e; 371 } 372 373 resEnv.addBodyElement(resBody); 374 } 375 376 384 protected Object invokeMethod(MessageContext msgContext, 385 Method method, Object obj, 386 Object [] argValues) 387 throws Exception { 388 return (method.invoke(obj, argValues)); 389 } 390 391 398 protected void checkMethodName(MessageContext msgContext, 399 String allowedMethods, 400 String methodName) 401 throws Exception { 402 } 405 } 406 | Popular Tags |