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