View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server.session;
20  
21  import java.io.IOException;
22  import java.util.Collections;
23  import java.util.Random;
24  
25  import javax.servlet.RequestDispatcher;
26  import javax.servlet.ServletContext;
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpSession;
32  
33  import org.eclipse.jetty.client.ContentExchange;
34  import org.eclipse.jetty.client.HttpClient;
35  import org.eclipse.jetty.http.HttpMethods;
36  import org.eclipse.jetty.servlet.ServletContextHandler;
37  import org.junit.Test;
38  import static org.junit.Assert.assertEquals;
39  import static org.junit.Assert.assertTrue;
40  
41  /**
42   * AbstractServerCrossContextSessionTest
43   */
44  public abstract class AbstractServerCrossContextSessionTest
45  {
46  
47      public abstract AbstractTestServer createServer(int port);
48      
49      @Test
50      public void testCrossContextDispatch() throws Exception
51      {
52          String contextA = "/contextA";
53          String contextB = "/contextB";
54          String servletMapping = "/server";
55          AbstractTestServer server = createServer(0);
56          ServletContextHandler ctxA = server.addContext(contextA);
57          ctxA.addServlet(TestServletA.class, servletMapping);
58          ServletContextHandler ctxB = server.addContext(contextB);
59          ctxB.addServlet(TestServletB.class, servletMapping);
60          server.start();
61          int port=server.getPort();
62          try
63          {
64              HttpClient client = new HttpClient();
65              client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
66              client.start();
67              try
68              {
69                  // Perform a request, on server side a cross context dispatch will be done
70                  ContentExchange exchange = new ContentExchange(true);
71                  exchange.setMethod(HttpMethods.GET);
72                  exchange.setURL("http://localhost:" + port + contextA + servletMapping);
73                  client.send(exchange);
74                  exchange.waitForDone();
75                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
76              }
77              finally
78              {
79                  client.stop();
80              }
81          }
82          finally
83          {
84              server.stop();
85          }
86      }
87  
88      public static class TestServletA extends HttpServlet
89      {
90          @Override
91          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
92          {
93              HttpSession session = request.getSession(false);
94              if (session == null) session = request.getSession(true);
95  
96              // Add something to the session
97              session.setAttribute("A", "A");
98              System.out.println("A: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
99  
100             // Perform cross context dispatch to another context
101             // Over there we will check that the session attribute added above is not visible
102             ServletContext contextB = getServletContext().getContext("/contextB");
103             RequestDispatcher dispatcherB = contextB.getRequestDispatcher(request.getServletPath());
104             dispatcherB.forward(request, response);
105 
106             // Check that we don't see things put in session by contextB
107             Object objectB = session.getAttribute("B");
108             assertTrue(objectB == null);
109             System.out.println("A: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
110         }
111     }
112 
113     public static class TestServletB extends HttpServlet
114     {
115         @Override
116         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
117         {
118             HttpSession session = request.getSession(false);
119             if (session == null) session = request.getSession(true);
120 
121             // Be sure nothing from contextA is present
122             Object objectA = session.getAttribute("A");
123             assertTrue(objectA == null);
124 
125             // Add something, so in contextA we can check if it is visible (it must not).
126             session.setAttribute("B", "B");
127             System.out.println("B: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
128         }
129     }
130 }