serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatServerResource.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 05 May 2013 17:20:46 +0200
changeset 10 80715a1dd72e
parent 8 3e9cf87d5af3
child 11 40fce839ac01
permissions -rw-r--r--
Using AsyncResponse
     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.bck2brwsr.demo.serverside;
    25 
    26 import java.io.Closeable;
    27 import java.lang.reflect.Field;
    28 import java.util.ArrayList;
    29 import java.util.List;
    30 import java.util.concurrent.Callable;
    31 import javax.ws.rs.Consumes;
    32 import javax.ws.rs.GET;
    33 import javax.ws.rs.PUT;
    34 import javax.ws.rs.Path;
    35 import javax.ws.rs.Produces;
    36 import javax.ws.rs.container.AsyncResponse;
    37 import javax.ws.rs.container.Suspended;
    38 import javax.ws.rs.core.MediaType;
    39 import net.java.html.json.Context;
    40 import org.apidesign.bck2brwsr.launcher.Launcher;
    41 import org.glassfish.grizzly.http.server.HttpServer;
    42 import org.glassfish.grizzly.http.server.ServerConfiguration;
    43 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer;
    44 import org.glassfish.jersey.server.ContainerFactory;
    45 import org.glassfish.jersey.server.ResourceConfig;
    46 
    47 /** Server side of the chat application.
    48  */
    49 @Path("/")
    50 public final class ChatServerResource {
    51     public static void main(String... args) throws Exception {
    52         ResourceConfig rc = new ResourceConfig(ChatServerResource.class);
    53         GrizzlyHttpContainer c = ContainerFactory.createContainer(GrizzlyHttpContainer.class, rc);
    54         Closeable s = Launcher.showURL(
    55             "bck2brwsr", ChatClient.class.getClassLoader(),
    56             "/org/apidesign/bck2brwsr/demo/serverside/chat.html"
    57         );
    58         Launcher l = (Launcher) s;
    59         Callable<?> f = (Callable<?>) l;
    60         HttpServer server = (HttpServer) f.call();
    61         ServerConfiguration conf = server.getServerConfiguration();
    62         conf.addHttpHandler(c, "/chat");
    63         System.in.read();
    64         s.close();
    65     }
    66     
    67     private List<Message> msgs = new ArrayList<>();
    68     {
    69         Message welcome = new Message(Context.findDefault(Message.class));
    70         welcome.setUser("system");
    71         welcome.setComment("Welcome and enjoy!");
    72         welcome.setWhen(System.currentTimeMillis());
    73         msgs.add(welcome);
    74     }
    75     
    76     @Produces(MediaType.APPLICATION_JSON)
    77     @GET public void getResources(@Suspended AsyncResponse ar) {
    78         Query q = new Query(Context.findDefault(Query.class));
    79         q.getMessages().addAll(msgs);
    80         ar.resume(q);
    81     }
    82     
    83     @Consumes(MediaType.APPLICATION_JSON)
    84     @PUT public void publish(Message msg) {
    85         if (msg != null) {
    86             msgs.add(msg);
    87         }
    88     }
    89 }