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