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.tests.ws;
20  
21  import javax.inject.Inject;
22  import javax.servlet.http.HttpSession;
23  import javax.websocket.OnMessage;
24  import javax.websocket.OnOpen;
25  import javax.websocket.RemoteEndpoint;
26  import javax.websocket.Session;
27  import javax.websocket.server.ServerEndpoint;
28  
29  import org.eclipse.jetty.tests.logging.JULog;
30  
31  @ServerEndpoint(value = "/sessioninfo")
32  public class SessionInfoSocket
33  {
34      @Inject
35      private JULog LOG;
36  
37      @Inject
38      private HttpSession httpSession;
39  
40      private Session wsSession;
41  
42      @OnOpen
43      public void onOpen(Session session)
44      {
45          LOG.info("onOpen({0})",asClassId(session));
46          this.wsSession = session;
47      }
48  
49      @OnMessage
50      public void onMessage(String message)
51      {
52          LOG.info("onMessage({0})",quoted(message));
53          
54          try
55          {
56              RemoteEndpoint.Basic remote = wsSession.getBasicRemote();
57              LOG.info("Remote.Basic: {0}", remote);
58              
59              if ("info".equalsIgnoreCase(message))
60              {
61                  LOG.info("returning 'info' details");
62                  remote.sendText("HttpSession = " + httpSession);
63              }
64              else if ("close".equalsIgnoreCase(message))
65              {
66                  LOG.info("closing session");
67                  wsSession.close();
68              }
69              else
70              {
71                  LOG.info("echoing message as-is");
72                  remote.sendText(message);
73              }
74          }
75          catch (Throwable t)
76          {
77              LOG.warn(t);
78          }
79      }
80  
81      private String asClassId(Object obj)
82      {
83          if (obj == null)
84          {
85              return "<null>";
86          }
87          return String.format("%s@%X",obj.getClass().getName(),obj.hashCode());
88      }
89  
90      private String quoted(String str)
91      {
92          if (str == null)
93          {
94              return "<null>";
95          }
96          return '"' + str + '"';
97      }
98  }