View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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  			if (authscheme instanceof NTLMScheme)
    59  			{
    60  				logger.info(host + ":" + port + " requires Windows authentication");
    61  				return new NTCredentials(wsdlRequest.getUsername(), wsdlRequest.getPassword(), host, wsdlRequest
    62  						.getDomain());
    63  			}
    64  			else if (authscheme instanceof RFC2617Scheme)
    65  			{
    66  				logger.info(host + ":" + port + " requires authentication with the realm '" + authscheme.getRealm() + "'");
    67  				return new UsernamePasswordCredentials(wsdlRequest.getUsername(), wsdlRequest.getPassword());
    68  			}
    69  			else
    70  			{
    71  				throw new CredentialsNotAvailableException("Unsupported authentication scheme: "
    72  						+ authscheme.getSchemeName());
    73  			}
    74  		}
    75  		catch (IOException e)
    76  		{
    77  			throw new CredentialsNotAvailableException(e.getMessage(), e);
    78  		}
    79  		finally
    80  		{
    81  			checkedCredentials = true;
    82  		}
    83  	}
    84  }