You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ke...@apache.org on 2007/03/22 07:02:30 UTC

svn commit: r521126 [2/2] - in /webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2: builder/ description/ util/

Added: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/util/WSDLSerializationUtil.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/util/WSDLSerializationUtil.java?view=auto&rev=521126
==============================================================================
--- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/util/WSDLSerializationUtil.java (added)
+++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/util/WSDLSerializationUtil.java Wed Mar 21 23:02:29 2007
@@ -0,0 +1,315 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+
+             http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axis2.util;
+
+import org.apache.axis2.description.AxisMessage;
+import org.apache.axis2.description.WSDL2Constants;
+import org.apache.axis2.description.AxisOperation;
+import org.apache.axis2.description.AxisService;
+import org.apache.axis2.wsdl.WSDLConstants;
+import org.apache.axis2.wsdl.SOAPHeaderMessage;
+import org.apache.axis2.wsdl.SOAPModuleMessage;
+import org.apache.axis2.wsdl.HTTPHeaderMessage;
+import org.apache.axis2.namespace.Constants;
+import org.apache.axis2.AxisFault;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.ws.java2wsdl.Java2WSDLConstants;
+
+import javax.xml.namespace.QName;
+import java.util.Map;
+import java.util.Iterator;
+import java.util.ArrayList;
+
+/**
+ * Helps the AxisService to WSDL process
+ */
+public class WSDLSerializationUtil {
+
+    /**
+     * Given a namespace it returns the prefix for that namespace
+     * @param namespace - The namespace that the prefix is needed for
+     * @param nameSpaceMap - The namespaceMap
+     * @return - The prefix of the namespace
+     */
+    public static String getPrefix(String namespace, Map nameSpaceMap) {
+        Iterator keys = nameSpaceMap.keySet().iterator();
+        while (keys.hasNext()) {
+            String key = (String) keys.next();
+            if (nameSpaceMap.get(key).equals(namespace)) {
+                return key;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Gets the correct element name for a given message
+     * @param axisMessage - The axisMessage
+     * @param nameSpaceMap - The namespaceMap
+     * @return - The element name
+     */
+    public static String getElementName(AxisMessage axisMessage, Map nameSpaceMap) {
+        QName elementQName = axisMessage.getElementQName();
+        if (elementQName == null) {
+            return WSDLConstants.WSDL20_2006Constants.NMTOKEN_NONE;
+        } else if (Constants.XSD_ANY.equals(elementQName)) {
+            return WSDLConstants.WSDL20_2006Constants.NMTOKEN_ANY;
+        } else {
+            String prefix =
+                    WSDLSerializationUtil.getPrefix(elementQName.getNamespaceURI(), nameSpaceMap);
+            return prefix + ":" + elementQName.getLocalPart();
+        }
+    }
+
+    /**
+     * Adds a soap header element to a given OMElement
+     * @param omFactory - An OMFactory
+     * @param list - The arraylist of soapHeaderMessages
+     * @param wsoap - The WSDL 2.0 SOAP namespace
+     * @param element - The element that the header should be added to
+     * @param nameSpaceMap - The namespaceMap
+     */
+    public static void addSOAPHeaderElements(OMFactory omFactory, ArrayList list, OMNamespace wsoap,
+                                             OMElement element, Map nameSpaceMap) {
+        for (int i = 0; i < list.size(); i++) {
+            SOAPHeaderMessage soapHeaderMessage = (SOAPHeaderMessage) list.get(i);
+            OMElement soapHeaderElement =
+                    omFactory.createOMElement(WSDL2Constants.ATTRIBUTE_HEADER, wsoap);
+            QName qName = soapHeaderMessage.getElement();
+            soapHeaderElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_ELEMENT, null,
+                    getPrefix(qName.getNamespaceURI(), nameSpaceMap) + ":" + qName.getLocalPart()));
+            soapHeaderElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_MUST_UNDERSTAND, null,
+                    new Boolean(soapHeaderMessage.isMustUnderstand()).toString()));
+            soapHeaderElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_REQUIRED, null,
+                    new Boolean(soapHeaderMessage.isRequired()).toString()));
+            element.addChild(soapHeaderElement);
+        }
+    }
+
+    /**
+     * Adds a soap module element to a given OMElement
+     * @param omFactory - An OMFactory
+     * @param list - The arraylist of soapModuleMessages
+     * @param wsoap - The WSDL 2.0 SOAP namespace
+     * @param element - The element that the header should be added to
+     */
+    public static void addSOAPModuleElements(OMFactory omFactory, ArrayList list, OMNamespace wsoap,
+                                             OMElement element) {
+        for (int i = 0; i < list.size(); i++) {
+            SOAPModuleMessage soapModuleMessage = (SOAPModuleMessage) list.get(i);
+            OMElement soapModuleElement =
+                    omFactory.createOMElement(WSDL2Constants.ATTRIBUTE_MODULE, wsoap);
+            soapModuleElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_REF, null, soapModuleMessage.getUri()));
+            element.addChild(soapModuleElement);
+        }
+    }
+
+    /**
+     * Adds a HTTP header element to a given OMElement
+     * @param omFactory - An OMFactory
+     * @param list - The arraylist of HTTPHeaderMessages
+     * @param whttp - The WSDL 2.0 HTTP namespace
+     * @param element - The element that the header should be added to
+     * @param nameSpaceMap - The namespaceMap
+     */
+    public static void addHTTPHeaderElements(OMFactory omFactory, ArrayList list, OMNamespace whttp,
+                                             OMElement element, Map nameSpaceMap) {
+        for (int i = 0; i < list.size(); i++) {
+            HTTPHeaderMessage httpHeaderMessage = (HTTPHeaderMessage) list.get(i);
+            OMElement httpHeaderElement =
+                    omFactory.createOMElement(WSDL2Constants.ATTRIBUTE_HEADER, whttp);
+            httpHeaderElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_NAME, null, httpHeaderMessage.getName()));
+            QName qName = httpHeaderMessage.getqName();
+            httpHeaderElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_TYPE, null,
+                    getPrefix(qName.getNamespaceURI(), nameSpaceMap) + ":" + qName.getLocalPart()));
+            httpHeaderElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_REQUIRED, null,
+                    new Boolean(httpHeaderMessage.isRequired()).toString()));
+            element.addChild(httpHeaderElement);
+        }
+    }
+
+    /**
+     * Generates a default SOAP 11 Binding for a given AxisService
+     * @param fac - The OMFactory
+     * @param axisService - The AxisService
+     * @param wsoap - The WSDL 2.0 SOAP namespace
+     * @param tns - The target namespace
+     * @return - The generated SOAP11Binding element
+     */
+    public static OMElement generateSOAP11Binding(OMFactory fac, AxisService axisService,
+                                                  OMNamespace wsoap, OMNamespace tns) {
+        OMElement binding = fac.createOMElement(WSDL2Constants.BINDING_LOCAL_NAME, null);
+        binding.addAttribute(
+                fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_NAME, null, axisService.getName() +
+                        Java2WSDLConstants.BINDING_NAME_SUFFIX));
+        binding.addAttribute(fac.createOMAttribute(WSDL2Constants.INTERFACE_LOCAL_NAME, null, tns
+                .getPrefix() + ":" + WSDL2Constants.DEFAULT_INTERFACE_NAME));
+
+        binding.addAttribute(fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_TYPE, null,
+                                                   WSDL2Constants.URI_WSDL2_SOAP));
+        binding.addAttribute(fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_VERSION, wsoap,
+                                                   WSDL2Constants.SOAP_VERSION_1_1));
+        return binding;
+    }
+
+    /**
+     * Generates a default SOAP 12 Binding for a given AxisService
+     * @param fac - The OMFactory
+     * @param axisService - The AxisService
+     * @param wsoap - The WSDL 2.0 SOAP namespace
+     * @param tns - The target namespace
+     * @return - The generated SOAP12Binding element
+     */
+    public static OMElement generateSOAP12Binding(OMFactory fac, AxisService axisService,
+                                                  OMNamespace wsoap, OMNamespace tns) {
+        OMElement binding = fac.createOMElement(WSDL2Constants.BINDING_LOCAL_NAME, null);
+        binding.addAttribute(
+                fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_NAME, null, axisService.getName() +
+                        Java2WSDLConstants.SOAP12BINDING_NAME_SUFFIX));
+        binding.addAttribute(fac.createOMAttribute(WSDL2Constants.INTERFACE_LOCAL_NAME, null, tns
+                .getPrefix() + ":" + WSDL2Constants.DEFAULT_INTERFACE_NAME));
+
+        binding.addAttribute(fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_TYPE, null,
+                                                   WSDL2Constants.URI_WSDL2_SOAP));
+        binding.addAttribute(fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_VERSION, wsoap,
+                                                   WSDL2Constants.SOAP_VERSION_1_2));
+        return binding;
+    }
+
+    /**
+     * Generates a default HTTP Binding for a given AxisService
+     * @param fac - The OMFactory
+     * @param axisService - The AxisService
+     * @param whttp - The WSDL 2.0 HTTP namespace
+     * @param tns - The target namespace
+     * @return - The generated HTTPBinding element
+     */
+    public static OMElement generateHTTPBinding(OMFactory fac, AxisService axisService,
+                                                OMNamespace whttp, OMNamespace tns) {
+        OMElement binding = fac.createOMElement(WSDL2Constants.BINDING_LOCAL_NAME, null);
+        binding.addAttribute(
+                fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_NAME, null, axisService.getName() +
+                        Java2WSDLConstants.HTTP_BINDING));
+        binding.addAttribute(fac.createOMAttribute(WSDL2Constants.INTERFACE_LOCAL_NAME, null, tns
+                .getPrefix() + ":" + WSDL2Constants.DEFAULT_INTERFACE_NAME));
+
+        binding.addAttribute(fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_TYPE, null,
+                                                   WSDL2Constants.URI_WSDL2_HTTP));
+        Iterator iterator = axisService.getChildren();
+        while (iterator.hasNext()) {
+            AxisOperation axisOperation = (AxisOperation) iterator.next();
+            OMElement opElement = fac.createOMElement(WSDL2Constants.OPERATION_LOCAL_NAME, null);
+            binding.addChild(opElement);
+            String name = axisOperation.getName().getLocalPart();
+            opElement.addAttribute(fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_REF, null,
+                                                         tns.getPrefix() + ":" + name));
+            opElement.addAttribute(fac.createOMAttribute(WSDL2Constants.ATTRIBUTE_LOCATION, whttp,
+                                                         name));
+        }
+        return binding;
+    }
+
+    /**
+     * Generates a default service element
+     * @param omFactory - The OMFactory
+     * @param tns - The targetnamespace
+     * @param axisService - The AxisService
+     * @return - The generated service element
+     * @throws AxisFault - Thrown in case an exception occurs
+     */
+    public static OMElement generateServiceElement(OMFactory omFactory, OMNamespace tns,
+                                                   AxisService axisService)
+            throws AxisFault {
+        String[] eprs = axisService.getEPRs();
+        if (eprs == null) {
+            eprs = new String[]{axisService.getName()};
+        }
+        OMElement serviceElement = null;
+        for (int i = 0; i < eprs.length; i++) {
+            serviceElement = omFactory.createOMElement(WSDL2Constants.ENDPOINT_LOCAL_NAME, null);
+            serviceElement.addAttribute(omFactory.createOMAttribute(WSDL2Constants.ATTRIBUTE_NAME,
+                                                                    null, axisService.getName()));
+            serviceElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.INTERFACE_LOCAL_NAME, null,
+                    tns.getPrefix() + ":" + WSDL2Constants.DEFAULT_INTERFACE_NAME));
+            OMElement soap11EndpointElement =
+                    omFactory.createOMElement(WSDL2Constants.ENDPOINT_LOCAL_NAME, null);
+            soap11EndpointElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_NAME, null,
+                    WSDL2Constants.DEFAULT_SOAP11_ENDPOINT_NAME));
+            soap11EndpointElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.BINDING_LOCAL_NAME, null,
+                    tns.getPrefix() + ":" + axisService.getName() +
+                            Java2WSDLConstants.BINDING_NAME_SUFFIX));
+            soap11EndpointElement.addAttribute(
+                    omFactory.createOMAttribute(WSDL2Constants.ATTRIBUTE_ADDRESS, null, eprs[i]));
+            serviceElement.addChild(soap11EndpointElement);
+            OMElement soap12EndpointElement =
+                    omFactory.createOMElement(WSDL2Constants.ENDPOINT_LOCAL_NAME, null);
+            soap12EndpointElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_NAME, null,
+                    WSDL2Constants.DEFAULT_SOAP12_ENDPOINT_NAME));
+            soap12EndpointElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.BINDING_LOCAL_NAME, null,
+                    tns.getPrefix() + ":" + axisService.getName() +
+                            Java2WSDLConstants.SOAP12BINDING_NAME_SUFFIX));
+            soap12EndpointElement.addAttribute(
+                    omFactory.createOMAttribute(WSDL2Constants.ATTRIBUTE_ADDRESS, null, eprs[i]));
+            serviceElement.addChild(soap12EndpointElement);
+            OMElement httpEndpointElement =
+                    omFactory.createOMElement(WSDL2Constants.ENDPOINT_LOCAL_NAME, null);
+            httpEndpointElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.ATTRIBUTE_NAME, null,
+                    WSDL2Constants.DEFAULT_HTTP_ENDPOINT_NAME));
+            httpEndpointElement.addAttribute(omFactory.createOMAttribute(
+                    WSDL2Constants.BINDING_LOCAL_NAME, null, axisService.getName() +
+                    Java2WSDLConstants.HTTP_BINDING));
+            httpEndpointElement.addAttribute(
+                    omFactory.createOMAttribute(WSDL2Constants.ATTRIBUTE_ADDRESS, null, eprs[i]));
+            serviceElement.addChild(httpEndpointElement);
+        }
+        return serviceElement;
+    }
+
+    /**
+     * Adds the namespaces to the given OMElement
+     * @param descriptionElement - The OMElement that the namespaces should be added to
+     * @param nameSpaceMap - The namespaceMap
+     */
+    public static void populateNamespaces(OMElement descriptionElement, Map nameSpaceMap) {
+        Iterator keys = nameSpaceMap.keySet().iterator();
+        while (keys.hasNext()) {
+            String key = (String) keys.next();
+            if ("".equals(key)) {
+                descriptionElement.declareDefaultNamespace((String) nameSpaceMap.get(key));
+            } else {
+                descriptionElement.declareNamespace((String) nameSpaceMap.get(key), key);
+            }
+        }
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org