View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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 static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpSession;
31  
32  import org.eclipse.jetty.client.HttpClient;
33  import org.eclipse.jetty.client.api.ContentResponse;
34  import org.eclipse.jetty.client.api.Request;
35  import org.eclipse.jetty.util.thread.QueuedThreadPool;
36  import org.junit.Test;
37  
38  /**
39   * AbstractInvalidationSessionTest
40   * Goal of the test is to be sure that invalidating a session on one node
41   * result in the session being unavailable in the other node also.
42   */
43  public abstract class AbstractInvalidationSessionTest
44  {
45      public abstract AbstractTestServer createServer(int port);
46      public abstract void pause();
47  
48      @Test
49      public void testInvalidation() throws Exception
50      {
51          String contextPath = "";
52          String servletMapping = "/server";
53          AbstractTestServer server1 = createServer(0);
54          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
55  
56  
57          try
58          {
59              server1.start();
60              int port1 = server1.getPort();
61              AbstractTestServer server2 = createServer(0);
62              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
63  
64              try
65              {
66                  server2.start();
67                  int port2=server2.getPort();
68                  HttpClient client = new HttpClient();
69                  QueuedThreadPool executor = new QueuedThreadPool();
70                  client.setExecutor(executor);
71                  client.start();
72                  try
73                  {
74                      String[] urls = new String[2];
75                      urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
76                      urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
77  
78                      // Create the session on node1
79                      ContentResponse response1 = client.GET(urls[0] + "?action=init");
80  
81                      assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
82                      String sessionCookie = response1.getHeaders().get("Set-Cookie");
83                      assertTrue(sessionCookie != null);
84                      // Mangle the cookie, replacing Path with $Path, etc.
85                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
86                      
87                      
88                      // Be sure the session is also present in node2
89  
90                      Request request2 = client.newRequest(urls[1] + "?action=increment");
91                      request2.header("Cookie", sessionCookie);
92                      ContentResponse response2 = request2.send();
93                      assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
94   
95  
96                      // Invalidate on node1
97                      Request request1 = client.newRequest(urls[0] + "?action=invalidate");
98                      request1.header("Cookie", sessionCookie);
99                      response1 = request1.send();
100                     assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
101            
102 
103                     pause();
104 
105                     // Be sure on node2 we don't see the session anymore
106                     request2 = client.newRequest(urls[1] + "?action=test");
107                     request2.header("Cookie", sessionCookie);
108                     response2 = request2.send();
109                     assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
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 }