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.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  
64          try
65          {
66              server.start();
67              int port = server.getPort();
68              
69              HttpClient client = new HttpClient();
70              client.start();
71              try
72              {
73                  // Perform a request to contextA
74                  ContentResponse response = client.GET("http://localhost:" + port + contextA + servletMapping);
75  
76                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
77                  String sessionCookie = response.getHeaders().get("Set-Cookie");
78                  assertTrue(sessionCookie != null);
79                  // Mangle the cookie, replacing Path with $Path, etc.
80                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
81  
82                  // Perform a request to contextB with the same session cookie
83                  Request request = client.newRequest("http://localhost:" + port + contextB + servletMapping);
84                  request.header("Cookie", sessionCookie);
85                  ContentResponse responseB = request.send();
86                  assertEquals(HttpServletResponse.SC_OK,responseB.getStatus());
87                  assertEquals(servletA.sessionId, servletB.sessionId);
88              }
89              finally
90              {
91                  client.stop();
92              }
93          }
94          finally
95          {
96              server.stop();
97          }
98      }
99  
100     public static class TestServletA extends HttpServlet
101     {
102         public String sessionId;
103 
104         @Override
105         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
106         {
107             HttpSession session = request.getSession(false);
108             if (session == null)
109             {
110                 session = request.getSession(true);
111                 sessionId = session.getId();
112             }
113 
114             // Add something to the session
115             session.setAttribute("A", "A");
116 
117             // Check that we don't see things put in session by contextB
118             Object objectB = session.getAttribute("B");
119             assertTrue(objectB == null);
120         }
121     }
122 
123     public static class TestServletB extends HttpServlet
124     {
125         public String sessionId;
126 
127         @Override
128         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
129         {
130             HttpSession session = request.getSession(false);
131             if (session == null)
132                 session = request.getSession(true);
133 
134             sessionId = session.getId();
135 
136 
137 
138             // Add something to the session
139             session.setAttribute("B", "B");
140 
141             // Check that we don't see things put in session by contextA
142             Object objectA = session.getAttribute("A");
143             assertTrue(objectA == null);
144         }
145     }
146 }