01: /**********************************************************************************
02: *
03: * $Id: AuthnFilter.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.ui.standalone;
22:
23: import java.io.IOException;
24:
25: import javax.servlet.Filter;
26: import javax.servlet.FilterChain;
27: import javax.servlet.FilterConfig;
28: import javax.servlet.ServletException;
29: import javax.servlet.ServletRequest;
30: import javax.servlet.ServletResponse;
31: import javax.servlet.http.HttpServletRequest;
32: import javax.servlet.http.HttpServletResponse;
33: import javax.servlet.http.HttpSession;
34:
35: import org.apache.commons.logging.Log;
36: import org.apache.commons.logging.LogFactory;
37: import org.sakaiproject.tool.gradebook.facades.Authn;
38: import org.springframework.web.context.support.WebApplicationContextUtils;
39:
40: /**
41: * An authentication filter for standalone use in demos and UI tests.
42: */
43: public class AuthnFilter implements Filter {
44: private static Log logger = LogFactory.getLog(AuthnFilter.class);
45:
46: private String authnRedirect;
47: private String authnServiceBean;
48:
49: public void init(FilterConfig filterConfig) throws ServletException {
50: authnRedirect = filterConfig.getInitParameter("authnRedirect");
51: authnServiceBean = filterConfig
52: .getInitParameter("authnServiceBean");
53: }
54:
55: public void doFilter(ServletRequest request,
56: ServletResponse response, FilterChain chain)
57: throws IOException, ServletException {
58: HttpSession session = ((HttpServletRequest) request)
59: .getSession();
60: Authn authnService = (Authn) WebApplicationContextUtils
61: .getWebApplicationContext(session.getServletContext())
62: .getBean(authnServiceBean);
63: authnService.setAuthnContext(request);
64: String userUid = authnService.getUserUid();
65: if (logger.isInfoEnabled())
66: logger.info("userUid=" + userUid);
67: if (userUid == null) {
68: if (authnRedirect != null) {
69: if (authnRedirect.equals(((HttpServletRequest) request)
70: .getRequestURI())) {
71: // Don't redirect to the same spot.
72: chain.doFilter(request, response);
73: } else {
74: // ((HttpServletRequest)request).getRequestDispatcher(authnRedirect).forward(request, response);
75: ((HttpServletResponse) response)
76: .sendRedirect(authnRedirect);
77: }
78: } else {
79: ((HttpServletResponse) response)
80: .sendError(HttpServletResponse.SC_UNAUTHORIZED);
81: }
82: } else {
83: chain.doFilter(request, response);
84: }
85: }
86:
87: public void destroy() {
88: }
89: }
|