01: /**
02: * JOnAS: Java(TM) Open Application Server
03: * Copyright (C) 2005 Bull S.A.
04: * Contact: jonas-team@objectweb.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19: * USA
20: *
21: * --------------------------------------------------------------------------
22: * $Id: ResetAuthenticationValve.java 9906 2007-01-09 10:25:37Z benoitf $
23: * --------------------------------------------------------------------------
24: */package org.objectweb.jonas.web.catalina55;
25:
26: import java.io.IOException;
27: import java.security.Principal;
28:
29: import javax.servlet.ServletException;
30: import javax.servlet.http.HttpSession;
31:
32: import org.apache.catalina.Session;
33: import org.apache.catalina.connector.Request;
34: import org.apache.catalina.connector.Response;
35: import org.apache.catalina.realm.GenericPrincipal;
36: import org.apache.catalina.valves.ValveBase;
37: import org.objectweb.security.context.SecurityContext;
38: import org.objectweb.security.context.SecurityCurrent;
39:
40: /**
41: * This valve reset the authentication and propagate the cached principal
42: * Authenticator valve will be called after (if an authentication is needed further).
43: * @author Florent Benoit
44: */
45: public class ResetAuthenticationValve extends ValveBase {
46:
47: /**
48: * Unauthenticated security context
49: */
50: private static final SecurityContext UNAUTHENTICATED = new SecurityContext();
51:
52: /**<o
53: * Remove the current authenticated user by setting the anonymous user.
54: * @param request The servlet request to be processed
55: * @param response The servlet response to be created
56: * @exception IOException if an input/output error occurs
57: * @exception ServletException if a servlet error occurs
58: */
59: public void invoke(Request request, Response response)
60: throws IOException, ServletException {
61:
62: // First, check if there is a principal in the current request
63: Principal principal = request.getPrincipal();
64: if (principal == null) {
65: // No principal in the request, check if there is a session
66: HttpSession httpSession = request.getSession(false);
67: if (httpSession != null) {
68: // There is a session, check if there is a principal associated to this session.
69: Session session = getContainer().getManager()
70: .findSession(httpSession.getId());
71: principal = session.getPrincipal();
72: }
73: }
74:
75: // We've found a principal (either in the request or in the session)
76: if (principal != null) {
77: // Cast if it is a generic principal
78: if (principal instanceof GenericPrincipal) {
79: GenericPrincipal genericPrincipal = (GenericPrincipal) principal;
80: SecurityContext ctx = new SecurityContext(principal
81: .getName(), genericPrincipal.getRoles());
82: SecurityCurrent current = SecurityCurrent.getCurrent();
83: current.setSecurityContext(ctx);
84: } else {
85: //
86: }
87: }
88:
89: // Call next valve
90: try {
91: getNext().invoke(request, response);
92: } finally {
93: // Reset authentication
94: SecurityCurrent.getCurrent().setSecurityContext(
95: UNAUTHENTICATED);
96: }
97: }
98:
99: }
|