@Override
  public void handleRequest(HttpServerExchange exchange) throws Exception {
    final String incomingSessionId = servletContext.getSessionConfig().findSessionId(exchange);
    if (incomingSessionId == null || !data.containsKey(incomingSessionId)) {
      next.handleRequest(exchange);
      return;
    }

    // we have some old data
    PersistentSession result = data.remove(incomingSessionId);
    if (result != null) {
      long time = System.currentTimeMillis();
      if (time < result.getExpiration().getTime()) {
        final HttpSessionImpl session = servletContext.getSession(exchange, true);
        final HttpSessionEvent event = new HttpSessionEvent(session);
        for (Map.Entry<String, Object> entry : result.getSessionData().entrySet()) {

          if (entry.getValue() instanceof HttpSessionActivationListener) {
            ((HttpSessionActivationListener) entry.getValue()).sessionDidActivate(event);
          }
          if (entry.getKey().startsWith(HttpSessionImpl.IO_UNDERTOW)) {
            session.getSession().setAttribute(entry.getKey(), entry.getValue());
          } else {
            session.setAttribute(entry.getKey(), entry.getValue());
          }
        }
      }
    }
    next.handleRequest(exchange);
  }
 private void initializeTempDir(
     final ServletContextImpl servletContext, final DeploymentInfo deploymentInfo) {
   if (deploymentInfo.getTempDir() != null) {
     servletContext.setAttribute(ServletContext.TEMPDIR, deploymentInfo.getTempDir());
   } else {
     servletContext.setAttribute(
         ServletContext.TEMPDIR, new File(SecurityActions.getSystemProperty("java.io.tmpdir")));
   }
 }
 public void stop() {
   ClassLoader old = getTccl();
   try {
     setTccl(servletContext.getClassLoader());
     this.started = false;
     final Map<String, SessionPersistenceManager.PersistentSession> objectData = new HashMap<>();
     for (String sessionId : sessionManager.getTransientSessions()) {
       Session session = sessionManager.getSession(sessionId);
       if (session != null) {
         final HttpSessionEvent event =
             new HttpSessionEvent(SecurityActions.forSession(session, servletContext, false));
         final Map<String, Object> sessionData = new HashMap<>();
         for (String attr : session.getAttributeNames()) {
           final Object attribute = session.getAttribute(attr);
           sessionData.put(attr, attribute);
           if (attribute instanceof HttpSessionActivationListener) {
             ((HttpSessionActivationListener) attribute).sessionWillPassivate(event);
           }
         }
         objectData.put(
             sessionId,
             new PersistentSession(
                 new Date(
                     session.getLastAccessedTime() + (session.getMaxInactiveInterval() * 1000)),
                 sessionData));
       }
     }
     sessionPersistenceManager.persistSessions(deploymentName, objectData);
     this.data.clear();
   } finally {
     setTccl(old);
   }
 }
 public String getVirtualServerName() {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (String) doPrivileged("getVirtualServerName", null);
   } else {
     return context.getVirtualServerName();
   }
 }
 @Override
 public String getServletContextName() {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (String) doPrivileged("getServletContextName", null);
   } else {
     return context.getServletContextName();
   }
 }
 @Override
 public void setAttribute(String name, Object object) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     doPrivileged("setAttribute", new Object[] {name, object});
   } else {
     context.setAttribute(name, object);
   }
 }
 @Override
 public void removeAttribute(String name) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     doPrivileged("removeAttribute", new Object[] {name});
   } else {
     context.removeAttribute(name);
   }
 }
 @Override
 public String getInitParameter(String name) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (String) doPrivileged("getInitParameter", new Object[] {name});
   } else {
     return context.getInitParameter(name);
   }
 }
 @Override
 public Object getAttribute(String name) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return doPrivileged("getAttribute", new Object[] {name});
   } else {
     return context.getAttribute(name);
   }
 }
 @Override
 public void log(String msg) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     doPrivileged("log", new Object[] {msg});
   } else {
     context.log(msg);
   }
 }
 @Override
 public String getRealPath(String path) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (String) doPrivileged("getRealPath", new Object[] {path});
   } else {
     return context.getRealPath(path);
   }
 }
  @Override
  public RequestDispatcher getNamedDispatcher(String name) {
    // Validate the name argument
    if (name == null) return (null);

    // Create and return a corresponding request dispatcher
    MobicentsSipServlet servlet = sipContext.findSipServletByName(name);

    if (servlet == null) return context.getNamedDispatcher(name);
    // return (null);

    if (servlet instanceof SipServletImpl) {
      return new SipRequestDispatcher((SipServletImpl) servlet);
    } else {
      return context.getNamedDispatcher(name);
    }
  }
 @Override
 public RequestDispatcher getRequestDispatcher(final String path) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (RequestDispatcher) doPrivileged("getRequestDispatcher", new Object[] {path});
   } else {
     return context.getRequestDispatcher(path);
   }
 }
 @Override
 public InputStream getResourceAsStream(String path) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (InputStream) doPrivileged("getResourceAsStream", new Object[] {path});
   } else {
     return context.getResourceAsStream(path);
   }
 }
 @Override
 public String getMimeType(String file) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (String) doPrivileged("getMimeType", new Object[] {file});
   } else {
     return context.getMimeType(file);
   }
 }
 public HttpSession getSession(final String sessionId) {
   final SessionManager sessionManager = context.getDeployment().getSessionManager();
   Session session = sessionManager.getSession(sessionId);
   if (session != null) {
     return SecurityActions.forSession(session, this, false, sessionManager);
   }
   return null;
 }
 @Override
 public int getEffectiveMinorVersion() {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return ((Integer) doPrivileged("getEffectiveMinorVersion", null)).intValue();
   } else {
     return context.getEffectiveMinorVersion();
   }
 }
 @Override
 public JspConfigDescriptor getJspConfigDescriptor() {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (JspConfigDescriptor) doPrivileged("getJspConfigDescriptor", null);
   } else {
     return context.getJspConfigDescriptor();
   }
 }
 @Override
 public ClassLoader getClassLoader() {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (ClassLoader) doPrivileged("getClassLoader", null);
   } else {
     return context.getClassLoader();
   }
 }
 @Override
 public void addListener(String className) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     doPrivileged("addListener", new Object[] {className});
   } else {
     context.addListener(className);
   }
 }
 @Override
 public <T extends EventListener> void addListener(T t) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     doPrivileged("addListener", new Object[] {t.getClass().getName()});
   } else {
     context.addListener(t);
   }
 }
 @Override
 public void addListener(Class<? extends EventListener> listenerClass) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     doPrivileged("addListener", new Object[] {listenerClass.getName()});
   } else {
     context.addListener(listenerClass);
   }
 }
 @Override
 public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     doPrivileged("setSessionTrackingModes", new Object[] {sessionTrackingModes});
   } else {
     context.setSessionTrackingModes(sessionTrackingModes);
   }
 }
 @Override
 public SessionCookieConfigImpl getSessionCookieConfig() {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (SessionCookieConfigImpl) doPrivileged("getSessionCookieConfig", null);
   } else {
     return context.getSessionCookieConfig();
   }
 }
 @Override
 public FilterRegistration getFilterRegistration(String filterName) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (FilterRegistration) doPrivileged("getFilterRegistration", new Object[] {filterName});
   } else {
     return context.getFilterRegistration(filterName);
   }
 }
  public HttpSession getSession(
      final ServletContextImpl originalServletContext,
      final HttpServerExchange exchange,
      boolean create) {
    SessionConfig c = originalServletContext.getSessionConfig();
    ConvergedHttpSessionFacade httpSession = exchange.getAttachment(sessionAttachmentKey);
    if (httpSession != null && httpSession.isValidIntern()) {
      exchange.removeAttachment(sessionAttachmentKey);
      httpSession = null;
    }
    if (httpSession == null) {
      final SessionManager sessionManager = context.getDeployment().getSessionManager();
      Session session = sessionManager.getSession(exchange, c);
      if (session != null) {
        httpSession =
            (ConvergedHttpSessionFacade)
                SecurityActions.forSession(session, this, false, sessionManager);
        exchange.putAttachment(sessionAttachmentKey, httpSession);
      } else if (create) {

        String existing = c.findSessionId(exchange);
        if (originalServletContext != this.context) {
          // this is a cross context request
          // we need to make sure there is a top level session
          originalServletContext.getSession(originalServletContext, exchange, true);
        } else if (existing != null) {
          c.clearSession(exchange, existing);
        }

        final Session newSession = sessionManager.createSession(exchange, c);
        httpSession =
            (ConvergedHttpSessionFacade)
                SecurityActions.forSession(newSession, this, true, sessionManager);
        // call access after creation to set LastAccessTime at sipAppSession.
        httpSession.access();
        // add delegate to InMemorySession to call sipAppSession.access(), when necessary:
        ((ConvergedInMemorySessionManager) sessionManager)
            .addConvergedSessionDeletegateToSession(
                newSession.getId(), httpSession.getConvergedSessionDelegate());

        exchange.putAttachment(sessionAttachmentKey, httpSession);
      }
    }
    return httpSession;
  }
 @Override
 public void declareRoles(String... roleNames) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     // FIXME
     doPrivileged("declareRoles", new Object[] {roleNames});
   } else {
     context.declareRoles(roleNames);
   }
 }
 @Override
 public FilterRegistration.Dynamic addFilter(String filterName, String className) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (FilterRegistration.Dynamic)
         doPrivileged("addFilter", new Object[] {filterName, className});
   } else {
     return context.addFilter(filterName, className);
   }
 }
 @Override
 public ServletRegistration.Dynamic addServlet(String servletName, String className) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (ServletRegistration.Dynamic)
         doPrivileged("addServlet", new Object[] {servletName, className});
   } else {
     return context.addServlet(servletName, className);
   }
 }
 @Override
 public ServletRegistration getServletRegistration(String servletName) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     return (ServletRegistration)
         doPrivileged("getServletRegistration", new Object[] {servletName});
   } else {
     return context.getServletRegistration(servletName);
   }
 }