View Javadoc

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