View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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 static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.Collections;
26  
27  import javax.servlet.RequestDispatcher;
28  import javax.servlet.ServletContext;
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServlet;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import javax.servlet.http.HttpSession;
34  
35  import org.eclipse.jetty.client.HttpClient;
36  import org.eclipse.jetty.client.api.ContentResponse;
37  import org.eclipse.jetty.servlet.ServletContextHandler;
38  import org.junit.Test;
39  
40  /**
41   * AbstractServerCrossContextSessionTest
42   */
43  public abstract class AbstractServerCrossContextSessionTest
44  {
45  
46      public abstract AbstractTestServer createServer(int port);
47  
48      @Test
49      public void testCrossContextDispatch() throws Exception
50      {
51          String contextA = "/contextA";
52          String contextB = "/contextB";
53          String servletMapping = "/server";
54          AbstractTestServer server = createServer(0);
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          try
60          {
61              server.start();
62              int port=server.getPort();
63  
64              HttpClient client = new HttpClient();
65              client.start();
66              try
67              {
68                  // Perform a request, on server side a cross context dispatch will be done
69                  ContentResponse response = client.GET("http://localhost:" + port + contextA + servletMapping);
70                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
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 }