I'm trying to use different methodes depending on from which domain the request is send.
e.g.
#RequestMapping(value = "/index.html", domain = "google.de", method = RequestMethod.GET)
public ModelAndView handleDeRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
}
#RequestMapping(value = "/index.html", domain = "google.com", method = RequestMethod.GET)
public ModelAndView handleComRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
}
The two domains are routing to one, the same, server and webapp, but I'd like to return a different modelAndView in the controllerclass depending from which URL the reqeust is comming.
Any ideas?
cheers.
Can you not have a single handleRequest method where you simply check HTTP referrer header and act correspondingly - fork into different methods, etc.?
Related
I have a Spring Boot API (Stateless) with a Controller that receives a POST request, extracts the parameters of the POST request to send them through GET to my angular client. My question is if it's possible to send hidden parameters in HttpServletResponse.sendRedirect()?
What I have so far is this, but I do not want to show the parameters in the browser ...
#RequestMapping(value = "/return", method = RequestMethod.POST, headers = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
#ResponseBody
#Transactional
public void returnData(UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request,
final HttpServletResponse response) throws IOException {
String parameter=request.getParameter("billCode");
response.sendRedirect("http://localhost:4200/payment?parameterOne="+parameter);
}
Update:
I can't uses HttpSession session = request.getSession(false); and then session.setAttribute("helloWorld", "Hello world") because session is Null
Many Thanks!
You can use the HTTP response header instead of sending the parameter in the queryString. An example:
#GetMapping(value="/")
public void init(HttpServletRequest request, HttpServletResponse response) throws IOException {
String billCode = request.getParameter("billCode");
response.addHeader("parameterOne", billCode);
response.sendRedirect("http://localhost:4200/payment");
}
To get the value from request:
String billCode = request.getHeader("parameterOne");
Or if you get from ajax with jQuery:
$.ajax({
url:'/api/v1/payment'
}).done(function (data, textStatus, xhr) {
console.log(xhr.getResponseHeader('parameterOne'));
});
Hope this helps.
I have the following controller:
#Controller
#RequestMapping("/my-account")
public class AccountController {
#RequestMapping(value = "/foo/post",
method = RequestMethod.POST)
public String doPost(final RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("flashAttribute", "flashAttributeValue");
return "redirect:/my-account/foo/get";
}
#RequestMapping(value = "/foo/get",
method = RequestMethod.GET)
public void doGet(final HttpServletRequest request, final Model model) {
System.out.println("in request: " + RequestContextUtils.getInputFlashMap(request).get("flashAttribute"));
System.out.println("in model: " + model.asMap().get("flashAttribute"));
}
}
I would also like to access the flash attribute flashAttribute during the invocation of a filter in the filter chain that finally invokes springs default DispatcherServlet which in turn invokes AccountController.
public class FlashAttributeBasedFilter extends OncePerRequestFilter {
#Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
throws ServletException, IOException {
String flashAttribute = // how to access the redirectAttribute flashAttribute here?
// do something with flashAttribute ...
filterChain.doFilter(request, response);
}
The DispatcherServlet uses a org.springframework.web.servlet.FlashMapManager that handles these flash attributes, but it doesn't provide read-only access so I think I would be messing something up if I would use it in the filter. And also the FlashMapManager instance is kept in the dispatcher servlet privately.
Does anybody have an idea how I can make the redirect attribute accessible in the filter chain for the GET request succeeding the POST?
Considering that all these methods return null into my filter (I don't understand why):
RequestContextUtils.getFlashMapManager(httpRequest)
RequestContextUtils.getInputFlashMap(httpRequest)
RequestContextUtils.getOutputFlashMap(httpRequest)
I used a drastic solution: read directly the into the session (where flash attributes are stored).
CopyOnWriteArrayList<FlashMap> what = (CopyOnWriteArrayList<FlashMap>) httpRequest.getSession().getAttribute("org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS");
if (what != null) {
FlashMap flashMap = what.get(0);
[read flashMap as you read a HashMap]
}
I know, this code is super ugly but at the moment I don't find another solution.
Had the same problem, following works for me.
FlashMap flashMap = new SessionFlashMapManager().retrieveAndUpdate(request, null);
flashMap.get("parameter");
Using SpringMVC, I have a method that catch all REST requests:
#RequestMapping(value = "/**")
public Object catchAll(#RequestBody(required = false) Object body, HttpMethod method, HttpServletRequest request, HttpServletResponse response) {
// ...
}
Now I would like to catch just a few requests with the following endpoint:
#RequestMapping(value = "/test", method = RequestMethod.POST)
public Object post(#RequestBody Object body, HttpMethod method, HttpServletRequest request, HttpServletResponse response) {
// ...
}
Right now, when I call:
/test
the second method is never called.
What can I do to have my 2nd method always called in place of the first one?
First of all as Asura points out, do not implement a 'catchAll' method. Having said that, Spring MVC allows you to use regular expressions in URL(s).
Read the documentation for using regular expressions in Spring URL(s) here.
In your case, use a negative lookahead in your first URL to remove the ones that start with /test. That way your /** controller will catch all requests except the ones that start with /test and you can use your other controller to catch those.
Use negative lookahead in your first URL:
#RequestMapping(value = "/^(?!test)")
public Object catchAll(#RequestBody(required = false) Object body, HttpMethod method, HttpServletRequest request, HttpServletResponse response) {
// ...
}
Now, this should catch /test requests:
#RequestMapping(value = "/test", method = RequestMethod.POST)
public Object post(#RequestBody Object body, HttpMethod method, HttpServletRequest request, HttpServletResponse response) {
// ...
}
i.e.
I have various URLs mapped using Spring MVC RequestMapping
#RequestMapping(value = "/mystuff", method = RequestMethod.GET)
#RequestMapping(value = "/mystuff/dsf", method = RequestMethod.GET)
#RequestMapping(value = "/mystuff/eee", method = RequestMethod.GET)
etc
I want to run some common action before about 90% of my requests. These are across several controllers.
Is there anyway to do that without delving into AOP? And if I have to use aspects, any guidance on how to do this?!
Thanks!
More info:
It is to run some app specific security - we are chained to a parent security set up, which we need to read and call into, and then need to access a cookie prior to some most of ours calls, but not all.
You can use an Interceptor:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-handlermapping
Interceptor is the solution. It has methods preHandler and postHandler, which will be called before and after each request respectively. You can hook into each HTTPServletRequest object and also by pass few by digging it.
here is a sample code:
#Component
public class AuthCodeInterceptor extends HandlerInterceptorAdapter {
#Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// set few parameters to handle ajax request from different host
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
response.addHeader("Access-Control-Max-Age", "1000");
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
response.addHeader("Cache-Control", "private");
String reqUri = request.getRequestURI();
String serviceName = reqUri.substring(reqUri.lastIndexOf("/") + 1,
reqUri.length());
if (serviceName.equals("SOMETHING")) {
}
return super.preHandle(request, response, handler);
}
#Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
super.postHandle(request, response, handler, modelAndView);
}
}
The HandlerInterceptor.preHandle() method gives you access to the request and response and also the target handler. In Spring 3.1 that will be of type HandlerMethod, which gives you access to the target controller class and method. If it helps you can try excluding entire controller classes by type name, which would be strongly typed and without specifying explicit URLs.
Another option would be created an interceptor mapped to a set of URL patterns. See the section on configuring Spring MVC in the reference documentation.
I realy appreciate Spring 3 anoation driven mapping of Web Controllers
I have a lot of Controllers with signatures like:
#RequestMapping(value = "solicitation/create",method = RequestMethod.POST)
public String handleSubmitForm(Model model, #ModelAttribute("solicitation") Solicitation solicitation, BindingResult result)
But my issue is, that I want to write an interceptor that would ho through BindingResults after processing - how do I get them from HttpRequest or HttpResponse?
as intercpetor methods are with alike signature
public boolean postHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
After execution of controller method BindingResult is stored as a model attribute named BindingResult.MODEL_KEY_PREFIX + <name of the model attribute>, later model attributes are merged into request attributes. So, before merging you can use Hurda's own answer, after merging use:
request.getAttribute(BindingResult.MODEL_KEY_PREFIX + "solicitation")
So with big help from #Axtavt I came to conlusion, that you can get to Bind reuslt from ModelAndView in postHandle method:
void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
String key = BindingResult.MODEL_KEY_PREFIX + "commandName";
BindingResult br = (BindingResult) modelAndView.getModel().get(key);
}