View Javadoc

1   // ========================================================================
2   // Copyright 2004-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.session;
15  
16  import java.io.IOException;
17  import java.util.Random;
18  import java.util.concurrent.TimeUnit;
19  
20  import javax.servlet.ServletException;
21  import javax.servlet.http.HttpServlet;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.http.HttpSession;
25  
26  import org.eclipse.jetty.client.ContentExchange;
27  import org.eclipse.jetty.client.HttpClient;
28  import org.eclipse.jetty.http.HttpMethods;
29  import org.junit.Test;
30  
31  /**
32   * AbstractOrphanedSessionTest
33   */
34  public abstract class AbstractOrphanedSessionTest
35  {
36  
37      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
38  
39      /**
40       * If nodeA creates a session, and just afterwards crashes, it is the only node that knows about the session.
41       * We want to test that the session data is gone after scavenging.
42       */
43      @Test
44      public void testOrphanedSession() throws Exception
45      {
46          Random random = new Random(System.nanoTime());
47  
48          // Disable scavenging for the first server, so that we simulate its "crash".
49          String contextPath = "";
50          String servletMapping = "/server";
51          int inactivePeriod = 5;
52          AbstractTestServer server1 = createServer(0, inactivePeriod, -1);
53          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
54          server1.start();
55          int port1 = server1.getPort();
56          try
57          {
58              int scavengePeriod = 2;
59              AbstractTestServer server2 = createServer(0, inactivePeriod, scavengePeriod);
60              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
61              server2.start();
62              int port2 = server2.getPort();
63              try
64              {
65                  HttpClient client = new HttpClient();
66                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
67                  client.start();
68                  try
69                  {
70                      // Connect to server1 to create a session and get its session cookie
71                      ContentExchange exchange1 = new ContentExchange(true);
72                      exchange1.setMethod(HttpMethods.GET);
73                      exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
74                      client.send(exchange1);
75                      exchange1.waitForDone();
76                      assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
77                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
78                      assert sessionCookie != null;
79                      // Mangle the cookie, replacing Path with $Path, etc.
80                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
81  
82                      // Wait for the session to expire.
83                      // The first node does not do any scavenging, but the session
84                      // must be removed by scavenging done in the other node.
85                      Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod));
86  
87                      System.err.println("FINISHED waiting for session to expire");
88                      // Perform one request to server2 to be sure that the session has been expired
89                      
90                      System.err.println("CHECKING NODE2");
91                      ContentExchange exchange2 = new ContentExchange(true);
92                      exchange2.setMethod(HttpMethods.GET);
93                      exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping + "?action=check");
94                      exchange2.getRequestFields().add("Cookie", sessionCookie);
95                      client.send(exchange2);
96                      exchange2.waitForDone();
97                      assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
98                  }
99                  finally
100                 {
101                     client.stop();
102                 }
103             }
104             finally
105             {
106                 server2.stop();
107             }
108         }
109         finally
110         {
111             server1.stop();
112         }
113     }
114 
115     public static class TestServlet extends HttpServlet
116     {
117         @Override
118         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
119         {
120             String action = request.getParameter("action");
121             if ("init".equals(action))
122             {
123                 HttpSession session = request.getSession(true);
124                 session.setAttribute("A", "A");
125             }
126             else if ("check".equals(action))
127             {
128                 HttpSession session = request.getSession(false);
129                 assert session == null;
130             }
131         }
132     }
133 }