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 com.eviware.x.dialogs.XProgressMonitor;
21  import org.apache.log4j.Logger;
22  import org.apache.xmlbeans.XmlObject;
23  import org.apache.xmlbeans.XmlOptions;
24  import org.xml.sax.InputSource;
25  
26  import java.io.InputStream;
27  import java.net.URL;
28  
29  /***
30   * Abstract WSDLLocator for loading definitions from either URL or cache..
31   *
32   * @author ole.matzura
33   */
34  
35  public abstract class AbstractWsdlDefinitionLoader extends AbstractDefinitionLoader implements WsdlDefinitionLoader
36  {
37     private final String url;
38     private String last;
39     private String username;
40     private String password;
41     protected static final Logger log = Logger.getLogger(AbstractWsdlDefinitionLoader.class);
42     private XProgressMonitor monitor;
43     private int progressIndex;
44  
45     public AbstractWsdlDefinitionLoader(String url)
46     {
47        this.url = url;
48  
49        if (!PathUtils.isFilePath(url) && !PathUtils.isRelativePath(url))
50        {
51           // check for username/password
52           try
53           {
54              URL u = new URL(url);
55              String authority = u.getAuthority();
56              if (authority != null)
57              {
58                 int ix1 = authority.indexOf('@');
59                 int ix2 = authority.indexOf(':');
60  
61                 if (ix1 > ix2 && ix2 > 0)
62                 {
63                    username = authority.substring(0, ix2);
64                    password = authority.substring(ix2 + 1, ix1);
65                 }
66              }
67           }
68           catch (Exception e)
69           {
70              SoapUI.logError(e);
71           }
72        }
73     }
74  
75     public String getUrl()
76     {
77        return url;
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 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          InputStream input = load(last);
133          return input == null ? null : new InputSource(input);
134       }
135       catch (Exception e)
136       {
137          throw new RuntimeException(e.toString());
138       }
139    }
140 
141    protected boolean isAbsoluteUrl(String tempImp)
142    {
143       tempImp = tempImp.toUpperCase();
144       return tempImp.startsWith("HTTP:") || tempImp.startsWith("HTTPS:") || tempImp.startsWith("FILE:");
145    }
146 
147    public String getLatestImportURI()
148    {
149       String result = last == null ? url : last;
150       log.debug("Returning latest import URI [" + result + "]");
151       return result;
152    }
153 
154    public boolean hasCredentials()
155    {
156       return !StringUtils.isNullOrEmpty(username) && !StringUtils.isNullOrEmpty(password);
157    }
158 
159    public String getPassword()
160    {
161       return password;
162    }
163 
164    public String getUsername()
165    {
166       return username;
167    }
168 }