chat/server/src/main/java/org/apidesign/bck2brwsr/demo/chatserver/impl/ChatServerResource.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 09 May 2013 22:41:43 +0200
changeset 15 91219d000cb8
parent 13 fd9a16bbfd0e
child 57 9984b9f7d8c6
permissions -rw-r--r--
Uses POST method to send new messages to the server
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@6
    42
import net.java.html.json.Context;
jtulach@13
    43
import org.apidesign.html.chatserver.model.Message;
jtulach@13
    44
import org.apidesign.html.chatserver.model.Query;
jtulach@6
    45
jtulach@11
    46
/** Server side of the chat application.*/
jtulach@13
    47
@Path("/chat/") @Singleton
jtulach@6
    48
public final class ChatServerResource {
jtulach@11
    49
    private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
jtulach@13
    50
    private static final long started = System.currentTimeMillis() - 10;
jtulach@6
    51
    
jtulach@6
    52
    private List<Message> msgs = new ArrayList<>();
jtulach@6
    53
    {
jtulach@6
    54
        Message welcome = new Message(Context.findDefault(Message.class));
jtulach@6
    55
        welcome.setUser("system");
jtulach@6
    56
        welcome.setComment("Welcome and enjoy!");
jtulach@13
    57
        welcome.setSince(10);
jtulach@6
    58
        msgs.add(welcome);
jtulach@6
    59
    }
jtulach@6
    60
    
jtulach@11
    61
    private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
jtulach@11
    62
    
jtulach@6
    63
    @Produces(MediaType.APPLICATION_JSON)
jtulach@11
    64
    @GET public synchronized void getResources(
jtulach@11
    65
        @QueryParam("since") @DefaultValue("0") long since,
jtulach@11
    66
        @Suspended AsyncResponse ar
jtulach@11
    67
    ) {
jtulach@11
    68
        Query q = new Query(Context.findDefault(ChatServerResource.class));
jtulach@11
    69
        for (Message m : msgs) {
jtulach@13
    70
            if (m.getSince()>= since) {
jtulach@11
    71
                q.getMessages().add(m);
jtulach@11
    72
            }
jtulach@11
    73
        }
jtulach@11
    74
        if (!q.getMessages().isEmpty()) {
jtulach@11
    75
            ar.resume(q);
jtulach@11
    76
        } else {
jtulach@11
    77
            awaiting.put(ar, since);
jtulach@11
    78
        }
jtulach@6
    79
    }
jtulach@6
    80
    
jtulach@11
    81
    private void handleAwaiting(long newest) {
jtulach@11
    82
        assert Thread.holdsLock(this);
jtulach@11
    83
        AGAIN: for (;;) {
jtulach@11
    84
            for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
jtulach@11
    85
                AsyncResponse ar = entry.getKey();
jtulach@11
    86
                Long since = entry.getValue();
jtulach@11
    87
                if (since <= newest) {
jtulach@11
    88
                    awaiting.remove(ar);
jtulach@11
    89
                    getResources(since, ar);
jtulach@11
    90
                    continue AGAIN;
jtulach@11
    91
                }
jtulach@11
    92
            }
jtulach@11
    93
            return;
jtulach@6
    94
        }
jtulach@6
    95
    }
jtulach@11
    96
    
jtulach@15
    97
    @POST @Consumes(value = MediaType.APPLICATION_JSON)
jtulach@15
    98
    public synchronized Message publish(Message msg) {
jtulach@13
    99
        msg.setSince(System.currentTimeMillis() - started);
jtulach@11
   100
        msgs.add(msg);
jtulach@13
   101
        handleAwaiting(msg.getSince());
jtulach@11
   102
        return msg;
jtulach@11
   103
    }
jtulach@6
   104
}