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.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.IOException;
27  
28  import javax.servlet.RequestDispatcher;
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServlet;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import javax.servlet.http.HttpSession;
34  
35  import org.eclipse.jetty.client.HttpClient;
36  import org.eclipse.jetty.client.api.ContentResponse;
37  import org.eclipse.jetty.client.api.Request;
38  import org.eclipse.jetty.servlet.ServletContextHandler;
39  import org.junit.Test;
40  
41  
42  /**
43   * AbstractForwardedSessionTest
44   *
45   * Test that creating a session inside a forward on the same context works, and that
46   * attributes set after the forward returns are preserved.
47   * 
48   * This test requires that the sessions will be persisted, as the server is stopped and
49   * then restarted in order to check that all the attributes were saved.
50   */
51  public abstract class AbstractForwardedSessionTest extends AbstractTestBase
52  {
53  
54      
55      @Test
56      public void testSessionCreateInForward() throws Exception
57      {
58          AbstractTestServer testServer = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE,  AbstractTestServer.DEFAULT_SCAVENGE_SEC,  AbstractTestServer.DEFAULT_EVICTIONPOLICY);
59          ServletContextHandler testServletContextHandler = testServer.addContext("/context");
60          testServletContextHandler.addServlet(Servlet1.class, "/one");
61          testServletContextHandler.addServlet(Servlet2.class, "/two");
62          testServletContextHandler.addServlet(Servlet3.class, "/three");
63          testServletContextHandler.addServlet(Servlet4.class, "/four");
64         
65        
66  
67          try
68          {
69              testServer.start();
70              int serverPort=testServer.getPort();
71              HttpClient client = new HttpClient();
72              client.start();
73              try
74              {
75                  //make a request to the first servlet, which will forward it to other servlets
76                  ContentResponse response = client.GET("http://localhost:" + serverPort + "/context/one");
77                  assertEquals(HttpServletResponse.SC_OK, response.getStatus());
78                  String sessionCookie = response.getHeaders().get("Set-Cookie");
79                  assertTrue(sessionCookie != null);
80                  // Mangle the cookie, replacing Path with $Path, etc.
81                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
82  
83                  //test that the session was created, and that it contains the attributes from servlet3 and servlet1
84                  
85                  //stop the server, to make sure any session persistence has happened
86                  testServer.stop();
87                              
88                  //restart
89                  testServer.start();
90                  serverPort = testServer.getPort();
91         
92                  //Make a fresh request
93                  Request request = client.newRequest("http://localhost:" + serverPort + "/context/four");
94                  request.header("Cookie", sessionCookie);
95                  response = request.send();
96                  assertEquals(HttpServletResponse.SC_OK, response.getStatus());
97                  
98              }
99              finally
100             {
101                 client.stop();
102             }
103         }
104         finally
105         {
106             testServer.stop();
107         }
108         
109     }
110     
111 
112     public static class Servlet1 extends HttpServlet
113     {
114         @Override
115         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
116         {
117             //Don't create a session, just forward to another session in the same context
118             assertNull(request.getSession(false));
119             
120             //The session will be created by the other servlet, so will exist as this dispatch returns
121             RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/two");
122             dispatcher.forward(request, response);
123    
124             HttpSession sess = request.getSession(false);
125             assertNotNull(sess);
126             assertNotNull(sess.getAttribute("servlet3"));
127             sess.setAttribute("servlet1", "servlet1");
128         }
129     }
130 
131     public static class Servlet2 extends HttpServlet
132     {
133         @Override
134         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
135         {
136             //forward to yet another servlet to do the creation
137             assertNull(request.getSession(false));
138 
139             RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/three");
140             dispatcher.forward(request, response);
141             
142             //the session should exist after the forward
143             HttpSession sess = request.getSession(false);
144             assertNotNull(sess);
145             assertNotNull(sess.getAttribute("servlet3"));
146         }
147     }
148 
149 
150 
151     public static class Servlet3 extends HttpServlet
152     {
153         @Override
154         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
155         {    
156             //No session yet
157             assertNull(request.getSession(false));
158             
159             //Create it
160             HttpSession session = request.getSession();
161             assertNotNull(session);
162             
163             //Set an attribute on it
164             session.setAttribute("servlet3", "servlet3");
165         }
166     }
167     
168     
169     public static class Servlet4 extends HttpServlet
170     {
171         @Override
172         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
173         {    
174             //Check that the session contains attributes set during and after the session forward
175             HttpSession session = request.getSession();
176             assertNotNull(session);
177             assertNotNull(session.getAttribute("servlet1"));
178             assertNotNull(session.getAttribute("servlet3"));
179         }
180     }
181     
182 }