1 Reply Latest reply on Sep 4, 2018 6:19 AM by kzivkovic

    Trouble accessing ServletRequestContext in custom HttpHandler

    johncaizjh

      As part of migrating an application from JBoss 4.x to Wildfly 10.10.  The existing application used Tomcat valve to invalidate the session and clear the cookies. Following the discussion at https://developer.jboss.org/thread/237073.  I have now ported the valve to HttpHandler and created a servelet extension class. Both extension class and handler are packeged into a module. I also registed the servlet extension in /META-INF/services/io.undertow/servlet/ServletExtension file.  Furthermore, I added the followings into standalone.xml configuration file following Tomaz's advice.

       

                  <global-modules>
                      <module name="com.mycompany.httphandlers" meta-inf="true" services="true"/>
                  </global-modules>

       

                 
      I am able to deploy the applicaition and the handler is called, but ServletRequestContext instance in the code is always null (see code below),  Can you please help me on this? (I am quite new to JBOSS/Wildfly). Am I missing anything or my configuration is not correct?

       

      Thanks,

       

      John

       

       

      Here is the handler:

       

      public class RenewSessionHandler implements HttpHandler {

       

              private static final Logger logger = Logger.getLogger(

      RenewSessionHandler.class); 
              private final HttpHandler next;
             
              RenewSessionHandler(HttpHandler next) {
                  this.next = next;
              }
             
             
              @Override
              public void handleRequest(final HttpServerExchange exchange) throws Exception
              {
                  ServletRequestContext context =    exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
                  if (context != null)
                  {
                      HttpServletRequest request = context.getOriginalRequest();
                      HttpServletResponse response = context.getOriginalResponse(); 
                      String contextPath = request.getContextPath();
                      String screenInfo = request.getRequestURI().substring(contextPath.length() + 1);
                      if(screenInfo.equals("revapp/logon") || screenInfo.equals("revapp/sso"))
                      {                  
                          HttpSession session = request.getSession(true);
                          if(session != null)
                          {
                              session.invalidate();                                   
                              request.getSession(true);
                              //request.changeSessionId();                  
                              for (Cookie c : request.getCookies())
                              {  
                                  c.setMaxAge(0); 
                                  c.setValue(""); 
                                  c.setPath("/"); 
                                  response.addCookie(c);
                              }
                           }
                      }               
                  }    
                  this.next.handleRequest(exchange);
              } 
      }

       

       

      Here is the servlet extension:

       

      public class UndertowHandlerExtension implements ServletExtension
      {
           @Override
            public void handleDeployment(final DeploymentInfo deployment, final ServletContext context)
           {
                deployment.addInitialHandlerChainWrapper(new HandlerWrapper()
                {
                    @Override
                    public HttpHandler wrap(final HttpHandler handler)
                    {
                       return new RenewSessionHandler(handler);
                    }
                });
            }
      }