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