chat/server/src/main/java/org/apidesign/bck2brwsr/demo/chatserver/impl/ChatServerResource.java
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.bck2brwsr.demo.chatserver.impl;
26 import java.util.ArrayList;
27 import java.util.IdentityHashMap;
28 import java.util.List;
30 import java.util.logging.Logger;
31 import javax.inject.Singleton;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.DefaultValue;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.QueryParam;
39 import javax.ws.rs.container.AsyncResponse;
40 import javax.ws.rs.container.Suspended;
41 import javax.ws.rs.core.MediaType;
42 import net.java.html.json.Context;
43 import org.apidesign.html.chatserver.model.Message;
44 import org.apidesign.html.chatserver.model.Query;
46 /** Server side of the chat application.*/
47 @Path("/chat/") @Singleton
48 public final class ChatServerResource {
49 private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
50 private static final long started = System.currentTimeMillis() - 10;
52 private List<Message> msgs = new ArrayList<>();
54 Message welcome = new Message(Context.findDefault(Message.class));
55 welcome.setUser("system");
56 welcome.setComment("Welcome and enjoy!");
61 private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
63 @Produces(MediaType.APPLICATION_JSON)
64 @GET public synchronized void getResources(
65 @QueryParam("since") @DefaultValue("0") long since,
66 @Suspended AsyncResponse ar
68 Query q = new Query(Context.findDefault(ChatServerResource.class));
69 for (Message m : msgs) {
70 if (m.getSince()>= since) {
71 q.getMessages().add(m);
74 if (!q.getMessages().isEmpty()) {
77 awaiting.put(ar, since);
81 private void handleAwaiting(long newest) {
82 assert Thread.holdsLock(this);
84 for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
85 AsyncResponse ar = entry.getKey();
86 Long since = entry.getValue();
87 if (since <= newest) {
89 getResources(since, ar);
97 @POST @Consumes(value = MediaType.APPLICATION_JSON)
98 public synchronized Message publish(Message msg) {
99 msg.setSince(System.currentTimeMillis() - started);
101 handleAwaiting(msg.getSince());