2 * The MIT License (MIT)
4 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package org.apidesign.demo.chat.server;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.HashSet;
30 import java.util.List;
32 import java.util.logging.Logger;
33 import net.java.html.BrwsrCtx;
34 import net.java.html.json.Models;
35 import org.apidesign.demo.chat.shared.Message;
36 import org.apidesign.demo.chat.shared.Query;
37 import org.apidesign.demo.chat.shared.Reply;
38 import org.glassfish.grizzly.websockets.DataFrame;
39 import org.glassfish.grizzly.websockets.WebSocket;
40 import org.glassfish.grizzly.websockets.WebSocketApplication;
42 /** Server side of the chat application.*/
43 public final class ChatServerResource extends WebSocketApplication {
44 private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
45 private static final long started = System.currentTimeMillis() - 10;
47 private List<Message> msgs = new ArrayList<>();
49 Message welcome = new Message();
50 welcome.setUser("system");
51 welcome.setComment("Welcome and enjoy!");
57 private final Set<WebSocket> connected = new HashSet<>();
60 @Produces(MediaType.APPLICATION_JSON)
61 @GET public synchronized void getResources(
62 @QueryParam("since") @DefaultValue("0") long since,
63 @Suspended AsyncResponse ar
65 Query q = new Query();
66 for (Message m : msgs) {
67 if (m.getSince()>= since) {
68 q.getMessages().add(m);
71 if (!q.getMessages().isEmpty()) {
74 awaiting.put(ar, since);
78 private void handleAwaiting(Message msg) {
79 assert Thread.holdsLock(this);
80 Reply reply = new Reply();
81 reply.getMessages().add(msg);
82 String txt = reply.toString();
83 for (WebSocket webSocket : connected) {
88 private synchronized Message publish(Message msg) {
89 msg.setSince(System.currentTimeMillis() - started);
96 public void onConnect(WebSocket socket) {
97 connected.add(socket);
101 public void onClose(WebSocket socket, DataFrame frame) {
102 connected.remove(socket);
106 public void onMessage(WebSocket socket, String text) {
108 ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
109 Query q = Models.parse(BrwsrCtx.findDefault(ChatServerResource.class), Query.class, is);
110 if (q.getPost() != null) {
111 publish(q.getPost());
114 socket.send(new Reply(true, msgs.toArray(new Message[0])).toString());
116 } catch (IOException ex) {
117 throw new IllegalStateException(ex);