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.IOException;
17 import java.io.Serializable;
18
19 import javax.servlet.ServletException;
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import javax.servlet.http.HttpSession;
24
25 import org.junit.Assert;
26
27
28 /**
29 * WebAppObjectInSessionServlet
30 *
31 *
32 */
33 public class WebAppObjectInSessionServlet extends HttpServlet
34 {
35 @Override
36 protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
37 {
38 try
39 {
40 String action = request.getParameter("action");
41 if ("set".equals(action))
42 {
43 HttpSession session = request.getSession(true);
44 session.setAttribute("staticAttribute", new TestSharedStatic());
45
46 Object staticAttribute = session.getAttribute("staticAttribute");
47 Assert.assertTrue(staticAttribute instanceof TestSharedStatic);
48
49 // session.setAttribute("objectAttribute", new TestSharedNonStatic());
50
51 // The session itself is not shareable, since the implementation class
52 // refers to the session manager via the hidden field this$0, and
53 // it seems there is no way to mark the hidden field as transient.
54 // session.setAttribute("sessionAttribute", session);
55 }
56 else if ("get".equals(action))
57 {
58 HttpSession session = request.getSession(false);
59 Object staticAttribute = session.getAttribute("staticAttribute");
60 System.err.println("staticAttribute="+staticAttribute);
61 Assert.assertTrue(staticAttribute instanceof TestSharedStatic);
62
63 // Object objectAttribute = session.getAttribute("objectAttribute");
64 // Assert.assertTrue(objectAttribute instanceof TestSharedNonStatic);
65
66 // Object sessionAttribute = session.getAttribute("sessionAttribute");
67 // assertTrue(sessionAttribute instanceof HttpSession);
68 }
69 }
70 catch (Exception e)
71 {
72 // e.printStackTrace();
73 httpServletResponse.sendError(500,e.toString());
74 throw new ServletException(e);
75 }
76 }
77
78 // Non static inner classes are not shareable, because even if this class is portable,
79 // the hidden field this$0 refers to the servlet, which is a non portable class.
80 public class TestSharedNonStatic implements Serializable
81 {
82 }
83
84 // Static inner classes are shareable
85 public static class TestSharedStatic implements Serializable
86 {
87 }
88 }