View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  
18  import org.apache.log4j.Logger;
19  import org.apache.xmlbeans.XmlObject;
20  import org.apache.xmlbeans.XmlOptions;
21  import org.xml.sax.InputSource;
22  
23  import com.eviware.soapui.SoapUI;
24  import com.eviware.soapui.impl.support.definition.support.AbstractDefinitionLoader;
25  import com.eviware.soapui.impl.wsdl.support.PathUtils;
26  import com.eviware.soapui.support.StringUtils;
27  import com.eviware.soapui.support.Tools;
28  import com.eviware.x.dialogs.XProgressMonitor;
29  
30  /***
31   * Abstract WSDLLocator for loading definitions from either URL or cache..
32   * 
33   * @author ole.matzura
34   */
35  
36  public abstract class AbstractWsdlDefinitionLoader extends AbstractDefinitionLoader implements WsdlDefinitionLoader
37  {
38  	private final String url;
39  	private String last;
40  	private String username;
41  	private String password;
42  	protected static final Logger log = Logger.getLogger( AbstractWsdlDefinitionLoader.class );
43  	private XProgressMonitor monitor;
44  	private int progressIndex;
45  
46  	public AbstractWsdlDefinitionLoader( String url )
47  	{
48  		this.url = url;
49  
50  		if( !PathUtils.isFilePath( url ) && !PathUtils.isRelativePath( url ) )
51  		{
52  			// check for username/password
53  			try
54  			{
55  				URL u = new URL( url );
56  				String authority = u.getAuthority();
57  				if( authority != null )
58  				{
59  					int ix1 = authority.indexOf( '@' );
60  					int ix2 = authority.indexOf( ':' );
61  
62  					if( ix1 > ix2 && ix2 > 0 )
63  					{
64  						username = authority.substring( 0, ix2 );
65  						password = authority.substring( ix2 + 1, ix1 );
66  					}
67  				}
68  			}
69  			catch( Exception e )
70  			{
71  				SoapUI.logError( e );
72  			}
73  		}
74  	}
75  
76  	public String getUrl()
77  	{
78  		return url;
79  	}
80  
81  	public InputSource getBaseInputSource()
82  	{
83  		try
84  		{
85  			log.debug( "Returning baseInputSource [" + url + "]" );
86  			return new InputSource( load( url ) );
87  		}
88  		catch( Exception e )
89  		{
90  			throw new RuntimeException( e.toString() );
91  		}
92  	}
93  
94  	public abstract InputStream load( String url ) throws Exception;
95  
96  	public XmlObject loadXmlObject( String url, XmlOptions options ) throws Exception
97  	{
98  		try
99  		{
100 			if( options == null )
101 			{
102 				options = new XmlOptions();
103 			}
104 
105 			if( monitor != null )
106 				monitor.setProgress( progressIndex, "Loading [" + url + "]" );
107 
108 			options.setLoadLineNumbers();
109 			return XmlObject.Factory.parse( load( url ), options );
110 		}
111 		catch( Exception e )
112 		{
113 			log.error( "Failed to load url [" + url + "]" );
114 			throw e;
115 		}
116 	}
117 
118 	public String getBaseURI()
119 	{
120 		// log.debug( "Returning baseURI [" + url + "]" );
121 		return url;
122 	}
123 
124 	public InputSource getImportInputSource( String parent, String imp )
125 	{
126 		if( isAbsoluteUrl( imp ) )
127 			last = imp;
128 		else
129 			last = Tools.joinRelativeUrl( parent, imp );
130 
131 		try
132 		{
133 			InputStream input = load( last );
134 			return input == null ? null : new InputSource( input );
135 		}
136 		catch( Exception e )
137 		{
138 			throw new RuntimeException( e.toString() );
139 		}
140 	}
141 
142 	protected boolean isAbsoluteUrl( String tempImp )
143 	{
144 		tempImp = tempImp.toUpperCase();
145 		return tempImp.startsWith( "HTTP:" ) || tempImp.startsWith( "HTTPS:" ) || tempImp.startsWith( "FILE:" );
146 	}
147 
148 	public String getLatestImportURI()
149 	{
150 		String result = last == null ? url : last;
151 		log.debug( "Returning latest import URI [" + result + "]" );
152 		return result;
153 	}
154 
155 	public boolean hasCredentials()
156 	{
157 		return !StringUtils.isNullOrEmpty( username ) && !StringUtils.isNullOrEmpty( password );
158 	}
159 
160 	public String getPassword()
161 	{
162 		return password;
163 	}
164 
165 	public String getUsername()
166 	{
167 		return username;
168 	}
169 }