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.submit.filters;
14  
15  import java.io.IOException;
16  
17  import org.apache.commons.httpclient.Credentials;
18  import org.apache.commons.httpclient.NTCredentials;
19  import org.apache.commons.httpclient.UsernamePasswordCredentials;
20  import org.apache.commons.httpclient.auth.AuthScheme;
21  import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
22  import org.apache.commons.httpclient.auth.CredentialsProvider;
23  import org.apache.commons.httpclient.auth.NTLMScheme;
24  import org.apache.commons.httpclient.auth.RFC2617Scheme;
25  import org.apache.log4j.Logger;
26  
27  import com.eviware.soapui.impl.wsdl.WsdlRequest;
28  
29  /***
30   * Provided credentials from equivilant WsdlRequest properties
31   * 
32   * @author Ole.Matzura
33   */
34  
35  public final class WsdlRequestCredentialsProvider implements CredentialsProvider
36  {	
37  	private boolean checkedCredentials;
38  	private final WsdlRequest wsdlRequest;
39  	private final static Logger logger = Logger.getLogger(WsdlRequestCredentialsProvider.class);
40  
41  	public WsdlRequestCredentialsProvider(WsdlRequest wsdlRequest)
42  	{
43  		this.wsdlRequest = wsdlRequest;
44  	}
45  
46  	public Credentials getCredentials(final AuthScheme authscheme, final String host, int port, boolean proxy)
47  	throws CredentialsNotAvailableException
48  	{
49  		if (checkedCredentials)
50  			throw new CredentialsNotAvailableException("Missing valid credentials");
51  
52  		if (authscheme == null)
53  		{
54  			return null;
55  		}
56  		try
57  		{
58  			String password = wsdlRequest.getPassword();
59  			if( password == null )
60  				password = "";
61  			
62  			if (authscheme instanceof NTLMScheme)
63  			{
64  				logger.info(host + ":" + port + " requires Windows authentication");
65  				return new NTCredentials(wsdlRequest.getUsername(), password, host, wsdlRequest
66  						.getDomain());
67  			}
68  			else if (authscheme instanceof RFC2617Scheme)
69  			{
70  				logger.info(host + ":" + port + " requires authentication with the realm '" + authscheme.getRealm() + "'");
71  				return new UsernamePasswordCredentials(wsdlRequest.getUsername(), password);
72  			}
73  			else
74  			{
75  				throw new CredentialsNotAvailableException("Unsupported authentication scheme: "
76  						+ authscheme.getSchemeName());
77  			}
78  		}
79  		catch (IOException e)
80  		{
81  			throw new CredentialsNotAvailableException(e.getMessage(), e);
82  		}
83  		finally
84  		{
85  			checkedCredentials = true;
86  		}
87  	}
88  }