View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server.session;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpSession;
31  
32  import org.eclipse.jetty.client.HttpClient;
33  import org.eclipse.jetty.client.api.ContentResponse;
34  import org.eclipse.jetty.servlet.ServletContextHandler;
35  import org.eclipse.jetty.servlet.ServletHolder;
36  import org.junit.Test;
37  
38  /**
39   * AbstractStopSessionManagerPreserveSessionTest
40   *
41   *
42   */
43  public abstract class AbstractStopSessionManagerPreserveSessionTest
44  {
45      public String _id;
46      
47      
48      public abstract void checkSessionPersisted (boolean expected);
49      
50      public abstract AbstractTestServer createServer (int port);
51      
52      public abstract void configureSessionManagement(ServletContextHandler context);
53      
54      @Test
55      public void testStopSessionManagerPreserveSession() throws Exception
56      {
57          String contextPath = "";
58          String servletMapping = "/server";
59          
60          AbstractTestServer server = createServer(0);
61          ServletContextHandler context = server.addContext(contextPath);
62          ServletHolder holder = new ServletHolder();
63          TestServlet servlet = new TestServlet();
64          holder.setServlet(servlet);
65          
66          context.addServlet(holder, servletMapping);
67       
68          configureSessionManagement(context);
69          
70          server.start();
71          int port=server.getPort();
72          try
73          {
74              HttpClient client = new HttpClient();
75              client.start();
76              try
77              {
78                  //Create a session
79                  ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
80                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
81                  String sessionCookie = response.getHeaders().get("Set-Cookie");
82                  assertTrue(sessionCookie != null);
83                  // Mangle the cookie, replacing Path with $Path, etc.
84                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
85  
86                  //stop the session manager
87                  context.getSessionHandler().getSessionManager().stop();
88                  
89                  //check the database to see that the session is still valid
90                  checkSessionPersisted(true);
91                  
92              }
93              finally
94              {
95                  client.stop();
96              }
97          }
98          finally
99          {
100             server.stop();
101         }
102     }
103     
104     public class TestServlet extends HttpServlet
105     {
106         
107         @Override
108         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
109         {
110             String action = request.getParameter("action");
111             if ("create".equals(action))
112             {
113                 HttpSession session = request.getSession(true);
114                 session.setAttribute("foo", "bar");
115                 assertTrue(session.isNew());
116                 _id = session.getId();
117             }
118         }
119     }
120 }