001: package com.sun.portal.providers.simplewebservice.util;
002:
003: import com.sun.portal.providers.simplewebservice.SimpleWebServiceParameter;
004: import com.sun.portal.providers.simplewebservice.ParameterDescriptor;
005: import com.sun.portal.providers.simplewebservice.util.XList;
006: import com.sun.portal.providers.simplewebservice.util.SimpleWebServiceUtil;
007: import com.sun.portal.providers.simplewebservice.util.SimpleWebServiceUtilImpl;
008: import com.sun.xml.rpc.util.StructMap;
009: import com.sun.portal.providers.simplewebservice.WebServiceDescriptor;
010:
011: import javax.xml.namespace.QName;
012: import javax.xml.soap.SOAPElement;
013: import javax.xml.soap.Node;
014: import javax.xml.soap.Name;
015: import javax.xml.rpc.Call;
016: import javax.xml.rpc.JAXRPCException;
017: import javax.xml.rpc.ParameterMode;
018: import java.util.*;
019: import java.lang.reflect.Array;
020:
021: /**
022: * User: Ravikiran
023: * Date: Oct 12, 2003
024: * Time: 3:35:35 PM
025: */
026: public class SWSParameterUtil {
027:
028: private SWSParameterUtil() {
029: }
030:
031: private static final SWSParameterUtil swsParameterUtil = new SWSParameterUtil();
032: private static final SimpleWebServiceUtil swsRPCUtil = new SimpleWebServiceUtilImpl();
033:
034: public static SWSParameterUtil getInstace() {
035: return swsParameterUtil;
036: }
037:
038: public Object[] getValues(SimpleWebServiceParameter[] inputValues,
039: WebServiceDescriptor webServiceDescriptor)
040: throws java.lang.IllegalArgumentException {
041:
042: Object[] values = new Object[inputValues.length];
043:
044: // For each input parameter, determine if complex or simple type
045: for (int i = 0; inputValues != null && i < inputValues.length; i++) {
046:
047: if (inputValues[i].isSimpleType()) {
048: values[i] = (Object) inputValues[i].getValue();
049: } else {
050: values[i] = getInputValues(inputValues[i]);
051: }
052: }
053: return values;
054: }
055:
056: private Object getInputValues(
057: SimpleWebServiceParameter swsInputParam) {
058:
059: Object valuesList = null;
060:
061: if (swsInputParam.isArrayType()) {
062: // get Array type input values
063: if (swsInputParam.isSimpleTypeArray()) {
064: return getSimpleArrayInputValues(swsInputParam);
065: } else {
066:
067: return getComplexArrayInputValues(swsInputParam);
068: }
069:
070: } else {
071:
072: // get Complex type input values as a Map
073: XList givenValuesList = (XList) swsInputParam.getValue();
074:
075: Iterator iterator = givenValuesList.iterator();
076:
077: StructMap valuesMap = new StructMap();
078:
079: while (iterator.hasNext()) {
080:
081: SimpleWebServiceParameter swsInnerInputParam = (SimpleWebServiceParameter) iterator
082: .next();
083:
084: // Check to see if this parameter is a complex type.
085: // If so, call this function and add the resulting XList
086: if (swsInnerInputParam.getType().equals(XList.class)) {
087: valuesMap.put(new QName(swsInnerInputParam
088: .getName()),
089: getInputValues(swsInnerInputParam));
090: } else {
091: valuesMap.put(new QName(swsInnerInputParam
092: .getName()), swsInnerInputParam.getValue());
093: }
094: }
095: valuesList = valuesMap;
096: }
097: return valuesList;
098: }
099:
100: private Object getSimpleArrayInputValues(
101: SimpleWebServiceParameter swsInputParam) {
102:
103: //Get # of SWSParameters
104: XList childParams = (XList) swsInputParam.getValue();
105: Object[] inputs = new Object[childParams.size()];
106: for (int i = 0; i < inputs.length; i++) {
107: inputs[i] = ((SimpleWebServiceParameter) childParams.get(i))
108: .getValue();
109: }
110:
111: return inputs;
112:
113: }
114:
115: private Object getComplexArrayInputValues(
116: SimpleWebServiceParameter swsInputParam) {
117: // Get the complex array elements list
118: XList swsArrayElementsList = (XList) swsInputParam.getValue();
119: ArrayList inputValues = new ArrayList();
120:
121: // For each element of the complex array
122: for (int i = 0; i < swsArrayElementsList.size(); i++) {
123: // Create holder for complex element
124: StructMap valuesMap = new StructMap();
125:
126: // Get the SimpleWebServiceParameter representing the complex type
127: SimpleWebServiceParameter swsComplexParam = (SimpleWebServiceParameter) swsArrayElementsList
128: .get(i);
129:
130: // Get the attributes of the complex type
131: XList complexTypeElementsList = (XList) swsComplexParam
132: .getValue();
133:
134: Iterator complexElemsIterator = complexTypeElementsList
135: .iterator();
136:
137: // For each attribute of the complex type
138: while (complexElemsIterator.hasNext()) {
139:
140: // Get the attribute of the complex type
141: SimpleWebServiceParameter swsComplexElemParam = (SimpleWebServiceParameter) complexElemsIterator
142: .next();
143:
144: //Fix for the issue where Complex Array Elements can be Nested
145: //Complex Types
146: if (swsComplexElemParam.getType().equals(XList.class)) {
147: valuesMap.put(new QName(swsComplexElemParam
148: .getName()),
149: getInputValues(swsComplexElemParam));
150: } else {
151: valuesMap
152: .put(new QName(swsComplexElemParam
153: .getName()), swsComplexElemParam
154: .getValue());
155: }
156: }
157:
158: // Add holder to complex array elements list
159: inputValues.add(valuesMap);
160: }
161: return inputValues;
162: }
163:
164: private SimpleWebServiceParameter getSWSPFromRPCObject(
165: Object returnValue,
166: WebServiceDescriptor webServiceDescriptor) {
167:
168: SimpleWebServiceParameter returnSWSParam = null;
169:
170: ParameterDescriptor outputParam = webServiceDescriptor
171: .getOutputParams()[0];
172: if (outputParam.isSimpleType()) {
173: returnSWSParam = new SimpleWebServiceParameter(outputParam,
174: returnValue.toString());
175: } else {
176: // Is it arrays?
177: if (outputParam.isArrayType()) {
178: XList retXList = null;
179: XList list = (XList) outputParam.getValue();
180:
181: // Is simple type array?
182: if (outputParam.isSimpleTypeArray()) {
183: retXList = swsRPCUtil.createXListFromSimpleArray(
184: returnValue,
185: outputParam.getArrayTypeName(), list
186: .getComplexTypeName(), list
187: .getTargetNameSpace());
188: } else {
189: // Complex type array.
190: retXList = swsRPCUtil.createXListFromComplexArray(
191: returnValue, outputParam);
192: }
193:
194: returnSWSParam = new SimpleWebServiceParameter(
195: outputParam, retXList);
196: } else {
197: // It is just a complex type. Handle it.
198: returnSWSParam = swsRPCUtil
199: .createSWSParameterFromComplexType(returnValue,
200: outputParam);
201: }
202: }
203: return returnSWSParam;
204:
205: }
206:
207: public SimpleWebServiceParameter getSWSPFromResponse(
208: Object returnValue,
209: WebServiceDescriptor webServiceDescriptor) {
210: if (returnValue instanceof SOAPElement)
211: return getSWSPFromSOAPElement((SOAPElement) returnValue,
212: webServiceDescriptor);
213: else
214: return getSWSPFromRPCObject(returnValue,
215: webServiceDescriptor);
216: }
217:
218: private SimpleWebServiceParameter getSWSPFromSOAPElement(
219: SOAPElement returnValue,
220: WebServiceDescriptor webServiceDescriptor) {
221: ParameterDescriptor outParam = webServiceDescriptor
222: .getOutputParams()[0];
223:
224: if (outParam != null) {
225: return createResponseSWSParameterwithnull(outParam,
226: returnValue);
227: } else
228: return null;
229:
230: }
231:
232: private void debugNode(Node node) {
233: System.out.println("Node Name" + node.getNodeName());
234: System.out.println("Node Type " + node.getNodeType());
235: System.out.println("Node Value" + node.getValue());
236: }
237:
238: private SimpleWebServiceParameter createResponseSWSParameter(
239: ParameterDescriptor param, SOAPElement responseBody) {
240:
241: SimpleWebServiceParameter swsParam = null;
242:
243: if (responseBody == null) {
244: return new SimpleWebServiceParameter(param, "");
245: }
246:
247: // System.out.println("Creating ParameterDescriptor For" + param.getName() + " with "+ responseBody.getTagName());
248: if (param.getType() != XList.class) {
249: //this means that the SOAPElement passed as argument would be like
250: //<responseBody>result</responseBody>
251: if (responseBody != null) {
252: swsParam = new SimpleWebServiceParameter(param,
253: responseBody.getValue());
254: } else {
255: swsParam = new SimpleWebServiceParameter(param,
256: (String) null);
257: }
258:
259: } else {
260: //It is a complex Type
261: //So 1. Get the Child Parameter Descriptors
262: // 2. Get Child Elements
263: // 3. Construct a SWS with correspondense
264: XList parentParam = (XList) param.getValue();
265: XList childrenValues = new XList(parentParam
266: .getComplexTypeName(), parentParam
267: .getTargetNameSpace());
268: ArrayList childElements = getElementNodesList(responseBody
269: .getChildElements());
270: //So now children values will have parameter Descriptors and childrenElements will
271: //have the corresponding Element
272: if (parentParam.size() != childElements.size()) {
273: System.out.println("Fatal Error" + parentParam.size()
274: + " " + childElements.size());
275: }
276: for (int i = 0; i < parentParam.size(); i++) {
277: SimpleWebServiceParameter childDesc = createResponseSWSParameter(
278: (ParameterDescriptor) parentParam.get(i),
279: (SOAPElement) childElements.get(i));
280: childrenValues.add(childDesc);
281: }
282: swsParam = new SimpleWebServiceParameter(param,
283: childrenValues);
284: }
285: return swsParam;
286: }
287:
288: private SimpleWebServiceParameter createResponseSWSParameterwithnull(
289: ParameterDescriptor param, SOAPElement responseBody) {
290:
291: SimpleWebServiceParameter swsParam = null;
292:
293: if (responseBody == null) {
294: return new SimpleWebServiceParameter(param, (String) null);
295: }
296:
297: //System.out.println("Creating ParameterDescriptor For" + param.getName() + " with "+ responseBody.getTagName());
298: if (param.getType() != XList.class) {
299: //this means that the SOAPElement passed as argument would be like
300: //<responseBody>result</responseBody>
301: if (responseBody != null) {
302: swsParam = new SimpleWebServiceParameter(param,
303: responseBody.getValue());
304: } else {
305: swsParam = new SimpleWebServiceParameter(param,
306: (String) null);
307: }
308:
309: } else {
310: //It is a complex Type
311: //So 1. Get the Child Parameter Descriptors
312: // 2. Get Child Elements
313: // 3. Construct a SWS with correspondense
314: XList childParams = (XList) param.getValue();
315: XList childrenValues = new XList(childParams
316: .getComplexTypeName(), childParams
317: .getTargetNameSpace());
318: ArrayList childElements = getElementNodesList(responseBody
319: .getChildElements());
320: //So now children values will have parameter Descriptors and childrenElements will
321: //have the corresponding Element
322: if (childParams.size() != childElements.size()) {
323: System.out.println("Fatal Error" + childParams.size()
324: + " " + childElements.size());
325: }
326: for (int i = 0; i < childParams.size(); i++) {
327:
328: ParameterDescriptor childDescriptor = (ParameterDescriptor) childParams
329: .get(i);
330: ArrayList children = getChildElementWithName(
331: childElements, childDescriptor.getName());
332: for (int j = 0; j < children.size(); j++) {
333: SimpleWebServiceParameter childDesc = createResponseSWSParameterwithnull(
334: childDescriptor, (SOAPElement) children
335: .get(j));
336: childrenValues.add(childDesc);
337: }
338: }
339: swsParam = new SimpleWebServiceParameter(param,
340: childrenValues);
341: }
342: return swsParam;
343: }
344:
345: private boolean isElementNode(Node nd) {
346: return (nd == null || (nd.getNodeType() == Node.ELEMENT_NODE));
347: }
348:
349: private ArrayList getChildElementWithName(ArrayList list,
350: String name) {
351: Node node = null;
352: ArrayList outValues = new ArrayList();
353: for (int i = 0; i < list.size(); i++) {
354: node = (Node) list.get(i);
355: if (node.getLocalName().equals(name))
356: outValues.add(node);
357: //System.out.println("VErifying "+node.getNodeName() + " " + name+ " " + node.getLocalName());
358: }
359: return outValues;
360: }
361:
362: private XList createRecursiveResponseSWSParameter(
363: ParameterDescriptor param, SOAPElement element) {
364: SimpleWebServiceParameter swsParam = null;
365:
366: XList paramsList = (XList) param.getValue();
367: XList valuesList = new XList(paramsList.getComplexTypeName(),
368: paramsList.getTargetNameSpace());
369: Iterator values = element.getChildElements();
370: ArrayList elementNodes = getElementNodesList(values);
371:
372: for (int i = 0; i < paramsList.size(); i++) {
373: ParameterDescriptor innerParam = (ParameterDescriptor) paramsList
374: .get(i);
375:
376: SOAPElement innerElement = null;
377:
378: innerElement = (SOAPElement) elementNodes.get(i);
379:
380: if (innerParam.getType() == XList.class) {
381: XList innerSWSList = createRecursiveResponseSWSParameter(
382: innerParam, innerElement);
383: swsParam = new SimpleWebServiceParameter(innerParam,
384: innerSWSList);
385: } else {
386: swsParam = new SimpleWebServiceParameter(innerParam,
387: innerElement.getValue());
388: }
389:
390: valuesList.add(swsParam);
391: }
392: return valuesList;
393: }
394:
395: private ArrayList getElementNodesList(Iterator nodesIterator) {
396: ArrayList list = new ArrayList();
397: Node node = null;
398:
399: while (nodesIterator.hasNext()) {
400:
401: node = (Node) nodesIterator.next();
402: if (node.getNodeType() == Node.ELEMENT_NODE)
403: list.add(node);
404:
405: }
406: return list;
407:
408: }
409:
410: private ArrayList getElementNodesList(Iterator nodesIterator,
411: String elementName) {
412: ArrayList list = new ArrayList();
413: Node node = null;
414: while (nodesIterator.hasNext()) {
415:
416: node = (Node) nodesIterator.next();
417: if (node.getNodeType() == Node.ELEMENT_NODE
418: && node.getNodeName().equals(elementName))
419: list.add(node);
420:
421: }
422: return list;
423: }
424:
425: /*
426: * Sets the input and output parameter types based on the
427: * information parsed into the WebServiceDescriptor.
428: *
429: * This function in turn, calls the setOutputParameter and
430: * setInputParameters to set the input and output parameter
431: * information.
432: */
433: public void setIOParameters(Call call,
434: WebServiceDescriptor webServiceDescriptor,
435: SimpleWebServiceParameter[] inputValues)
436: throws JAXRPCException, IllegalArgumentException {
437:
438: if (webServiceDescriptor.getOutputParams().length > 0) {
439: setOutputParameter(call, webServiceDescriptor, swsRPCUtil);
440: }
441:
442: setInputParameters(call, webServiceDescriptor, inputValues,
443: swsRPCUtil);
444: }
445:
446: private void setOutputParameter(Call call,
447: WebServiceDescriptor webServiceDescriptor,
448: SimpleWebServiceUtil swsRPCUtil) throws JAXRPCException,
449: IllegalArgumentException {
450:
451: ParameterDescriptor outputParam = webServiceDescriptor
452: .getOutputParams()[0];
453:
454: QName type = swsRPCUtil.getSimpleSchemaType(outputParam
455: .getType(), "");
456:
457: if (type != null) {
458: call.setReturnType(type);
459: } else {
460: // Handle complex return types
461: XList value = (XList) outputParam.getValue();
462:
463: type = new QName(value.getTargetNameSpace(), value
464: .getComplexTypeName());
465:
466: call.setReturnType(type);
467: }
468: if (webServiceDescriptor.getSOAPBindingStyle() == "document") {
469: doExtendedConfiguration(call, webServiceDescriptor, type);
470: }
471: }
472:
473: private void setInputParameters(Call call,
474: WebServiceDescriptor webServiceDescriptor,
475: SimpleWebServiceParameter[] inputValues,
476: SimpleWebServiceUtil swsRPCUtil) throws JAXRPCException,
477: IllegalArgumentException {
478:
479: for (int count = 0; count < inputValues.length; count++) {
480: SimpleWebServiceParameter inputParam = inputValues[count];
481:
482: QName type = swsRPCUtil.getSimpleSchemaType(inputParam
483: .getType(), "");
484:
485: String name = inputParam.getName();
486: if ((name == null) && name.trim().equals(""))
487: name = inputParam.getType().getName();
488:
489: if (type != null) {
490: call.addParameter(name, type, ParameterMode.IN);
491:
492: } else {
493: // Handle complex input types
494: XList value = (XList) inputParam.getValue();
495: String nmSpace = value.getTargetNameSpace();
496:
497: QName complexQName = new QName(nmSpace, value
498: .getComplexTypeName());
499: //call.addParameter(name, complexQName, inputParam.getType(), ParameterMode.IN);
500: call.addParameter(name, complexQName, ParameterMode.IN);
501: }
502: }
503: }
504:
505: //RK: Note DocumentLiteral Services need that the call Objects
506: //should be updated with both return type and return type class
507: // In case of document-literal service,the return type would be
508: // SOAPElement
509: //Revert Back if does not work
510: private void doExtendedConfiguration(Call call,
511: WebServiceDescriptor wsd, QName returntype) {
512:
513: if (returntype != null
514: && wsd.getSOAPBindingStyle() == "document") {
515: call.setReturnType(returntype, SOAPElement.class);
516:
517: } else {
518: return;
519: }
520: }
521:
522: }
|