PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/AuthenticRoast/AuthenticRoast-Impl/src/main/java/name/aikesommer/authenticator/CrossContextPrincipalStore.java

http://authenticroast.googlecode.com/
Java | 69 lines | 32 code | 10 blank | 27 comment | 6 complexity | a8bd16f95fef0c6944bb9a6c909479b9 MD5 | raw file
 1/**
 2 *    Copyright (C) 2007-2010 Aike J Sommer (http://aikesommer.name/)
 3 *
 4 *    This file is part of AuthenticRoast.
 5 *
 6 *    This library is free software; you can redistribute it and/or
 7 *    modify it under the terms of the GNU Lesser General Public
 8 *    License as published by the Free Software Foundation; either
 9 *    version 3 of the License, or (at your option) 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
17 *    Public License along with this library; if not, write to the
18 *    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 *    Boston, MA 02110-1301 USA
20 *
21 *    You can reach the author and get more information about this
22 *    project at: http://aikesommer.name/
23 */
24package name.aikesommer.authenticator;
25
26import java.lang.ref.WeakReference;
27import javax.servlet.http.HttpServletRequest;
28import javax.servlet.http.HttpServletResponse;
29
30
31/**
32 *
33 * @author Aike J Sommer
34 */
35public class CrossContextPrincipalStore implements PrincipalStore {
36
37    private static final String PRINCIPAL_NOTE = CrossContextPrincipalStore.class.getName()
38            + ".PRINCIPAL";
39
40    private final WeakReference<SuperSession> session;
41
42    public CrossContextPrincipalStore(HttpServletRequest request, HttpServletResponse response) {
43        session = new WeakReference<SuperSession>(SuperSession.self(request,
44                response, true));
45    }
46
47    public void store(SimplePrincipal principal) {
48        SuperSession s = session.get();
49        if (s != null) {
50            s.attributes().put(PRINCIPAL_NOTE, principal);
51        }
52    }
53
54    public SimplePrincipal fetch() {
55        SuperSession s = session.get();
56        if (s != null) {
57            return (SimplePrincipal) s.attributes().get(PRINCIPAL_NOTE);
58        }
59        return null;
60    }
61
62    public void invalidate() {
63        SuperSession s = session.get();
64        if (s != null) {
65            s.attributes().remove(PRINCIPAL_NOTE);
66        }
67    }
68
69}