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  package org.eclipse.jetty.server.session;
14  
15  import java.io.IOException;
16  import java.util.Random;
17  
18  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.servlet.http.HttpSession;
23  
24  import org.eclipse.jetty.client.ContentExchange;
25  import org.eclipse.jetty.client.HttpClient;
26  import org.eclipse.jetty.http.HttpMethods;
27  import org.junit.Test;
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertTrue;
30  
31  
32  /**
33   * AbstractLastAccessTimeTest
34   */
35  public abstract class AbstractLastAccessTimeTest
36  {
37      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
38  
39      @Test
40      public void testLastAccessTime() throws Exception
41      {
42          String contextPath = "";
43          String servletMapping = "/server";
44          int maxInactivePeriod = 8;
45          int scavengePeriod = 2;
46          AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod);
47          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
48          server1.start();
49          int port1=server1.getPort();
50          try
51          {
52              AbstractTestServer server2 = createServer(0, maxInactivePeriod, scavengePeriod);
53              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
54              server2.start();
55              int port2=server2.getPort();
56              try
57              {
58                  HttpClient client = new HttpClient();
59                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
60                  client.start();
61                  try
62                  {
63                      // Perform one request to server1 to create a session
64                      ContentExchange exchange1 = new ContentExchange(true);
65                      exchange1.setMethod(HttpMethods.GET);
66                      exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
67                      client.send(exchange1);
68                      exchange1.waitForDone();
69                      assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
70                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
71                      assertTrue( sessionCookie != null );
72                      // Mangle the cookie, replacing Path with $Path, etc.
73                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
74  
75                      // Perform some request to server2 using the session cookie from the previous request
76                      // This should migrate the session from server1 to server2, and leave server1's
77                      // session in a very stale state, while server2 has a very fresh session.
78                      // We want to test that optimizations done to the saving of the shared lastAccessTime
79                      // do not break the correct working
80                      int requestInterval = 500;
81                      for (int i = 0; i < maxInactivePeriod * (1000 / requestInterval); ++i)
82                      {
83                          ContentExchange exchange2 = new ContentExchange(true);
84                          exchange2.setMethod(HttpMethods.GET);
85                          exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping);
86                          exchange2.getRequestFields().add("Cookie", sessionCookie);
87                          client.send(exchange2);
88                          exchange2.waitForDone();
89                          assertEquals(HttpServletResponse.SC_OK , exchange2.getResponseStatus());
90                          String setCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
91                          if (setCookie!=null)                    
92                              sessionCookie = setCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
93                          
94                          Thread.sleep(requestInterval);
95                      }
96  
97                      System.out.println("Waiting for scavenging on node1...");
98  
99                      // At this point, session1 should be eligible for expiration.
100                     // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
101                     Thread.sleep(scavengePeriod * 2500L);
102 
103                     // Access again server1, and be sure we can
104                     exchange1 = new ContentExchange(true);
105                     exchange1.setMethod(HttpMethods.GET);
106                     exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping);
107                     exchange1.getRequestFields().add("Cookie", sessionCookie);
108                     client.send(exchange1);
109                     exchange1.waitForDone();
110                     assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
111                     
112                     // TODO shouldn't the session be expired????
113                 }
114                 finally
115                 {
116                     client.stop();
117                 }
118             }
119             finally
120             {
121                 server2.stop();
122             }
123         }
124         finally
125         {
126             server1.stop();
127         }
128     }
129 
130     public static class TestServlet extends HttpServlet
131     {
132         @Override
133         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
134         {
135             String action = request.getParameter("action");
136             if ("init".equals(action))
137             {
138                 HttpSession session = request.getSession(true);
139                 session.setAttribute("test", "test");
140             }
141             else
142             {
143                 HttpSession session = request.getSession(false);
144                 session.setAttribute("test", "test");
145             }
146         }
147     }
148 }