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