chat/server/src/main/java/org/apidesign/demo/chat/server/ChatServerResource.java
branchNewChat
changeset 238 a0f15cb8c730
child 240 2d0750864a98
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/chat/server/src/main/java/org/apidesign/demo/chat/server/ChatServerResource.java	Fri Apr 22 05:56:47 2016 +0200
     1.3 @@ -0,0 +1,103 @@
     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.demo.chat.server;
    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.Consumes;
    1.36 +import javax.ws.rs.DefaultValue;
    1.37 +import javax.ws.rs.GET;
    1.38 +import javax.ws.rs.POST;
    1.39 +import javax.ws.rs.Path;
    1.40 +import javax.ws.rs.Produces;
    1.41 +import javax.ws.rs.QueryParam;
    1.42 +import javax.ws.rs.container.AsyncResponse;
    1.43 +import javax.ws.rs.container.Suspended;
    1.44 +import javax.ws.rs.core.MediaType;
    1.45 +import org.apidesign.demo.chat.shared.Message;
    1.46 +import org.apidesign.demo.chat.shared.Query;
    1.47 +
    1.48 +/** Server side of the chat application.*/
    1.49 +@Path("/chat/") @Singleton
    1.50 +public final class ChatServerResource {
    1.51 +    private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
    1.52 +    private static final long started = System.currentTimeMillis() - 10;
    1.53 +    
    1.54 +    private List<Message> msgs = new ArrayList<>();
    1.55 +    {
    1.56 +        Message welcome = new Message();
    1.57 +        welcome.setUser("system");
    1.58 +        welcome.setComment("Welcome and enjoy!");
    1.59 +        welcome.setSince(10);
    1.60 +        msgs.add(welcome);
    1.61 +    }
    1.62 +    
    1.63 +    private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
    1.64 +    
    1.65 +    @Produces(MediaType.APPLICATION_JSON)
    1.66 +    @GET public synchronized void getResources(
    1.67 +        @QueryParam("since") @DefaultValue("0") long since,
    1.68 +        @Suspended AsyncResponse ar
    1.69 +    ) {
    1.70 +        Query q = new Query();
    1.71 +        for (Message m : msgs) {
    1.72 +            if (m.getSince()>= since) {
    1.73 +                q.getMessages().add(m);
    1.74 +            }
    1.75 +        }
    1.76 +        if (!q.getMessages().isEmpty()) {
    1.77 +            ar.resume(q);
    1.78 +        } else {
    1.79 +            awaiting.put(ar, since);
    1.80 +        }
    1.81 +    }
    1.82 +    
    1.83 +    private void handleAwaiting(long newest) {
    1.84 +        assert Thread.holdsLock(this);
    1.85 +        AGAIN: for (;;) {
    1.86 +            for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
    1.87 +                AsyncResponse ar = entry.getKey();
    1.88 +                Long since = entry.getValue();
    1.89 +                if (since <= newest) {
    1.90 +                    awaiting.remove(ar);
    1.91 +                    getResources(since, ar);
    1.92 +                    continue AGAIN;
    1.93 +                }
    1.94 +            }
    1.95 +            return;
    1.96 +        }
    1.97 +    }
    1.98 +    
    1.99 +    @POST @Consumes(value = MediaType.APPLICATION_JSON)
   1.100 +    public synchronized Message publish(Message msg) {
   1.101 +        msg.setSince(System.currentTimeMillis() - started);
   1.102 +        msgs.add(msg);
   1.103 +        handleAwaiting(msg.getSince());
   1.104 +        return msg;
   1.105 +    }
   1.106 +}