Trouble accessing HttpSession and setting cookies in custom HttpHandler
natedrake2 Sep 29, 2016 9:30 PMHi,
As part of migrating and application from JBoss 7.1.1 to Wildfly 10.1, I'm converting a Valve to a HttpHandler. The handler needs to check if an HttpSession attribute is null, and if so it needs to delete a cookie.
I've got a custom HttpHandler deploying in Wildfly and getting executed, but I'm having trouble getting the ServletRequestContext object. It's always coming back null. Do I need to do something else to my undertow configuration? Is there a better was to access the HttpSession and modified cookies on the HttpResponse?
Thanks!
Nate
Here is the custom handler:
package com.example.handlers; import io.undertow.server.ExchangeCompletionListener; import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.server.session.Session; import io.undertow.servlet.handlers.ServletRequestContext; import io.undertow.servlet.spec.HttpServletRequestImpl; import io.undertow.servlet.spec.HttpServletResponseImpl; import io.undertow.util.Sessions; import javax.servlet.http.Cookie; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; public class TestHandler implements HttpHandler { private static final Logger logger = Logger.getLogger(TestHandler.class); private HttpHandler next; public TestHandler(HttpHandler next) { this.next = next; } @Override public void handleRequest(HttpServerExchange exchange) throws Exception { logger.info("Handling request..."); ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY); // src is always null if(src != null) { HttpServletRequestImpl request = src.getOriginalRequest(); HttpServletResponseImpl response = src.getOriginalResponse(); final HttpSession session = request.getSession(false); if(session != null) { logger.info("Session is not null!"); final Object attr = session.getAttribute("attr"); if (attr == null) { logger.info("attr is not null " + attr); for (Cookie c : request.getCookies()) { if (c.getName().equals("the-cookie")) { logger.info("Found cookie...deleting..."); c.setMaxAge(0); c.setValue(""); c.setPath("/"); response.addCookie(c); break; } } } else { logger.info("attr is null"); } } else { logger.info("session is null"); } } else { logger.info("src is null"); } next.handleRequest(exchange); } }
Here is the module xml file:
<module name="com.example.handlers" xmlns="urn:jboss:module:1.3"> <dependencies> <module name="javax.api"/> <module name="io.undertow.core"/> <module name="io.undertow.servlet"/> <module name="org.apache.log4j"/> </dependencies> <resources> <resource-root path="."/> </resources> </module>
Here is the undertow subsystem configuration standalone.xml:
<subsystem xmlns="urn:jboss:domain:undertow:3.1"> <buffer-cache name="default"/> <server name="default-server"> <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true" record-request-start-time="true"/> <https-listener name="https" socket-binding="https" security-realm="ManagementRealm" enable-http2="true" record-request-start-time="true"/> <host name="default-host" alias="localhost"> <access-log prefix="access." rotate="true" pattern="%h %l %u %t "%r" %s %b %D "%{i,Referer}" "%{i,User-Agent}" "%I""/> <filter-ref name="test"/> </host> </server> <servlet-container name="default"> <jsp-config/> <websockets/> </servlet-container> <filters> <filter name="test" class-name="com.example.handlers.TestHandler" module="com.example.handlers"/> </filters> </subsystem>