-1

How to get original destination in wicket for example : I clicked one link in a page but it then redirected to login page as it requires authentication but I need to know in java code as to what is the original destination(the link which I clicked before login) as I have to apply some checks(for this specific link).

1 Answers1

0

You could set a session variable on link click as below:

      HttpServletRequest request = (HttpServletRequest) getWebRequest().getContainerRequest();
        request.getSession().setAttribute("linkclicked", "linkname");

Then, in your Wicket Application, add Request Listener to read the session variable stored above and create PageParameter for the Login Page. Redirect user to the Login Page with PageParameter supplied. On successful login you can get that param value and do your validation before calling gotoOriginalDestinationPage method call.

Request Listener could be something as below. I have not tried but should be working fine.

getRequestCycleListeners().add(
            new AbstractRequestCycleListener()
            {
                public void onBeginRequest(RequestCycle cycle)
                {
                    if( cycle.getRequest().getContainerRequest() instanceof HttpServletRequest)
                    {
                        HttpServletRequest containerRequest =
                                (HttpServletRequest)cycle.getRequest().getContainerRequest();

                        String linkClicked = (String) containerRequest.getSession().getAttribute("linkclicked");
                        PageParameters parameters = new PageParameters();
                        parameters.add("link", linkClicked);

                        cycle.setResponse(new LoginPage(parameters));
                    }
                };
            }
    );

You can use component.continueToOriginalDestination() after successful login.

  /**
     * @param component
     */
    protected void gotoOriginalDestinationPage(Component component) {
        component.continueToOriginalDestination();
        //If there was no interceptor page, so we reached here. In this case, go to home page.
        setResponsePage(GarbageApp.get().getHomePage());
    }

Assuming your Login page is LoginPage.java, then inside onSubmit of the form/button, you could call above method as gotoOriginalDestinationPage(this);

Mihir
  • 270
  • 3
  • 9