All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fabric3.wsdl.processor.Wsdl11ContractProcessor Maven / Gradle / Ivy

There is a newer version: 2.5.3
Show newest version
/*
 * Fabric3
 * Copyright (c) 2009-2015 Metaform Systems
 *
 * 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.
 */
package org.fabric3.wsdl.processor;

import javax.wsdl.Definition;
import javax.wsdl.Fault;
import javax.wsdl.Input;
import javax.wsdl.Message;
import javax.wsdl.Output;
import javax.wsdl.Part;
import javax.wsdl.PortType;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaObject;
import org.apache.ws.commons.schema.XmlSchemaSequence;
import org.apache.ws.commons.schema.XmlSchemaSimpleType;
import org.apache.ws.commons.schema.XmlSchemaType;
import org.fabric3.api.model.type.contract.DataType;
import org.fabric3.api.model.type.contract.Operation;
import org.fabric3.spi.introspection.IntrospectionContext;
import org.fabric3.spi.model.type.xsd.XSDComplexType;
import org.fabric3.spi.model.type.xsd.XSDSimpleType;
import org.fabric3.spi.model.type.xsd.XSDType;
import org.fabric3.wsdl.model.WsdlServiceContract;

/**
 * WSDL 1.1 processor implementation.
 */
public class Wsdl11ContractProcessor implements WsdlContractProcessor {

    public WsdlServiceContract introspect(PortType portType, Definition definition, XmlSchemaCollection collection, IntrospectionContext context) {
        List operations = new LinkedList<>();
        for (Object object : portType.getOperations()) {
            javax.wsdl.Operation wsdlOperation = (javax.wsdl.Operation) object;
            Operation operation = createOperation(wsdlOperation, collection, portType, context);
            operations.add(operation);
        }
        WsdlServiceContract contract = new WsdlServiceContract(portType, definition);
        contract.setOperations(operations);
        return contract;
    }

    /**
     * Creates a operation model object from a WSDL operation.
     *
     * @param operation  the WSDL operation
     * @param collection parsed schema included in or referenced by the WSDL
     * @param portType   the port type being introspected
     * @param context    the introspection context
     * @return the operation model object
     */
    private Operation createOperation(javax.wsdl.Operation operation, XmlSchemaCollection collection, PortType portType, IntrospectionContext context) {
        Input input = operation.getInput();
        Message message = input.getMessage();
        List inputTypes = getInputTypes(message, collection, portType, context);

        Map faults = operation.getFaults();
        List faultTypes = getFaultTypes(faults, collection, portType, context);

        Output output = operation.getOutput();
        DataType outputType = getOutputType(output, collection, portType, context);

        String name = operation.getName();
        Operation op = new Operation(name, inputTypes, outputType, faultTypes);
        op.setRemotable(true);
        return op;
    }

    @SuppressWarnings({"unchecked"})
    private List getInputTypes(Message message, XmlSchemaCollection collection, PortType portType, IntrospectionContext context) {
        List types = new ArrayList<>();
        // Note Message.getParts() may not return the parts in proper order; Message.getOrderedParts(null) does
        List parts = message.getOrderedParts(null);
        if (parts.isEmpty()) {
            DataType type = getElementDataType(message.getQName(), collection, portType, context);
            types.add(type);
        } else {
            for (Part part : (Collection) parts) {
                XSDType dataType = getDataType(part, collection, portType, context);
                if (dataType != null) {
                    types.add(dataType);
                }
            }
        }
        return types;
    }

    @SuppressWarnings("unchecked")
    private List getFaultTypes(Map faults, XmlSchemaCollection collection, PortType portType, IntrospectionContext context) {
        List types = new LinkedList<>();
        for (Fault fault : (Collection) faults.values()) {
            Part part = (Part) fault.getMessage().getOrderedParts(null).get(0);
            XSDType dataType = getDataType(part, collection, portType, context);
            if (dataType != null) {
                types.add(dataType);
            }
        }
        return types;

    }

