1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }