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.wsdl;
    14
    15  import java.io.InputStream;
    16  import java.net.URL;
    17  import java.util.Iterator;
    18  import java.util.Map;
    19
    20  import javax.wsdl.xml.WSDLLocator;
    21
    22  import org.apache.log4j.Logger;
    23  import org.apache.xmlbeans.XmlObject;
    24  import org.apache.xmlbeans.XmlOptions;
    25  import org.w3c.dom.Document;
    26  import org.xml.sax.InputSource;
    27
    28  import com.eviware.soapui.SoapUI;
    29  import com.eviware.soapui.config.DefinitionCacheConfig;
    30  import com.eviware.soapui.config.DefintionPartConfig;
    31  import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
    32  import com.eviware.soapui.support.StringUtils;
    33  import com.eviware.soapui.support.Tools;
    34
    35  /***
    36   * Abstract WSDLLocator for loading definitions from either URL or cache..
    37   *
    38   * @author ole.matzura
    39   */
    40
    41  public abstract class WsdlLoader implements WSDLLocator
    42  {
    43  	private final String url;
    44  	private String last;
    45  	private String username;
    46  	private String password;
    47  	protected static final Logger log = Logger.getLogger( WsdlLoader.class );
    48
    49  	public WsdlLoader( String url )
    50  	{
    51  		this.url = url;
    52
    53  		// check for username/password
    54  		try
    55  		{
    56  			URL u = new URL( url );
    57  			String authority = u.getAuthority();
    58  			if( authority != null )
    59  			{
    60  				int ix1 = authority.indexOf( '@' );
    61  				int ix2 = authority.indexOf( ':' );
    62
    63  				if( ix1 > ix2 && ix2 > 0 )
    64  				{
    65  					username = authority.substring( 0, ix2 );
    66  					password = authority.substring( ix2+1, ix1 );
    67  				}
    68  			}
    69  		}
    70  		catch( Exception e )
    71  		{
    72  			SoapUI.logError( e );
    73  		}
    74  	}
    75
    76  	public InputSource getBaseInputSource()
    77  	{
    78  		try
    79  		{
    80  			log.debug( "Returning baseInputSource [" + url + "]" );
    81  			return new InputSource( load( url ) );
    82  		}
    83  		catch (Exception e)
    84  		{
    85  			throw new RuntimeException( e.toString() );
    86  		}
    87  	}
    88
    89  	public abstract InputStream load( String url ) throws Exception;
    90
    91  	public XmlObject loadXmlObject( String url, XmlOptions options ) throws Exception
    92  	{
    93  		if( options == null )
    94  		{
    95  			return XmlObject.Factory.parse( load( url ), new XmlOptions().setLoadLineNumbers());
    96  		}
    97  		else
    98  		{
    99  			options.setLoadLineNumbers();
    100 			return XmlObject.Factory.parse( load( url ), options );
    101 		}
    102 	}
    103
    104 	public String getBaseURI()
    105 	{
    106 //		log.debug( "Returning baseURI [" + url + "]" );
    107 		return url;
    108 	}
    109
    110 	public InputSource getImportInputSource(String parent, String imp)
    111 	{
    112 		if( isAbsoluteUrl( imp ))
    113 			last = imp;
    114 		else
    115 			last = Tools.joinRelativeUrl( parent, imp);
    116
    117 		try
    118 		{
    119 			return new InputSource( load( last ) );
    120 		}
    121 		catch (Exception e)
    122 		{
    123 			throw new RuntimeException( e.toString() );
    124 		}
    125 	}
    126
    127 	protected boolean isAbsoluteUrl(String tempImp)
    128 	{
    129 		tempImp = tempImp.toUpperCase();
    130 		return tempImp.startsWith( "HTTP:" ) || tempImp.startsWith( "HTTPS:" ) || tempImp.startsWith( "FILE:" );
    131 	}
    132
    133 	public String getLatestImportURI()
    134 	{
    135 		String result = last == null ? url : last;
    136 		log.debug( "Returning latest import URI [" + result + "]" );
    137 		return result;
    138 	}
    139
    140 	public boolean hasCredentials()
    141 	{
    142 		return !StringUtils.isNullOrEmpty( username ) && !StringUtils.isNullOrEmpty( password );
    143 	}
    144
    145 	public String getPassword()
    146 	{
    147 		return password;
    148 	}
    149
    150 	public String getUsername()
    151 	{
    152 		return username;
    153 	}
    154
    155 	public abstract boolean abort();
    156
    157 	public abstract boolean isAborted();
    158
    159 	public static DefinitionCacheConfig cacheWsdl( WsdlLoader loader ) throws Exception
    160 	{
    161 		DefinitionCacheConfig definitionCache = DefinitionCacheConfig.Factory.newInstance();
    162 		definitionCache.setRootPart( loader.getBaseURI() );
    163
    164 		Map<String, XmlObject> urls = SchemaUtils.getDefinitionParts( loader );
    165
    166 		for( Iterator<String> i = urls.keySet().iterator(); i.hasNext(); )
    167 		{
    168 			DefintionPartConfig definitionPart = definitionCache.addNewPart();
    169 			String url = i.next();
    170 			definitionPart.setUrl( url );
    171 			XmlObject xmlObject = urls.get( url );
    172 			definitionPart.setContent( xmlObject);
    173
    174 			Document domNode = (Document) xmlObject.getDomNode();
    175 			definitionPart.setType( domNode.getDocumentElement().getNamespaceURI());
    176 		}
    177
    178 		return definitionCache;
    179 	}
    180 }