From 1490e8a707d44a28c95d312c732f8ca9ed6b3200 Mon Sep 17 00:00:00 2001 From: Francois Beausoleil Date: Tue, 23 Mar 2004 17:27:04 +0000 Subject: [PATCH] * samples/contacts/src/sample/contact/SecureIndexController.java: Prevent a NullPointerException when no SecureContext can be found. Instead, throw a real exception, explaining what's wrong. --- .../java/sample/contact/SecureIndexController.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/samples/contacts/src/main/java/sample/contact/SecureIndexController.java b/samples/contacts/src/main/java/sample/contact/SecureIndexController.java index 96275d09ad..7d2b72869a 100644 --- a/samples/contacts/src/main/java/sample/contact/SecureIndexController.java +++ b/samples/contacts/src/main/java/sample/contact/SecureIndexController.java @@ -17,6 +17,7 @@ package sample.contact; import net.sf.acegisecurity.Authentication; import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.AuthenticationCredentialsNotFoundException; import net.sf.acegisecurity.context.ContextHolder; import net.sf.acegisecurity.context.SecureContext; @@ -65,8 +66,14 @@ public class SecureIndexController implements Controller, InitializingBean { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - Authentication currentUser = ((SecureContext) ContextHolder.getContext()) - .getAuthentication(); + SecureContext secureContext = ((SecureContext) ContextHolder.getContext()); + if (null == secureContext) { + throw new AuthenticationCredentialsNotFoundException( + "Authentication credentials were not found in the " + + "SecureContext"); + } + + final Authentication currentUser = secureContext.getAuthentication(); boolean supervisor = false; GrantedAuthority[] granted = currentUser.getAuthorities();