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  
14  package org.eclipse.jetty.server.session;
15  
16  import java.io.IOException;
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.http.HttpMethods;
26  import org.eclipse.jetty.client.ContentExchange;
27  import org.eclipse.jetty.client.HttpClient;
28  import org.testng.annotations.Test;
29  
30  
31  /**
32   * AbstractInvalidationSessionTest
33   * Goal of the test is to be sure that invalidating a session on one node
34   * result in the session being unavailable in the other node also.
35   *
36   */
37  
38  public abstract class AbstractInvalidationSessionTest
39  {
40      public abstract AbstractTestServer createServer(int port);
41      public abstract void pause();
42  
43      @Test
44      public void testInvalidation() throws Exception
45      {
46          Random random = new Random(System.nanoTime());
47  
48          String contextPath = "";
49          String servletMapping = "/server";
50          int port1 = random.nextInt(50000) + 10000;
51          AbstractTestServer server1 = createServer(port1);
52          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
53          server1.start();
54          try
55          {
56              int port2 = random.nextInt(50000) + 10000;
57              AbstractTestServer server2 = createServer(port2);
58              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
59              server2.start();
60              try
61              {
62                  HttpClient client = new HttpClient();
63                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
64                  client.start();
65                  try
66                  {
67                      String[] urls = new String[2];
68                      urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
69                      urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
70  
71                      // Create the session on node1
72                      ContentExchange exchange1 = new ContentExchange(true);
73                      exchange1.setMethod(HttpMethods.GET);
74                      exchange1.setURL(urls[0] + "?action=init");
75                      client.send(exchange1);
76                      exchange1.waitForDone();
77                      assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
78                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
79                      assert sessionCookie != null;
80                      // Mangle the cookie, replacing Path with $Path, etc.
81                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
82  
83                      // Be sure the session is also present in node2
84                      ContentExchange exchange2 = new ContentExchange(true);
85                      exchange2.setMethod(HttpMethods.GET);
86                      exchange2.setURL(urls[1] + "?action=increment");
87                      exchange2.getRequestFields().add("Cookie", sessionCookie);
88                      client.send(exchange2);
89                      exchange2.waitForDone();
90                      assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
91  
92                      // Invalidate on node1
93                      exchange1 = new ContentExchange(true);
94                      exchange1.setMethod(HttpMethods.GET);
95                      exchange1.setURL(urls[0] + "?action=invalidate");
96                      exchange1.getRequestFields().add("Cookie", sessionCookie);
97                      client.send(exchange1);
98                      exchange1.waitForDone();
99                      assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
100 
101                     pause();
102                     
103                     // Be sure on node2 we don't see the session anymore
104                     exchange2 = new ContentExchange(true);
105                     exchange2.setMethod(HttpMethods.GET);
106                     exchange2.setURL(urls[1] + "?action=test");
107                     exchange2.getRequestFields().add("Cookie", sessionCookie);
108                     client.send(exchange2);
109                     exchange2.waitForDone();
110                     assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
111                 }
112                 finally
113                 {
114                     client.stop();
115                 }
116             }
117             finally
118             {
119                 server2.stop();
120             }
121         }
122         finally
123         {
124             server1.stop();
125         }
126     }
127 
128     public static class TestServlet extends HttpServlet
129     {
130         @Override
131         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
132         {
133             String action = request.getParameter("action");
134             if ("init".equals(action))
135             {
136                 HttpSession session = request.getSession(true);
137                 session.setAttribute("value", 0);
138             }
139             else if ("increment".equals(action))
140             {
141                 HttpSession session = request.getSession(false);
142                 int value = (Integer)session.getAttribute("value");
143                 session.setAttribute("value", value + 1);
144             }
145             else if ("invalidate".equals(action))
146             {
147                 HttpSession session = request.getSession(false);
148                 session.invalidate();
149             }
150             else if ("test".equals(action))
151             {
152                 HttpSession session = request.getSession(false);
153                 assert session == null;
154             }
155         }
156     }
157 }