001: /**
002: * $Id: SimpleWebServiceUtilImpl.java,v 1.1 2003/06/03 10:45:15 sy131129 Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.providers.simplewebservice.util;
014:
015: import java.util.ArrayList;
016: import javax.xml.namespace.QName;
017:
018: import com.sun.xml.rpc.wsdl.document.schema.SchemaConstants;
019: import com.sun.xml.rpc.wsdl.document.soap.SOAPConstants;
020: import com.sun.xml.rpc.util.StructMap;
021: import com.sun.portal.providers.simplewebservice.ParameterDescriptor;
022: import com.sun.portal.providers.simplewebservice.SimpleWebServiceParameter;
023:
024: /*
025: * Implements the various utility functions exposed via interface SimpleWebServiceUtil.
026: */
027:
028: public class SimpleWebServiceUtilImpl implements SimpleWebServiceUtil {
029:
030: public QName getSimpleSchemaType(Class type, String encodingToUse) {
031:
032: if (encodingToUse.equals("")
033: || encodingToUse.equals(SchemaConstants.NS_XSD)) {
034: if (type.equals(java.lang.String.class)) {
035: return SchemaConstants.QNAME_TYPE_STRING;
036: } else if (type == Float.class) {
037: return SchemaConstants.QNAME_TYPE_FLOAT;
038: } else if (type == Integer.class) {
039: return SchemaConstants.QNAME_TYPE_INT;
040: } else if (type == Double.class) {
041: return SchemaConstants.QNAME_TYPE_DOUBLE;
042: } else if (type == Boolean.class) {
043: return SchemaConstants.QNAME_TYPE_BOOLEAN;
044: } else if (type == Long.class) {
045: return SchemaConstants.QNAME_TYPE_LONG;
046: } else if (type == Byte.class) {
047: return SchemaConstants.QNAME_TYPE_BYTE;
048: } else if (type == Short.class) {
049: return SchemaConstants.QNAME_TYPE_SHORT;
050: }
051: } else if (encodingToUse.equals(SOAPConstants.NS_SOAP_ENCODING)) {
052: if (type.equals(java.lang.String.class)) {
053: return SOAPConstants.QNAME_TYPE_STRING;
054: } else if (type == Float.class) {
055: return SOAPConstants.QNAME_TYPE_FLOAT;
056: } else if (type == Integer.class) {
057: return SOAPConstants.QNAME_TYPE_INT;
058: } else if (type == Double.class) {
059: return SOAPConstants.QNAME_TYPE_DOUBLE;
060: } else if (type == Boolean.class) {
061: return SOAPConstants.QNAME_TYPE_BOOLEAN;
062: } else if (type == Long.class) {
063: return SOAPConstants.QNAME_TYPE_LONG;
064: } else if (type == Byte.class) {
065: return SOAPConstants.QNAME_TYPE_BYTE;
066: } else if (type == Short.class) {
067: return SOAPConstants.QNAME_TYPE_SHORT;
068: }
069: }
070: return null;
071: }
072:
073: public Class getSimpleClassType(String type) {
074:
075: if (type.equals(SimpleWebServiceTypeConstants.STRING)) {
076: return String.class;
077: } else if (type.equals(SimpleWebServiceTypeConstants.FLOAT)) {
078: return Float.class;
079: } else if (type.equals(SimpleWebServiceTypeConstants.INT)) {
080: return Integer.class;
081: } else if (type.equals(SimpleWebServiceTypeConstants.DOUBLE)) {
082: return Double.class;
083: } else if (type.equals(SimpleWebServiceTypeConstants.BOOLEAN)) {
084: return Boolean.class;
085: } else if (type.equals(SimpleWebServiceTypeConstants.LONG)) {
086: return Long.class;
087: } else if (type.equals(SimpleWebServiceTypeConstants.BYTE)) {
088: return Byte.class;
089: } else if (type.equals(SimpleWebServiceTypeConstants.SHORT)) {
090: return Short.class;
091: }
092: return null;
093: }
094:
095: public XList createXListFromSimpleArray(Object returnValues,
096: String arrayElementTypeName, String arrayName,
097: String arrayNamespace) {
098:
099: XList xlist = new XList(arrayName, arrayNamespace);
100:
101: if (arrayElementTypeName
102: .equals(SimpleWebServiceTypeConstants.FLOAT)) {
103: float[] values = (float[]) returnValues;
104: for (int count = 0; count < values.length; count++)
105: xlist.add(new Float(values[count]));
106: } else if (arrayElementTypeName
107: .equals(SimpleWebServiceTypeConstants.INT)) {
108: int[] values = (int[]) returnValues;
109: for (int count = 0; count < values.length; count++)
110: xlist.add(new Integer(values[count]));
111: } else if (arrayElementTypeName
112: .equals(SimpleWebServiceTypeConstants.LONG)) {
113: long[] values = (long[]) returnValues;
114: for (int count = 0; count < values.length; count++)
115: xlist.add(new Long(values[count]));
116: } else if (arrayElementTypeName
117: .equals(SimpleWebServiceTypeConstants.DOUBLE)) {
118: double[] values = (double[]) returnValues;
119: for (int count = 0; count < values.length; count++)
120: xlist.add(new Double(values[count]));
121: } else if (arrayElementTypeName
122: .equals(SimpleWebServiceTypeConstants.SHORT)) {
123: short[] values = (short[]) returnValues;
124: for (int count = 0; count < values.length; count++)
125: xlist.add(new Short(values[count]));
126: } else if (arrayElementTypeName
127: .equals(SimpleWebServiceTypeConstants.BYTE)) {
128: byte[] values = (byte[]) returnValues;
129: for (int count = 0; count < values.length; count++)
130: xlist.add(new Byte(values[count]));
131: } else if (arrayElementTypeName
132: .equals(SimpleWebServiceTypeConstants.BOOLEAN)) {
133: boolean[] values = (boolean[]) returnValues;
134: for (int count = 0; count < values.length; count++)
135: xlist.add(new Boolean(values[count]));
136: } else if (arrayElementTypeName
137: .equals(SimpleWebServiceTypeConstants.STRING)) {
138: String[] values = (String[]) returnValues;
139: for (int count = 0; count < values.length; count++)
140: xlist.add(values[count]);
141: }
142: return xlist;
143: }
144:
145: /*
146: * Returns an XList of SimpleWebServiceParameter. The returned XList is constructed using
147: * the meta data ParameterDescriptor, outputParamDesc. The outputParamDesc is expected
148: * to be the ParameterDescriptor of the array. Any nested objects are also constructed as
149: * SimpleWebServiceParameters, using meta data in outputParamDesc and its nested objects.
150: */
151: public XList createXListFromComplexArray(Object returnValues,
152: ParameterDescriptor outputParamDesc) {
153:
154: XList paramDescsList = (XList) outputParamDesc.getValue();
155: XList swsXList = new XList(paramDescsList.getComplexTypeName(),
156: paramDescsList.getTargetNameSpace());
157:
158: // Complex arrays returned by CollectionSerializer are expected in an ArrayList.
159: // However, an array is returned sometimes.
160: if (returnValues instanceof ArrayList) {
161: ArrayList returnValuesList = (ArrayList) returnValues;
162:
163: for (int count = 0; count < returnValuesList.size(); count++) {
164: swsXList.add(createSWSParameterFromComplexType(
165: returnValuesList.get(count),
166: (ParameterDescriptor) paramDescsList.get(0)));
167: }
168: } else if (returnValues instanceof StructMap[]) {
169: StructMap[] returnValuesList = (StructMap[]) returnValues;
170:
171: for (int count = 0; count < returnValuesList.length; count++) {
172: swsXList.add(createSWSParameterFromComplexType(
173: returnValuesList[count],
174: (ParameterDescriptor) paramDescsList.get(0)));
175: }
176: }
177:
178: return swsXList;
179: }
180:
181: /*
182: * Returns a SimpleWebServiceParameter. The returned SimpleWebServiceParameter is
183: * constructed using the meta data ParameterDescriptor, outputParamDesc. Any nested objects
184: * are also constructed as SimpleWebServiceParameters, using meta data in outputParamDesc
185: * and its nested objects.
186: */
187: public SimpleWebServiceParameter createSWSParameterFromComplexType(
188: Object returnValue, ParameterDescriptor outputParamDesc) {
189:
190: XList paramDescsList = (XList) outputParamDesc.getValue();
191:
192: // Complex object returned by XMapSerializer is expected to be a StructMap
193: StructMap outValue = (StructMap) returnValue;
194:
195: SimpleWebServiceParameter swsOuterParam = null;
196:
197: // Create SimpleWebServiceParameter for complex type
198: XList swsXList = new XList(paramDescsList.getComplexTypeName(),
199: paramDescsList.getTargetNameSpace());
200: swsOuterParam = new SimpleWebServiceParameter(outputParamDesc,
201: swsXList);
202:
203: // Create SimpleWebServiceParameters for complex type's elements
204: for (int count = 0; count < paramDescsList.size(); count++) {
205: SimpleWebServiceParameter innerSWSParam = null;
206: ParameterDescriptor innerParamDesc = (ParameterDescriptor) paramDescsList
207: .get(count);
208:
209: QName currentParamName = new QName("", innerParamDesc
210: .getName());
211:
212: Object value = outValue.get(currentParamName);
213:
214: if (innerParamDesc.getType() == XList.class) {
215: // Element is Array type or Complex type.
216: // TODO - Handle simple arrays
217: if (innerParamDesc.isArrayType()) {
218: innerSWSParam = new SimpleWebServiceParameter(
219: innerParamDesc,
220: createXListFromComplexArray(value,
221: innerParamDesc));
222: } else {
223: innerSWSParam = createSWSParameterFromComplexType(
224: value, innerParamDesc);
225: }
226: } else {
227: // Simple type element. So create the SWSParameter.
228: innerSWSParam = new SimpleWebServiceParameter(
229: innerParamDesc, value.toString());
230: }
231: swsXList.add(innerSWSParam);
232: }
233: return swsOuterParam;
234: }
235:
236: }
|