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