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.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNotSame;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.io.IOException;
29  
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServlet;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.http.HttpSession;
35  import javax.servlet.http.HttpSessionEvent;
36  import javax.servlet.http.HttpSessionIdListener;
37  
38  import org.eclipse.jetty.client.HttpClient;
39  import org.eclipse.jetty.client.api.ContentResponse;
40  import org.eclipse.jetty.client.api.Request;
41  import org.eclipse.jetty.servlet.ServletContextHandler;
42  import org.eclipse.jetty.webapp.WebAppContext;
43  
44  
45  public abstract class AbstractSessionRenewTest
46  {
47      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
48  
49      public void testSessionRenewal() throws Exception
50      {
51          String contextPath = "";
52          String servletMapping = "/server";
53          int scavengePeriod = 3;
54          AbstractTestServer server = createServer(0, 1, scavengePeriod);
55          WebAppContext context = server.addWebAppContext(".", contextPath);
56          context.addServlet(TestServlet.class, servletMapping);
57          TestHttpSessionIdListener testListener = new TestHttpSessionIdListener();
58          context.addEventListener(testListener);
59          
60  
61  
62          HttpClient client = new HttpClient();
63          try
64          {
65              server.start();
66              int port=server.getPort();
67              
68              client.start();
69  
70              //make a request to create a session
71              ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
72              assertEquals(HttpServletResponse.SC_OK,response.getStatus());
73  
74              String sessionCookie = response.getHeaders().getStringField("Set-Cookie");
75              assertTrue(sessionCookie != null);
76              assertFalse(testListener.isCalled());
77  
78              //make a request to change the sessionid
79              Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=renew");
80              request.header("Cookie", sessionCookie);
81              ContentResponse renewResponse = request.send();
82              assertEquals(HttpServletResponse.SC_OK,renewResponse.getStatus());
83              String renewSessionCookie = renewResponse.getHeaders().getStringField("Set-Cookie");
84              assertNotNull(renewSessionCookie);
85              assertNotSame(sessionCookie, renewSessionCookie);
86              assertTrue(testListener.isCalled());
87          }
88          finally
89          {
90              client.stop();
91              server.stop();
92          }
93      }
94  
95      
96      
97      public static class TestHttpSessionIdListener implements HttpSessionIdListener
98      {
99          boolean called = false;
100         
101         @Override
102         public void sessionIdChanged(HttpSessionEvent event, String oldSessionId)
103         {
104             assertNotNull(event.getSession());
105             assertNotSame(oldSessionId, event.getSession().getId());
106             called = true;
107         }
108         
109         public boolean isCalled()
110         {
111             return called;
112         }
113     }
114 
115 
116     public static class TestServlet extends HttpServlet
117     {
118         
119         @Override
120         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
121         {
122             String action = request.getParameter("action");
123             if ("create".equals(action))
124             {
125                 HttpSession session = request.getSession(true);
126                 assertTrue(session.isNew());
127             }
128             else if ("renew".equals(action))
129             {
130                 HttpSession beforeSession = request.getSession(false);
131                 assertTrue(beforeSession != null);
132                 String beforeSessionId = beforeSession.getId();
133 
134 
135                 ((AbstractSession)beforeSession).renewId(request);
136 
137                 HttpSession afterSession = request.getSession(false);
138                 assertTrue(afterSession != null);
139                 String afterSessionId = afterSession.getId();
140 
141                 assertTrue(beforeSession==afterSession);
142                 assertFalse(beforeSessionId.equals(afterSessionId));
143 
144                 AbstractSessionManager sessionManager = (AbstractSessionManager)((AbstractSession)afterSession).getSessionManager();
145                 AbstractSessionIdManager sessionIdManager = (AbstractSessionIdManager)sessionManager.getSessionIdManager();
146 
147                 assertTrue(sessionIdManager.idInUse(afterSessionId));
148                 assertFalse(sessionIdManager.idInUse(beforeSessionId));
149 
150                 HttpSession session = sessionManager.getSession(afterSessionId);
151                 assertNotNull(session);
152                 session = sessionManager.getSession(beforeSessionId);
153                 assertNull(session);
154 
155                 if (((AbstractSession)afterSession).isIdChanged())
156                 {
157                     ((org.eclipse.jetty.server.Response)response).addCookie(sessionManager.getSessionCookie(afterSession, request.getContextPath(), request.isSecure()));
158                 }
159             }
160         }
161     }
162 
163 }