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  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpSession;
31  
32  import org.eclipse.jetty.client.HttpClient;
33  import org.eclipse.jetty.client.api.ContentResponse;
34  import org.junit.Test;
35  
36  
37  /**
38   * AbstractReentrantRequestSessionTest
39   */
40  public abstract class AbstractReentrantRequestSessionTest
41  {
42      public abstract AbstractTestServer createServer(int port);
43  
44      @Test
45      public void testReentrantRequestSession() throws Exception
46      {
47          String contextPath = "";
48          String servletMapping = "/server";
49          AbstractTestServer server = createServer(0);
50          server.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
51          try
52          {
53              server.start();
54              int port = server.getPort();
55  
56              HttpClient client = new HttpClient();
57              client.start();
58              try
59              {
60                  ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=reenter&port=" + port + "&path=" + contextPath + servletMapping);
61                  assertEquals(HttpServletResponse.SC_OK,response.getStatus());
62              }
63              finally
64              {
65                  client.stop();
66              }
67          }
68          finally
69          {
70              server.stop();
71          }
72      }
73  
74      public static class TestServlet extends HttpServlet
75      {
76          @Override
77          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
78          {
79              doPost(request, response);
80          }
81  
82          @Override
83          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
84          {
85              HttpSession session = request.getSession(false);
86  
87              String action = request.getParameter("action");
88              if ("reenter".equals(action))
89              {
90                  if (session == null)
91                      session = request.getSession(true);
92                  int port = Integer.parseInt(request.getParameter("port"));
93                  String path = request.getParameter("path");
94  
95                  // We want to make another request
96                  // while this request is still pending, to see if the locking is
97                  // fine grained (per session at least).
98                  try
99                  {
100                     HttpClient client = new HttpClient();
101                     client.start();
102                     try
103                     {
104                         ContentResponse resp = client.GET("http://localhost:" + port + path + ";jsessionid="+session.getId()+"?action=none");
105                         assertEquals(HttpServletResponse.SC_OK,resp.getStatus());
106                         assertEquals("true",session.getAttribute("reentrant"));
107                     }
108                     finally
109                     {
110                         client.stop();
111                     }
112                 }
113                 catch (Exception x)
114                 {
115                     throw new ServletException(x);
116                 }
117             }
118             else
119             {
120                 assertTrue(session!=null);
121                 session.setAttribute("reentrant","true");
122             }
123         }
124     }
125 }