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          Random random = new Random(System.nanoTime());
46  
47          String contextA = "/contextA";
48          String contextB = "/contextB";
49          String servletMapping = "/server";
50          int port = random.nextInt(50000) + 10000;
51          AbstractTestServer server = createServer(port);
52          ServletContextHandler ctxA = server.addContext(contextA);
53          ctxA.addServlet(TestServletA.class, servletMapping);
54          ServletContextHandler ctxB = server.addContext(contextB);
55          ctxB.addServlet(TestServletB.class, servletMapping);
56          server.start();
57          try
58          {
59              HttpClient client = new HttpClient();
60              client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
61              client.start();
62              try
63              {
64                  // Perform a request to contextA
65                  ContentExchange exchangeA = new ContentExchange(true);
66                  exchangeA.setMethod(HttpMethods.GET);
67                  exchangeA.setURL("http://localhost:" + port + contextA + servletMapping);
68                  client.send(exchangeA);
69                  exchangeA.waitForDone();
70                  assertEquals(HttpServletResponse.SC_OK,exchangeA.getResponseStatus());
71                  String sessionCookie = exchangeA.getResponseFields().getStringField("Set-Cookie");
72                  assertTrue(sessionCookie != null);
73                  // Mangle the cookie, replacing Path with $Path, etc.
74                  sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
75  
76                  // Perform a request to contextB with the same session cookie
77                  ContentExchange exchangeB = new ContentExchange(true);
78                  exchangeB.setMethod(HttpMethods.GET);
79                  exchangeB.setURL("http://localhost:" + port + contextB + servletMapping);
80                  exchangeB.getRequestFields().add("Cookie", sessionCookie);
81                  client.send(exchangeB);
82                  exchangeB.waitForDone();
83                  assertEquals(HttpServletResponse.SC_OK,exchangeB.getResponseStatus());
84              }
85              finally
86              {
87                  client.stop();
88              }
89          }
90          finally
91          {
92              server.stop();
93          }
94      }
95  
96      public static class TestServletA extends HttpServlet
97      {
98          @Override
99          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
100         {
101             HttpSession session = request.getSession(false);
102             if (session == null) session = request.getSession(true);
103 
104             // Add something to the session
105             session.setAttribute("A", "A");
106 
107             // Check that we don't see things put in session by contextB
108             Object objectB = session.getAttribute("B");
109             assertTrue(objectB == null);
110             System.out.println("A: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
111         }
112     }
113 
114     public static class TestServletB extends HttpServlet
115     {
116         @Override
117         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
118         {
119             HttpSession session = request.getSession(false);
120             if (session == null) session = request.getSession(true);
121 
122             // Add something to the session
123             session.setAttribute("B", "B");
124 
125             // Check that we don't see things put in session by contextA
126             Object objectA = session.getAttribute("A");
127             assertTrue(objectA == null);
128             System.out.println("B: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
129         }
130     }
131 }