KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > HttpAuthenticatorFactory


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.text.MessageFormat JavaDoc;
23
24 /**
25  * Implementation of an {@link HttpMethod} that is specified to <i>SSL-Explorer</i>
26  * and used for SSL-Tunnnels.
27  *
28  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
29  */

30 public class HttpAuthenticatorFactory {
31
32     public static final String JavaDoc NTLM = "NTLM"; //$NON-NLS-1$
33
public static final String JavaDoc BASIC = "Basic"; //$NON-NLS-1$
34
public static final String JavaDoc DIGEST = "Digest"; //$NON-NLS-1$
35
public static final String JavaDoc NONE = "None"; //$NON-NLS-1$
36

37     // #ifdef DEBUG
38
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(HttpAuthenticatorFactory.class);
39
40     // #endif
41

42     public static HttpAuthenticator createAuthenticator(HttpConnection con, String JavaDoc[] challenges, String JavaDoc authenticationHeader,
43                                                         String JavaDoc authorizationHeader, String JavaDoc pref, String JavaDoc uri)
44                     throws UnsupportedAuthenticationException {
45
46         HttpAuthenticator authenticator = null;
47
48         String JavaDoc actualChallenge = ""; //$NON-NLS-1$
49

50         if (pref != null) {
51
52             boolean prefAvailable = false;
53             for (int x = 0; pref != null && x < challenges.length; x++) {
54                 if (challenges[x].toLowerCase().startsWith(pref.toLowerCase())) {
55                     prefAvailable = true;
56                     actualChallenge = challenges[x];
57                     break;
58                 }
59             }
60
61             if (prefAvailable) {
62                 if (pref.equalsIgnoreCase(BASIC)) {
63                     authenticator = new BasicAuthentication(uri, con.getHost(), con.getPort(), con.isSecure());
64                 } else if (pref.equalsIgnoreCase(NTLM)) {
65                     authenticator = new NTLMAuthentication(uri, con.getHost(), con.getPort(), con.isSecure());
66                 } else if (pref.equalsIgnoreCase(DIGEST)) {
67                     authenticator = new DigestAuthentication(uri, con.getHost(), con.getPort(), con.isSecure());
68                 }
69             }
70         }
71
72         if(authenticator==null) {
73             
74             // No prefered method available so look in challenges and pick one
75
for (int i = 0; i < challenges.length; i++) {
76                 String JavaDoc method = getAuthenticationMethod(challenges[i]);
77
78                 if (method.equalsIgnoreCase(BASIC)) {
79                     authenticator = new BasicAuthentication(uri, con.getHost(), con.getPort(), con.isSecure());
80                 } else if (method.equalsIgnoreCase(NTLM)) {
81                     authenticator = new NTLMAuthentication(uri, con.getHost(), con.getPort(), con.isSecure());
82                 } else if (method.equalsIgnoreCase(DIGEST)) {
83                     authenticator = new DigestAuthentication(uri, con.getHost(), con.getPort(), con.isSecure());
84                 }
85
86                 if (authenticator != null) {
87                     actualChallenge = challenges[i];
88                     break;
89                 }
90             }
91         }
92         if (authenticator != null) {
93             // #ifdef DEBUG
94
log.info(MessageFormat.format(Messages.getString("HttpAuthenticatorFactory.created"), new Object JavaDoc[] { authenticator.getScheme() })); //$NON-NLS-1$
95
// #endif
96

97             authenticator.setConnection(con);
98             authenticator.setChallenge(actualChallenge);
99             authenticator.setAuthenicationHeader(authenticationHeader);
100             authenticator.setAuthorizationHeader(authorizationHeader);
101
102             return authenticator;
103         }
104
105         if (pref == null)
106             throw new UnsupportedAuthenticationException(challenges);
107         else
108             throw new UnsupportedAuthenticationException(challenges,
109                 MessageFormat.format(Messages.getString("HttpAuthenticatorFactory.notSupported"), new Object JavaDoc[] { pref })); //$NON-NLS-1$
110
}
111
112     public static String JavaDoc getAuthenticationMethod(String JavaDoc challenge) {
113         String JavaDoc method = challenge;
114
115         if (method != null) {
116             int n = method.indexOf(' ');
117             if (n > -1)
118                 method = method.substring(0, n);
119         }
120
121         return method;
122     }
123
124     public String JavaDoc getAuthenticationRealm(String JavaDoc challenge) {
125         String JavaDoc auth = challenge;
126         String JavaDoc realm = ""; //$NON-NLS-1$
127

128         if (auth != null) {
129             int l;
130             int r = auth.indexOf('=');
131
132             while (r >= 0) {
133                 l = auth.lastIndexOf(' ', r);
134                 realm = auth.substring(l + 1, r);
135
136                 if (realm.equalsIgnoreCase("realm")) { //$NON-NLS-1$
137
l = r + 2;
138                     r = auth.indexOf('"', l);
139                     realm = auth.substring(l, r);
140
141                     break;
142                 }
143
144                 r = auth.indexOf('=', r + 1);
145             }
146         }
147
148         return realm;
149     }
150 }
151
Popular Tags