axisservice2wsdl11.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,070 行 · 第 1/4 页

JAVA
1,070
字号
package org.apache.axis2.description;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axis2.addressing.AddressingConstants;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.namespace.Constants;
import org.apache.axis2.util.ExternalPolicySerializer;
import org.apache.axis2.util.PolicyUtil;
import org.apache.axis2.util.XMLUtils;
import org.apache.axis2.util.WSDLSerializationUtil;
import org.apache.axis2.util.JavaUtils;
import org.apache.axis2.wsdl.SOAPHeaderMessage;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
import org.apache.neethi.Policy;
import org.apache.neethi.PolicyComponent;
import org.apache.neethi.PolicyReference;
import org.apache.neethi.PolicyRegistry;
import org.apache.ws.commons.schema.XmlSchema;

import javax.xml.namespace.QName;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

/*
 * Copyright 2004,2005 The Apache Software Foundation.
 *
 * Licensed 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.
 *
 *
 */

public class AxisService2WSDL11 implements Java2WSDLConstants {

    private AxisService axisService;

    private String[] serviceEndpointURLs;

    private String targetNamespace;

    private OMElement definition;

    private OMNamespace soap;

    private OMNamespace soap12;

    private OMNamespace http;

    private OMNamespace mime;

    private OMNamespace tns;

    private OMNamespace wsdl;

    private OMNamespace wsaw;

    private String style = DOCUMENT;

    private String use = LITERAL;

    private HashMap policiesInDefinitions;

    private ExternalPolicySerializer serializer;

    private HashMap messagesMap;

    public AxisService2WSDL11(AxisService service) throws Exception {
        this.axisService = service;

        // the EPR list of AxisService contains REST EPRs as well. Those REST EPRs will be used to generated HTTPBinding
        // and rest of the EPRs will be used to generate SOAP 1.1 and 1.2 bindings. Let's first initialize those set of
        // EPRs now to be used later, especially when we generate the WSDL.
        serviceEndpointURLs = service.getEPRs();
        if (serviceEndpointURLs == null) {
            Map endpointMap = service.getEndpoints();
             if (endpointMap.size() > 0) {
                Iterator endpointItr = endpointMap.values().iterator();
                if (endpointItr.hasNext()) {
                    AxisEndpoint endpoint = (AxisEndpoint) endpointItr.next();
                    serviceEndpointURLs =  new String[]{endpoint.getEndpointURL()};
                }

            } else {
                 serviceEndpointURLs = new String[]{service.getEndpointName()};
             }
        }

        this.targetNamespace = service.getTargetNamespace();

        serializer = new ExternalPolicySerializer();
        // CHECKME check whether service.getAxisConfiguration() return null ???

        AxisConfiguration configuration = service.getAxisConfiguration();
        if (configuration != null) {
            serializer.setAssertionsToFilter(configuration
                    .getLocalPolicyAssertions());
        }

    }

