KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > DefaultCredentialsProvider


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit;
39
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import org.org.apache.commons.httpclient.Credentials;
45 import org.org.apache.commons.httpclient.UsernamePasswordCredentials;
46 import org.org.apache.commons.httpclient.auth.AuthScheme;
47 import org.org.apache.commons.httpclient.auth.AuthScope;
48 import org.org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
49 import org.org.apache.commons.httpclient.auth.CredentialsProvider;
50
51 /**
52  * Default HtmlUnit implementation of the <tt>CredentialsProvider</tt> interface. Provides
53  * credentials for both web servers and proxies. Supports NTLM authentication, Digest
54  * authentication, and Basic HTTP authentication.
55  *
56  * @author Daniel Gredler
57  * @version $Revision: 100 $
58  */

59 public class DefaultCredentialsProvider implements CredentialsProvider {
60
61     private final Map JavaDoc credentials_;
62     private final Map JavaDoc proxyCredentials_;
63
64     /**
65      * Creates a new <tt>DefaultCredentialsProvider</tt> instance.
66      */

67     public DefaultCredentialsProvider() {
68         credentials_ = new HashMap JavaDoc();
69         proxyCredentials_ = new HashMap JavaDoc();
70     }
71
72     /**
73      * Adds credentials for the specified username/password for any host/port/realm combination.
74      * The credentials may be for any authentication scheme, including NTLM, digest and basic
75      * HTTP authentication. If you are using sensitive username/password information, please do
76      * NOT use this method. If you add credentials using this method, any server that requires
77      * authentication will receive the specified username and password.
78      * @param username The username for the new credentials.
79      * @param password The password for the new credentials.
80      */

81     public void addCredentials( final String JavaDoc username, final String JavaDoc password ) {
82         addCredentials( username, password, AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM );
83     }
84
85     /**
86      * Adds credentials for the specified username/password on the specified host/port for the
87      * specified realm. The credentials may be for any authentication scheme, including NTLM,
88      * digest and basic HTTP authentication.
89      * @param username The username for the new credentials.
90      * @param password The password for the new credentials.
91      * @param host The host to which to the new credentials apply (<tt>null</tt> if applicable to any host).
92      * @param port The port to which to the new credentials apply (negative if applicable to any port).
93      * @param realm The realm to which to the new credentials apply (<tt>null</tt> if applicable to any realm).
94      */

95     public void addCredentials( final String JavaDoc username, final String JavaDoc password, final String JavaDoc host,
96             final int port, final String JavaDoc realm ) {
97         final AuthScope scope = new AuthScope( host, port, realm, AuthScope.ANY_SCHEME );
98         final Credentials c = new UsernamePasswordCredentials( username, password );
99         credentials_.put( scope, c );
100     }
101
102     /**
103      * Adds proxy credentials for the specified username/password for any host/port/realm combination.
104      * @param username The username for the new credentials.
105      * @param password The password for the new credentials.
106      */

107     public void addProxyCredentials( final String JavaDoc username, final String JavaDoc password ) {
108         addProxyCredentials( username, password, AuthScope.ANY_HOST, AuthScope.ANY_PORT );
109     }
110
111     /**
112      * Adds proxy credentials for the specified username/password on the specified host/port.
113      * @param username The username for the new credentials.
114      * @param password The password for the new credentials.
115      * @param host The host to which to the new credentials apply (<tt>null</tt> if applicable to any host).
116      * @param port The port to which to the new credentials apply (negative if applicable to any port).
117      */

118     public void addProxyCredentials( final String JavaDoc username, final String JavaDoc password, final String JavaDoc host, final int port ) {
119         final AuthScope scope = new AuthScope( host, port, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME );
120         final Credentials c = new UsernamePasswordCredentials( username, password );
121         proxyCredentials_.put( scope, c );
122     }
123
124     /**
125      * Returns the credentials associated with the specified scheme, host and port.
126      * @param scheme The authentication scheme being used (basic, digest, NTLM, etc).
127      * @param host The host we are authenticating for.
128      * @param port The port we are authenticating for.
129      * @param proxy Whether or not we are authenticating using a proxy.
130      * @return The credentials correponding to the specified schem, host and port.
131      * @throws CredentialsNotAvailableException If the specified credentials cannot be provided due to an error.
132      * @see CredentialsProvider#getCredentials(AuthScheme, String, int, boolean)
133      */

134     public Credentials getCredentials( final AuthScheme scheme, final String JavaDoc host, final int port, final boolean proxy )
135         throws CredentialsNotAvailableException {
136         final Map JavaDoc credentials;
137         if( proxy ) {
138             credentials = proxyCredentials_;
139         }
140         else {
141             credentials = credentials_;
142         }
143         for( final Iterator JavaDoc i = credentials.entrySet().iterator(); i.hasNext(); ) {
144             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
145             final AuthScope scope = (AuthScope) entry.getKey();
146             final Credentials c = (Credentials) entry.getValue();
147             if( scope.getScheme() == AuthScope.ANY_SCHEME || scope.getScheme().equals(scheme.getSchemeName()) ) {
148                 if( scope.getHost() == AuthScope.ANY_HOST || scope.getHost().equals(host) ) {
149                     if( scope.getPort() == AuthScope.ANY_PORT || scope.getPort() == port ) {
150                         if( scope.getRealm() == AuthScope.ANY_REALM || scope.getRealm().equals(scheme.getRealm()) ) {
151                             return c;
152                         }
153                     }
154                 }
155             }
156         }
157         return null;
158     }
159
160 }
161
Popular Tags