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  import java.io.PrintWriter;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpSession;
32  
33  import org.eclipse.jetty.client.HttpClient;
34  import org.eclipse.jetty.client.api.ContentResponse;
35  import org.eclipse.jetty.client.api.Request;
36  import org.junit.Test;
37  
38  
39  /**
40   * AbstractSessionValueSavingTest
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   
56          try
57          {
58              server1.start();
59              int port1=server1.getPort();
60              
61                  HttpClient client = new HttpClient();
62                  client.start();
63                  try
64                  {
65                      long sessionTestValue = 0;
66  
67                      // Perform one request to server1 to create a session
68                      ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
69  
70                      assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
71                      assertTrue(sessionTestValue < Long.parseLong(response1.getContentAsString()));
72  
73                      sessionTestValue = Long.parseLong(response1.getContentAsString());
74  
75                      String sessionCookie = response1.getHeaders().get("Set-Cookie");
76                      assertTrue( sessionCookie != null );
77                      // Mangle the cookie, replacing Path with $Path, etc.
78                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
79  
80                      // Perform some request to server2 using the session cookie from the previous request
81                      // This should migrate the session from server1 to server2, and leave server1's
82                      // session in a very stale state, while server2 has a very fresh session.
83                      // We want to test that optimizations done to the saving of the shared lastAccessTime
84                      // do not break the correct working
85                      int requestInterval = 500;
86  
87  
88                      for (int i = 0; i < 10; ++i)
89                      {
90                          Request request2 = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping);
91                          request2.header("Cookie", sessionCookie);
92                          ContentResponse response2 = request2.send();
93  
94                          assertEquals(HttpServletResponse.SC_OK , response2.getStatus());
95                          assertTrue(sessionTestValue < Long.parseLong(response2.getContentAsString()));
96                          sessionTestValue = Long.parseLong(response2.getContentAsString());
97  
98                          String setCookie = response1.getHeaders().get("Set-Cookie");
99                          if (setCookie!=null)
100                             sessionCookie = setCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
101 
102                         Thread.sleep(requestInterval);
103                     }
104 
105                 }
106                 finally
107                 {
108                     client.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 httpServletResponse) 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("test", System.currentTimeMillis());
127 
128                 sendResult(session, httpServletResponse.getWriter());
129             }
130             else
131             {
132                 HttpSession session = request.getSession(false);
133                 System.out.println("not init call " + session);
134                 if (session!=null)
135                 {
136                         long value = System.currentTimeMillis();
137                         System.out.println("Setting test to : " + value);
138                     session.setAttribute("test", value);
139 
140                 }
141 
142                 sendResult(session, httpServletResponse.getWriter());
143 
144             }
145 
146 
147         }
148 
149         private void sendResult(HttpSession session, PrintWriter writer)
150         {
151                 if (session != null)
152                 {
153                         writer.print(session.getAttribute("test"));
154                 }
155                 else
156                 {
157                         writer.print(0);
158                 }
159         }
160 
161     }
162 }