chat/server/src/main/java/org/apidesign/demo/chat/server/ChatServerResource.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Apr 2016 08:58:08 +0200
branchNewChat
changeset 241 6a59fdb91011
parent 240 2d0750864a98
child 242 58596208d06d
permissions -rw-r--r--
Can parse the message on the server to Query model class
     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.io.ByteArrayInputStream;
    27 import java.io.IOException;
    28 import java.util.ArrayList;
    29 import java.util.List;
    30 import java.util.logging.Logger;
    31 import net.java.html.BrwsrCtx;
    32 import net.java.html.json.Models;
    33 import org.apidesign.demo.chat.shared.Message;
    34 import org.apidesign.demo.chat.shared.Query;
    35 import org.glassfish.grizzly.websockets.WebSocket;
    36 import org.glassfish.grizzly.websockets.WebSocketApplication;
    37 
    38 /** Server side of the chat application.*/
    39 public final class ChatServerResource extends WebSocketApplication {
    40     private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
    41     private static final long started = System.currentTimeMillis() - 10;
    42     
    43     private List<Message> msgs = new ArrayList<>();
    44     {
    45         Message welcome = new Message();
    46         welcome.setUser("system");
    47         welcome.setComment("Welcome and enjoy!");
    48         welcome.setSince(10);
    49         msgs.add(welcome);
    50     }
    51 
    52 
    53 /*
    54     private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
    55     
    56     @Produces(MediaType.APPLICATION_JSON)
    57     @GET public synchronized void getResources(
    58         @QueryParam("since") @DefaultValue("0") long since,
    59         @Suspended AsyncResponse ar
    60     ) {
    61         Query q = new Query();
    62         for (Message m : msgs) {
    63             if (m.getSince()>= since) {
    64                 q.getMessages().add(m);
    65             }
    66         }
    67         if (!q.getMessages().isEmpty()) {
    68             ar.resume(q);
    69         } else {
    70             awaiting.put(ar, since);
    71         }
    72     }
    73     
    74     private void handleAwaiting(long newest) {
    75         assert Thread.holdsLock(this);
    76         AGAIN: for (;;) {
    77             for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
    78                 AsyncResponse ar = entry.getKey();
    79                 Long since = entry.getValue();
    80                 if (since <= newest) {
    81                     awaiting.remove(ar);
    82                     getResources(since, ar);
    83                     continue AGAIN;
    84                 }
    85             }
    86             return;
    87         }
    88     }
    89     
    90     @POST @Consumes(value = MediaType.APPLICATION_JSON)
    91     public synchronized Message publish(Message msg) {
    92         msg.setSince(System.currentTimeMillis() - started);
    93         msgs.add(msg);
    94         handleAwaiting(msg.getSince());
    95         return msg;
    96     }
    97 */
    98 
    99     @Override
   100     public void onMessage(WebSocket socket, String text) {
   101         try {
   102             ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
   103             Query q = Models.parse(BrwsrCtx.findDefault(ChatServerResource.class), Query.class, is);
   104             super.onMessage(socket, text);
   105         } catch (IOException ex) {
   106             throw new IllegalStateException(ex);
   107         }
   108     }
   109 }