KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > receivers > RawXMLINOutMessageReceiver


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.receivers;
17
18 import org.apache.axis2.Constants;
19 import org.apache.axis2.context.MessageContext;
20 import org.apache.axis2.description.OperationDescription;
21 import org.apache.axis2.engine.AxisFault;
22 import org.apache.axis2.engine.DependencyManager;
23 import org.apache.axis2.engine.MessageReceiver;
24 import org.apache.axis2.om.OMElement;
25 import org.apache.axis2.om.OMNamespace;
26 import org.apache.axis2.soap.SOAPEnvelope;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.wsdl.WSDLService;
30
31 import java.lang.reflect.Method JavaDoc;
32
33 /**
34  * This is a Simple java Provider.
35  */

36 public class RawXMLINOutMessageReceiver
37     extends AbstractInOutSyncMessageReceiver
38     implements MessageReceiver {
39     /**
40      * Field log
41      */

42     protected Log log = LogFactory.getLog(getClass());
43
44     /**
45      * Field scope
46      */

47     private String JavaDoc scope;
48
49     /**
50      * Field classLoader
51      */

52     private ClassLoader JavaDoc classLoader;
53
54     /**
55      * Constructor RawXMLProvider
56      */

57     public RawXMLINOutMessageReceiver() {
58         scope = Constants.APPLICATION_SCOPE;
59     }
60
61     public void invokeBusinessLogic(MessageContext msgContext, MessageContext newmsgContext)
62         throws AxisFault {
63         try {
64
65             // get the implementation class for the Web Service
66
Object JavaDoc obj = getTheImplementationObject(msgContext);
67
68             // find the WebService method
69
Class JavaDoc ImplClass = obj.getClass();
70
71             //Inject the Message Context if it is asked for
72
DependencyManager.configureBusinussLogicProvider(obj, msgContext);
73
74             OperationDescription opDesc = msgContext.getOperationContext().getAxisOperation();
75             Method JavaDoc method = findOperation(opDesc, ImplClass);
76             if (method != null) {
77                 String JavaDoc style = msgContext.getOperationContext().getAxisOperation().getStyle();
78
79                 Class JavaDoc[] parameters = method.getParameterTypes();
80                 Object JavaDoc[] args = null;
81
82                 if (parameters == null || parameters.length == 0) {
83                     args = new Object JavaDoc[0];
84                 } else if (parameters.length == 1) {
85                     OMElement omElement = null;
86                     if (WSDLService.STYLE_DOC.equals(style)) {
87                         omElement = msgContext.getEnvelope().getBody().getFirstElement();
88                     } else if (WSDLService.STYLE_RPC.equals(style)) {
89                         OMElement operationElement = msgContext.getEnvelope().getBody().getFirstElement();
90                         if (operationElement != null) {
91                             if (method.getName().equals(operationElement.getLocalName())
92                                || operationElement.getLocalName() != null && operationElement.getLocalName().startsWith(method.getName()) ) {
93                                 omElement = operationElement.getFirstElement();
94                             } else {
95                                 throw new AxisFault("Operation Name does not match the immediate child name, expected "+ method.getName() + " but get " + operationElement.getLocalName());
96                             }
97                         } else {
98                             throw new AxisFault("rpc style expect the immediate child of the SOAP body ");
99                         }
100                     } else {
101                         throw new AxisFault("Unknown style ");
102                     }
103                     args = new Object JavaDoc[] { omElement };
104                 } else {
105                     throw new AxisFault(
106                         "Raw Xml provider supports only the methods bearing the signature public OMElement "
107                             + "<method-name>(OMElement) where the method name is anything");
108                 }
109
110                 OMElement result = (OMElement) method.invoke(obj, args);
111                 
112                 OMElement bodyContent = null;
113                 if (WSDLService.STYLE_RPC.equals(style)) {
114                     OMNamespace ns = getSOAPFactory().createOMNamespace("http://soapenc/", "res");
115                     bodyContent =
116                         getSOAPFactory().createOMElement(method.getName() + "Response", ns);
117                     bodyContent.addChild(result);
118                 }else{
119                     bodyContent = result;
120                 }
121
122                 SOAPEnvelope envelope = getSOAPFactory().getDefaultEnvelope();
123                 if(bodyContent!= null){
124                     envelope.getBody().addChild(bodyContent);
125                 }
126                 newmsgContext.setEnvelope(envelope);
127             } else {
128                 throw new AxisFault(
129                     "Implementation class does not define a method called" + opDesc.getName());
130             }
131         } catch (Exception JavaDoc e) {
132             throw AxisFault.makeFault(e);
133         }
134
135     }
136
137     public Method JavaDoc findOperation(OperationDescription op, Class JavaDoc ImplClass) {
138         Method JavaDoc method = null;
139         String JavaDoc methodName = op.getName().getLocalPart();
140         Method JavaDoc[] methods = ImplClass.getMethods();
141         for (int i = 0; i < methods.length; i++) {
142             if (methods[i].getName().equals(methodName)) {
143                 method = methods[i];
144                 break;
145             }
146         }
147         return method;
148     }
149 }
150
Popular Tags