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.server.Request;
35  import org.eclipse.jetty.server.SessionManager;
36  import org.junit.Test;
37  
38  /**
39   * AbstractLocalSessionScavengingTest
40   */
41  public abstract class AbstractLocalSessionScavengingTest
42  {
43      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
44  
45      public void pause(int scavengePeriod)
46      {
47          try
48          {
49              Thread.sleep(scavengePeriod * 2500L);
50          }
51          catch (InterruptedException e)
52          {
53              e.printStackTrace();
54          }
55      }
56  
57      @Test
58      public void testLocalSessionsScavenging() throws Exception
59      {
60          String contextPath = "";
61          String servletMapping = "/server";
62          int inactivePeriod = 3;
63          int scavengePeriod = 1;
64          AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod);
65          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
66  
67          try
68          {
69              server1.start();
70              int port1 = server1.getPort();
71              AbstractTestServer server2 = createServer(0, inactivePeriod, scavengePeriod * 3);
72              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
73  
74              try
75              {
76                  server2.start();
77                  int port2 = server2.getPort();
78                  HttpClient client = new HttpClient();
79                  client.start();
80                  try
81                  {
82                      String[] urls = new String[2];
83                      urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
84                      urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
85  
86                      // Create the session on node1
87                      ContentResponse response1 = client.GET(urls[0] + "?action=init");
88                      assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
89                      String sessionCookie = response1.getHeaders().get("Set-Cookie");
90                      assertTrue(sessionCookie != null);
91                      // Mangle the cookie, replacing Path with $Path, etc.
92                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
93  
94                      // Be sure the session is also present in node2
95                      org.eclipse.jetty.client.api.Request request = client.newRequest(urls[1] + "?action=test");
96                      request.header("Cookie", sessionCookie);
97                      ContentResponse response2 = request.send();
98                      assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
99  
100                     // Wait for the scavenger to run on node1, waiting 2.5 times the scavenger period
101                     pause(inactivePeriod+scavengePeriod);
102 
103                     // Check that node1 does not have any local session cached
104                     request = client.newRequest(urls[0] + "?action=check");
105                     request.header("Cookie", sessionCookie);
106                     response1 = request.send();
107                     assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
108 
109                     // Wait for the scavenger to run on node2, waiting 2 times the scavenger period
110                     // This ensures that the scavenger on node2 runs at least once.
111                     pause(inactivePeriod+(3*scavengePeriod));
112                     
113                     // Check that node2 does not have any local session cached
114                     request = client.newRequest(urls[1] + "?action=check");
115                     request.header("Cookie", sessionCookie);
116                     response2 = request.send();
117                     assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
118                 }
119                 finally
120                 {
121                     client.stop();
122                 }
123             }
124             finally
125             {
126                 server2.stop();
127             }
128         }
129         finally
130         {
131             server1.stop();
132         }
133     }
134 
135     public static class TestServlet extends HttpServlet
136     {
137         private SessionManager sessionManager;
138 
139         @Override
140         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
141         {
142             String action = request.getParameter("action");
143             if ("init".equals(action))
144             {
145                 HttpSession session = request.getSession(true);
146                 session.setAttribute("test", "test");
147                 this.sessionManager = ((Request)request).getSessionManager();
148             }
149             else if ("test".equals(action))
150             {
151                 HttpSession session = request.getSession(false);
152                 session.setAttribute("test", "test");
153                 this.sessionManager = ((Request)request).getSessionManager();
154             }
155             else if ("check".equals(action))
156             {
157                 HttpSession session = request.getSession(false);
158                 assertTrue(session == null);
159             }
160         }
161     }
162 }