weemonger
weemonger

Reputation: 156

How to set the HTTP-Session-Timeout in Jetty 9 (embedded)?

I would like to use Jetty 9 (v9.2.12.v20150709) embedded for my test cases. But I am unable to change the HTTP-Session-Timeout programmatically.

This call webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(timeoutInSeconds); doesn't seem to work.

Here is reduced code segement, which shows what I do:

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

@SuppressWarnings("javadoc")
public class EmbeddedJetty
{

    @SuppressWarnings("serial")
    public static class TimeoutServlet extends HttpServlet
    {
        @Override
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws IOException
        {
            // return the value of the currently used HTTP-Session Timeout
            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println("<h1>Timeout: " + request.getSession()
                    .getMaxInactiveInterval() + "</h1>");
        }
    }

    public static void main(String[] args) throws Exception
    {
        // the custom timeout, which I am trying to set
        int timeoutInSeconds = 1234;

        Server server = new Server(0);
        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        webapp.setResourceBase(System.getProperty("user.dir"));

        // Can't set custom timeout. Following Statement doesn't work.
        webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(
                timeoutInSeconds);

        server.setHandler(webapp);

        webapp.addServlet(TimeoutServlet.class.getName(), "/*");

        server.start();

        // get current URL of the server
        String url = server.getURI().toString();
        System.out.println("\n URL: " + url);
        // server.dumpStdErr();

        // make a request to get the used timeout setting
        HttpClient httpClient = new HttpClient();
        httpClient.start();
        ContentResponse response = httpClient.GET(url);
        httpClient.stop();

        String timeoutInfo = response.getContentAsString();
        System.out.println(timeoutInfo);

        // check if the custom timeout is used
        if( timeoutInfo.contains(String.valueOf(timeoutInSeconds)) )
        {
            System.out.println("Custom Timeout is used.");
        }
        else
        {
            // Unfortunately, I get the default(?) everytime
            System.out.println("Default Timeout? Custom Value is NOT used.");
        }

        System.out.println("Press Enter to exit ...");
        System.in.read();

        server.stop();
        server.join();
    }
}

I am using the WebAppContext-Style of setup, because this allowed me to get my ServletContextListeners to work by using WebAppContext.addEventListener(). Which I couldn't get to work by using a ServletHandler.

Also I am using the Version 9.2.12.v20150709 of Jetty, because it is Classpath-compatible with Selenium v2.5.2 (which supports Java 7 (project requirement)).

Have you any suggestions, what i am doing wrong?

Thank you for your time.

Upvotes: 2

Views: 8228

Answers (1)

weemonger
weemonger

Reputation: 156

A WebAppContext has some defaults, which are loaded during server.start() (WebAppContext.startContext()). These defaults contain also a DefaultWebDescriptor located in the jetty-webapp.jar under /org/eclipse/jetty/webapp/webdefault.xml. This Descriptor includes a session-config, which sets the timeout to the default of 30m (1800s).

To overwrite the defaults the call of setMaxInactiveInterval() must be done after the server is started:

server.start();
webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(timeoutInSeconds);

Or to avoid these defaults, it might be better to use a ServletContextHandler instead.

Upvotes: 5

Related Questions