KickJava   Java API By Example, From Geeks To Geeks.

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


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.OMAbstractFactory;
25 import org.apache.axis2.om.OMElement;
26 import org.apache.axis2.om.OMNamespace;
27 import org.apache.axis2.soap.SOAPEnvelope;
28 import org.apache.axis2.soap.SOAPFactory;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.wsdl.WSDLService;
32
33 import java.lang.reflect.Method JavaDoc;
34
35 /**
36  * This is a Simple java Provider.
37  */

38 public class RawXMLINOnlyMessageReceiver
39     extends AbstractInMessageReceiver
40     implements MessageReceiver {
41     /**
42      * Field log
43      */

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

49     private String JavaDoc scope;
50
51     /**
52      * Field method
53      */

54     private Method JavaDoc method;
55
56     /**
57      * Field classLoader
58      */

59     private ClassLoader JavaDoc classLoader;
60
61     /**
62      * Constructor RawXMLProvider
63      */

64     public RawXMLINOnlyMessageReceiver() {
65         scope = Constants.APPLICATION_SCOPE;
66     }
67
68     public void invokeBusinessLogic(MessageContext msgContext)
69         throws AxisFault {
70         try {
71
72             // get the implementation class for the Web Service
73
Object JavaDoc obj = getTheImplementationObject(msgContext);
74
75             // find the WebService method
76
Class JavaDoc ImplClass = obj.getClass();
77             DependencyManager.configureBusinussLogicProvider(obj,msgContext);
78
79             OperationDescription op = msgContext.getOperationContext().getAxisOperation();
80             if (op == null) {
81                 throw new AxisFault("Operation is not located, if this is doclit style the SOAP-ACTION should specified via the SOAP Action to use the RawXMLProvider");
82             }
83             String JavaDoc methodName = op.getName().getLocalPart();
84             Method JavaDoc[] methods = ImplClass.getMethods();
85             for (int i = 0; i < methods.length; i++) {
86                 if (methods[i].getName().equals(methodName)) {
87                     this.method = methods[i];
88                     break;
89                 }
90             }
91             Class JavaDoc[] parameters = method.getParameterTypes();
92             if ((parameters != null)
93                 && (parameters.length == 1)
94                 && OMElement.class.getName().equals(parameters[0].getName())) {
95                 OMElement methodElement = msgContext.getEnvelope().getBody().getFirstElement();
96
97                 OMElement parmeter = null;
98                 SOAPEnvelope envelope = null;
99
100                 String JavaDoc style = msgContext.getOperationContext().getAxisOperation().getStyle();
101
102                 if (WSDLService.STYLE_DOC.equals(style)) {
103                     parmeter = methodElement;
104                     Object JavaDoc[] parms = new Object JavaDoc[] { parmeter };
105
106                     // invoke the WebService
107
OMElement result = (OMElement) method.invoke(obj, parms);
108                     envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
109                     envelope.getBody().setFirstChild(result);
110
111                 } else if (WSDLService.STYLE_RPC.equals(style)) {
112                     parmeter = methodElement.getFirstElement();
113                     Object JavaDoc[] parms = new Object JavaDoc[] { parmeter };
114
115                     // invoke the WebService
116
OMElement result = (OMElement) method.invoke(obj, parms);
117                     SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
118                     envelope = fac.getDefaultEnvelope();
119
120                     OMNamespace ns = fac.createOMNamespace("http://soapenc/", "res");
121                     OMElement responseMethodName = fac.createOMElement(methodName + "Response", ns);
122                     if (result != null) {
123                         responseMethodName.addChild(result);
124                     }
125                     if (responseMethodName != null) {
126                         envelope.getBody().addChild(responseMethodName);
127                     }
128                 } else {
129                     throw new AxisFault("Unknown style ");
130                 }
131             } else {
132                 throw new AxisFault(
133                     "Raw Xml provider supports only the methods bearing the signature public OMElement "
134                         + "&lt;method-name&gt;(OMElement) where the method name is anything");
135             }
136         } catch (Exception JavaDoc e) {
137             throw AxisFault.makeFault(e);
138         }
139
140     }
141 }
142
Popular Tags