1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.objectledge.authentication;
30
31 import java.security.Principal;
32
33 import org.objectledge.context.Context;
34 import org.objectledge.pipeline.ProcessingException;
35 import org.objectledge.pipeline.Valve;
36 import org.objectledge.web.HttpContext;
37 import org.objectledge.web.WebConstants;
38
39 /***
40 * Pipeline processing valve that sets the context variable describing currently authenticated user.
41 *
42 * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
43 * @version $Id: AuthenticationValve.java,v 1.13 2005/07/22 17:25:47 pablo Exp $
44 */
45 public class AuthenticationValve
46 implements Valve
47 {
48 /*** the authentication component */
49 private UserManager userManager;
50
51 /***
52 * Constructor.
53 *
54 * @param userManager the user manager component.
55 */
56 public AuthenticationValve(UserManager userManager)
57 {
58 this.userManager = userManager;
59 }
60
61 /***
62 * Run the pipeline valve - authenticate user.
63 *
64 * @param context the thread's processing context.
65 * @throws ProcessingException if authentication failed.
66 */
67 public void process(Context context)
68 throws ProcessingException
69 {
70 HttpContext httpContext = HttpContext.getHttpContext(context);
71 Principal principal = (Principal)httpContext.getRequest().
72 getSession().getAttribute(WebConstants.PRINCIPAL_SESSION_KEY);
73 Principal anonymous = null;
74 try
75 {
76 anonymous = userManager.getAnonymousAccount();
77 }
78 catch(AuthenticationException e)
79 {
80 throw new ProcessingException("Failed to retrieve anonymous account");
81 }
82 boolean authenticated = false;
83 if(principal == null)
84 {
85 principal = anonymous;
86 }
87 else
88 {
89 authenticated = !principal.equals(anonymous);
90 }
91 AuthenticationContext authenticationContext = new AuthenticationContext();
92 authenticationContext.setUserPrincipal(principal, authenticated);
93 context.setAttribute(AuthenticationContext.class, authenticationContext);
94
95 httpContext.getRequest().getSession().setAttribute(WebConstants.PRINCIPAL_SESSION_KEY,
96 principal);
97 }
98 }