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.servlet.ServletContextHandler;
36  import org.eclipse.jetty.servlet.ServletHolder;
37  import org.junit.Test;
38  
39  
40  /**
41   * AbstractClientCrossContextSessionTest
42   */
43  public abstract class AbstractClientCrossContextSessionTest
44  {
45  
46      public abstract AbstractTestServer createServer(int port);
47  
48      @Test
49      public void testCrossContextDispatch() throws Exception
50      {
51          String contextA = "/contextA";
52          String contextB = "/contextB";
53          String servletMapping = "/server";
54          AbstractTestServer server = createServer(0);
55          TestServletA servletA = new TestServletA();
56          ServletHolder holderA = new ServletHolder(servletA);
57          ServletContextHandler ctxA = server.addContext(contextA);
58          ctxA.addServlet(holderA, servletMapping);
59          ServletContextHandler ctxB = server.addContext(contextB);
60          TestServletB servletB = new TestServletB();
61          ServletHolder holderB = new ServletHolder(servletB);
62          ctxB.addServlet(holderB, servletMapping);
63          server.start();
64          int port = server.getPort();
65  
66          try
67          {
68              HttpClient client = new HttpClient();
69              client.start();
70              try
71              {
72                  // Perform a request to contextA
73                  ContentResponse response = client.GET("http://localhost:" + port + contextA + servletMapping);
74  
75                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
76                  String sessionCookie = response.getHeaders().getStringField("Set-Cookie");
77                  assertTrue(sessionCookie != null);
78                  // Mangle the cookie, replacing Path with $Path, etc.
79                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
80  
81                  // Perform a request to contextB with the same session cookie
82                  Request request = client.newRequest("http://localhost:" + port + contextB + servletMapping);
83                  request.header("Cookie", sessionCookie);
84                  ContentResponse responseB = request.send();
85                  assertEquals(HttpServletResponse.SC_OK,responseB.getStatus());
86                  assertEquals(servletA.sessionId, servletB.sessionId);
87              }
88              finally
89              {
90                  client.stop();
91              }
92          }
93          finally
94          {
95              server.stop();
96          }
97      }
98  
99      public static class TestServletA extends HttpServlet
100     {
101         public String sessionId;
102 
103         @Override
104         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
105         {
106             HttpSession session = request.getSession(false);
107             if (session == null)
108             {
109                 session = request.getSession(true);
110                 sessionId = session.getId();
111             }
112 
113             // Add something to the session
114             session.setAttribute("A", "A");
115 
116             // Check that we don't see things put in session by contextB
117             Object objectB = session.getAttribute("B");
118             assertTrue(objectB == null);
119         }
120     }
121 
122     public static class TestServletB extends HttpServlet
123     {
124         public String sessionId;
125 
126         @Override
127         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
128         {
129             HttpSession session = request.getSession(false);
130             if (session == null)
131                 session = request.getSession(true);
132 
133             sessionId = session.getId();
134 
135 
136 
137             // Add something to the session
138             session.setAttribute("B", "B");
139 
140             // Check that we don't see things put in session by contextA
141             Object objectA = session.getAttribute("A");
142             assertTrue(objectA == null);
143         }
144     }
145 }