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