View Javadoc

1   // ========================================================================
2   // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.terracotta.servlet;
16  
17  import java.io.File;
18  import java.io.FileWriter;
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpSession;
27  
28  import org.mortbay.jetty.HttpMethods;
29  import org.mortbay.jetty.Server;
30  import org.mortbay.jetty.client.ContentExchange;
31  import org.mortbay.jetty.client.HttpClient;
32  import org.mortbay.jetty.client.HttpExchange;
33  import org.mortbay.jetty.servlet.SessionHandler;
34  import org.mortbay.jetty.webapp.WebAppContext;
35  
36  /**
37   * @version $Revision: 1216 $ $Date: 2008-09-19 09:24:33 -0500 (Fri, 19 Sep 2008) $
38   */
39  public class TIMSessionMigrationTest
40  {
41      public static void main(String[] args) throws Exception
42      {
43          new TIMSessionMigrationTest().testSessionMigration();
44      }
45  
46      public void testSessionMigration() throws Exception
47      {
48          File warDir = new File(System.getProperty("java.io.tmpdir"), "sessionMigration");
49          warDir.deleteOnExit();
50          warDir.mkdir();
51          File webInfDir = new File(warDir, "WEB-INF");
52          webInfDir.deleteOnExit();
53          webInfDir.mkdir();
54          File webXml = new File(webInfDir, "web.xml");
55          webXml.deleteOnExit();
56          String xml =
57                  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
58                  "<web-app xmlns=\"http://java.sun.com/xml/ns/j2ee\"\n" +
59                  "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
60                  "         xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\"\n" +
61                  "         version=\"2.4\">\n" +
62                  "\n" +
63                  "</web-app>";
64          FileWriter w = new FileWriter(webXml);
65          w.write(xml);
66          w.close();
67  
68          String contextPath = "/migration";
69          String servletMapping = "/server";
70          int port1 = 8080;
71          Server server1 = new Server(port1);
72          WebAppContext context1 = new WebAppContext(warDir.getCanonicalPath(), contextPath);
73          context1.setSessionHandler(new SessionHandler());
74          context1.addServlet(TestServlet.class, servletMapping);
75          server1.setHandler(context1);
76          server1.start();
77          try
78          {
79              int port2 = 8181;
80              Server server2 = new Server(port2);
81              WebAppContext context2 = new WebAppContext(warDir.getCanonicalPath(), contextPath);
82              context2.setSessionHandler(new SessionHandler());
83              context2.addServlet(TestServlet.class, servletMapping);
84              server2.setHandler(context2);
85              server2.start();
86              try
87              {
88                  HttpClient client = new HttpClient();
89                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
90                  client.start();
91                  try
92                  {
93                      // Perform one request to server1 to create a session
94                      int value = 1;
95                      ContentExchange exchange1 = new ContentExchange(true);
96                      exchange1.setMethod(HttpMethods.POST);
97                      exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=set&value=" + value);
98                      client.send(exchange1);
99                      exchange1.waitForStatus(HttpExchange.STATUS_COMPLETED);
100                     assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK : exchange1.getResponseStatus();
101                     String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
102                     System.out.println("sessionCookie = " + sessionCookie);
103 
104                     // Perform a request to server2 using the session cookie from the previous request
105                     // This should migrate the session from server1 to server2.
106                     ContentExchange exchange2 = new ContentExchange(true);
107                     exchange2.setMethod(HttpMethods.GET);
108                     exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping + "?action=get");
109                     exchange2.getRequestFields().add("Cookie", sessionCookie);
110                     client.send(exchange2);
111                     exchange2.waitForStatus(HttpExchange.STATUS_COMPLETED);
112                     assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
113                     String response = exchange2.getResponseContent();
114                     assert response.trim().equals(String.valueOf(value));
115                 }
116                 finally
117                 {
118                     client.stop();
119                 }
120             }
121             finally
122             {
123                 server2.stop();
124             }
125         }
126         finally
127         {
128             server1.stop();
129         }
130     }
131 
132     public static class TestServlet extends HttpServlet
133     {
134         @Override
135         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
136         {
137             doPost(request, response);
138         }
139 
140         @Override
141         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
142         {
143             HttpSession session = request.getSession(false);
144             if (session == null) session = request.getSession(true);
145 
146             String action = request.getParameter("action");
147             if ("set".equals(action))
148             {
149                 int value = Integer.parseInt(request.getParameter("value"));
150                 session.setAttribute("value", value);
151                 PrintWriter writer = response.getWriter();
152                 writer.println(value);
153                 writer.flush();
154             }
155             else if ("get".equals(action))
156             {
157                 int value = (Integer)session.getAttribute("value");
158                 PrintWriter writer = response.getWriter();
159                 writer.println(value);
160                 writer.flush();
161             }
162         }
163     }
164 }