View Javadoc

1   // ========================================================================
2   // Copyright 2004-2010 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.Random;
18  
19  import javax.servlet.ServletException;
20  import javax.servlet.http.HttpServlet;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import org.eclipse.jetty.client.ContentExchange;
26  import org.eclipse.jetty.client.HttpClient;
27  import org.eclipse.jetty.http.HttpMethods;
28  import org.junit.Test;
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  
33  /**
34   * AbstractReentrantRequestSessionTest
35   */
36  public abstract class AbstractReentrantRequestSessionTest
37  {
38      public abstract AbstractTestServer createServer(int port);
39  
40      @Test
41      public void testReentrantRequestSession() throws Exception
42      {
43          String contextPath = "";
44          String servletMapping = "/server";
45          AbstractTestServer server = createServer(0);
46          server.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
47          server.start();
48          int port = server.getPort();
49          try
50          {
51              HttpClient client = new HttpClient();
52              client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
53              client.start();
54              try
55              {
56                  ContentExchange exchange = new ContentExchange(true);
57                  exchange.setMethod(HttpMethods.GET);
58                  exchange.setURL("http://localhost:" + port + contextPath + servletMapping + "?action=reenter&port=" + port + "&path=" + contextPath + servletMapping);
59                  client.send(exchange);
60                  exchange.waitForDone();
61                  assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
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 with a different session
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.setConnectorType(HttpClient.CONNECTOR_SOCKET);
102                     client.start();
103                     try
104                     {
105                         ContentExchange exchange = new ContentExchange(true);
106                         exchange.setMethod(HttpMethods.GET);
107                         exchange.setURL("http://localhost:" + port + path + ";jsessionid="+session.getId()+"?action=none");
108                         client.send(exchange);
109                         exchange.waitForDone();
110                         assertEquals(HttpServletResponse.SC_OK,exchange.getResponseStatus());
111                         assertEquals("true",session.getAttribute("reentrant"));
112                     }
113                     finally
114                     {
115                         client.stop();
116                     }
117                 }
118                 catch (Exception x)
119                 {
120                     throw new ServletException(x);
121                 }
122             }
123             else
124             {
125                 assertTrue(session!=null);
126                 session.setAttribute("reentrant","true");
127             }
128         }
129     }
130 }