Retrieve Request or Session information anywhere in a Spring Web Application

Spring uses ThreadLocal to allow for retrieving current session/request information within a web application. In fact, Spring MVC, Spring Webflow and Spring Security all have their own ways to retrieve information via a context holder. Each one mirrors one another. Spring MVC simply contains session and request information while Webflow has that as well as the current webflow object. Spring Security has a security object where you can get the username and authorizations.

Because Spring MVC and Spring Webflow even use the same class name, I will include the Package names as well

Spring MVC

org.springframework.web.context.request.RequestAttributes ra; 
org.springframework.web.context.request.RequestAttributes cr; 

You can pull objects from the RequestAttributes object by providing the scope

Object shoppingCart=cr.getAttribute("shoppingCart",RequestAttributes.SCOPE_SESSION);
Object itemId=cr.getAttribute("itemId",RequestAttributes.SCOPE_REQUEST);

The RequestAttributes object can either be of type ServletRequestAttributes or PortletRequestAttributes type. If you want to pull information about the request or session then it needs to be casted. For instance, the need of retrieving the IP address of the requesting client

if (cr instanceof ServletRequestAttributes ) {
	org.springframework.web.context.request.ServletRequestAttributes sr;
	sr = (ServletRequestAttributes)cr;
	HttpServletRequest request = sr.getRequest();
	String ipAddress = request.getRemoteAddr();

Spring Webflow

As above, the class that retrieves information for the request/session is named RequestContextHolder but in a different package. This RequestContext allows for retrieval of the flow, state, view, transition, and many more are available. It also can retrieve the original request and session objects.

//Retrieving webflow specific information
org.springframework.webflow.execution.RequestContext rc;
rc = org.springframework.webflow.execution.RequestContextHolder.getRequestContext();
FlowDefinition flow = rc.getActiveFlow();
StateDefinition state = rc.getCurrentState();
TransitionDefinition transition = rc.getCurrentTransition();
org.springframework.webflow.execution.View = rc.getCurrentView();
//Webflow can also get the native request and session information
org.springframework.webflow.context.ExternalContext ec = rc.getExternalContext();
Object nativeRequest = ec.getNativeRequest();
HttpServletResponse response = (HttpServletResponse) ec.getNativeResponse();
HttpServletRequest request = (HttpServletRequest) ec.getNativeRequest();
HttpSession session = request.getSession();
Object shoppingCart = session.getAttribute("shoppingCart");

Spring Security

Spring Security allows you to retrieve the Authentication object by using the SecurityContextHolder. From here, access is given to a list of authorities, name and principal.

Authentication a = SecurityContextHolder.getContext().getAuthentication();
Collection<GrantedAuthority> authorities = a.getAuthorities();
String name = a.getName();
Object principal = a.getPrincipal();
Object details = a.getDetails();

Post new comment

Subscribe to Syndicate