Showing posts with label session timpout panel. Show all posts
Showing posts with label session timpout panel. Show all posts

Wednesday, April 3, 2013

Handling Session Timeout in Wicket

Define the timeout period in web.xml :
 
<session-config>

<session-timeout>60</session-timeout>

</session-config>



Create a class which extend the AjaxselfUpdatingTimerBehavior and implements IAjaxIndicatorAware :


import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;

import org.apache.wicket.ajax.IAjaxIndicatorAware;

import org.apache.wicket.util.time.Duration;

 

public class TimerBehavior extends AjaxSelfUpdatingTimerBehavior implements IAjaxIndicatorAware{

 

      public TimerBehavior(Duration updateInterval) {

            super(updateInterval);

      }


      private static final long serialVersionUID = 1L;

 

      @Override

      public String getAjaxIndicatorMarkupId() {

            return null;

      }


}
Create a panel which ill show the confirmation popup to decide whether to continue with current session or close :
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.panel.Panel;
 
public class SessionTimeOutPanel extends Panel{
      /**
       *
       */
      private static final long serialVersionUID = 1L;
     
      private boolean status;
     
      public boolean isStatus() {
            return status;
      }
 
      public void setStatus(boolean status) {
            this.status = status;
      }
     
      public SessionTimeOutPanel(String id,final ModalWindow modalWindow) {
            super(id);
            status = false;              
            AjaxLink okLink = new AjaxLink("OkButton") {
                 
                  @Override
                  public void onClick(AjaxRequestTarget target) {
                        setStatus(true);
                        modalWindow.close(target);
                  }
            };
            AjaxLink cancelLink = new AjaxLink("CancelButton") {
                 
                  @Override
                  public void onClick(AjaxRequestTarget target) {
                        setStatus(false);
                        modalWindow.close(target);
                  }
            };
            add(okLink);
           add(cancelLink);
      }
     
}
 HTML page for the above panel:
<wicket:panel>
      <span class="" style="width:99%;background: #4286c5 !important;">Session Time Out</span>
      <div class="">
            <label class="" style="width:100%">Your session is about to expire.Do you wish to continue?</label><br />
      </div><br/>
      <div id="" style="width:100%  padding:1.3em 0em;padding-top:32%
height:2.3em; ">
<a   class=""   href="#" tabindex=""
style="margin-right: 3%;margin-left:35%"  wicket:id="OkButton"> <span>Ok</span>
</a>
<a   class=""   href="#" tabindex=""
style="margin-right: 2%;" wicket:id="CancelButton"><span >Cancel</span>
</a>
      </div>
</wicket:panel>
Create a abstract web page ,all pages should implement this abstractpage:


import org.apache.wicket.Request;

import org.apache.wicket.RequestCycle;

import org.apache.wicket.ajax.AjaxRequestTarget;

import org.apache.wicket.ajax.IAjaxIndicatorAware;

import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;

import org.apache.wicket.markup.html.WebMarkupContainer;

import org.apache.wicket.markup.html.WebPage;

import org.apache.wicket.protocol.http.PageExpiredException;

import org.apache.wicket.protocol.http.WebRequest;

import org.apache.wicket.util.time.Duration;

 

public abstract class AbstractPage extends WebPage implements

IAjaxIndicatorAware {

 

     

      protected ModalWindow sessionTimeOutModalWindow;

     

      private transient HttpSession httpSession;

      private  int timeOut=0;

      private int timOutCount=0;

     

     

      public ModalWindow getSessionTimeOutModalWindow() {

            sessionTimeOutModalWindow = new ModalWindow("sessionTimeout");

        final SessionTimeOutPanel sessionTimeOutPanel = new SessionTimeOutPanel(

                  sessionTimeOutModalWindow.getContentId(),

                  sessionTimeOutModalWindow);

        sessionTimeOutPanel.setOutputMarkupId(true);

        sessionTimeOutModalWindow.setContent(sessionTimeOutPanel);

        sessionTimeOutModalWindow.setInitialHeight(135);

        sessionTimeOutModalWindow.setInitialWidth(400);

        sessionTimeOutModalWindow.setResizable(false);

       

        sessionTimeOutModalWindow.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {                        

            public void onClose(AjaxRequestTarget target) { 

          

          if (sessionTimeOutPanel.isStatus()) {

              if(httpSession!=null){

              httpSession.setMaxInactiveInterval(timeOut);

              timOutCount=timeOut;

              }else{

                   throw new PageExpiredException("PageExpiredException");

                    }

             

          }

          else

          {

            target.prependJavascript("window.opener='X';window.open('','_parent','');" +

                        "window.close();");

          }

            }});

                 

        Request request = RequestCycle.get().getRequest();

        if( request instanceof WebRequest )

        {

            WebRequest wr = (WebRequest)request;

            httpSession = wr.getHttpServletRequest().getSession();

            if( httpSession != null ) {

                timeOut=httpSession.getMaxInactiveInterval();

            }

        }

 

 

        return sessionTimeOutModalWindow;

    }

     

     

     

      /**

       * constructor

       */

      public AbstractPage() {

 

          add(getSessionTimeOutModalWindow());

          final WebMarkupContainer timer=new WebMarkupContainer("timer");

        timer.setOutputMarkupPlaceholderTag(true);

        timer.add(new TimerBehavior(Duration.ONE_SECOND){

 

                /**

                   *

                   */

                  private static final long serialVersionUID = 1L;

 

                        @Override

                protected void onPostProcessTarget(AjaxRequestTarget target) {

                    timOutCount--;

                 

                    if(timOutCount==60){

                      sessionTimeOutModalWindow.show(target);

                     // target.addComponent(sessionTimeOutModalWindow);

                    }

                }

         

        });

 

      add(timer);

 

      }

@Override

    public String getAjaxIndicatorMarkupId() {

            timOutCount=timeOut;

           

            return null;

    }

In Html Page add the below lines :

<div wicket:id="sessionTimeout"></div>


<div wicket:id="timer"></div>