    private DataType getOutputType(Output output, XmlSchemaCollection collection, PortType portType, IntrospectionContext context) {
        if (output == null) {
            // no output type specified (e.g. one-way operation), use void
            return new XSDSimpleType(Void.TYPE, new QName("void"));
        }
        Message message = output.getMessage();
        List parts = message.getOrderedParts(null);
        if (parts.isEmpty()) {
            return getElementDataType(message.getQName(), collection, portType, context);
        } else {
            Part part = (Part) parts.get(0);
            return getDataType(part, collection, portType, context);
        }
    }

    private XSDType getDataType(Part part, XmlSchemaCollection collection, PortType portType, IntrospectionContext context) {
        QName elementName = part.getElementName();
        XSDType dataType = null;
        QName typeName = part.getTypeName();
        if (elementName != null) {
            dataType = getElementDataType(elementName, collection, portType, context);
        } else if (typeName != null) {
            dataType = getSchemaDataType(typeName, collection, portType, context);
        }
        return dataType;
    }

    private XSDType getElementDataType(QName elementName, XmlSchemaCollection collection, PortType portType, IntrospectionContext context) {
        XmlSchemaElement element = collection.getElementByQName(elementName);
        if (element == null) {
            SchemaTypeNotFound error = new SchemaTypeNotFound("Schema type " + elementName + " not found referenced in: " + portType.getQName());
            context.addError(error);
            return null;
        }
        XmlSchemaType type = element.getSchemaType();
        if (type == null) {
            SchemaTypeNotFound error = new SchemaTypeNotFound("Invalid schema type " + elementName + " referenced in: " + portType.getQName());
            context.addError(error);
            return null;
        }
        return createDataType(type, elementName);
    }

    private XSDType getSchemaDataType(QName typeName, XmlSchemaCollection collection, PortType portType, IntrospectionContext context) {
        XmlSchemaType type = collection.getTypeByQName(typeName);
        if (type == null) {
            SchemaTypeNotFound error = new SchemaTypeNotFound("Schema type " + typeName + " not found referenced in: " + portType.getQName());
            context.addError(error);
            return null;
        }
        return createDataType(type, null);
    }

    private XSDType createDataType(XmlSchemaType type, QName elementName) {
        QName name = type.getQName();
        if (name == null) {
            name = elementName;
        }
        if (type instanceof XmlSchemaComplexType) {
            return introspectComplexType((XmlSchemaComplexType) type, name);
        } else if (type instanceof XmlSchemaSimpleType) {
            return new XSDSimpleType(Object.class, name);
        } else {
            // should not happen
            throw new AssertionError("Unknown Schema type" + type);
        }
    }

    @SuppressWarnings({"unchecked"})
    private XSDType introspectComplexType(XmlSchemaComplexType type, QName name) {
        if (type.getParticle() instanceof XmlSchemaSequence) {
            XmlSchemaSequence sequence = (XmlSchemaSequence) type.getParticle();
            List sequenceTypes = new ArrayList<>();
            Iterator iter = sequence.getItems().getIterator();
            while (iter.hasNext()) {
                XmlSchemaObject o = iter.next();
                if (o instanceof XmlSchemaElement) {
                    XmlSchemaElement schemaElement = (XmlSchemaElement) o;
                    QName schemaTypeName = schemaElement.getSchemaTypeName();
                    XmlSchemaType schemaType = schemaElement.getSchemaType();
                    if (schemaType instanceof XmlSchemaComplexType) {
                        sequenceTypes.add(new XSDComplexType(Object.class, schemaTypeName));
                    } else if (schemaType instanceof XmlSchemaSimpleType) {
                        sequenceTypes.add(new XSDSimpleType(Object.class, schemaTypeName));
                    }
                } else if (o instanceof XmlSchemaType) {
                    XmlSchemaType schemaType = (XmlSchemaType) o;
                    QName schemaTypeName = schemaType.getQName();
                    if (schemaType instanceof XmlSchemaComplexType) {
                        sequenceTypes.add(new XSDComplexType(Object.class, schemaTypeName));
                    } else if (schemaType instanceof XmlSchemaSimpleType) {
                        sequenceTypes.add(new XSDSimpleType(Object.class, schemaTypeName));
                    }
                }
            }
            return new XSDComplexType(Object.class, name, sequenceTypes);
        }
        return new XSDComplexType(Object.class, name);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy