Throw exception when request/response isn't set on WebUtils so that you get a nice error instead of a NPE.
git-svn-id: https://svn.apache.org/repos/asf/incubator/jsecurity/trunk@711068 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/org/jsecurity/web/WebRememberMeManager.java b/src/org/jsecurity/web/WebRememberMeManager.java
index 708d08c..b6af3dd 100644
--- a/src/org/jsecurity/web/WebRememberMeManager.java
+++ b/src/org/jsecurity/web/WebRememberMeManager.java
@@ -18,6 +18,8 @@
*/
package org.jsecurity.web;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.jsecurity.codec.Base64;
import org.jsecurity.subject.AbstractRememberMeManager;
import org.jsecurity.web.attr.CookieAttribute;
@@ -58,6 +60,8 @@
*/
public static final String DEFAULT_REMEMBER_ME_COOKIE_NAME = "rememberMe";
+ private static final Log log = LogFactory.getLog(WebRememberMeManager.class);
+
protected WebAttribute<String> identityAttribute = null;
public WebRememberMeManager() {
diff --git a/src/org/jsecurity/web/WebUtils.java b/src/org/jsecurity/web/WebUtils.java
index 1962a6c..a9b01ca 100644
--- a/src/org/jsecurity/web/WebUtils.java
+++ b/src/org/jsecurity/web/WebUtils.java
@@ -53,6 +53,15 @@
private static final Log log = LogFactory.getLog(WebUtils.class);
+
+ /**
+ * Message displayed when a servlet request or response is not bound to the current thread context.
+ */
+ private static final String NOT_BOUND_ERROR_MESSAGE =
+ "Make sure WebUtils.bind() is being called. (typically called by JSecurityFilter) " +
+ "This could also happen when running integration tests that don't properly call WebUtils.bind().";
+
+
public static final String SERVLET_REQUEST_KEY = ServletRequest.class.getName() + "_JSECURITY_THREAD_CONTEXT_KEY";
public static final String SERVLET_RESPONSE_KEY = ServletResponse.class.getName() + "_JSECURITY_THREAD_CONTEXT_KEY";
@@ -300,10 +309,15 @@
* This method only returns the bound value if it exists - it does not remove it
* from the thread. To remove it, one must call {@link #unbindServletRequest() unbindServletRequest} instead.
*
- * @return the ServletRequest bound to the thread, or <tt>null</tt> if there isn't one bound.
+ * @return the ServletRequest bound to the thread. Never returns null.
+ * @throws IllegalStateException if no servlet request is bound in the thread context.
*/
public static ServletRequest getServletRequest() {
- return (ServletRequest) ThreadContext.get(SERVLET_REQUEST_KEY);
+ ServletRequest request = (ServletRequest) ThreadContext.get(SERVLET_REQUEST_KEY);
+ if( request == null ) {
+ throw new IllegalStateException( "No ServletRequest found in ThreadContext. " + NOT_BOUND_ERROR_MESSAGE );
+ }
+ return request;
}
/**
@@ -353,10 +367,15 @@
* This method only returns the bound value if it exists - it does not remove it
* from the thread. To remove it, one must call {@link #unbindServletResponse() unbindServletResponse} instead.
*
- * @return the ServletResponse bound to the thread, or <tt>null</tt> if there isn't one bound.
+ * @return the ServletResponse bound to the thread. Never returns null.
+ * @throws IllegalStateException if no servlet response is bound in the thread context.
*/
public static ServletResponse getServletResponse() {
- return (ServletResponse) ThreadContext.get(SERVLET_RESPONSE_KEY);
+ ServletResponse response = (ServletResponse) ThreadContext.get(SERVLET_RESPONSE_KEY);
+ if( response == null ) {
+ throw new IllegalStateException( "No ServletResponse found in ThreadContext. " + NOT_BOUND_ERROR_MESSAGE );
+ }
+ return response;
}
/**