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.HttpClient;
    19  import org.apache.commons.httpclient.HttpState;
    20  import org.apache.commons.httpclient.NTCredentials;
    21  import org.apache.commons.httpclient.UsernamePasswordCredentials;
    22  import org.apache.commons.httpclient.auth.AuthScheme;
    23  import org.apache.commons.httpclient.auth.AuthScope;
    24  import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
    25  import org.apache.commons.httpclient.auth.CredentialsProvider;
    26  import org.apache.commons.httpclient.auth.NTLMScheme;
    27  import org.apache.commons.httpclient.auth.RFC2617Scheme;
    28  import org.apache.log4j.Logger;
    29
    30  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    31  import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
    32  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
    33  import com.eviware.soapui.impl.wsdl.submit.transports.http.TimeablePostMethod;
    34  import com.eviware.soapui.model.iface.SubmitContext;
    35  import com.eviware.soapui.model.settings.Settings;
    36  import com.eviware.soapui.settings.HttpSettings;
    37
    38  /***
    39   * RequestFilter for setting preemptive authentication and related credentials
    40   */
    41
    42  public class HttpAuthenticationRequestFilter implements RequestFilter
    43  {
    44  	public void filterRequest(SubmitContext context, WsdlRequest wsdlRequest)
    45  	{
    46  		String username = PropertyExpansionRequestFilter.expandProperties( context, wsdlRequest.getUsername());
    47
    48  		// check for authorization prerequisites
    49  		if( username == null || username.length() == 0 )
    50  			return;
    51
    52  		Settings settings = wsdlRequest.getSettings();
    53  		String password =PropertyExpansionRequestFilter.expandProperties( context,  wsdlRequest.getPassword() );
    54  		String domain = PropertyExpansionRequestFilter.expandProperties( context, wsdlRequest.getDomain() );
    55
    56  		initRequestCredentials( context, username, settings, password, domain );
    57  	}
    58
    59  	public static void initRequestCredentials( SubmitContext context, String username, Settings settings, String password, String domain )
    60  	{
    61  		HttpClient httpClient = (HttpClient) context.getProperty( BaseHttpRequestTransport.HTTP_CLIENT );
    62
    63  		//	 set preemptive authentication
    64  		if (settings.getBoolean(HttpSettings.AUTHENTICATE_PREEMPTIVELY))
    65  		{
    66  			httpClient.getParams().setAuthenticationPreemptive(true);
    67  			HttpState state = (HttpState) context.getProperty( SubmitContext.HTTP_STATE_PROPERTY );
    68
    69  			if( state != null )
    70  			{
    71  				Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
    72  				state.setCredentials(AuthScope.ANY, defaultcreds);
    73  			}
    74  		}
    75  		else
    76  		{
    77  			httpClient.getParams().setAuthenticationPreemptive(false);
    78  		}
    79
    80  		TimeablePostMethod postMethod = (TimeablePostMethod) context.getProperty( BaseHttpRequestTransport.POST_METHOD );
    81
    82  		postMethod.getParams().setParameter(CredentialsProvider.PROVIDER,
    83  					new UPDCredentialsProvider( username, password, domain ));
    84
    85  		postMethod.setDoAuthentication(true);
    86  	}
    87
    88  	public static class UPDCredentialsProvider implements CredentialsProvider
    89  	{
    90  		private boolean checkedCredentials;
    91  		private final static Logger logger = Logger.getLogger(WsdlRequestCredentialsProvider.class);
    92  		private final String username;
    93  		private final String password;
    94  		private final String domain;
    95
    96  		public UPDCredentialsProvider(String username, String password, String domain)
    97  		{
    98  			this.username = username;
    99  			this.password = password;
    100 			this.domain = domain;
    101 		}
    102
    103 		public Credentials getCredentials(final AuthScheme authscheme, final String host, int port, boolean proxy)
    104 		throws CredentialsNotAvailableException
    105 		{
    106 			if (checkedCredentials)
    107 				throw new CredentialsNotAvailableException("Missing valid credentials");
    108
    109 			if (authscheme == null)
    110 			{
    111 				return null;
    112 			}
    113 			try
    114 			{
    115 				if (authscheme instanceof NTLMScheme)
    116 				{
    117 					logger.info(host + ":" + port + " requires Windows authentication");
    118 					return new NTCredentials(username, password, host, domain );
    119 				}
    120 				else if (authscheme instanceof RFC2617Scheme)
    121 				{
    122 					logger.info(host + ":" + port + " requires authentication with the realm '" + authscheme.getRealm() + "'");
    123 					return new UsernamePasswordCredentials(username, password);
    124 				}
    125 				else
    126 				{
    127 					throw new CredentialsNotAvailableException("Unsupported authentication scheme: "
    128 							+ authscheme.getSchemeName());
    129 				}
    130 			}
    131 			catch (IOException e)
    132 			{
    133 				throw new CredentialsNotAvailableException(e.getMessage(), e);
    134 			}
    135 			finally
    136 			{
    137 				checkedCredentials = true;
    138 			}
    139 		}
    140 	}
    141 }