View Javadoc

1   // ========================================================================
2   // Copyright 2004-2008 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  
14  package org.eclipse.jetty.server.session;
15  
16  import java.io.IOException;
17  import java.util.Collections;
18  import java.util.Random;
19  
20  import javax.servlet.RequestDispatcher;
21  import javax.servlet.ServletContext;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpSession;
27  
28  import org.eclipse.jetty.http.HttpMethods;
29  import org.eclipse.jetty.servlet.ServletContextHandler;
30  import org.eclipse.jetty.client.ContentExchange;
31  import org.eclipse.jetty.client.HttpClient;
32  import org.testng.annotations.Test;
33  
34  /**
35   * AbstractServerCrossContextSessionTest
36   *
37   *
38   */
39  
40  public abstract class AbstractServerCrossContextSessionTest
41  {
42  
43      public abstract AbstractTestServer createServer(int port);
44      
45      @Test
46      public void testCrossContextDispatch() throws Exception
47      {
48          Random random = new Random(System.nanoTime());
49  
50          String contextA = "/contextA";
51          String contextB = "/contextB";
52          String servletMapping = "/server";
53          int port = random.nextInt(50000) + 10000;
54          AbstractTestServer server = createServer(port);
55          ServletContextHandler ctxA = server.addContext(contextA);
56          ctxA.addServlet(TestServletA.class, servletMapping);
57          ServletContextHandler ctxB = server.addContext(contextB);
58          ctxB.addServlet(TestServletB.class, servletMapping);
59          server.start();
60          try
61          {
62              HttpClient client = new HttpClient();
63              client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
64              client.start();
65              try
66              {
67                  // Perform a request, on server side a cross context dispatch will be done
68                  ContentExchange exchange = new ContentExchange(true);
69                  exchange.setMethod(HttpMethods.GET);
70                  exchange.setURL("http://localhost:" + port + contextA + servletMapping);
71                  client.send(exchange);
72                  exchange.waitForDone();
73                  assert exchange.getResponseStatus() == HttpServletResponse.SC_OK;
74              }
75              finally
76              {
77                  client.stop();
78              }
79          }
80          finally
81          {
82              server.stop();
83          }
84      }
85  
86      public static class TestServletA extends HttpServlet
87      {
88          @Override
89          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
90          {
91              HttpSession session = request.getSession(false);
92              if (session == null) session = request.getSession(true);
93  
94              // Add something to the session
95              session.setAttribute("A", "A");
96              System.out.println("A: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
97  
98              // Perform cross context dispatch to another context
99              // Over there we will check that the session attribute added above is not visible
100             ServletContext contextB = getServletContext().getContext("/contextB");
101             RequestDispatcher dispatcherB = contextB.getRequestDispatcher(request.getServletPath());
102             dispatcherB.forward(request, response);
103 
104             // Check that we don't see things put in session by contextB
105             Object objectB = session.getAttribute("B");
106             assert objectB == null;
107             System.out.println("A: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
108         }
109     }
110 
111     public static class TestServletB extends HttpServlet
112     {
113         @Override
114         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
115         {
116             HttpSession session = request.getSession(false);
117             if (session == null) session = request.getSession(true);
118 
119             // Be sure nothing from contextA is present
120             Object objectA = session.getAttribute("A");
121             assert objectA == null;
122 
123             // Add something, so in contextA we can check if it is visible (it must not).
124             session.setAttribute("B", "B");
125             System.out.println("B: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
126         }
127     }
128 }