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          String contextPath = "";
46          String servletMapping = "/server";
47          AbstractTestServer server1 = createServer(0);
48          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
49          server1.start();
50          int port1 = server1.getPort();
51          try
52          {
53              AbstractTestServer server2 = createServer(0);
54              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
55              server2.start();
56              int port2=server2.getPort();
57              try
58              {
59                  HttpClient client = new HttpClient();
60                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
61                  client.start();
62                  try
63                  {
64                      String[] urls = new String[2];
65                      urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
66                      urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
67  
68                      // Create the session on node1
69                      ContentExchange exchange1 = new ContentExchange(true);
70                      exchange1.setMethod(HttpMethods.GET);
71                      exchange1.setURL(urls[0] + "?action=init");
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                      // Be sure the session is also present in node2
81                      ContentExchange exchange2 = new ContentExchange(true);
82                      exchange2.setMethod(HttpMethods.GET);
83                      exchange2.setURL(urls[1] + "?action=increment");
84                      exchange2.getRequestFields().add("Cookie", sessionCookie);
85                      client.send(exchange2);
86                      exchange2.waitForDone();
87                      assertEquals(HttpServletResponse.SC_OK,exchange2.getResponseStatus());
88  
89                      // Invalidate on node1
90                      exchange1 = new ContentExchange(true);
91                      exchange1.setMethod(HttpMethods.GET);
92                      exchange1.setURL(urls[0] + "?action=invalidate");
93                      exchange1.getRequestFields().add("Cookie", sessionCookie);
94                      client.send(exchange1);
95                      exchange1.waitForDone();
96                      assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
97  
98                      pause();
99                      
100                     // Be sure on node2 we don't see the session anymore
101                     exchange2 = new ContentExchange(true);
102                     exchange2.setMethod(HttpMethods.GET);
103                     exchange2.setURL(urls[1] + "?action=test");
104                     exchange2.getRequestFields().add("Cookie", sessionCookie);
105                     client.send(exchange2);
106                     exchange2.waitForDone();
107                     assertEquals(HttpServletResponse.SC_OK,exchange2.getResponseStatus());
108                 }
109                 finally
110                 {
111                     client.stop();
112                 }
113             }
114             finally
115             {
116                 server2.stop();
117             }
118         }
119         finally
120         {
121             server1.stop();
122         }
123     }
124 
125     public static class TestServlet extends HttpServlet
126     {
127         @Override
128         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
129         {
130             String action = request.getParameter("action");
131             if ("init".equals(action))
132             {
133                 HttpSession session = request.getSession(true);
134                 session.setAttribute("value", 0);
135             }
136             else if ("increment".equals(action))
137             {
138                 HttpSession session = request.getSession(false);
139                 int value = (Integer)session.getAttribute("value");
140                 session.setAttribute("value", value + 1);
141             }
142             else if ("invalidate".equals(action))
143             {
144                 HttpSession session = request.getSession(false);
145                 session.invalidate();
146             }
147             else if ("test".equals(action))
148             {
149                 HttpSession session = request.getSession(false);
150                 assertEquals(null,session);
151             }
152         }
153     }
154 }