Spring WebFlow: Getting HTTPServletRequest from ServletExternalContext - spring-webflow

Two related questions:
Old posts I have read state that ServletExternalContext has a method getRequest which returns the httpServletRequest. In the current version of Spring Webflow (2.4) this method is not visible (i.e., protected). So, can someone confirm that if I want to get httpServletRequest data (such as request URL) I have to do it through Spring MVC instead?
Second question: I understand that webflow is designed to work with multiple frameworks, not just servlets. However, I don't understand why it has to be completely brickwalled from the framework within which it is embedded. Can someone explain the reasoning

Yes, it is possible
ServletExternalContext externalContext = (ServletExternalContext) ctx.getExternalContext();
HttpServletRequest httpServletRequest = (HttpServletRequest)externalContext.getNativeRequest();
is moot

Related

Annotation Based Handler Mapping in spring mvc

I want to understand how HandlerMapping work in Annotation based Spring MVC applications. As while working in XML based configuration we have one default and if we want to use other implementation then we have to define it in XML file as well as URL mapping strategies.
To understand HandlerMapping in Annotation based Controller application i came across to
DefaultAnnotationHandlerMapping which used to be default before v3. 1 and now
RequestMappingHandlerMapping is currently used. So I want to understand how to define this HandlerMapping if we are not using XML based configuration and URL mapping strategies. Another thing I came across was HandlerAdapter. So these two things are confusing me.
Please explain step by step when Dispatcher Servlet intercept a request how it find which HandlerMapping to use and how URL mapping strategies work in Annotation based apps.
You can think that HandlerMapping determine a request/URL should be handled by which ways/frameworks (e.g use #Controller to handle ? Use JSP to handle ? etc)
HandlerAdapter drives the actual workflow of handling this request , containing the actual implementation of handling logic.
High Level Logic:
DispatcherServlet intercepts a request
Find out which HandlerMapping can handle this request. Refer to HandlerMapping#getHandler() for the matching logic. It will return a generic object (called a handler object) if the request can be handled.
Find out which HandlerAdapter can handle this handler object (By checking HandlerAdapter#supports()). If a HandlerAdapter can handle , it will handle it (by HandlerAdapter#handle).

Spring MVC - log every incoming http request call with payload into database

experts, I would log every incoming http request call with payload into database.
I checked there would be 2 approaches.
use filter or interceptor.
I feel filter is so easier for me to implement.
what would be best approach for my purpose?
please kindly advise.
thank you very much!
if you have a need to do something completely generic (e.g. log all requests), then a filter is sufficient - but if the behavior depends on the target handler or you want to do something between the request handling and view rendering, then the HandlerInterceptor provides that flexibility.
But anyway, just do the way which make you feel easily and simply.
Note:
Interceptor work in spring application context
Servlet work in web context
Use Spring AOP. Use any advice according to your needs.
#Aspect
#Component
public class Test {
#Around("#annotation(mapping) ")
public Object preAuthUserPersmission(ProceedingJoinPoint joinPoint, RequestMapping mapping) throws Throwable {
Object[] parameters = joinPoint.getArgs();
// Your actions on the input parameters
return joinPoint.proceed(joinPoint.getArgs());
}
}

SessionAware design in Struts 2

I have been working with Struts 2 for a long time.
In case of implementing SessionAware interface to our action class we will get SessionMap but not HttpSession object.
In case of ServletRequestAware and ServletResposeAware we get HttpServletRequest and HttpServletResponse object but not wrapper objects like SessionMap in case of SessionAware.
My question is, if Struts is giving us SessionMap instead of HttpSession to decouple our action classes from Servlet API and Http protocol,then why it is giving us HttpServletRequest and HttpServletResponse objects in case ServletRequestAware and ServletResponseAware.
If Struts doesn't want to decouple Servlet API and HTTP protocol from the action classes then why it is giving us SessionMap in case of SessionAware interface.
Why we don't get HttpSession object?
In case of ServlectRequestAware and ServletResposeAware we get HttpServletRequest and HttpServletRespose object but not wrapper objects like SessionMap in case of SessionAware.
Because those directly expose the servlet request and response on rare occasions where they're actually necessary (or at least useful).
My question is, if struts is giving us SessionMap instead of HttpSession to decouple our action classes from Servlet API and Http protocol,then why it is giving us HttpServletRequest and HttpServletRespose objects in case ServlectRequestAware and ServletResposeAware.
Because it's much less likely you'd specifically need an HttpSession than the actual request or response.
If struts don't want to decouple Servlet API and HTTP protocol from the action classes then why it is giving us SessionMap in case of SessionAware interface.
It does want to decouple the Servlet API, for good reasons. It forces you to explicitly ask for Servlet API artifacts because they're a code smell. It doesn't prevent you from getting them because on rare occasions they're important.
An HttpSession is pretty much just an attribute map, it doesn't contain information generally useful in an action. On the even-rarer occasions you need one you can still get it.
Why we don't get HttpSession object?
You can get this object from the servlet's HTTP request. No reason to define additional interface to inject the HttpSession into the action. On the other hand Struts defines maps for HTTP request, session, application for easier access/modify its attributes using a Map interface. The servletConfig interceptor can inject these objects to the action if it implements corresponding xxxAware interface.
The list of interfaces that could be injected by this interceptor:
ServletContextAware
ServletRequestAware
ServletResponseAware
ParameterAware
RequestAware
SessionAware
ApplicationAware
PrincipalAware

Updating ant style path capture from spring mvc 3.0 to 3.1

In spring 3.0.x I have a handler method that works perfectly like:
#RequestMapping("/foo/{version:\\d+}/**")
public void handleVersionedResource(#PathVariable("version") Long version, HttpServletRequest request, HttpServletResponse response) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
// rest of the code maps path and version to a real resource and serves
// content with extra long cache control / expires headers
}
In 3.0.x, ff the request url is /foo/12345/some/resource, then path = "some/resource" (the ** part of the request match).
However, in 3.1.x (tried with both 3.1.0.RELEASE and 3.1.1.RELEASE) path = to the full request path match, so "/foo/12345/some/resource" in my previous example.
So the question is, does anyone have the simplest way to replicate the behavior I was getting in 3.0.x? Also, any insight to why this behavior has changed? Was I doing it wrong in the first place?
Update:
repeat of Spring 3.1.RC1 and PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE - no good answer was given there either.
Spring 3.1.RC1 and PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE suggests that HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE is internal to Spring and should not be used in your application
Doing something similar to How to match a Spring #RequestMapping having a #pathVariable containing "/"? would probably be your best bet

Can I let some function executed before any controller in spring mvc

I use spring mvc 3.0, in our system there are some object like current user will be used in controller and in page. Now, in all function, I always get user from spring security context, and set it into ModelMap, then it can be got in page. And in most of the function, the user object will also be got from ModelMap and used as some parameters.
I want to know that, except interceptor, is there some way that I can set these objects into ModelMap before any function?
And in interceptor, I can only set into request, but actually, some data are already in servlet context.
Thanks.
Try for this a annotation style as #PreHandle with this can be annotated your method or function,
and means that Handler invoke execution of this function/method right before Dispatcher handle appropriate controller.
exact explain can be found here: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/HandlerInterceptor.html

Resources