    public OMElement generateOM() throws Exception {

        OMFactory fac = OMAbstractFactory.getOMFactory();
        wsdl = fac.createOMNamespace(WSDL_NAMESPACE,
                                     DEFAULT_WSDL_NAMESPACE_PREFIX);
        OMElement ele = fac.createOMElement("definitions", wsdl);
        setDefinitionElement(ele);

        policiesInDefinitions = new HashMap();

        Map namespaceMap = axisService.getNamespaceMap();
        if (namespaceMap == null) namespaceMap = new HashMap();
        
        WSDLSerializationUtil.populateNamespaces(ele, namespaceMap);
        soap = ele.declareNamespace(URI_WSDL11_SOAP, SOAP11_PREFIX);
        soap12 = ele.declareNamespace(URI_WSDL12_SOAP, SOAP12_PREFIX);
        http = ele.declareNamespace(HTTP_NAMESPACE, HTTP_PREFIX);
        mime = ele.declareNamespace(MIME_NAMESPACE, MIME_PREFIX);
        wsaw =ele.declareNamespace(AddressingConstants.Final.WSAW_NAMESPACE, "wsaw");
        String prefix = WSDLSerializationUtil.getPrefix(axisService.getTargetNamespace(),
                                                        namespaceMap);
        if (prefix == null || "".equals(prefix)) {
            prefix = DEFAULT_TARGET_NAMESPACE_PREFIX;
        }

        namespaceMap.put(prefix, axisService.getTargetNamespace());
        tns = ele.declareNamespace(axisService.getTargetNamespace(), prefix);

        boolean disableREST = false;
        Parameter disableRESTParameter =
                axisService.getParameter(org.apache.axis2.Constants.Configuration.DISABLE_REST);
        if (disableRESTParameter != null &&
                JavaUtils.isTrueExplicitly(disableRESTParameter.getValue())) {
            disableREST = true;
        }

        // adding documentation element
        // <documentation>&lt;b&gt;NEW!&lt;/b&gt; This method accepts an ISBN
        // string and returns &lt;b&gt;Amazon.co.uk&lt;/b&gt; Sales Rank for
        // that book.</documentation>
        WSDLSerializationUtil.addWSDLDocumentationElement(axisService, ele, fac, wsdl);

        ele.addAttribute("targetNamespace", axisService.getTargetNamespace(),
                         null);
        OMElement wsdlTypes = fac.createOMElement("types", wsdl);
        ele.addChild(wsdlTypes);

        // populate the schema mappings
        axisService.populateSchemaMappings();

        ArrayList schemas = axisService.getSchema();
        for (int i = 0; i < schemas.size(); i++) {
            StringWriter writer = new StringWriter();

            // XmlSchema schema = (XmlSchema) schemas.get(i);
            XmlSchema schema = axisService.getSchema(i);

            String targetNamespace = schema.getTargetNamespace();
            if (!Constants.NS_URI_XML.equals(targetNamespace)) {
                schema.write(writer);
                String schemaString = writer.toString();
                if (!"".equals(schemaString)) {
                    wsdlTypes.addChild(XMLUtils.toOM(new StringReader(
                            schemaString)));
                }
            }
        }
        generateMessages(fac, ele);
        generatePortType(fac, ele);
        generateSOAP11Binding(fac, ele);
        generateSOAP12Binding(fac, ele);
        if (!disableREST) {
            generateHTTPBinding(fac, ele);
        }

        generateService(fac, ele, disableREST);
        addPoliciesToDefinitionElement(policiesInDefinitions.values()
                .iterator(), definition);

        return ele;
    }

    private void generateMessages(OMFactory fac, OMElement defintions) {
        HashSet faultMessageNames = new HashSet();
        messagesMap = new HashMap();

        Iterator operations = axisService.getOperations();
        while (operations.hasNext()) {
            AxisOperation axisOperation = (AxisOperation) operations.next();
            if (axisOperation.isControlOperation()) {
                continue;
            }
            String MEP = axisOperation.getMessageExchangePattern();
            if (WSDL2Constants.MEP_URI_IN_ONLY.equals(MEP)
                    || WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_ROBUST_IN_ONLY
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_IN_OUT
                    .equals(MEP)) {
                AxisMessage inaxisMessage = axisOperation
                        .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
                if (inaxisMessage != null) {
                    writeMessage(inaxisMessage, fac, defintions);
                    generateHeaderMessages(inaxisMessage, fac, defintions);
                }
            }

            if (WSDL2Constants.MEP_URI_OUT_ONLY.equals(MEP)
                    || WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_ROBUST_IN_ONLY
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_IN_OUT
                    .equals(MEP)) {
                AxisMessage outAxisMessage = axisOperation
                        .getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
                if (outAxisMessage != null) {
                    writeMessage(outAxisMessage, fac, defintions);
                    generateHeaderMessages(outAxisMessage, fac, defintions);
                }
            }

            // generate fault Messages
            ArrayList faultyMessages = axisOperation.getFaultMessages();
            if (faultyMessages != null) {
                for (int i = 0; i < faultyMessages.size(); i++) {
                    AxisMessage axisMessage = (AxisMessage) faultyMessages
                            .get(i);
                    String name = axisMessage.getName();
                    if (faultMessageNames.add(name)) {
                        writeMessage(axisMessage, fac, defintions);
                        generateHeaderMessages(axisMessage, fac, defintions);
                    }
                }
            }
        }
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?