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.io.PrintWriter;
17  import java.util.Random;
18  
19  import javax.servlet.ServletException;
20  import javax.servlet.http.HttpServlet;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import org.eclipse.jetty.client.ContentExchange;
26  import org.eclipse.jetty.client.HttpClient;
27  import org.eclipse.jetty.http.HttpMethods;
28  import org.junit.Test;
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  
33  /**
34   * AbstractLastAccessTimeTest
35   */
36  public abstract class AbstractSessionValueSavingTest
37  {
38      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
39      
40      @Test
41      public void testSessionValueSaving() throws Exception
42      {
43          String contextPath = "";
44          String servletMapping = "/server";
45          int maxInactivePeriod = 10000;
46          int scavengePeriod = 20000;
47          AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod);
48          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
49          server1.start();
50          int port1=server1.getPort();
51          try
52          {
53              
54                  HttpClient client = new HttpClient();
55                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
56                  client.start();
57                  try
58                  {
59                      long sessionTestValue = 0;
60  
61                      // Perform one request to server1 to create a session
62                      ContentExchange exchange1 = new ContentExchange(true);
63                      exchange1.setMethod(HttpMethods.GET);
64                      exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
65                      client.send(exchange1);
66                      exchange1.waitForDone();
67                      assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
68                      
69                      System.out.println("Checking: " + sessionTestValue + " vs " + exchange1.getResponseContent());
70                      assertTrue(sessionTestValue < Long.parseLong(exchange1.getResponseContent()));
71                     
72                      sessionTestValue = Long.parseLong(exchange1.getResponseContent());
73                      
74                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
75                      assertTrue( sessionCookie != null );
76                      // Mangle the cookie, replacing Path with $Path, etc.
77                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
78  
79                      // Perform some request to server2 using the session cookie from the previous request
80                      // This should migrate the session from server1 to server2, and leave server1's
81                      // session in a very stale state, while server2 has a very fresh session.
82                      // We want to test that optimizations done to the saving of the shared lastAccessTime
83                      // do not break the correct working
84                      int requestInterval = 500;
85                      
86                      
87                      for (int i = 0; i < 10; ++i)
88                      {
89                          ContentExchange exchange2 = new ContentExchange(true);
90                          exchange2.setMethod(HttpMethods.GET);
91                          exchange2.setURL("http://localhost:" + port1 + contextPath + servletMapping);
92                          exchange2.getRequestFields().add("Cookie", sessionCookie);
93                          client.send(exchange2);
94                          exchange2.waitForDone();
95                          assertEquals(HttpServletResponse.SC_OK , exchange2.getResponseStatus());
96                          
97                          System.out.println("Checking: " + sessionTestValue + " vs " + exchange2.getResponseContent());
98                          assertTrue(sessionTestValue < Long.parseLong(exchange2.getResponseContent()));
99                          
100                         sessionTestValue = Long.parseLong(exchange2.getResponseContent());
101                         
102                         String setCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
103                         if (setCookie!=null)                    
104                             sessionCookie = setCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
105                         
106                         Thread.sleep(requestInterval);
107                     }
108 
109                 }
110                 finally
111                 {
112                     client.stop();
113                 }
114         }
115         finally
116         {
117             server1.stop();
118         }
119     }
120 
121     public static class TestServlet extends HttpServlet
122     {
123         @Override
124         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
125         {
126             String action = request.getParameter("action");
127             if ("init".equals(action))
128             {
129                 HttpSession session = request.getSession(true);
130                 session.setAttribute("test", System.currentTimeMillis());
131                 
132                 sendResult(session, httpServletResponse.getWriter());
133             }
134             else
135             {
136                 HttpSession session = request.getSession(false);
137                 System.out.println("not init call " + session);
138                 if (session!=null)
139                 {
140                 	long value = System.currentTimeMillis();
141                 	System.out.println("Setting test to : " + value);
142                     session.setAttribute("test", value);
143                     
144                 }
145                 
146                 sendResult(session, httpServletResponse.getWriter());
147 
148             }
149             
150             
151         }
152         
153         private void sendResult(HttpSession session, PrintWriter writer)
154         {
155         	if (session != null)
156         	{
157         		writer.print(session.getAttribute("test"));
158         	}
159         	else
160         	{
161         		writer.print(0);
162         	}
163         }
164         
165     }
166 }