Class ServletRequestAttributes

java.lang.Object
org.springframework.web.context.request.AbstractRequestAttributes
org.springframework.web.context.request.ServletRequestAttributes
All Implemented Interfaces:
RequestAttributes
Direct Known Subclasses:
ServletWebRequest

public class ServletRequestAttributes extends AbstractRequestAttributes
Servlet-based implementation of the RequestAttributes interface.

Accesses objects from servlet request and HTTP session scope, with no distinction between "session" and "global session".

Since:
2.0
Author:
Juergen Hoeller
See Also:
  • Field Details

    • DESTRUCTION_CALLBACK_NAME_PREFIX

      public static final String DESTRUCTION_CALLBACK_NAME_PREFIX
      Constant identifying the String prefixed to the name of a destruction callback when it is stored in a HttpSession.
    • immutableValueTypes

      protected static final Set<Class<?>> immutableValueTypes
  • Constructor Details

    • ServletRequestAttributes

      public ServletRequestAttributes(HttpServletRequest request)
      Create a new ServletRequestAttributes instance for the given request.
      Parameters:
      request - current HTTP request
    • ServletRequestAttributes

      public ServletRequestAttributes(HttpServletRequest request, @Nullable HttpServletResponse response)
      Create a new ServletRequestAttributes instance for the given request.
      Parameters:
      request - current HTTP request
      response - current HTTP response (for optional exposure)
  • Method Details

    • getRequest

      public final HttpServletRequest getRequest()
      Exposes the native HttpServletRequest that we're wrapping.
    • getResponse

      @Nullable public final HttpServletResponse getResponse()
      Exposes the native HttpServletResponse that we're wrapping (if any).
    • getSession

      @Nullable protected final HttpSession getSession(boolean allowCreate)
      Exposes the HttpSession that we're wrapping.
      Parameters:
      allowCreate - whether to allow creation of a new session if none exists yet
    • getAttribute

      public Object getAttribute(String name, int scope)
      Description copied from interface: RequestAttributes
      Return the value for the scoped attribute of the given name, if any.
      Parameters:
      name - the name of the attribute
      scope - the scope identifier
      Returns:
      the current attribute value, or null if not found
    • setAttribute

      public void setAttribute(String name, Object value, int scope)
      Description copied from interface: RequestAttributes
      Set the value for the scoped attribute of the given name, replacing an existing value (if any).
      Parameters:
      name - the name of the attribute
      value - the value for the attribute
      scope - the scope identifier
    • removeAttribute

      public void removeAttribute(String name, int scope)
      Description copied from interface: RequestAttributes
      Remove the scoped attribute of the given name, if it exists.

      Note that an implementation should also remove a registered destruction callback for the specified attribute, if any. It does, however, not need to execute a registered destruction callback in this case, since the object will be destroyed by the caller (if appropriate).

      Parameters:
      name - the name of the attribute
      scope - the scope identifier
    • getAttributeNames

      public String[] getAttributeNames(int scope)
      Description copied from interface: RequestAttributes
      Retrieve the names of all attributes in the scope.
      Parameters:
      scope - the scope identifier
      Returns:
      the attribute names as String array
    • registerDestructionCallback

      public void registerDestructionCallback(String name, Runnable callback, int scope)
      Description copied from interface: RequestAttributes
      Register a callback to be executed on destruction of the specified attribute in the given scope.

      Implementations should do their best to execute the callback at the appropriate time: that is, at request completion or session termination, respectively. If such a callback is not supported by the underlying runtime environment, the callback must be ignored and a corresponding warning should be logged.

      Note that 'destruction' usually corresponds to destruction of the entire scope, not to the individual attribute having been explicitly removed by the application. If an attribute gets removed via this facade's RequestAttributes.removeAttribute(String, int) method, any registered destruction callback should be disabled as well, assuming that the removed object will be reused or manually destroyed.

      NOTE: Callback objects should generally be serializable if they are being registered for a session scope. Otherwise the callback (or even the entire session) might not survive web app restarts.

      Parameters:
      name - the name of the attribute to register the callback for
      callback - the destruction callback to be executed
      scope - the scope identifier
    • resolveReference

      public Object resolveReference(String key)
      Description copied from interface: RequestAttributes
      Resolve the contextual reference for the given key, if any.

      At a minimum: the HttpServletRequest reference for key "request", and the HttpSession reference for key "session".

      Parameters:
      key - the contextual key
      Returns:
      the corresponding object, or null if none found
    • getSessionId

      public String getSessionId()
      Description copied from interface: RequestAttributes
      Return an id for the current underlying session.
      Returns:
      the session id as String (never null)
    • getSessionMutex

      public Object getSessionMutex()
      Description copied from interface: RequestAttributes
      Expose the best available mutex for the underlying session: that is, an object to synchronize on for the underlying session.
      Returns:
      the session mutex to use (never null)
    • updateAccessedSessionAttributes

      protected void updateAccessedSessionAttributes()
      Update all accessed session attributes through session.setAttribute calls, explicitly indicating to the container that they might have been modified.
      Specified by:
      updateAccessedSessionAttributes in class AbstractRequestAttributes
    • isImmutableSessionAttribute

      protected boolean isImmutableSessionAttribute(String name, @Nullable Object value)
      Determine whether the given value is to be considered as an immutable session attribute, that is, doesn't have to be re-set via session.setAttribute since its value cannot meaningfully change internally.

      The default implementation returns true for String, Character, Boolean and standard Number values.

      Parameters:
      name - the name of the attribute
      value - the corresponding value to check
      Returns:
      true if the value is to be considered as immutable for the purposes of session attribute management; false otherwise
      See Also:
    • registerSessionDestructionCallback

      protected void registerSessionDestructionCallback(String name, Runnable callback)
      Register the given callback as to be executed after session termination.

      Note: The callback object should be serializable in order to survive web app restarts.

      Parameters:
      name - the name of the attribute to register the callback for
      callback - the callback to be executed for destruction
    • toString

      public String toString()
      Overrides:
      toString in class Object