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