1
2
3
4
5
6
7
8
9
10
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 }