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 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.w3c.dom.DocumentFragment;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29 import org.xml.sax.InputSource;
30
31 import com.eviware.soapui.SoapUI;
32 import com.eviware.soapui.config.DefinitionCacheConfig;
33 import com.eviware.soapui.config.DefinitionCacheTypeConfig;
34 import com.eviware.soapui.config.DefintionPartConfig;
35 import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
36 import com.eviware.soapui.support.StringUtils;
37 import com.eviware.soapui.support.Tools;
38 import com.eviware.soapui.support.xml.XmlUtils;
39
40 /***
41 * Abstract WSDLLocator for loading definitions from either URL or cache..
42 *
43 * @author ole.matzura
44 */
45
46 public abstract class WsdlLoader implements WSDLLocator
47 {
48 private final String url;
49 private String last;
50 private String username;
51 private String password;
52 protected static final Logger log = Logger.getLogger( WsdlLoader.class );
53
54 public WsdlLoader( String url )
55 {
56 this.url = url;
57
58
59 try
60 {
61 URL u = new URL( url );
62 String authority = u.getAuthority();
63 if( authority != null )
64 {
65 int ix1 = authority.indexOf( '@' );
66 int ix2 = authority.indexOf( ':' );
67
68 if( ix1 > ix2 && ix2 > 0 )
69 {
70 username = authority.substring( 0, ix2 );
71 password = authority.substring( ix2+1, ix1 );
72 }
73 }
74 }
75 catch( Exception e )
76 {
77 SoapUI.logError( e );
78 }
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 if( options == null )
99 {
100 try
101 {
102 return XmlObject.Factory.parse( load( url ), new XmlOptions().setLoadLineNumbers() );
103 }
104 catch( Exception e )
105 {
106 log.error( "Failed to load url [" + url + "]" );
107 throw e;
108 }
109 }
110 else
111 {
112 options.setLoadLineNumbers();
113 return XmlObject.Factory.parse( load( url ), options );
114 }
115 }
116
117 public String getBaseURI()
118 {
119
120 return url;
121 }
122
123 public InputSource getImportInputSource(String parent, String imp)
124 {
125 if( isAbsoluteUrl( imp ))
126 last = imp;
127 else
128 last = Tools.joinRelativeUrl( parent, imp);
129
130 try
131 {
132 return new InputSource( load( last ) );
133 }
134 catch (Exception e)
135 {
136 throw new RuntimeException( e.toString() );
137 }
138 }
139
140 protected boolean isAbsoluteUrl(String tempImp)
141 {
142 tempImp = tempImp.toUpperCase();
143 return tempImp.startsWith( "HTTP:" ) || tempImp.startsWith( "HTTPS:" ) || tempImp.startsWith( "FILE:" );
144 }
145
146 public String getLatestImportURI()
147 {
148 String result = last == null ? url : last;
149 log.debug( "Returning latest import URI [" + result + "]" );
150 return result;
151 }
152
153 public boolean hasCredentials()
154 {
155 return !StringUtils.isNullOrEmpty( username ) && !StringUtils.isNullOrEmpty( password );
156 }
157
158 public String getPassword()
159 {
160 return password;
161 }
162
163 public String getUsername()
164 {
165 return username;
166 }
167
168 public abstract boolean abort();
169
170 public abstract boolean isAborted();
171
172 public static DefinitionCacheConfig cacheWsdl( WsdlLoader loader ) throws Exception
173 {
174 DefinitionCacheConfig definitionCache = DefinitionCacheConfig.Factory.newInstance();
175 definitionCache.setRootPart( loader.getBaseURI() );
176 definitionCache.setType( DefinitionCacheTypeConfig.TEXT );
177
178 Map<String, XmlObject> urls = SchemaUtils.getDefinitionParts( loader );
179
180 for( Iterator<String> i = urls.keySet().iterator(); i.hasNext(); )
181 {
182 DefintionPartConfig definitionPart = definitionCache.addNewPart();
183 String url = i.next();
184 definitionPart.setUrl( url );
185 XmlObject xmlObject = urls.get( url );
186 Node domNode = xmlObject.getDomNode();
187
188 if( domNode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE )
189 {
190 Node node = ((DocumentFragment)domNode).getFirstChild();
191 if( node.getNodeType() == Node.TEXT_NODE )
192 {
193 domNode = XmlUtils.parseXml( node.getNodeValue() );
194 xmlObject = XmlObject.Factory.parse( domNode );
195 }
196 }
197
198 Element contentElement = (Element)((Document)domNode).getDocumentElement();
199
200 Node newDomNode = definitionPart.addNewContent().getDomNode();
201 newDomNode.appendChild( newDomNode.getOwnerDocument().createTextNode( xmlObject.toString() ) );
202 definitionPart.setType( contentElement.getNamespaceURI());
203 }
204
205 return definitionCache;
206 }
207 }