View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpSession;
32  
33  import org.eclipse.jetty.client.HttpClient;
34  import org.eclipse.jetty.client.api.ContentResponse;
35  import org.eclipse.jetty.servlet.ServletContextHandler;
36  import org.eclipse.jetty.servlet.ServletHolder;
37  import org.junit.Test;
38  
39  public abstract class AbstractStopSessionManagerPreserveSessionTest
40  {
41      public String _id;
42      
43      
44      public abstract void checkSessionPersisted (boolean expected);
45      
46      public abstract AbstractTestServer createServer (int port);
47      
48      public abstract void configureSessionManagement(ServletContextHandler context);
49      
50      @Test
51      public void testStopSessionManagerPreserveSession() throws Exception
52      {
53          String contextPath = "";
54          String servletMapping = "/server";
55          
56          AbstractTestServer server = createServer(0);
57          ServletContextHandler context = server.addContext(contextPath);
58          ServletHolder holder = new ServletHolder();
59          TestServlet servlet = new TestServlet();
60          holder.setServlet(servlet);
61          
62          context.addServlet(holder, servletMapping);
63       
64          configureSessionManagement(context);
65          
66          server.start();
67          int port=server.getPort();
68          try
69          {
70              HttpClient client = new HttpClient();
71              client.start();
72              try
73              {
74                  //Create a session
75                  ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
76                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
77                  String sessionCookie = response.getHeaders().getStringField("Set-Cookie");
78                  assertTrue(sessionCookie != null);
79                  // Mangle the cookie, replacing Path with $Path, etc.
80                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
81  
82                  //stop the session manager
83                  context.getSessionHandler().getSessionManager().stop();
84                  
85                  //check the database to see that the session is still valid
86                  checkSessionPersisted(true);
87                  
88              }
89              finally
90              {
91                  client.stop();
92              }
93          }
94          finally
95          {
96              server.stop();
97          }
98      }
99      
100     public class TestServlet extends HttpServlet
101     {
102         
103         @Override
104         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
105         {
106             String action = request.getParameter("action");
107             if ("create".equals(action))
108             {
109                 HttpSession session = request.getSession(true);
110                 session.setAttribute("foo", "bar");
111                 assertTrue(session.isNew());
112                 _id = session.getId();
113             }
114         }
115     }
116 }