01: /**********************************************************************************
02: *
03: * $Id: AuthnStandaloneImpl.java 9271 2006-05-10 21:52:49Z ray@media.berkeley.edu $
04: *
05: ***********************************************************************************
06: *
07: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
08: *
09: * Licensed under the Educational Community License, Version 1.0 (the "License");
10: * you may not use this file except in compliance with the License.
11: * You may obtain a copy of the License at
12: *
13: * http://www.opensource.org/licenses/ecl1.php
14: *
15: * Unless required by applicable law or agreed to in writing, software
16: * distributed under the License is distributed on an "AS IS" BASIS,
17: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18: * See the License for the specific language governing permissions and
19: * limitations under the License.
20: *
21: **********************************************************************************/package org.sakaiproject.tool.gradebook.facades.standalone;
22:
23: import javax.faces.context.FacesContext;
24: import javax.servlet.http.HttpServletRequest;
25: import javax.servlet.http.HttpSession;
26:
27: import org.apache.commons.logging.Log;
28: import org.apache.commons.logging.LogFactory;
29: import org.sakaiproject.tool.gradebook.facades.Authn;
30:
31: /**
32: * An implementation of the Authn facade to support demos and UI tests.
33: */
34: public class AuthnStandaloneImpl implements Authn {
35: private static Log log = LogFactory
36: .getLog(AuthnStandaloneImpl.class);
37:
38: private static String USER_ID_PARAMETER = "userUid";
39:
40: private ThreadLocal authnContext = new ThreadLocal();
41:
42: public String getUserUid() {
43: Object whatToAuthn = authnContext.get();
44: if (log.isDebugEnabled())
45: log.debug("whatToAuthn=" + whatToAuthn);
46:
47: // If we got a null, get the request from the faces context
48: if (whatToAuthn == null) {
49: whatToAuthn = FacesContext.getCurrentInstance()
50: .getExternalContext().getRequest();
51: }
52:
53: HttpServletRequest request = (HttpServletRequest) whatToAuthn;
54: HttpSession session = request.getSession();
55:
56: // Try to get the user ID from a session variable.
57: String userUid = (String) session
58: .getAttribute(USER_ID_PARAMETER);
59:
60: if (userUid == null) {
61: // Try to get the user ID from a request parameter.
62: userUid = request.getParameter(USER_ID_PARAMETER);
63: if (userUid != null) {
64: // Copy the request parameter into the session.
65: session.setAttribute(USER_ID_PARAMETER, userUid);
66: }
67: }
68:
69: return userUid;
70: }
71:
72: /**
73: * This is usually redundant, since all the necessary information
74: * is available through the FacesContext object.
75: * Unfortunately, servlets and filters might call this service before
76: * the Faces context is fully initialized.
77: */
78: public void setAuthnContext(Object whatToAuthn) {
79: authnContext.set(whatToAuthn);
80: }
81:
82: }
|