chat/server/src/main/java/org/apidesign/bck2brwsr/demo/chatserver/impl/ChatServerResource.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 06 May 2013 17:40:19 +0200
changeset 13 fd9a16bbfd0e
parent 11 serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatServerResource.java@40fce839ac01
child 15 91219d000cb8
permissions -rw-r--r--
Splitting the client/server example into three independent modules
     1 /**
     2  * The MIT License (MIT)
     3  *
     4  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5  *
     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:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    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
    22  * THE SOFTWARE.
    23  */
    24 package org.apidesign.bck2brwsr.demo.chatserver.impl;
    25 
    26 import java.util.ArrayList;
    27 import java.util.IdentityHashMap;
    28 import java.util.List;
    29 import java.util.Map;
    30 import java.util.logging.Logger;
    31 import javax.inject.Singleton;
    32 import javax.ws.rs.DefaultValue;
    33 import javax.ws.rs.GET;
    34 import javax.ws.rs.Path;
    35 import javax.ws.rs.Produces;
    36 import javax.ws.rs.QueryParam;
    37 import javax.ws.rs.container.AsyncResponse;
    38 import javax.ws.rs.container.Suspended;
    39 import javax.ws.rs.core.MediaType;
    40 import net.java.html.json.Context;
    41 import org.apidesign.html.chatserver.model.Message;
    42 import org.apidesign.html.chatserver.model.Query;
    43 
    44 /** Server side of the chat application.*/
    45 @Path("/chat/") @Singleton
    46 public final class ChatServerResource {
    47     private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
    48     private static final long started = System.currentTimeMillis() - 10;
    49     
    50     private List<Message> msgs = new ArrayList<>();
    51     {
    52         Message welcome = new Message(Context.findDefault(Message.class));
    53         welcome.setUser("system");
    54         welcome.setComment("Welcome and enjoy!");
    55         welcome.setSince(10);
    56         msgs.add(welcome);
    57     }
    58     
    59     private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
    60     
    61     @Produces(MediaType.APPLICATION_JSON)
    62     @GET public synchronized void getResources(
    63         @QueryParam("since") @DefaultValue("0") long since,
    64         @Suspended AsyncResponse ar
    65     ) {
    66         Query q = new Query(Context.findDefault(ChatServerResource.class));
    67         for (Message m : msgs) {
    68             if (m.getSince()>= since) {
    69                 q.getMessages().add(m);
    70             }
    71         }
    72         if (!q.getMessages().isEmpty()) {
    73             ar.resume(q);
    74         } else {
    75             awaiting.put(ar, since);
    76         }
    77     }
    78     
    79     private void handleAwaiting(long newest) {
    80         assert Thread.holdsLock(this);
    81         AGAIN: for (;;) {
    82             for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
    83                 AsyncResponse ar = entry.getKey();
    84                 Long since = entry.getValue();
    85                 if (since <= newest) {
    86                     awaiting.remove(ar);
    87                     getResources(since, ar);
    88                     continue AGAIN;
    89                 }
    90             }
    91             return;
    92         }
    93     }
    94     
    95     @Path("addComment") @GET 
    96     public synchronized Message publish(
    97         @QueryParam("user") String user,
    98         @QueryParam("comment") String comment
    99     ) {
   100         Message msg = new Message(Context.findDefault(ChatServerResource.class));
   101         msg.setUser(user);
   102         msg.setComment(comment);
   103         msg.setSince(System.currentTimeMillis() - started);
   104         msgs.add(msg);
   105         handleAwaiting(msg.getSince());
   106         return msg;
   107     }
   108 }