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   * AbstractSessionMigrationTest
40   */
41  public abstract class AbstractSessionMigrationTest
42  {
43      public abstract AbstractTestServer createServer (int port);
44  
45      @Test
46      public void testSessionMigration() throws Exception
47      {
48          String contextPath = "";
49          String servletMapping = "/server";
50          AbstractTestServer server1 = createServer(0);
51          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
52          server1.start();
53          int port1=server1.getPort();
54          try
55          {
56              AbstractTestServer server2 = createServer(0);
57              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
58              server2.start();
59              int port2=server2.getPort();
60              try
61              {
62                  HttpClient client = new HttpClient();
63                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
64                  client.start();
65                  try
66                  {
67                      // Perform one request to server1 to create a session
68                      int value = 1;
69                      ContentExchange exchange1 = new ContentExchange(true);
70                      exchange1.setMethod(HttpMethods.POST);
71                      exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=set&value=" + value);
72                      client.send(exchange1);
73                      exchange1.waitForDone();
74                      assertEquals(HttpServletResponse.SC_OK,exchange1.getResponseStatus());
75                      String sessionCookie = exchange1.getResponseFields().getStringField("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 a request to server2 using the session cookie from the previous request
81                      // This should migrate the session from server1 to server2.
82                      ContentExchange exchange2 = new ContentExchange(true);
83                      exchange2.setMethod(HttpMethods.GET);
84                      exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping + "?action=get");
85                      exchange2.getRequestFields().add("Cookie", sessionCookie);
86                      client.send(exchange2);
87                      exchange2.waitForDone();
88                      assertEquals(HttpServletResponse.SC_OK,exchange2.getResponseStatus());
89                      String response = exchange2.getResponseContent();
90                      assertEquals(response.trim(),String.valueOf(value));               }
91                  finally
92                  {
93                      client.stop();
94                  }
95              }
96              finally
97              {
98                  server2.stop();
99              }
100         }
101         finally
102         {
103             server1.stop();
104         }
105     }
106 
107     public static class TestServlet extends HttpServlet
108     {
109         @Override
110         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
111         {
112             doPost(request, response);
113         }
114 
115         @Override
116         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
117         {
118             HttpSession session = request.getSession(false);
119 
120             String action = request.getParameter("action");
121             if ("set".equals(action))
122             {
123                 if (session == null) session = request.getSession(true);
124                 int value = Integer.parseInt(request.getParameter("value"));
125                 session.setAttribute("value", value);
126                 PrintWriter writer = response.getWriter();
127                 writer.println(value);
128                 writer.flush();
129             }
130             else if ("get".equals(action))
131             {
132                 int value = (Integer)session.getAttribute("value");
133                 int x = ((AbstractSession)session).getMaxInactiveInterval();
134                 assertTrue(x > 0);
135                 PrintWriter writer = response.getWriter();
136                 writer.println(value);
137                 writer.flush();
138             }
139         }
140     }
141 }