01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * 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
36: CredentialsProvider {
37: private boolean checkedCredentials;
38: private final WsdlRequest wsdlRequest;
39: private final static Logger logger = Logger
40: .getLogger(WsdlRequestCredentialsProvider.class);
41:
42: public WsdlRequestCredentialsProvider(WsdlRequest wsdlRequest) {
43: this .wsdlRequest = wsdlRequest;
44: }
45:
46: public Credentials getCredentials(final AuthScheme authscheme,
47: final String host, int port, boolean proxy)
48: throws CredentialsNotAvailableException {
49: if (checkedCredentials)
50: throw new CredentialsNotAvailableException(
51: "Missing valid credentials");
52:
53: if (authscheme == null) {
54: return null;
55: }
56: try {
57: if (authscheme instanceof NTLMScheme) {
58: logger.info(host + ":" + port
59: + " requires Windows authentication");
60: return new NTCredentials(wsdlRequest.getUsername(),
61: wsdlRequest.getPassword(), host, wsdlRequest
62: .getDomain());
63: } else if (authscheme instanceof RFC2617Scheme) {
64: logger.info(host + ":" + port
65: + " requires authentication with the realm '"
66: + authscheme.getRealm() + "'");
67: return new UsernamePasswordCredentials(wsdlRequest
68: .getUsername(), wsdlRequest.getPassword());
69: } else {
70: throw new CredentialsNotAvailableException(
71: "Unsupported authentication scheme: "
72: + authscheme.getSchemeName());
73: }
74: } catch (IOException e) {
75: throw new CredentialsNotAvailableException(e.getMessage(),
76: e);
77: } finally {
78: checkedCredentials = true;
79: }
80: }
81: }
|