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 org.apache.commons.httpclient.Credentials;
    16  import org.apache.commons.httpclient.HttpClient;
    17  import org.apache.commons.httpclient.HttpState;
    18  import org.apache.commons.httpclient.UsernamePasswordCredentials;
    19  import org.apache.commons.httpclient.auth.AuthScope;
    20  import org.apache.commons.httpclient.auth.CredentialsProvider;
    21
    22  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    23  import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
    24  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
    25  import com.eviware.soapui.impl.wsdl.submit.transports.http.TimeablePostMethod;
    26  import com.eviware.soapui.model.iface.SubmitContext;
    27  import com.eviware.soapui.model.settings.Settings;
    28  import com.eviware.soapui.settings.HttpSettings;
    29
    30  /***
    31   * RequestFilter for setting preemptive authentication and related credentials
    32   */
    33
    34  public class HttpAuthenticationRequestFilter implements RequestFilter
    35  {
    36  	public void filterRequest(SubmitContext context, WsdlRequest wsdlRequest)
    37  	{
    38  		String username = wsdlRequest.getUsername();
    39
    40  		// check for authorization prerequisites
    41  		if( username == null || username.length() == 0 )
    42  			return;
    43
    44  		HttpClient httpClient = (HttpClient) context.getProperty( BaseHttpRequestTransport.HTTP_CLIENT );
    45  		Settings settings = wsdlRequest.getSettings();
    46  		String password = wsdlRequest.getPassword();
    47
    48  		//	 set preemptive authentication
    49  		if (settings.getBoolean(HttpSettings.AUTHENTICATE_PREEMPTIVELY))
    50  		{
    51  			httpClient.getParams().setAuthenticationPreemptive(true);
    52  			HttpState state = (HttpState) context.getProperty( SubmitContext.HTTP_STATE_PROPERTY );
    53
    54  			if( state != null )
    55  			{
    56  				Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
    57  				state.setCredentials(AuthScope.ANY, defaultcreds);
    58  			}
    59  		}
    60  		else
    61  		{
    62  			httpClient.getParams().setAuthenticationPreemptive(false);
    63  		}
    64
    65  		TimeablePostMethod postMethod = (TimeablePostMethod) context.getProperty( BaseHttpRequestTransport.POST_METHOD );
    66
    67  		postMethod.getParams().setParameter(CredentialsProvider.PROVIDER, new WsdlRequestCredentialsProvider( wsdlRequest ));
    68  		postMethod.setDoAuthentication(true);
    69  	}
    70  }