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