View Javadoc

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