1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 /*
21 * Reflection based RPCMessageReceiver , request will be processed by looking at the method signature
22 * of the invocation method
23 */
24 package org.apache.axis2.rpc.receivers;
25
26 import org.apache.axiom.om.OMElement;
27 import org.apache.axiom.om.OMNamespace;
28 import org.apache.axiom.soap.SOAPEnvelope;
29 import org.apache.axiom.soap.SOAPFactory;
30 import org.apache.axis2.AxisFault;
31 import org.apache.axis2.context.MessageContext;
32 import org.apache.axis2.description.AxisMessage;
33 import org.apache.axis2.description.AxisOperation;
34 import org.apache.axis2.description.AxisService;
35 import org.apache.axis2.description.Parameter;
36 import org.apache.axis2.description.WSDL2Constants;
37 import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
38 import org.apache.axis2.receivers.AbstractInOutMessageReceiver;
39 import org.apache.axis2.wsdl.WSDLConstants;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import java.lang.reflect.InvocationTargetException;
44 import java.lang.reflect.Method;
45
46 public class RPCMessageReceiver extends AbstractInOutMessageReceiver {
47 private static Log log = LogFactory.getLog(RPCMessageReceiver.class);
48
49 /**
50 * reflect and get the Java method - for each i'th param in the java method - get the first
51 * child's i'th child -if the elem has an xsi:type attr then find the deserializer for it - if
52 * not found, lookup deser for th i'th param (java type) - error if not found - deserialize &
53 * save in an object array - end for
54 * <p/>
55 * - invoke method and get the return value
56 * <p/>
57 * - look up serializer for return value based on the value and type
58 * <p/>
59 * - create response msg and add return value as grand child of <soap:body>
60 *
61 * @param inMessage incoming MessageContext
62 * @param outMessage outgoing MessageContext
63 * @throws AxisFault
64 */
65
66 public void invokeBusinessLogic(MessageContext inMessage, MessageContext outMessage)
67 throws AxisFault {
68 Method method = null;
69 try {
70 // get the implementation class for the Web Service
71 Object obj = getTheImplementationObject(inMessage);
72
73 Class ImplClass = obj.getClass();
74
75 AxisOperation op = inMessage.getOperationContext().getAxisOperation();
76 method = (Method)(op.getParameterValue("myMethod"));
77 AxisService service = inMessage.getAxisService();
78 OMElement methodElement = inMessage.getEnvelope().getBody()
79 .getFirstElement();
80 AxisMessage inAxisMessage = op.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
81 String messageNameSpace = null;
82
83
84 if (method == null) {
85 String methodName = op.getName().getLocalPart();
86 Method[] methods = ImplClass.getMethods();
87
88 for (Method method1 : methods) {
89 if (method1.getName().equals(methodName)) {
90 method = method1;
91 op.addParameter("myMethod", method);
92 break;
93 }
94 }
95 if (method == null) {
96 throw new AxisFault("No such method '" + methodName +
97 "' in class " + ImplClass.getName());
98 }
99 }
100 Object resObject = null;
101 if (inAxisMessage != null) {
102 resObject = RPCUtil.invokeServiceClass(inAxisMessage,
103 method,
104 obj,
105 messageNameSpace,
106 methodElement,inMessage);
107 }
108
109
110 SOAPFactory fac = getSOAPFactory(inMessage);
111
112 // Handling the response
113 AxisMessage outaxisMessage = op.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
114 if (outaxisMessage != null && outaxisMessage.getElementQName() !=null) {
115 messageNameSpace = outaxisMessage.getElementQName().getNamespaceURI();
116 } else {
117 messageNameSpace = service.getTargetNamespace();
118 }
119
120 OMNamespace ns = fac.createOMNamespace(messageNameSpace,
121 service.getSchemaTargetNamespacePrefix());
122 SOAPEnvelope envelope = fac.getDefaultEnvelope();
123 OMElement bodyContent = null;
124
125 if (WSDL2Constants.MEP_URI_ROBUST_IN_ONLY.equals(
126 op.getMessageExchangePattern())){
127 OMElement bodyChild = fac.createOMElement(outMessage.getAxisMessage().getName(), ns);
128 envelope.getBody().addChild(bodyChild);
129 outMessage.setEnvelope(envelope);
130 return;
131 }
132 Parameter generateBare = service.getParameter(Java2WSDLConstants.DOC_LIT_BARE_PARAMETER);
133 if (generateBare!=null && "true".equals(generateBare.getValue())) {
134 RPCUtil.processResonseAsDocLitBare(resObject, service,
135 envelope, fac, ns,
136 bodyContent, outMessage);
137 } else {
138 RPCUtil.processResponseAsDocLitWrapped(resObject, service,
139 method, envelope, fac, ns,
140 bodyContent, outMessage);
141 }
142 outMessage.setEnvelope(envelope);
143 } catch (InvocationTargetException e) {
144 String msg = null;
145 Throwable cause = e.getCause();
146 if (cause != null) {
147 msg = cause.getMessage();
148 }
149 if (msg == null) {
150 msg = "Exception occurred while trying to invoke service method " +
151 (method != null ? method.getName() : "null");
152 }
153 if (cause instanceof AxisFault) {
154 log.debug(msg, cause);
155 throw (AxisFault)cause;
156 }
157 log.error(msg, e);
158 throw new AxisFault(msg, e);
159 } catch(RuntimeException e) {
160 log.error(e.getMessage(), e);
161 throw AxisFault.makeFault(e);
162 } catch (Exception e) {
163 String msg = "Exception occurred while trying to invoke service method " +
164 (method != null ? method.getName() : "null");
165 log.error(msg, e);
166 throw AxisFault.makeFault(e);
167 }
168 }
169 }