com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter.java Source code

Java tutorial

Introduction

Here is the source code for com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter.java

Source

/*
 *  soapui, copyright (C) 2006 eviware.com 
 *
 *  SoapUI is free software; you can redistribute it and/or modify it under the 
 *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
 *  either version 2.1 of the License, or (at your option) any later version.
 *
 *  SoapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details at gnu.org.
 */

package com.eviware.soapui.impl.wsdl.support.wsdl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.wsdl.Binding;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.Service;
import javax.wsdl.extensions.soap.SOAPAddress;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.support.BindingImporter;
import com.eviware.soapui.impl.wsdl.support.soap.SoapHttpBindingImporter;
import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
import com.eviware.soapui.model.iface.Interface;
import com.eviware.soapui.support.UISupport;

/**
 * Importer for WsdlInterfaces from WSDL urls / files
 * 
 * @author Ole.Matzura
 */

public class WsdlImporter {
    private List<BindingImporter> bindingImporters = new ArrayList<BindingImporter>();
    private static WsdlImporter instance;

    private final static Log log = LogFactory.getLog(WsdlImporter.class);

    private WsdlImporter() {
        try {
            bindingImporters.add(new SoapHttpBindingImporter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static WsdlImporter getInstance() {
        if (instance == null)
            instance = new WsdlImporter();

        return instance;
    }

    public WsdlInterface[] importWsdl(WsdlProject project, String wsdlUrl) throws Exception {
        WsdlContext wsdlContext = new WsdlContext(wsdlUrl, SoapVersion.Soap11, null, null);

        wsdlContext.load();

        List<Interface> result = new ArrayList<Interface>();

        Definition definition = wsdlContext.getDefinition();
        Map bindingMap = definition.getBindings();
        if (bindingMap.isEmpty()) {
            Map serviceMap = definition.getServices();
            if (serviceMap.isEmpty())
                throw new RuntimeException("Missing bindings and services in [" + wsdlUrl + "]");

            Iterator i = serviceMap.values().iterator();
            while (i.hasNext()) {
                Service service = (Service) i.next();
                Map portMap = service.getPorts();
                Iterator i2 = portMap.values().iterator();
                while (i2.hasNext()) {
                    Port port = (Port) i2.next();

                    Binding binding = port.getBinding();

                    WsdlInterface ifc = (WsdlInterface) project
                            .getInterfaceByName(binding.getPortType().getQName().getLocalPart());
                    if (ifc != null) {
                        Boolean res = UISupport.confirmOrCancel(
                                "Interface [" + ifc.getName() + "] already exists in project, update instead?",
                                "Import WSDL");
                        if (res == null)
                            return new WsdlInterface[0];

                        if (res.booleanValue()) {
                            ifc.updateDefinition(wsdlUrl, false);
                        }

                        continue;
                    }

                    WsdlInterface iface = importBinding(project, wsdlContext, binding);
                    if (iface != null) {
                        SOAPAddress address = (SOAPAddress) WsdlUtils
                                .getExtensiblityElement(port.getExtensibilityElements(), SOAPAddress.class);
                        if (address != null)
                            iface.addEndpoint(address.getLocationURI());

                        result.add(iface);
                        iface.setWsdlContext(wsdlContext);
                    }
                }
            }
        } else {
            Iterator i = bindingMap.values().iterator();
            while (i.hasNext()) {
                Binding binding = (Binding) i.next();

                WsdlInterface ifc = (WsdlInterface) project
                        .getInterfaceByName(binding.getPortType().getQName().getLocalPart());
                if (ifc != null && result.indexOf(ifc) == -1) {
                    Boolean res = UISupport.confirmOrCancel(
                            "Interface [" + ifc.getName() + "] already exists in project, update instead?",
                            "Import WSDL");
                    if (res == null)
                        return new WsdlInterface[0];

                    if (res.booleanValue()) {
                        ifc.updateDefinition(wsdlUrl, false);
                    }

                    continue;
                }

                WsdlInterface iface = importBinding(project, wsdlContext, binding);
                if (iface != null) {
                    result.add(iface);
                    iface.setWsdlContext(wsdlContext);
                }
            }
        }

        return result.toArray(new WsdlInterface[result.size()]);
    }

    private WsdlInterface importBinding(WsdlProject project, WsdlContext wsdlContext, Binding binding)
            throws Exception {
        log.info("Finding importer for " + binding.getQName());
        for (int c = 0; c < bindingImporters.size(); c++) {
            BindingImporter importer = bindingImporters.get(c);
            if (importer.canImport(binding)) {
                log.info("Importing binding " + binding.getQName());
                WsdlInterface iface = importer.importBinding(project, wsdlContext, binding);
                iface.setDefinition(wsdlContext.getUrl(), false);
                return iface;
            }
        }
        log.info("Missing importer for " + binding.getQName());

        return null;
    }

}