View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.impl.wsdl.support.soap;
    14
    15  import java.text.Collator;
    16  import java.util.Collections;
    17  import java.util.Comparator;
    18  import java.util.Iterator;
    19  import java.util.List;
    20
    21  import javax.wsdl.Binding;
    22  import javax.wsdl.BindingOperation;
    23  import javax.wsdl.extensions.soap12.SOAP12Binding;
    24
    25  import org.apache.log4j.Logger;
    26
    27  import com.eviware.soapui.impl.wsdl.WsdlInterface;
    28  import com.eviware.soapui.impl.wsdl.WsdlOperation;
    29  import com.eviware.soapui.impl.wsdl.WsdlProject;
    30  import com.eviware.soapui.impl.wsdl.support.BindingImporter;
    31  import com.eviware.soapui.impl.wsdl.support.Constants;
    32  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
    33  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlUtils;
    34  import com.eviware.soapui.settings.WsdlSettings;
    35
    36  /***
    37   * BindingImporter that can import a WsdlInterface from an SOAP 1.2/HTTP binding
    38   *
    39   * @author Ole.Matzura
    40   */
    41
    42  public class Soap12HttpBindingImporter implements BindingImporter
    43  {
    44     private final static Logger log = Logger.getLogger( Soap12HttpBindingImporter.class );
    45
    46     public boolean canImport(Binding binding)
    47     {
    48        List list = binding.getExtensibilityElements();
    49        SOAP12Binding soapBinding = WsdlUtils.getExtensiblityElement( list, SOAP12Binding.class );
    50        return soapBinding == null ? false :
    51           soapBinding.getTransportURI().startsWith( Constants.SOAP_HTTP_BINDING_NS );
    52     }
    53
    54     public WsdlInterface importBinding( WsdlProject project, WsdlContext wsdlContext, Binding binding ) throws Exception
    55     {
    56     	String name = project.getSettings().getBoolean( WsdlSettings.NAME_WITH_BINDING ) ?
    57     				binding.getQName().getLocalPart() : binding.getPortType().getQName().getLocalPart();
    58
    59        WsdlInterface iface = project.addNewInterface( name );
    60        iface.setBindingName( binding.getQName() );
    61        iface.setSoapVersion( SoapVersion.Soap12 );
    62
    63        String [] endpoints = WsdlUtils.getEndpointsForBinding( wsdlContext.getDefinition(), binding );
    64        for (int i = 0; i < endpoints.length; i++)
    65        {
    66           log.info("importing endpoint " + endpoints[i]);
    67           iface.addEndpoint( endpoints[i] );
    68        }
    69
    70        List<BindingOperation> list = binding.getBindingOperations();
    71        Collections.sort( list, new BindingOperationComparator());
    72
    73        for (Iterator iter = list.iterator(); iter.hasNext();)
    74        {
    75           BindingOperation operation = (BindingOperation) iter.next();
    76
    77           // sanity check
    78           if( operation.getOperation() == null || operation.getOperation().isUndefined() )
    79           {
    80           	log.error( "BindingOperation [" + operation.getName() + "] is missing or referring to an invalid operation");
    81           }
    82           else
    83           {
    84           	log.info("importing operation " + operation.getName() );
    85           	WsdlOperation op = iface.addNewOperation( operation );
    86           }
    87        }
    88
    89        return iface;
    90     }
    91
    92     private static final class BindingOperationComparator implements Comparator<BindingOperation>
    93  	{
    94  		public int compare(BindingOperation o1, BindingOperation o2)
    95  		{
    96  		   return Collator.getInstance().compare( o1.getOperation().getName(), o2.getOperation().getName() );
    97  		}
    98  	}
    99  }