serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatServerResource.java
1.1 --- a/serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatServerResource.java Sun May 05 18:11:29 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,133 +0,0 @@
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.serverside;
1.28 -
1.29 -import java.io.Closeable;
1.30 -import java.lang.reflect.Field;
1.31 -import java.util.ArrayList;
1.32 -import java.util.IdentityHashMap;
1.33 -import java.util.List;
1.34 -import java.util.Map;
1.35 -import java.util.concurrent.Callable;
1.36 -import java.util.logging.Level;
1.37 -import java.util.logging.Logger;
1.38 -import javax.inject.Singleton;
1.39 -import javax.ws.rs.Consumes;
1.40 -import javax.ws.rs.DefaultValue;
1.41 -import javax.ws.rs.GET;
1.42 -import javax.ws.rs.PUT;
1.43 -import javax.ws.rs.Path;
1.44 -import javax.ws.rs.Produces;
1.45 -import javax.ws.rs.QueryParam;
1.46 -import javax.ws.rs.container.AsyncResponse;
1.47 -import javax.ws.rs.container.Suspended;
1.48 -import javax.ws.rs.core.MediaType;
1.49 -import net.java.html.json.Context;
1.50 -import org.apidesign.bck2brwsr.launcher.Launcher;
1.51 -import org.glassfish.grizzly.http.server.HttpServer;
1.52 -import org.glassfish.grizzly.http.server.ServerConfiguration;
1.53 -import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer;
1.54 -import org.glassfish.jersey.server.ContainerFactory;
1.55 -import org.glassfish.jersey.server.ResourceConfig;
1.56 -
1.57 -/** Server side of the chat application.*/
1.58 -@Path("/") @Singleton
1.59 -public final class ChatServerResource {
1.60 - private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
1.61 -
1.62 - public static void main(String... args) throws Exception {
1.63 - ResourceConfig rc = new ResourceConfig(ChatServerResource.class);
1.64 - GrizzlyHttpContainer c = ContainerFactory.createContainer(GrizzlyHttpContainer.class, rc);
1.65 - Closeable s = Launcher.showURL(
1.66 - "bck2brwsr", ChatClient.class.getClassLoader(),
1.67 - "/org/apidesign/bck2brwsr/demo/serverside/chat.html"
1.68 - );
1.69 - Launcher l = (Launcher) s;
1.70 - Callable<?> f = (Callable<?>) l;
1.71 - HttpServer server = (HttpServer) f.call();
1.72 - ServerConfiguration conf = server.getServerConfiguration();
1.73 - conf.addHttpHandler(c, "/chat");
1.74 - System.in.read();
1.75 - s.close();
1.76 - }
1.77 -
1.78 - private List<Message> msgs = new ArrayList<>();
1.79 - {
1.80 - Message welcome = new Message(Context.findDefault(Message.class));
1.81 - welcome.setUser("system");
1.82 - welcome.setComment("Welcome and enjoy!");
1.83 - welcome.setWhen(System.currentTimeMillis());
1.84 - msgs.add(welcome);
1.85 - }
1.86 -
1.87 - private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
1.88 -
1.89 - @Produces(MediaType.APPLICATION_JSON)
1.90 - @GET public synchronized void getResources(
1.91 - @QueryParam("since") @DefaultValue("0") long since,
1.92 - @Suspended AsyncResponse ar
1.93 - ) {
1.94 - Query q = new Query(Context.findDefault(ChatServerResource.class));
1.95 - for (Message m : msgs) {
1.96 - if (m.getWhen() >= since) {
1.97 - q.getMessages().add(m);
1.98 - }
1.99 - }
1.100 - if (!q.getMessages().isEmpty()) {
1.101 - ar.resume(q);
1.102 - } else {
1.103 - awaiting.put(ar, since);
1.104 - }
1.105 - }
1.106 -
1.107 - private void handleAwaiting(long newest) {
1.108 - assert Thread.holdsLock(this);
1.109 - AGAIN: for (;;) {
1.110 - for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
1.111 - AsyncResponse ar = entry.getKey();
1.112 - Long since = entry.getValue();
1.113 - if (since <= newest) {
1.114 - awaiting.remove(ar);
1.115 - getResources(since, ar);
1.116 - continue AGAIN;
1.117 - }
1.118 - }
1.119 - return;
1.120 - }
1.121 - }
1.122 -
1.123 - @Path("addComment") @GET
1.124 - public synchronized Message publish(
1.125 - @QueryParam("user") String user,
1.126 - @QueryParam("comment") String comment
1.127 - ) {
1.128 - Message msg = new Message(Context.findDefault(ChatServerResource.class));
1.129 - msg.setUser(user);
1.130 - msg.setComment(comment);
1.131 - msg.setWhen(System.currentTimeMillis());
1.132 - msgs.add(msg);
1.133 - handleAwaiting(msg.getWhen());
1.134 - return msg;
1.135 - }
1.136 -}