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