01: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.security.provider;
07:
08: import org.jasig.portal.ChannelRuntimeData;
09: import org.jasig.portal.ChannelStaticData;
10: import org.jasig.portal.security.LocalConnectionContext;
11:
12: import sun.misc.BASE64Encoder;
13: import java.text.MessageFormat;
14: import java.net.HttpURLConnection;
15:
16: /**
17: * <p>
18: * BasicLocalConnectionContext implements HTTP Basic Authentication as a
19: * LocalConnectionContext. Connections are provided as Objects:
20: * they may be URL, LDAP, database connections, etc.
21: * </p>
22: *
23: * <p>
24: * In order to use this class you should define two publish time parameters for
25: * your channel: <code>remote.username</code> and <code>remote.password</code>.
26: * The username and password default to "tomcat" if you don't set them.
27: * </p>
28: *
29: * @author Stephen Barrett, smb1@cornell.edu
30: * @version $Revision: 35700 $ $Date: 2005-05-01 15:07:29 -0700 (Sun, 01 May 2005) $
31: */
32:
33: public class BasicLocalConnectionContext extends LocalConnectionContext {
34: protected static final String CHANPARAMUSERNAME = "remote.username";
35:
36: protected static final String CHANPARAMPASSWORD = "remote.password";
37:
38: protected static final String AUTHORIZATIONHDR = "Authorization";
39:
40: protected static final String AUTHORIZATIONTYPE = "Basic ";
41:
42: protected static final String USERNAMEANDPWDMASK = "{0}:{1}";
43:
44: // default to tomcat examples default
45: private String usernameandpassword = "tomcat:tomcat";
46:
47: /**
48: * Constructs the username/password combination from the parameters
49: * set at publish time.
50: *
51: * @param sd
52: * The calling channel's ChannelStaticData.
53: */
54:
55: public void init(ChannelStaticData sd) {
56: staticData = sd;
57:
58: /*
59: * Construct the username/password combination.
60: */
61: if (sd.getParameter(CHANPARAMUSERNAME) != null)
62: usernameandpassword = MessageFormat.format(
63: USERNAMEANDPWDMASK, (Object[]) (new String[] {
64: sd.getParameter(CHANPARAMUSERNAME),
65: sd.getParameter(CHANPARAMPASSWORD) }));
66:
67: }
68:
69: /**
70: * Sets the headers so that the connection will authenticate using
71: * HTTP Basic Authentication using the username and password passed
72: * set at publish time.
73: *
74: * @param connection
75: * Must be an instance of HttpURLConnection
76: * @param rd
77: * The calling channel's ChannelRuntimeData.
78: */
79: public void sendLocalData(Object connection, ChannelRuntimeData rd) {
80: HttpURLConnection modified_connection = (HttpURLConnection) connection;
81:
82: // encode and set the authentication credentials
83: modified_connection
84: .setRequestProperty(AUTHORIZATIONHDR, AUTHORIZATIONTYPE
85: + (new BASE64Encoder())
86: .encode(usernameandpassword.getBytes()));
87:
88: // all done. This will be sent with the request now.
89: }
90: }
|