chat/server/src/main/java/org/apidesign/demo/chat/server/ChatServerResource.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Apr 2016 05:56:47 +0200
branchNewChat
changeset 238 a0f15cb8c730
child 240 2d0750864a98
permissions -rw-r--r--
Switching to newer version of the libraries
     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.demo.chat.server;
    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.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 org.apidesign.demo.chat.shared.Message;
    43 import org.apidesign.demo.chat.shared.Query;
    44 
    45 /** Server side of the chat application.*/
    46 @Path("/chat/") @Singleton
    47 public final class ChatServerResource {
    48     private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
    49     private static final long started = System.currentTimeMillis() - 10;
    50     
    51     private List<Message> msgs = new ArrayList<>();
    52     {
    53         Message welcome = new Message();
    54         welcome.setUser("system");
    55         welcome.setComment("Welcome and enjoy!");
    56         welcome.setSince(10);
    57         msgs.add(welcome);
    58     }
    59     
    60     private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
    61     
    62     @Produces(MediaType.APPLICATION_JSON)
    63     @GET public synchronized void getResources(
    64         @QueryParam("since") @DefaultValue("0") long since,
    65         @Suspended AsyncResponse ar
    66     ) {
    67         Query q = new Query();
    68         for (Message m : msgs) {
    69             if (m.getSince()>= since) {
    70                 q.getMessages().add(m);
    71             }
    72         }
    73         if (!q.getMessages().isEmpty()) {
    74             ar.resume(q);
    75         } else {
    76             awaiting.put(ar, since);
    77         }
    78     }
    79     
    80     private void handleAwaiting(long newest) {
    81         assert Thread.holdsLock(this);
    82         AGAIN: for (;;) {
    83             for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
    84                 AsyncResponse ar = entry.getKey();
    85                 Long since = entry.getValue();
    86                 if (since <= newest) {
    87                     awaiting.remove(ar);
    88                     getResources(since, ar);
    89                     continue AGAIN;
    90                 }
    91             }
    92             return;
    93         }
    94     }
    95     
    96     @POST @Consumes(value = MediaType.APPLICATION_JSON)
    97     public synchronized Message publish(Message msg) {
    98         msg.setSince(System.currentTimeMillis() - started);
    99         msgs.add(msg);
   100         handleAwaiting(msg.getSince());
   101         return msg;
   102     }
   103 }