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