001: /**
002: * $Id: DefinitionDescriptorBuilder.java,v 1.2 2003/12/01 10:47:45 vv138407 Exp $
003: * Copyright 2002-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.wsdl.impl;
014:
015: /**
016: * RFE: 4954276
017: */
018: import com.sun.portal.providers.simplewebservice.wsdl.*;
019: import com.sun.xml.rpc.processor.model.*;
020: import com.sun.xml.rpc.wsdl.document.soap.SOAPStyle;
021: import com.sun.xml.rpc.wsdl.document.soap.SOAPUse;
022: import com.sun.xml.rpc.encoding.soap.SOAPConstants;
023:
024: import javax.xml.namespace.QName;
025: import java.util.*;
026:
027: class DefinitionDescriptorBuilder {
028:
029: public DefinitionDescriptorBuilder() {
030: }
031:
032: /**
033: *
034: * @param model
035: * @return DefinitionDescriptor
036: *
037: */
038: public DefinitionDescriptor getDefinitionDescriptor(Model model)
039: throws WSDLException {
040: DefinitionDescriptor definitionDescriptor = null;
041: services = getServiceDescriptors(model);
042: definitionDescriptor = new DefinitionDescriptorImpl(model
043: .getName().getLocalPart(), model
044: .getTargetNamespaceURI(),
045: new HashMap(), //namespaces are not provided
046: typeDescriptors, messages, bindings, portTypes,
047: services);
048: return definitionDescriptor;
049: }
050:
051: private Map getServiceDescriptors(Model model) throws WSDLException {
052: Map servicesMap = new HashMap();
053: Iterator it = model.getServices();
054: Service service = null;
055:
056: while (it.hasNext()) {
057: service = (Service) it.next();
058: String documentation = (String) service
059: .getProperty("com.sun.xml.rpc.processor.modeler.wsdl.documentation");
060: ServiceDescriptor svrDesc = new ServiceDescriptorImpl(
061: service.getName().getLocalPart(), documentation,
062: getPortDescriptors(service));
063: servicesMap.put(svrDesc.getName(), svrDesc);
064: }
065: if (servicesMap.size() == 0)
066: throw new WSDLException(WSDLException.INVALID_WSDL,
067: "WSDL Does not contain" + "Services");
068: return servicesMap;
069: }
070:
071: private Map getPortDescriptors(Service svr) throws WSDLException {
072: Iterator it = svr.getPorts();
073: Port port = null;
074: Map ports = new HashMap();
075:
076: while (it.hasNext()) {
077: port = (Port) it.next();
078: String bindingname = "";
079: QName binding = (QName) port
080: .getProperty(ModelProperties.PROPERTY_WSDL_BINDING_NAME);
081: if (binding != null)
082: bindingname = binding.getLocalPart();
083: PortDescriptor pp = new PortDescriptorImpl(port.getName()
084: .getLocalPart(), bindingname, port.getAddress());
085: ports.put(pp.getName(), pp);
086: fillBindingDescriptors(port);
087: fillPortTypeDescriptors(port);
088: }
089: if (ports.size() == 0)
090: throw new WSDLException(WSDLException.INVALID_WSDL,
091: "Service" + svr.getName() + "does not contain"
092: + "any Ports");
093:
094: return ports;
095:
096: }
097:
098: private void fillBindingDescriptors(Port pt) throws WSDLException {
099:
100: Iterator ops = pt.getOperations();
101: Operation op = null;
102: List bindingOpdescs = new ArrayList();
103: String style = "";
104: while (ops.hasNext()) {
105: op = (Operation) ops.next();
106: BindingOperationDescriptor bod = new BindingOperationDescriptorImpl(
107: op.getName().getLocalPart(), op.getSOAPAction(),
108: getInputDesc(op), getOutputDesc(op));
109: bindingOpdescs.add(bod);
110: style = op.getStyle().equals(SOAPStyle.RPC) ? "rpc"
111: : "document";
112: }
113: String bindingname = "";
114: QName binding = (QName) pt
115: .getProperty(ModelProperties.PROPERTY_WSDL_BINDING_NAME);
116: if (binding != null)
117: bindingname = binding.getLocalPart();
118: String portTypeName = "";
119: QName portType = (QName) pt
120: .getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME);
121: if (portType != null)
122: portTypeName = portType.getLocalPart();
123:
124: BindingDescriptor bd = new BindingDescriptorImpl(
125: bindingname,
126: portTypeName,
127: bindingOpdescs,
128: style,
129: com.sun.xml.rpc.wsdl.document.soap.SOAPConstants.URI_SOAP_TRANSPORT_HTTP);
130: bindings.put(bd.getName(), bd);
131: }
132:
133: private void fillPortTypeDescriptors(Port pt) {
134: Iterator ops = pt.getOperations();
135: Operation op = null;
136: List portDescs = new ArrayList();
137: QName inputQName, outputQName;
138: while (ops.hasNext()) {
139: op = (Operation) ops.next();
140: inputQName = (QName) op.getRequest().getProperty(
141: ModelProperties.PROPERTY_WSDL_MESSAGE_NAME);
142: String inputMessage = (inputQName == null ? "" : inputQName
143: .getLocalPart());
144: outputQName = (QName) op.getResponse().getProperty(
145: ModelProperties.PROPERTY_WSDL_MESSAGE_NAME);
146: String outputMessage = (outputQName == null ? ""
147: : outputQName.getLocalPart());
148: OperationDescriptor od = new OperationDescriptorImpl(op
149: .getName().getLocalPart(), inputMessage,
150: outputMessage);
151: fillMessageDescriptors(op);
152: portDescs.add(od);
153: }
154:
155: String portTypeName = "";
156: QName portType = (QName) pt
157: .getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME);
158: if (portType != null)
159: portTypeName = portType.getLocalPart();
160: PortTypeDescriptor ptd = new PortTypeDescriptorImpl(
161: portTypeName, portDescs);
162: portTypes.put(ptd.getName(), ptd);
163: }
164:
165: private void fillMessageDescriptorsForDocLiteral(Operation op) {
166: Iterator it = op.getRequest().getBodyBlocks();
167: Block p = null;
168: Map inpartDescs = new HashMap();
169: Map outpartDescs = new HashMap();
170: List inputs = new ArrayList();
171: List outputs = new ArrayList();
172: Iterator it2 = op.getRequest().getParameters();
173: String inputMessage = ((QName) op.getRequest().getProperty(
174: ModelProperties.PROPERTY_WSDL_MESSAGE_NAME))
175: .getLocalPart();
176: String outputMessage = ((QName) op.getResponse().getProperty(
177: ModelProperties.PROPERTY_WSDL_MESSAGE_NAME))
178: .getLocalPart();
179: while (it.hasNext()) {
180: p = (Block) it.next();
181: Parameter param = null;
182: String partName = "";
183: if (it2.hasNext()) {
184: param = (Parameter) it2.next();
185: partName = (String) param
186: .getProperty(ModelProperties.PROPERTY_PARAM_MESSAGE_PART_NAME);
187: }
188:
189: //jwsdp-1.3
190: PartDescriptor pp = new PartDescriptorImpl(partName, p
191: .getType().getName().getLocalPart());
192:
193: inpartDescs.put(pp, pp);
194: inputs.add(pp);
195: }
196: MessageDescriptor inMsg = new MessageDescriptorImpl(
197: inputMessage, inpartDescs, inputs);
198: it = op.getResponse().getBodyBlocks();
199: it2 = op.getResponse().getParameters();
200: while (it.hasNext()) {
201: p = (Block) it.next();
202: Parameter param = null;
203: String partName = "";
204: if (it2.hasNext()) {
205: param = (Parameter) it2.next();
206: partName = (String) param
207: .getProperty(ModelProperties.PROPERTY_PARAM_MESSAGE_PART_NAME);
208: }
209: PartDescriptor pp = new PartDescriptorImpl(partName, p
210: .getType().getName().getLocalPart());
211: //outpartDescs.put(pp.getName(),pp);
212: outpartDescs.put(pp, pp);
213: outputs.add(pp);
214: }
215: MessageDescriptor outMsg = new MessageDescriptorImpl(
216: outputMessage, outpartDescs, outputs);
217: messages.put(inMsg.getName(), inMsg);
218: messages.put(outMsg.getName(), outMsg);
219:
220: }
221:
222: private void fillMessageDescriptors(Operation op) {
223: if (op.getStyle() == SOAPStyle.DOCUMENT) {
224: fillMessageDescriptorsForDocLiteral(op);
225: return;
226: }
227: Iterator it = op.getRequest().getParameters();
228: Parameter p = null;
229: Map inpartDescs = new HashMap();
230: Map outpartDescs = new HashMap();
231: List inputs = new ArrayList();
232: List outputs = new ArrayList();
233:
234: String inputMessage = ((QName) op.getRequest().getProperty(
235: ModelProperties.PROPERTY_WSDL_MESSAGE_NAME))
236: .getLocalPart();
237: String outputMessage = ((QName) op.getResponse().getProperty(
238: ModelProperties.PROPERTY_WSDL_MESSAGE_NAME))
239: .getLocalPart();
240: while (it.hasNext()) {
241: p = (Parameter) it.next();
242:
243: PartDescriptor pp = new PartDescriptorImpl(p.getName(), p
244: .getType().getName().getLocalPart());
245:
246: inpartDescs.put(pp, pp);
247: inputs.add(pp);
248: }
249: MessageDescriptor inMsg = new MessageDescriptorImpl(
250: inputMessage, inpartDescs, inputs);
251: it = op.getResponse().getParameters();
252: while (it.hasNext()) {
253: p = (Parameter) it.next();
254: PartDescriptor pp = new PartDescriptorImpl(p.getName(), p
255: .getType().getName().getLocalPart());
256: //outpartDescs.put(pp.getName(),pp);
257: outpartDescs.put(pp, pp);
258: outputs.add(pp);
259: }
260: MessageDescriptor outMsg = new MessageDescriptorImpl(
261: outputMessage, outpartDescs, outputs);
262: messages.put(inMsg.getName(), inMsg);
263: messages.put(outMsg.getName(), outMsg);
264: }
265:
266: private BindingOperationInputDescriptor getInputDesc(Operation op)
267: throws WSDLException {
268: String use = "";
269: String enc = "";
270: use = (op.getUse() == SOAPUse.ENCODED ? "encoded" : "literal");
271: if (use.equals("encoded"))
272: enc = SOAPConstants.URI_ENCODING;
273: String nmspace = "";
274: Iterator it = op.getRequest().getParameters();
275: if (it.hasNext()) {
276: nmspace = ((Parameter) it.next()).getBlock().getName()
277: .getNamespaceURI();
278: } else {
279: // throw new WSDLException(WSDLException.OTHER_ERROR, "One Way Operations are not supported");
280: }
281:
282: BindingOperationInputDescriptor bb = new BindingOperationInputDescriptorImpl(
283: "", use, nmspace, enc);
284: fillInTypeDescriptors(op);
285: return bb;
286:
287: }
288:
289: private BindingOperationOutputDescriptor getOutputDesc(Operation op)
290: throws WSDLException {
291: String use = "";
292: String enc = "";
293: use = (op.getUse() == SOAPUse.ENCODED ? "encoded" : "literal");
294: if (use.equals("encoded"))
295: enc = SOAPConstants.URI_ENCODING;
296: String nmspace = "";
297: Iterator it = op.getResponse().getParameters();
298: if (it.hasNext()) {
299: nmspace = ((Parameter) it.next()).getBlock().getName()
300: .getNamespaceURI();
301: } else {
302: // throw new WSDLException(WSDLException.OTHER_ERROR, "One Way Operations are not supported");
303: }
304:
305: BindingOperationOutputDescriptor bb = new BindingOperationOutputDescriptorImpl(
306: "", use, nmspace, enc);
307: fillOutTypeDescriptors(op);
308: return bb;
309:
310: }
311:
312: private void fillInTypeDescriptorsForDocLiteral(Operation op)
313: throws WSDLException {
314: Request req = op.getRequest();
315:
316: Block param = null;
317: Iterator it = req.getBodyBlocks();
318: WSDLTypesCollector typesVisitor = new WSDLTypesCollector("",
319: typeDescriptors);
320: try {
321:
322: while (it.hasNext()) {
323:
324: param = (Block) it.next();
325: typesVisitor.doAccept(param.getName().getLocalPart(),
326: param.getType());
327: }
328: } catch (Exception e) {
329: throw new WSDLException(WSDLException.INVALID_WSDL, e
330: .getMessage());
331: }
332:
333: }
334:
335: private void fillInTypeDescriptors(Operation op)
336: throws WSDLException {
337: if (op.getStyle() == SOAPStyle.DOCUMENT) {
338: fillInTypeDescriptorsForDocLiteral(op);
339: return;
340: }
341: Request req = op.getRequest();
342: Iterator it = req.getParameters();
343: Parameter param = null;
344:
345: WSDLTypesCollector typesVisitor = new WSDLTypesCollector("",
346: typeDescriptors);
347: try {
348:
349: while (it.hasNext()) {
350:
351: param = (Parameter) it.next();
352: typesVisitor.doAccept(param.getName(), param.getType());
353: }
354: } catch (Exception e) {
355: throw new WSDLException(WSDLException.INVALID_WSDL, e
356: .getMessage());
357: }
358:
359: }
360:
361: private void fillOutTypeDescriptorsForDocLiteral(Operation op)
362: throws WSDLException {
363: Response resp = op.getResponse();
364:
365: Block param = null;
366: Iterator it = resp.getBodyBlocks();
367: WSDLTypesCollector typesVisitor = new WSDLTypesCollector("",
368: typeDescriptors);
369: try {
370:
371: while (it.hasNext()) {
372:
373: param = (Block) it.next();
374: typesVisitor.doAccept(param.getName().getLocalPart(),
375: param.getType());
376: }
377: } catch (Exception e) {
378: throw new WSDLException(WSDLException.INVALID_WSDL, e
379: .getMessage());
380: }
381:
382: }
383:
384: private void fillOutTypeDescriptors(Operation op)
385: throws WSDLException {
386: if (op.getStyle() == SOAPStyle.DOCUMENT) {
387: fillOutTypeDescriptorsForDocLiteral(op);
388: return;
389: }
390: Response resp = op.getResponse();
391: Iterator it = resp.getParameters();
392: Parameter param = null;
393: WSDLTypesCollector typesVisitor = new WSDLTypesCollector("",
394: typeDescriptors);
395: try {
396:
397: while (it.hasNext()) {
398: param = (Parameter) it.next();
399: typesVisitor.doAccept(param.getName(), param.getType());
400: }
401: } catch (Exception e) {
402: throw new WSDLException(WSDLException.INVALID_WSDL, e
403: .getMessage());
404: }
405:
406: }
407:
408: private List getFaults(Operation op) {
409: List faultsList = new ArrayList();
410: faultsList.addAll(op.getFaultsSet());
411:
412: return faultsList;
413:
414: }
415:
416: private Map bindings = new HashMap();
417: private Map portTypes = new HashMap();
418: private Map messages = new HashMap();
419: private Map services = new HashMap();
420: private Map typeDescriptors = new HashMap();
421: }
|