KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > auth > BasicScheme


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/BasicScheme.java,v 1.4.2.3 2004/02/22 18:21:14 olegk Exp $
3  * $Revision: 1.4.2.3 $
4  * $Date: 2004/02/22 18:21:14 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.auth;
33
34 import org.apache.commons.httpclient.HttpConstants;
35 import org.apache.commons.httpclient.Credentials;
36 import org.apache.commons.httpclient.UsernamePasswordCredentials;
37 import org.apache.commons.httpclient.util.Base64;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 /**
42  * <p>
43  * Basic authentication scheme as defined in RFC 2617.
44  * </p>
45  *
46  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
47  * @author Rodney Waldhoff
48  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
49  * @author Ortwin Glück
50  * @author Sean C. Sullivan
51  * @author <a HREF="mailto:adrian@ephox.com">Adrian Sutton</a>
52  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
54  */

55
56 public class BasicScheme extends RFC2617Scheme {
57     
58     /** Log object for this class. */
59     private static final Log LOG = LogFactory.getLog(BasicScheme.class);
60     
61     /**
62      * Constructor for the basic authetication scheme.
63      *
64      * @param challenge authentication challenge
65      *
66      * @throws MalformedChallengeException is thrown if the authentication challenge
67      * is malformed
68      */

69     public BasicScheme(final String JavaDoc challenge) throws MalformedChallengeException {
70         super(challenge);
71     }
72
73
74     /**
75      * Returns textual designation of the basic authentication scheme.
76      *
77      * @return <code>basic</code>
78      */

79     public String JavaDoc getSchemeName() {
80         return "basic";
81     }
82
83     /**
84      * Produces basic authorization string for the given set of
85      * {@link Credentials}.
86      *
87      * @param credentials The set of credentials to be used for athentication
88      * @param method Method name is ignored by the basic authentication scheme
89      * @param uri URI is ignored by the basic authentication scheme
90      * @throws AuthenticationException if authorization string cannot
91      * be generated due to an authentication failure
92      *
93      * @return a basic authorization string
94      */

95     public String JavaDoc authenticate(Credentials credentials, String JavaDoc method, String JavaDoc uri)
96       throws AuthenticationException {
97
98         LOG.trace("enter BasicScheme.authenticate(Credentials, String, String)");
99
100         UsernamePasswordCredentials usernamepassword = null;
101         try {
102             usernamepassword = (UsernamePasswordCredentials) credentials;
103         } catch (ClassCastException JavaDoc e) {
104             throw new AuthenticationException(
105              "Credentials cannot be used for basic authentication: "
106               + credentials.getClass().getName());
107         }
108         return BasicScheme.authenticate(usernamepassword);
109     }
110
111     /**
112      * Return a basic <tt>Authorization</tt> header value for the given
113      * {@link UsernamePasswordCredentials}.
114      *
115      * @param credentials The credentials to encode.
116      *
117      * @return a basic authorization string
118      */

119     public static String JavaDoc authenticate(UsernamePasswordCredentials credentials) {
120
121         LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials)");
122
123         if (credentials == null) {
124             throw new IllegalArgumentException JavaDoc("Credentials may not be null");
125         }
126         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
127         buffer.append(credentials.getUserName());
128         buffer.append(":");
129         buffer.append(credentials.getPassword());
130         
131         return "Basic " + HttpConstants.getAsciiString(
132             Base64.encode(HttpConstants.getContentBytes(buffer.toString())));
133     }
134 }
135
Popular Tags