View Javadoc

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