View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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.File;
25  import java.io.FileOutputStream;
26  import java.io.FileWriter;
27  import java.util.concurrent.Future;
28  
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.eclipse.jetty.client.HttpClient;
32  import org.eclipse.jetty.client.api.ContentResponse;
33  import org.eclipse.jetty.client.api.Request;
34  import org.eclipse.jetty.http.HttpMethod;
35  import org.eclipse.jetty.util.IO;
36  import org.eclipse.jetty.util.resource.Resource;
37  import org.junit.Test;
38  
39  /**
40   * AbstractWebAppObjectInSessionTest
41   *
42   * Target of this test is to check that when a webapp on nodeA puts in the session
43   * an object of a class loaded from the war (and hence with a WebAppClassLoader),
44   * the same webapp on nodeB is able to load that object from the session.
45   *
46   * This test is only appropriate for clustered session managers.
47   */
48  public abstract class AbstractWebAppObjectInSessionTest
49  {
50      public abstract AbstractTestServer createServer(int port);
51  
52      @Test
53      public void testWebappObjectInSession() throws Exception
54      {
55          String contextName = "webappObjectInSessionTest";
56          String contextPath = "/" + contextName;
57          String servletMapping = "/server";
58  
59          File targetDir = new File(System.getProperty("basedir"), "target");
60          File warDir = new File(targetDir, contextName);
61          warDir.mkdir();
62          File webInfDir = new File(warDir, "WEB-INF");
63          webInfDir.mkdir();
64          // Write web.xml
65          File webXml = new File(webInfDir, "web.xml");
66          String xml =
67                  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
68                  "<web-app xmlns=\"http://java.sun.com/xml/ns/j2ee\"\n" +
69                  "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
70                  "         xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\"\n" +
71                  "         version=\"2.4\">\n" +
72                  "\n" +
73                  "</web-app>";
74          FileWriter w = new FileWriter(webXml);
75          w.write(xml);
76          w.close();
77          File classesDir = new File(webInfDir, "classes");
78          classesDir.mkdir();
79          String packageName = WebAppObjectInSessionServlet.class.getPackage().getName();
80          File packageDirs = new File(classesDir, packageName.replace('.', File.separatorChar));
81          packageDirs.mkdirs();
82  
83          String resourceName = WebAppObjectInSessionServlet.class.getSimpleName() + ".class";
84          Resource resource = Resource.newResource(getClass().getResource(resourceName));
85  
86          //File sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
87          File targetFile = new File(packageDirs, resourceName);
88          //copy(sourceFile, targetFile);
89          IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
90  
91          resourceName = WebAppObjectInSessionServlet.class.getSimpleName() + "$" + WebAppObjectInSessionServlet.TestSharedStatic.class.getSimpleName() + ".class";
92          resource = Resource.newResource(getClass().getResource(resourceName));
93          //sourceFile = new File(getClass().getClassLoader().getResource(resourceName).toURI());
94          targetFile = new File(packageDirs, resourceName);
95          //copy(sourceFile, targetFile);
96          IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
97  
98          AbstractTestServer server1 = createServer(0);
99          server1.addWebAppContext(warDir.getCanonicalPath(), contextPath).addServlet(WebAppObjectInSessionServlet.class.getName(), servletMapping);
100         server1.start();
101         int port1 = server1.getPort();
102         try
103         {
104             AbstractTestServer server2 = createServer(0);
105             server2.addWebAppContext(warDir.getCanonicalPath(), contextPath).addServlet(WebAppObjectInSessionServlet.class.getName(), servletMapping);
106             server2.start();
107             int port2 = server2.getPort();
108             try
109             {
110                 HttpClient client = new HttpClient();
111                 client.start();
112                 try
113                 {
114                     // Perform one request to server1 to create a session
115                     Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=set");
116                     request.method(HttpMethod.GET);
117                     
118                     Future<ContentResponse> future = request.send();
119                     ContentResponse response = future.get();
120                     assertEquals( HttpServletResponse.SC_OK, response.getStatus());
121                     String sessionCookie = response.getHeaders().getStringField("Set-Cookie");
122                     assertTrue(sessionCookie != null);
123                     // Mangle the cookie, replacing Path with $Path, etc.
124                     sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
125 
126                     // Perform a request to server2 using the session cookie from the previous request
127                     Request request2 = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=get");
128                     request2.method(HttpMethod.GET);
129                     request2.header("Cookie", sessionCookie);
130                     future = request2.send();
131                     ContentResponse response2 = future.get();
132 
133                     assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
134                 }
135                 finally
136                 {
137                     client.stop();
138                 }
139             }
140             finally
141             {
142                 server2.stop();
143             }
144         }
145         finally
146         {
147             server1.stop();
148         }
149     }
150 }