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: package org.apache.axis2.rpc.receivers;
020:
021: import org.apache.axiom.om.OMAbstractFactory;
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.om.OMNamespace;
024: import org.apache.axiom.om.OMText;
025: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
026: import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
027: import org.apache.axiom.om.util.Base64;
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.databinding.typemapping.SimpleTypeMapper;
033: import org.apache.axis2.databinding.utils.BeanUtil;
034: import org.apache.axis2.databinding.utils.reader.NullXMLStreamReader;
035: import org.apache.axis2.description.AxisMessage;
036: import org.apache.axis2.description.AxisService;
037: import org.apache.axis2.description.WSDL2Constants;
038: import org.apache.axis2.description.java2wsdl.TypeTable;
039: import org.apache.axis2.engine.ObjectSupplier;
040: import org.apache.axis2.util.StreamWrapper;
041:
042: import javax.xml.namespace.QName;
043: import javax.xml.stream.XMLStreamReader;
044: import java.lang.reflect.Array;
045: import java.lang.reflect.InvocationTargetException;
046: import java.lang.reflect.Method;
047: import java.util.Collection;
048: import java.util.Iterator;
049:
050: public class RPCUtil {
051:
052: private static String RETURN_WRAPPER = "return";
053:
054: public static void processResponse(SOAPFactory fac,
055: Object resObject, OMElement bodyContent, OMNamespace ns,
056: SOAPEnvelope envelope, Method method, boolean qualified,
057: TypeTable typeTable) {
058: if (resObject != null) {
059: //simple type
060: if (resObject instanceof OMElement) {
061: OMElement result = (OMElement) resObject;
062: bodyContent = fac.createOMElement(method.getName()
063: + "Response", ns);
064: OMElement resWrapper;
065: if (qualified) {
066: resWrapper = fac.createOMElement(RETURN_WRAPPER, ns
067: .getNamespaceURI(), ns.getPrefix());
068: } else {
069: resWrapper = fac.createOMElement(RETURN_WRAPPER,
070: null);
071: }
072: resWrapper.addChild(result);
073: bodyContent.addChild(resWrapper);
074: } else if (SimpleTypeMapper.isSimpleType(resObject)) {
075: bodyContent = fac.createOMElement(method.getName()
076: + "Response", ns);
077: OMElement child;
078: if (qualified) {
079: child = fac.createOMElement(RETURN_WRAPPER, ns);
080: } else {
081: child = fac.createOMElement(RETURN_WRAPPER, null);
082: }
083: child.addChild(fac.createOMText(child, SimpleTypeMapper
084: .getStringValue(resObject)));
085: bodyContent.addChild(child);
086: } else {
087: bodyContent = fac.createOMElement(method.getName()
088: + "Response", ns);
089: // Java Beans
090: QName returnWrapper;
091: if (qualified) {
092: returnWrapper = new QName(ns.getNamespaceURI(),
093: RETURN_WRAPPER, ns.getPrefix());
094: } else {
095: returnWrapper = new QName(RETURN_WRAPPER);
096: }
097: XMLStreamReader xr = BeanUtil.getPullParser(resObject,
098: returnWrapper, typeTable, qualified, false);
099: StAXOMBuilder stAXOMBuilder = OMXMLBuilderFactory
100: .createStAXOMBuilder(OMAbstractFactory
101: .getOMFactory(), new StreamWrapper(xr));
102: OMElement documentElement = stAXOMBuilder
103: .getDocumentElement();
104: if (documentElement != null) {
105: bodyContent.addChild(documentElement);
106: }
107: }
108: }
109: if (bodyContent != null) {
110: envelope.getBody().addChild(bodyContent);
111: }
112: }
113:
114: public static void processObjectAsDocLitBare(SOAPFactory fac,
115: Object resObject, OMElement bodyContent, OMNamespace ns,
116: SOAPEnvelope envelope, boolean qualified,
117: TypeTable typeTable, String partName) {
118: if (resObject instanceof OMElement) {
119: OMElement result = (OMElement) resObject;
120: bodyContent = fac.createOMElement(partName, ns);
121: bodyContent.addChild(result);
122: } else if (SimpleTypeMapper.isSimpleType(resObject)) {
123: bodyContent = fac.createOMElement(partName, ns);
124: bodyContent.addChild(fac.createOMText(bodyContent,
125: SimpleTypeMapper.getStringValue(resObject)));
126: } else {
127: QName returnWrapper = new QName(ns.getNamespaceURI(),
128: partName, ns.getPrefix());
129: XMLStreamReader xr = BeanUtil.getPullParser(resObject,
130: returnWrapper, typeTable, qualified, true);
131: StAXOMBuilder stAXOMBuilder = OMXMLBuilderFactory
132: .createStAXOMBuilder(OMAbstractFactory
133: .getOMFactory(), new StreamWrapper(xr));
134: OMElement documentElement = stAXOMBuilder
135: .getDocumentElement();
136: if (documentElement != null) {
137: envelope.getBody().addChild(documentElement);
138: }
139: }
140: if (bodyContent != null) {
141: envelope.getBody().addChild(bodyContent);
142: }
143: }
144:
145: public static Object[] processRequest(OMElement methodElement,
146: Method method, ObjectSupplier objectSupplier)
147: throws AxisFault {
148: Class[] parameters = method.getParameterTypes();
149: return BeanUtil.deserialize(methodElement, parameters,
150: objectSupplier);
151: }
152:
153: public static Object invokeServiceClass(AxisMessage inAxisMessage,
154: Method method, Object implClass, String messageNameSpace,
155: OMElement methodElement, MessageContext inMessage)
156: throws AxisFault, IllegalAccessException,
157: InvocationTargetException {
158: if (inAxisMessage.getElementQName() == null) {
159: // method accept empty SOAPbody
160: return method.invoke(implClass, new Object[0]);
161: } else {
162: QName elementQName = inAxisMessage.getElementQName();
163: messageNameSpace = elementQName.getNamespaceURI();
164: OMNamespace namespace = methodElement.getNamespace();
165: if (messageNameSpace != null) {
166: if (namespace == null) {
167: throw new AxisFault("namespace mismatch require "
168: + messageNameSpace + " found none");
169: }
170: if (!messageNameSpace.equals(namespace
171: .getNamespaceURI())) {
172: throw new AxisFault("namespace mismatch require "
173: + messageNameSpace
174: + " found "
175: + methodElement.getNamespace()
176: .getNamespaceURI());
177: }
178: } else if (namespace != null) {
179: throw new AxisFault(
180: "namespace mismatch. Axis Oepration expects non-namespace "
181: + "qualified element. But received a namespace qualified element");
182: }
183: Object[] objectArray;
184: if (inAxisMessage.isWrapped()) {
185: objectArray = RPCUtil.processRequest(methodElement,
186: method, inMessage.getAxisService()
187: .getObjectSupplier());
188: } else {
189: objectArray = RPCUtil.processRequest(
190: (OMElement) methodElement.getParent(), method,
191: inMessage.getAxisService().getObjectSupplier());
192: }
193: return method.invoke(implClass, objectArray);
194:
195: }
196: }
197:
198: public static OMElement getResponseElement(QName resname,
199: Object[] objs, boolean qualified, TypeTable typeTable) {
200: if (qualified) {
201: return BeanUtil.getOMElement(resname, objs, new QName(
202: resname.getNamespaceURI(), RETURN_WRAPPER, resname
203: .getPrefix()), qualified, typeTable);
204: } else {
205: return BeanUtil.getOMElement(resname, objs, new QName(
206: RETURN_WRAPPER), qualified, typeTable);
207: }
208: }
209:
210: public static void processResonseAsDocLitBare(Object resObject,
211: AxisService service, SOAPEnvelope envelope,
212: SOAPFactory fac, OMNamespace ns, OMElement bodyContent,
213: MessageContext outMessage) throws Exception {
214: QName elementQName = outMessage.getAxisMessage()
215: .getElementQName();
216: String partName = outMessage.getAxisMessage().getPartName();
217: if (resObject == null) {
218: processNullReturns(service, envelope, partName);
219: } else {
220: if (resObject instanceof Object[]) {
221: QName resName = new QName(elementQName
222: .getNamespaceURI(), partName, elementQName
223: .getPrefix());
224: OMElement bodyChild = RPCUtil.getResponseElement(
225: resName, (Object[]) resObject, service
226: .isElementFormDefault(), service
227: .getTypeTable());
228: envelope.getBody().addChild(bodyChild);
229: } else {
230: if (resObject.getClass().isArray()) {
231: int length = Array.getLength(resObject);
232: Object objArray[];
233: if (resObject instanceof byte[]) {
234: objArray = new Object[1];
235: objArray[0] = Base64.encode((byte[]) resObject);
236: } else {
237: objArray = new Object[length];
238: for (int i = 0; i < length; i++) {
239: objArray[i] = Array.get(resObject, i);
240: }
241: }
242:
243: QName resName = new QName(elementQName
244: .getNamespaceURI(), partName, elementQName
245: .getPrefix());
246: OMElement bodyChild = RPCUtil.getResponseElement(
247: resName, objArray, service
248: .isElementFormDefault(), service
249: .getTypeTable());
250: envelope.getBody().addChild(bodyChild);
251: } else {
252: if (SimpleTypeMapper.isCollection(resObject
253: .getClass())) {
254: Collection collection = (Collection) resObject;
255: int size = collection.size();
256: Object values[] = new Object[size];
257: int count = 0;
258: for (Iterator iterator = collection.iterator(); iterator
259: .hasNext();) {
260: values[count] = iterator.next();
261: count++;
262:
263: }
264: QName resName = new QName(elementQName
265: .getNamespaceURI(), partName,
266: elementQName.getPrefix());
267: OMElement bodyChild = RPCUtil
268: .getResponseElement(resName, values,
269: service.isElementFormDefault(),
270: service.getTypeTable());
271: envelope.getBody().addChild(bodyChild);
272: } else if (SimpleTypeMapper.isDataHandler(resObject
273: .getClass())) {
274: OMElement resElemt;
275: if (service.isElementFormDefault()) {
276: resElemt = fac
277: .createOMElement(partName, ns);
278: } else {
279: resElemt = fac.createOMElement(partName,
280: null);
281: }
282: OMText text = fac.createOMText(resObject, true);
283: resElemt.addChild(text);
284: envelope.getBody().addChild(resElemt);
285: } else {
286: if (service.isElementFormDefault()) {
287: RPCUtil.processObjectAsDocLitBare(fac,
288: resObject, bodyContent, ns,
289: envelope, service
290: .isElementFormDefault(),
291: service.getTypeTable(), partName);
292: } else {
293: RPCUtil.processObjectAsDocLitBare(fac,
294: resObject, bodyContent, ns,
295: envelope, service
296: .isElementFormDefault(),
297: null, partName);
298: }
299: }
300: }
301: }
302: }
303: outMessage.setEnvelope(envelope);
304: }
305:
306: /**
307: * This method is use to to crete the reposne when , the return value is null
308: *
309: * @param service Current AxisService
310: * @param envelope response enevelop
311: */
312: private static void processNullReturns(AxisService service,
313: SOAPEnvelope envelope, String partName) {
314: QName resName;
315: if (service.isElementFormDefault()) {
316: resName = new QName(service.getSchematargetNamespace(),
317: partName, service.getSchemaTargetNamespacePrefix());
318: } else {
319: resName = new QName(partName);
320: }
321: XMLStreamReader xr = new NullXMLStreamReader(resName);
322: StreamWrapper parser = new StreamWrapper(xr);
323: StAXOMBuilder stAXOMBuilder = OMXMLBuilderFactory
324: .createStAXOMBuilder(OMAbstractFactory
325: .getSOAP11Factory(), parser);
326: envelope.getBody().addChild(stAXOMBuilder.getDocumentElement());
327: }
328:
329: public static void processResponseAsDocLitWrapped(Object resObject,
330: AxisService service, Method method, SOAPEnvelope envelope,
331: SOAPFactory fac, OMNamespace ns, OMElement bodyContent,
332: MessageContext outMessage) throws Exception {
333: QName elementQName = outMessage.getAxisMessage()
334: .getElementQName();
335: if (resObject == null) {
336: QName resName;
337: if (service.isElementFormDefault()) {
338: resName = new QName(service.getSchematargetNamespace(),
339: RETURN_WRAPPER, service
340: .getSchemaTargetNamespacePrefix());
341: } else {
342: resName = new QName(RETURN_WRAPPER);
343: }
344: XMLStreamReader xr = new NullXMLStreamReader(resName);
345: StreamWrapper parser = new StreamWrapper(xr);
346: StAXOMBuilder stAXOMBuilder = OMXMLBuilderFactory
347: .createStAXOMBuilder(OMAbstractFactory
348: .getSOAP11Factory(), parser);
349: ns = fac.createOMNamespace(service
350: .getSchematargetNamespace(), service
351: .getSchemaTargetNamespacePrefix());
352: OMElement bodyChild = fac.createOMElement(method.getName()
353: + "Response", ns);
354: bodyChild.addChild(stAXOMBuilder.getDocumentElement());
355: envelope.getBody().addChild(bodyChild);
356: } else {
357: if (resObject instanceof Object[]) {
358:
359: QName resName = new QName(elementQName
360: .getNamespaceURI(), method.getName()
361: + "Response", elementQName.getPrefix());
362: OMElement bodyChild = RPCUtil.getResponseElement(
363: resName, (Object[]) resObject, service
364: .isElementFormDefault(), service
365: .getTypeTable());
366: envelope.getBody().addChild(bodyChild);
367: } else {
368: if (resObject.getClass().isArray()) {
369: int length = Array.getLength(resObject);
370: Object objArray[];
371: if (resObject instanceof byte[]) {
372: objArray = new Object[1];
373: objArray[0] = Base64.encode((byte[]) resObject);
374: } else {
375: objArray = new Object[length];
376: for (int i = 0; i < length; i++) {
377: objArray[i] = Array.get(resObject, i);
378: }
379: }
380:
381: QName resName = new QName(elementQName
382: .getNamespaceURI(), method.getName()
383: + "Response", elementQName.getPrefix());
384: OMElement bodyChild = RPCUtil.getResponseElement(
385: resName, objArray, service
386: .isElementFormDefault(), service
387: .getTypeTable());
388: envelope.getBody().addChild(bodyChild);
389: } else {
390: if (SimpleTypeMapper.isCollection(resObject
391: .getClass())) {
392: Collection collection = (Collection) resObject;
393: int size = collection.size();
394: Object values[] = new Object[size];
395: int count = 0;
396: for (Iterator iterator = collection.iterator(); iterator
397: .hasNext();) {
398: values[count] = iterator.next();
399: count++;
400:
401: }
402: QName resName = new QName(elementQName
403: .getNamespaceURI(), method.getName()
404: + "Response", elementQName.getPrefix());
405: OMElement bodyChild = RPCUtil
406: .getResponseElement(resName, values,
407: service.isElementFormDefault(),
408: service.getTypeTable());
409: envelope.getBody().addChild(bodyChild);
410: } else if (SimpleTypeMapper.isDataHandler(resObject
411: .getClass())) {
412: OMElement resElemt = fac.createOMElement(method
413: .getName()
414: + "Response", ns);
415: OMText text = fac.createOMText(resObject, true);
416: OMElement returnElement;
417: if (service.isElementFormDefault()) {
418: returnElement = fac.createOMElement(
419: RETURN_WRAPPER, ns);
420: } else {
421: returnElement = fac.createOMElement(
422: RETURN_WRAPPER, null);
423: }
424: returnElement.addChild(text);
425: resElemt.addChild(returnElement);
426: envelope.getBody().addChild(resElemt);
427: } else {
428: if (service.isElementFormDefault()) {
429: RPCUtil.processResponse(fac, resObject,
430: bodyContent, ns, envelope, method,
431: service.isElementFormDefault(),
432: service.getTypeTable());
433: } else {
434: RPCUtil.processResponse(fac, resObject,
435: bodyContent, ns, envelope, method,
436: service.isElementFormDefault(),
437: null);
438: }
439: }
440: }
441: }
442: }
443: outMessage.setEnvelope(envelope);
444: }
445: }
|