1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
101
102
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 }