001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: /*
021: * Reflection based RPCMessageReceiver , request will be processed by looking at the method signature
022: * of the invocation method
023: */
024: package org.apache.axis2.rpc.receivers;
025:
026: import org.apache.axiom.om.OMElement;
027: import org.apache.axiom.om.OMNamespace;
028: import org.apache.axiom.soap.SOAPEnvelope;
029: import org.apache.axiom.soap.SOAPFactory;
030: import org.apache.axis2.AxisFault;
031: import org.apache.axis2.context.MessageContext;
032: import org.apache.axis2.description.*;
033: import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
034: import org.apache.axis2.receivers.AbstractInOutMessageReceiver;
035: import org.apache.axis2.wsdl.WSDLConstants;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: import java.lang.reflect.InvocationTargetException;
040: import java.lang.reflect.Method;
041:
042: public class RPCMessageReceiver extends AbstractInOutMessageReceiver {
043: private static Log log = LogFactory
044: .getLog(RPCMessageReceiver.class);
045:
046: /**
047: * reflect and get the Java method - for each i'th param in the java method - get the first
048: * child's i'th child -if the elem has an xsi:type attr then find the deserializer for it - if
049: * not found, lookup deser for th i'th param (java type) - error if not found - deserialize &
050: * save in an object array - end for
051: * <p/>
052: * - invoke method and get the return value
053: * <p/>
054: * - look up serializer for return value based on the value and type
055: * <p/>
056: * - create response msg and add return value as grand child of <soap:body>
057: *
058: * @param inMessage incoming MessageContext
059: * @param outMessage outgoing MessageContext
060: * @throws AxisFault
061: */
062:
063: public void invokeBusinessLogic(MessageContext inMessage,
064: MessageContext outMessage) throws AxisFault {
065: Method method = null;
066: try {
067: // get the implementation class for the Web Service
068: Object obj = getTheImplementationObject(inMessage);
069:
070: Class ImplClass = obj.getClass();
071:
072: AxisOperation op = inMessage.getOperationContext()
073: .getAxisOperation();
074: method = (Method) (op.getParameterValue("myMethod"));
075: AxisService service = inMessage.getAxisService();
076: OMElement methodElement = inMessage.getEnvelope().getBody()
077: .getFirstElement();
078: AxisMessage inAxisMessage = op
079: .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
080: String messageNameSpace = null;
081:
082: if (method == null) {
083: String methodName = op.getName().getLocalPart();
084: Method[] methods = ImplClass.getMethods();
085:
086: for (int i = 0; i < methods.length; i++) {
087: if (methods[i].getName().equals(methodName)) {
088: method = methods[i];
089: op.addParameter("myMethod", method);
090: break;
091: }
092: }
093: if (method == null) {
094: throw new AxisFault("No such method '" + methodName
095: + "' in class " + ImplClass.getName());
096: }
097: }
098: Object resObject = null;
099: if (inAxisMessage != null) {
100: resObject = RPCUtil.invokeServiceClass(inAxisMessage,
101: method, obj, messageNameSpace, methodElement,
102: inMessage);
103: }
104:
105: SOAPFactory fac = getSOAPFactory(inMessage);
106:
107: // Handling the response
108: AxisMessage outaxisMessage = op
109: .getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
110: if (outaxisMessage != null
111: && outaxisMessage.getElementQName() != null) {
112: messageNameSpace = outaxisMessage.getElementQName()
113: .getNamespaceURI();
114: } else {
115: messageNameSpace = service.getTargetNamespace();
116: }
117:
118: OMNamespace ns = fac.createOMNamespace(messageNameSpace,
119: service.getSchemaTargetNamespacePrefix());
120: SOAPEnvelope envelope = fac.getDefaultEnvelope();
121: OMElement bodyContent = null;
122:
123: if (WSDL2Constants.MEP_URI_ROBUST_IN_ONLY.equals(op
124: .getMessageExchangePattern())) {
125: OMElement bodyChild = fac.createOMElement(outMessage
126: .getAxisMessage().getName(), ns);
127: envelope.getBody().addChild(bodyChild);
128: outMessage.setEnvelope(envelope);
129: return;
130: }
131: Parameter generateBare = service
132: .getParameter(Java2WSDLConstants.DOC_LIT_BARE_PARAMETER);
133: if (generateBare != null
134: && "true".equals(generateBare.getValue())) {
135: RPCUtil.processResonseAsDocLitBare(resObject, service,
136: envelope, fac, ns, bodyContent, outMessage);
137: } else {
138: RPCUtil.processResponseAsDocLitWrapped(resObject,
139: service, 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.getName();
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: throw AxisFault.makeFault(e);
161: } catch (Exception e) {
162: String msg = "Exception occurred while trying to invoke service method "
163: + method.getName();
164: log.error(msg, e);
165: throw AxisFault.makeFault(e);
166: }
167: }
168: }
|