简体   繁体   中英

Intercepting and changing request url Using Stripes framework

I am facing problem in intercepting and changing request url to link it with correct URL. I am using stripes framework, and want to redirect user to their correct subdomain. For eg. if user belongs to abc.sitename.com and request came from xyz.sitename.com he should be redirected to abc.sitename.com with all request post. For getparameter i have done this by simply getting request url and request query and redirecting user from custom intercept class before lifecycle resolution execution. But all post parameter are flushed as it is redirect. Another solution was to forward resolution, but it can take user within context and i need to override it as:

resolution = new OnwardResolution<ForwardResolution>(reponseUrl) {
                    @Override
                    public void execute(HttpServletRequest request,
                            HttpServletResponse response) throws Exception {
                        request = ctx.getRequest();
                        response = ctx.getResponse();
                        String path = reponseUrl; //getUrl(request.getLocale());
                        // Set event name as a request attribute
                        String oldEvent = (String) request.getAttribute(StripesConstants.REQ_ATTR_EVENT_NAME);
                        //request.setAttribute(StripesConstants.REQ_ATTR_EVENT_NAME, event);
                        log.info("check: {}", path);
                        // Revert event name to its original value
                        request.setAttribute(StripesConstants.REQ_ATTR_EVENT_NAME, oldEvent);
                        // Figure out if we're inside an include, and use an include instead of a forward
                        RequestDispatcher dispatcher = request.getRequestDispatcher(path);
                        //request.getRequestDispatcher(path).forward(request, response);
                        dispatcher.forward(request, response);

                    }
                };

But it is calling in application context path, whereas it should call the complete url. Another solution, If I use redirect resolution and set includerequest parameter true will do the job, but put all request parameter into url which makes it unacceptable.

Request url should be changed before resolution executuion as after it it will be of no use, I have tried it. is there any way to set request url in HTTPServeletRequest.

I have also tried to intercept during ActionBeanResolution, It is the best place to do such activity. My main problem is POST parameters. I can not redirect (as they get flushed or if use include reuest parameter they are visible in URL). Forward resolution on forward in context. If there is a way to forward it to new URL it will do the job.

Thanks in advance.

It's impossible to redirect using POST. End of story.

Just make sure an abc user never sees a link or form pointing to xyz .

Redirect for GET requests (in case the user enters the xyz URL directly in the address bar), and redirect to some error page or home page for POST requests, since that should never happen anyway.

@JB Nizet and @Pointy Thanks for your time and replies. I figured out a solution for my problem. @Pointy I am changing subdomain of user which require a redirect but also i don't want to loose post parameters in that request. I hope now it make sense.

Here what I did (in Steps):

  • First I figured when i need to do redirect and need to save post parameter.
  • Save all the post data in session (temporally).
  • If i have any temporally post value in session I put it in new request and removes the post value from session.

Code: For setting post parameter in session:

resolution = new RedirectResolution(reponseUrl);
            Iterator<Map.Entry<String, String[]>> requestParamter = ctx.getRequest().getParameterMap().entrySet().iterator();
            List<IdName> requestParams = new ArrayList<IdName>();
            while(requestParamter.hasNext()){
                IdName param = new IdName();
                Map.Entry<String, String[]> entry = requestParamter.next();
                param.setName(entry.getKey());
                param.setParameterValues(entry.getValue());
                log.trace("intercept - Adding Key: {} with value: {} to redirect request parameter.", entry.getKey(), entry.toString() );
                requestParams.add(param);
            }
            ctx.setRedirectRequestParameter(requestParams);

For getting it back:

if(ctx.getRedirectRequestParameter() != null && !ctx.getRedirectRequestParameter().isEmpty()){
                //Removes parameter which are already in request from redirect request parameter.
                Map<String, String[]> additionalParams = new TreeMap<String, String[]>();
                Map requestParameterMap = ctx.getRequest().getParameterMap();
                ListIterator<IdName> oldRequestParams = ctx.getRedirectRequestParameter().listIterator();
                while(oldRequestParams.hasNext()){
                    IdName oldRequestParam = oldRequestParams.next();
                    log.trace("Lopping requestparameter key: {} ", oldRequestParam.getName() );
                    if (!requestParameterMap.containsValue(oldRequestParam.getName())) {
                        additionalParams.put(oldRequestParam.getName(), oldRequestParam.getParameterValues());
                    }
                }
                HttpServletRequest newRequest = new MyperksHttpServletRequestWrapper(ctx.getRequest(), additionalParams);
                ctx.setRequest(newRequest);
                ctx.setRedirectRequestParameter(null);
            }

I have also created a wrapper class which adds new parameter to request which create a new request wrapper that will merge additional parameters into the request object without prematurely reading parameters from the original request. For deatil about this class kindly follow the link: PrettyFaceRequestWrapper .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM