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