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 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: public void filterRequest(SubmitContext context,
36: WsdlRequest wsdlRequest) {
37: String username = wsdlRequest.getUsername();
38:
39: // check for authorization prerequisites
40: if (username == null || username.length() == 0)
41: return;
42:
43: HttpClient httpClient = (HttpClient) context
44: .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: httpClient.getParams().setAuthenticationPreemptive(true);
51: HttpState state = (HttpState) context
52: .getProperty(SubmitContext.HTTP_STATE_PROPERTY);
53:
54: if (state != null) {
55: Credentials defaultcreds = new UsernamePasswordCredentials(
56: username, password);
57: state.setCredentials(AuthScope.ANY, defaultcreds);
58: }
59: } else {
60: httpClient.getParams().setAuthenticationPreemptive(false);
61: }
62:
63: TimeablePostMethod postMethod = (TimeablePostMethod) context
64: .getProperty(BaseHttpRequestTransport.POST_METHOD);
65:
66: postMethod.getParams().setParameter(
67: CredentialsProvider.PROVIDER,
68: new WsdlRequestCredentialsProvider(wsdlRequest));
69: postMethod.setDoAuthentication(true);
70: }
71: }
|