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