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