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.http.HttpMethods;
27  import org.eclipse.jetty.client.ContentExchange;
28  import org.eclipse.jetty.client.HttpClient;
29  import org.testng.annotations.Test;
30  
31  /**
32   * AbstractOrphanedSessionTest
33   *
34   *
35   */
36  
37  public abstract class AbstractOrphanedSessionTest
38  {
39  
40      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
41  
42      /**
43       * If nodeA creates a session, and just afterwards crashes, it is the only node that knows about the session.
44       * We want to test that the session data is gone after scavenging.
45       */
46      @Test
47      public void testOrphanedSession() throws Exception
48      {
49          Random random = new Random(System.nanoTime());
50  
51          // Disable scavenging for the first server, so that we simulate its "crash".
52          String contextPath = "";
53          String servletMapping = "/server";
54          int port1 = random.nextInt(50000) + 10000;
55          int inactivePeriod = 5;
56          AbstractTestServer server1 = createServer(port1, inactivePeriod, -1);
57          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
58          server1.start();
59          try
60          {
61              int port2 = random.nextInt(50000) + 10000;
62              int scavengePeriod = 2;
63              AbstractTestServer server2 = createServer(port2, inactivePeriod, scavengePeriod);
64              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
65              server2.start();
66              try
67              {
68                  HttpClient client = new HttpClient();
69                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
70                  client.start();
71                  try
72                  {
73                      // Connect to server1 to create a session and get its session cookie
74                      ContentExchange exchange1 = new ContentExchange(true);
75                      exchange1.setMethod(HttpMethods.GET);
76                      exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
77                      client.send(exchange1);
78                      exchange1.waitForDone();
79                      assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
80                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
81                      assert sessionCookie != null;
82                      // Mangle the cookie, replacing Path with $Path, etc.
83                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
84  
85                      // Wait for the session to expire.
86                      // The first node does not do any scavenging, but the session
87                      // must be removed by scavenging done in the other node.
88                      Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod));
89  
90                      System.err.println("FINISHED waiting for session to expire");
91                      // Perform one request to server2 to be sure that the session has been expired
92                      
93                      System.err.println("CHECKING NODE2");
94                      ContentExchange exchange2 = new ContentExchange(true);
95                      exchange2.setMethod(HttpMethods.GET);
96                      exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping + "?action=check");
97                      exchange2.getRequestFields().add("Cookie", sessionCookie);
98                      client.send(exchange2);
99                      exchange2.waitForDone();
100                     assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
101                 }
102                 finally
103                 {
104                     client.stop();
105                 }
106             }
107             finally
108             {
109                 server2.stop();
110             }
111         }
112         finally
113         {
114             server1.stop();
115         }
116     }
117 
118     public static class TestServlet extends HttpServlet
119     {
120         @Override
121         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
122         {
123             String action = request.getParameter("action");
124             if ("init".equals(action))
125             {
126                 HttpSession session = request.getSession(true);
127                 session.setAttribute("A", "A");
128             }
129             else if ("check".equals(action))
130             {
131                 HttpSession session = request.getSession(false);
132                 assert session == null;
133             }
134         }
135     }
136 }