chat/server/src/main/java/org/apidesign/bck2brwsr/demo/chatserver/impl/ChatServerResource.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 12 Jan 2014 23:25:42 +0100
branchNbHtml4J
changeset 57 9984b9f7d8c6
parent 15 91219d000cb8
permissions -rw-r--r--
Adjusting to new naming scheme (which includes netbeans name)
jtulach@6
     1
/**
jtulach@6
     2
 * The MIT License (MIT)
jtulach@6
     3
 *
jtulach@6
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@6
     5
 *
jtulach@6
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jtulach@6
     7
 * of this software and associated documentation files (the "Software"), to deal
jtulach@6
     8
 * in the Software without restriction, including without limitation the rights
jtulach@6
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jtulach@6
    10
 * copies of the Software, and to permit persons to whom the Software is
jtulach@6
    11
 * furnished to do so, subject to the following conditions:
jtulach@6
    12
 *
jtulach@6
    13
 * The above copyright notice and this permission notice shall be included in
jtulach@6
    14
 * all copies or substantial portions of the Software.
jtulach@6
    15
 *
jtulach@6
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jtulach@6
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jtulach@6
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jtulach@6
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jtulach@6
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jtulach@6
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jtulach@6
    22
 * THE SOFTWARE.
jtulach@6
    23
 */
jtulach@13
    24
package org.apidesign.bck2brwsr.demo.chatserver.impl;
jtulach@6
    25
jtulach@6
    26
import java.util.ArrayList;
jtulach@11
    27
import java.util.IdentityHashMap;
jtulach@6
    28
import java.util.List;
jtulach@11
    29
import java.util.Map;
jtulach@11
    30
import java.util.logging.Logger;
jtulach@11
    31
import javax.inject.Singleton;
jtulach@15
    32
import javax.ws.rs.Consumes;
jtulach@11
    33
import javax.ws.rs.DefaultValue;
jtulach@6
    34
import javax.ws.rs.GET;
jtulach@15
    35
import javax.ws.rs.POST;
jtulach@6
    36
import javax.ws.rs.Path;
jtulach@6
    37
import javax.ws.rs.Produces;
jtulach@11
    38
import javax.ws.rs.QueryParam;
jtulach@10
    39
import javax.ws.rs.container.AsyncResponse;
jtulach@10
    40
import javax.ws.rs.container.Suspended;
jtulach@6
    41
import javax.ws.rs.core.MediaType;
jtulach@13
    42
import org.apidesign.html.chatserver.model.Message;
jtulach@13
    43
import org.apidesign.html.chatserver.model.Query;
jtulach@6
    44
jtulach@11
    45
/** Server side of the chat application.*/
jtulach@13
    46
@Path("/chat/") @Singleton
jtulach@6
    47
public final class ChatServerResource {
jtulach@11
    48
    private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
jtulach@13
    49
    private static final long started = System.currentTimeMillis() - 10;
jtulach@6
    50
    
jtulach@6
    51
    private List<Message> msgs = new ArrayList<>();
jtulach@6
    52
    {
jtulach@57
    53
        Message welcome = new Message();
jtulach@6
    54
        welcome.setUser("system");
jtulach@6
    55
        welcome.setComment("Welcome and enjoy!");
jtulach@13
    56
        welcome.setSince(10);
jtulach@6
    57
        msgs.add(welcome);
jtulach@6
    58
    }
jtulach@6
    59
    
jtulach@11
    60
    private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
jtulach@11
    61
    
jtulach@6
    62
    @Produces(MediaType.APPLICATION_JSON)
jtulach@11
    63
    @GET public synchronized void getResources(
jtulach@11
    64
        @QueryParam("since") @DefaultValue("0") long since,
jtulach@11
    65
        @Suspended AsyncResponse ar
jtulach@11
    66
    ) {
jtulach@57
    67
        Query q = new Query();
jtulach@11
    68
        for (Message m : msgs) {
jtulach@13
    69
            if (m.getSince()>= since) {
jtulach@11
    70
                q.getMessages().add(m);
jtulach@11
    71
            }
jtulach@11
    72
        }
jtulach@11
    73
        if (!q.getMessages().isEmpty()) {
jtulach@11
    74
            ar.resume(q);
jtulach@11
    75
        } else {
jtulach@11
    76
            awaiting.put(ar, since);
jtulach@11
    77
        }
jtulach@6
    78
    }
jtulach@6
    79
    
jtulach@11
    80
    private void handleAwaiting(long newest) {
jtulach@11
    81
        assert Thread.holdsLock(this);
jtulach@11
    82
        AGAIN: for (;;) {
jtulach@11
    83
            for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
jtulach@11
    84
                AsyncResponse ar = entry.getKey();
jtulach@11
    85
                Long since = entry.getValue();
jtulach@11
    86
                if (since <= newest) {
jtulach@11
    87
                    awaiting.remove(ar);
jtulach@11
    88
                    getResources(since, ar);
jtulach@11
    89
                    continue AGAIN;
jtulach@11
    90
                }
jtulach@11
    91
            }
jtulach@11
    92
            return;
jtulach@6
    93
        }
jtulach@6
    94
    }
jtulach@11
    95
    
jtulach@15
    96
    @POST @Consumes(value = MediaType.APPLICATION_JSON)
jtulach@15
    97
    public synchronized Message publish(Message msg) {
jtulach@13
    98
        msg.setSince(System.currentTimeMillis() - started);
jtulach@11
    99
        msgs.add(msg);
jtulach@13
   100
        handleAwaiting(msg.getSince());
jtulach@11
   101
        return msg;
jtulach@11
   102
    }
jtulach@6
   103
}