View Javadoc

1   // ========================================================================
2   // Copyright 2004-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  package org.eclipse.jetty.server.session;
14  
15  import java.io.IOException;
16  import java.util.Collections;
17  import java.util.Random;
18  
19  import javax.servlet.ServletException;
20  import javax.servlet.http.HttpServlet;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import org.eclipse.jetty.client.ContentExchange;
26  import org.eclipse.jetty.client.HttpClient;
27  import org.eclipse.jetty.http.HttpMethods;
28  import org.eclipse.jetty.servlet.ServletContextHandler;
29  import org.junit.Test;
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  
34  /**
35   * AbstractClientCrossContextSessionTest
36   */
37  public abstract class AbstractClientCrossContextSessionTest
38  {
39  
40      public abstract AbstractTestServer createServer(int port);
41  
42      @Test
43      public void testCrossContextDispatch() throws Exception
44      {
45          String contextA = "/contextA";
46          String contextB = "/contextB";
47          String servletMapping = "/server";
48          AbstractTestServer server = createServer(0);
49          ServletContextHandler ctxA = server.addContext(contextA);
50          ctxA.addServlet(TestServletA.class, servletMapping);
51          ServletContextHandler ctxB = server.addContext(contextB);
52          ctxB.addServlet(TestServletB.class, servletMapping);
53          server.start();
54          int port = server.getPort();
55          
56          try
57          {
58              HttpClient client = new HttpClient();
59              client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
60              client.start();
61              try
62              {
63                  // Perform a request to contextA
64                  ContentExchange exchangeA = new ContentExchange(true);
65                  exchangeA.setMethod(HttpMethods.GET);
66                  exchangeA.setURL("http://localhost:" + port + contextA + servletMapping);
67                  client.send(exchangeA);
68                  exchangeA.waitForDone();
69                  assertEquals(HttpServletResponse.SC_OK,exchangeA.getResponseStatus());
70                  String sessionCookie = exchangeA.getResponseFields().getStringField("Set-Cookie");
71                  assertTrue(sessionCookie != null);
72                  // Mangle the cookie, replacing Path with $Path, etc.
73                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
74  
75                  // Perform a request to contextB with the same session cookie
76                  ContentExchange exchangeB = new ContentExchange(true);
77                  exchangeB.setMethod(HttpMethods.GET);
78                  exchangeB.setURL("http://localhost:" + port + contextB + servletMapping);
79                  exchangeB.getRequestFields().add("Cookie", sessionCookie);
80                  client.send(exchangeB);
81                  exchangeB.waitForDone();
82                  assertEquals(HttpServletResponse.SC_OK,exchangeB.getResponseStatus());
83              }
84              finally
85              {
86                  client.stop();
87              }
88          }
89          finally
90          {
91              server.stop();
92          }
93      }
94  
95      public static class TestServletA extends HttpServlet
96      {
97          @Override
98          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
99          {
100             HttpSession session = request.getSession(false);
101             if (session == null) session = request.getSession(true);
102 
103             // Add something to the session
104             session.setAttribute("A", "A");
105 
106             // Check that we don't see things put in session by contextB
107             Object objectB = session.getAttribute("B");
108             assertTrue(objectB == null);
109             System.out.println("A: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
110         }
111     }
112 
113     public static class TestServletB extends HttpServlet
114     {
115         @Override
116         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
117         {
118             HttpSession session = request.getSession(false);
119             if (session == null) session = request.getSession(true);
120 
121             // Add something to the session
122             session.setAttribute("B", "B");
123 
124             // Check that we don't see things put in session by contextA
125             Object objectA = session.getAttribute("A");
126             assertTrue(objectA == null);
127             System.out.println("B: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
128         }
129     }
130 }