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  
20  package org.eclipse.jetty.server.session;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.File;
27  import java.io.FileOutputStream;
28  import java.io.InputStream;
29  import java.net.URL;
30  import java.net.URLClassLoader;
31  
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.eclipse.jetty.client.HttpClient;
35  import org.eclipse.jetty.client.api.ContentResponse;
36  import org.eclipse.jetty.client.api.Request;
37  import org.eclipse.jetty.servlet.ServletContextHandler;
38  import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
39  import org.eclipse.jetty.util.IO;
40  import org.junit.Test;
41  
42  /**
43   * AbstractProxySerializationTest
44   *
45   *
46   */
47  public abstract class AbstractProxySerializationTest
48  {
49      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
50      
51      public abstract void customizeContext (ServletContextHandler c);
52      
53     
54      
55      /**
56       * @param msec milliseconds to sleep
57       */
58      public void pause(int msec)
59      {
60          try
61          {
62              Thread.sleep(msec);
63          }
64          catch (InterruptedException e)
65          {
66              e.printStackTrace();
67          }
68      }
69      
70      @Test
71      public void testProxySerialization() throws Exception
72      {
73          String contextPath = "";
74          String servletMapping = "/server";
75          int scavengePeriod = 10;
76          AbstractTestServer server = createServer(0, 20, scavengePeriod);
77          ServletContextHandler context = server.addContext(contextPath);
78  
79          InputStream is = this.getClass().getClassLoader().getResourceAsStream("proxy-serialization.jar");
80          
81          File testDir = MavenTestingUtils.getTargetTestingDir("proxy-serialization");
82          testDir.mkdirs();
83          
84          File extractedJar = new File (testDir, "proxy-serialization.jar");
85          extractedJar.createNewFile();
86          IO.copy(is, new FileOutputStream(extractedJar));
87   
88          
89          URLClassLoader loader = new URLClassLoader(new URL[] {extractedJar.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
90          context.setClassLoader(loader);
91          context.addServlet("TestServlet", servletMapping);
92          customizeContext(context);
93          
94          try
95          {
96              server.start();
97              int port=server.getPort();
98              HttpClient client = new HttpClient();
99              client.start();
100             try
101             {
102                 ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
103                 assertEquals(HttpServletResponse.SC_OK,response.getStatus());
104                 String sessionCookie = response.getHeaders().get("Set-Cookie");
105                 assertTrue(sessionCookie != null);
106                 // Mangle the cookie, replacing Path with $Path, etc.
107                 sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
108 
109                 //stop the context to be sure the sesssion will be passivated
110                 context.stop();
111                 
112                 //restart the context
113                 context.start();
114                
115                 // Make another request using the session id from before
116                 Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
117                 request.header("Cookie", sessionCookie);
118                 response = request.send();
119                 assertEquals(HttpServletResponse.SC_OK,response.getStatus());
120             }
121             finally
122             {
123                 client.stop();
124             }
125         }
126         finally
127         {
128             server.stop();
129         }
130 
131     }
132 }