/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. */
  24. package name.aikesommer.authenticator;
  25. import java.lang.ref.WeakReference;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. /**
  29. *
  30. * @author Aike J Sommer
  31. */
  32. public class CrossContextPrincipalStore implements PrincipalStore {
  33. private static final String PRINCIPAL_NOTE = CrossContextPrincipalStore.class.getName()
  34. + ".PRINCIPAL";
  35. private final WeakReference<SuperSession> session;
  36. public CrossContextPrincipalStore(HttpServletRequest request, HttpServletResponse response) {
  37. session = new WeakReference<SuperSession>(SuperSession.self(request,
  38. response, true));
  39. }
  40. public void store(SimplePrincipal principal) {
  41. SuperSession s = session.get();
  42. if (s != null) {
  43. s.attributes().put(PRINCIPAL_NOTE, principal);
  44. }
  45. }
  46. public SimplePrincipal fetch() {
  47. SuperSession s = session.get();
  48. if (s != null) {
  49. return (SimplePrincipal) s.attributes().get(PRINCIPAL_NOTE);
  50. }
  51. return null;
  52. }
  53. public void invalidate() {
  54. SuperSession s = session.get();
  55. if (s != null) {
  56. s.attributes().remove(PRINCIPAL_NOTE);
  57. }
  58. }
  59. }