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: package org.apache.axis2.receivers;
021:
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.soap.SOAPEnvelope;
024: import org.apache.axiom.soap.SOAPFactory;
025: import org.apache.axis2.AxisFault;
026: import org.apache.axis2.context.MessageContext;
027: import org.apache.axis2.description.AxisOperation;
028: import org.apache.axis2.engine.MessageReceiver;
029: import org.apache.axis2.i18n.Messages;
030:
031: import java.lang.reflect.Method;
032:
033: /**
034: * The RawXMLINOutMessageReceiver MessageReceiver hands over the raw request received to
035: * the service implementation class as an OMElement. The implementation class is expected
036: * to return back the OMElement to be returned to the caller. This is a synchronous
037: * MessageReceiver, and finds the service implementation class to invoke by referring to
038: * the "ServiceClass" parameter value specified in the service.xml and looking at the
039: * methods of the form OMElement <<methodName>>(OMElement request)
040: *
041: * @see RawXMLINOnlyMessageReceiver
042: * @see RawXMLINOutAsyncMessageReceiver
043: */
044: public class RawXMLINOutMessageReceiver extends
045: AbstractInOutSyncMessageReceiver implements MessageReceiver {
046:
047: private Method findOperation(AxisOperation op, Class implClass) {
048: Method method = (Method) (op.getParameterValue("myMethod"));
049: if (method != null)
050: return method;
051:
052: String methodName = op.getName().getLocalPart();
053:
054: try {
055: // Looking for a method of the form "OMElement method(OMElement)"
056: method = implClass.getMethod(methodName,
057: new Class[] { OMElement.class });
058: if (method.getReturnType().equals(OMElement.class)) {
059: try {
060: op.addParameter("myMethod", method);
061: } catch (AxisFault axisFault) {
062: // Do nothing here
063: }
064: return method;
065: }
066: } catch (NoSuchMethodException e) {
067: // Fault through
068: }
069:
070: return null;
071: }
072:
073: /**
074: * Invokes the bussiness logic invocation on the service implementation class
075: *
076: * @param msgContext the incoming message context
077: * @param newmsgContext the response message context
078: * @throws AxisFault on invalid method (wrong signature) or behaviour (return null)
079: */
080: public void invokeBusinessLogic(MessageContext msgContext,
081: MessageContext newmsgContext) throws AxisFault {
082: try {
083:
084: // get the implementation class for the Web Service
085: Object obj = getTheImplementationObject(msgContext);
086:
087: // find the WebService method
088: Class implClass = obj.getClass();
089:
090: AxisOperation opDesc = msgContext.getAxisOperation();
091: Method method = findOperation(opDesc, implClass);
092:
093: if (method == null) {
094: throw new AxisFault(Messages.getMessage(
095: "methodDoesNotExistInOut", opDesc.getName()
096: .toString()));
097: }
098:
099: OMElement result = (OMElement) method.invoke(obj,
100: new Object[] { msgContext.getEnvelope().getBody()
101: .getFirstElement() });
102: SOAPFactory fac = getSOAPFactory(msgContext);
103: SOAPEnvelope envelope = fac.getDefaultEnvelope();
104:
105: if (result != null) {
106: envelope.getBody().addChild(result);
107: }
108:
109: newmsgContext.setEnvelope(envelope);
110: } catch (Exception e) {
111: throw AxisFault.makeFault(e);
112: }
113: }
114: }
|