chat/server/src/main/java/org/apidesign/bck2brwsr/demo/chatserver/impl/ChatServerResource.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/chat/server/src/main/java/org/apidesign/bck2brwsr/demo/chatserver/impl/ChatServerResource.java Mon May 06 17:40:19 2013 +0200
1.3 @@ -0,0 +1,108 @@
1.4 +/**
1.5 + * The MIT License (MIT)
1.6 + *
1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.8 + *
1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1.10 + * of this software and associated documentation files (the "Software"), to deal
1.11 + * in the Software without restriction, including without limitation the rights
1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1.13 + * copies of the Software, and to permit persons to whom the Software is
1.14 + * furnished to do so, subject to the following conditions:
1.15 + *
1.16 + * The above copyright notice and this permission notice shall be included in
1.17 + * all copies or substantial portions of the Software.
1.18 + *
1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1.25 + * THE SOFTWARE.
1.26 + */
1.27 +package org.apidesign.bck2brwsr.demo.chatserver.impl;
1.28 +
1.29 +import java.util.ArrayList;
1.30 +import java.util.IdentityHashMap;
1.31 +import java.util.List;
1.32 +import java.util.Map;
1.33 +import java.util.logging.Logger;
1.34 +import javax.inject.Singleton;
1.35 +import javax.ws.rs.DefaultValue;
1.36 +import javax.ws.rs.GET;
1.37 +import javax.ws.rs.Path;
1.38 +import javax.ws.rs.Produces;
1.39 +import javax.ws.rs.QueryParam;
1.40 +import javax.ws.rs.container.AsyncResponse;
1.41 +import javax.ws.rs.container.Suspended;
1.42 +import javax.ws.rs.core.MediaType;
1.43 +import net.java.html.json.Context;
1.44 +import org.apidesign.html.chatserver.model.Message;
1.45 +import org.apidesign.html.chatserver.model.Query;
1.46 +
1.47 +/** Server side of the chat application.*/
1.48 +@Path("/chat/") @Singleton
1.49 +public final class ChatServerResource {
1.50 + private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
1.51 + private static final long started = System.currentTimeMillis() - 10;
1.52 +
1.53 + private List<Message> msgs = new ArrayList<>();
1.54 + {
1.55 + Message welcome = new Message(Context.findDefault(Message.class));
1.56 + welcome.setUser("system");
1.57 + welcome.setComment("Welcome and enjoy!");
1.58 + welcome.setSince(10);
1.59 + msgs.add(welcome);
1.60 + }
1.61 +
1.62 + private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
1.63 +
1.64 + @Produces(MediaType.APPLICATION_JSON)
1.65 + @GET public synchronized void getResources(
1.66 + @QueryParam("since") @DefaultValue("0") long since,
1.67 + @Suspended AsyncResponse ar
1.68 + ) {
1.69 + Query q = new Query(Context.findDefault(ChatServerResource.class));
1.70 + for (Message m : msgs) {
1.71 + if (m.getSince()>= since) {
1.72 + q.getMessages().add(m);
1.73 + }
1.74 + }
1.75 + if (!q.getMessages().isEmpty()) {
1.76 + ar.resume(q);
1.77 + } else {
1.78 + awaiting.put(ar, since);
1.79 + }
1.80 + }
1.81 +
1.82 + private void handleAwaiting(long newest) {
1.83 + assert Thread.holdsLock(this);
1.84 + AGAIN: for (;;) {
1.85 + for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
1.86 + AsyncResponse ar = entry.getKey();
1.87 + Long since = entry.getValue();
1.88 + if (since <= newest) {
1.89 + awaiting.remove(ar);
1.90 + getResources(since, ar);
1.91 + continue AGAIN;
1.92 + }
1.93 + }
1.94 + return;
1.95 + }
1.96 + }
1.97 +
1.98 + @Path("addComment") @GET
1.99 + public synchronized Message publish(
1.100 + @QueryParam("user") String user,
1.101 + @QueryParam("comment") String comment
1.102 + ) {
1.103 + Message msg = new Message(Context.findDefault(ChatServerResource.class));
1.104 + msg.setUser(user);
1.105 + msg.setComment(comment);
1.106 + msg.setSince(System.currentTimeMillis() - started);
1.107 + msgs.add(msg);
1.108 + handleAwaiting(msg.getSince());
1.109 + return msg;
1.110 + }
1.111 +}