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 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().getStringField("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                      // Be sure the session is also present in node2
88  
89                      Request request2 = client.newRequest(urls[1] + "?action=increment");
90                      request2.header("Cookie", sessionCookie);
91                      ContentResponse response2 = request2.send();
92                      assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
93  
94                      // Invalidate on node1
95                      Request request1 = client.newRequest(urls[0] + "?action=invalidate");
96                      request1.header("Cookie", sessionCookie);
97                      response1 = request1.send();
98                      assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
99  
100                     pause();
101 
102                     // Be sure on node2 we don't see the session anymore
103                     request2 = client.newRequest(urls[1] + "?action=test");
104                     request2.header("Cookie", sessionCookie);
105                     response2 = request2.send();
106                     assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
107                 }
108                 finally
109                 {
110                     client.stop();
111                 }
112             }
113             finally
114             {
115                 server2.stop();
116             }
117         }
118         finally
119         {
120             server1.stop();
121         }
122     }
123 
124     public static class TestServlet extends HttpServlet
125     {
126         @Override
127         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
128         {
129             String action = request.getParameter("action");
130             if ("init".equals(action))
131             {
132                 HttpSession session = request.getSession(true);
133                 session.setAttribute("value", 0);
134             }
135             else if ("increment".equals(action))
136             {
137                 HttpSession session = request.getSession(false);
138                 int value = (Integer)session.getAttribute("value");
139                 session.setAttribute("value", value + 1);
140             }
141             else if ("invalidate".equals(action))
142             {
143                 HttpSession session = request.getSession(false);
144                 session.invalidate();
145             }
146             else if ("test".equals(action))
147             {
148                 HttpSession session = request.getSession(false);
149                 assertEquals(null,session);
150             }
151         }
152     }
153 }