View Javadoc

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