serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatServerResource.java
changeset 6 ecb1ef414295
child 8 3e9cf87d5af3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatServerResource.java	Sun May 05 15:38:59 2013 +0200
     1.3 @@ -0,0 +1,86 @@
     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.bck2brwsr.demo.serverside;
    1.28 +
    1.29 +import java.io.Closeable;
    1.30 +import java.lang.reflect.Field;
    1.31 +import java.util.ArrayList;
    1.32 +import java.util.List;
    1.33 +import java.util.concurrent.Callable;
    1.34 +import javax.ws.rs.Consumes;
    1.35 +import javax.ws.rs.GET;
    1.36 +import javax.ws.rs.PUT;
    1.37 +import javax.ws.rs.Path;
    1.38 +import javax.ws.rs.Produces;
    1.39 +import javax.ws.rs.core.MediaType;
    1.40 +import net.java.html.json.Context;
    1.41 +import org.apidesign.bck2brwsr.launcher.Launcher;
    1.42 +import org.glassfish.grizzly.http.server.HttpServer;
    1.43 +import org.glassfish.grizzly.http.server.ServerConfiguration;
    1.44 +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer;
    1.45 +import org.glassfish.jersey.server.ContainerFactory;
    1.46 +import org.glassfish.jersey.server.ResourceConfig;
    1.47 +
    1.48 +/** Server side of the chat application.
    1.49 + */
    1.50 +@Path("/")
    1.51 +public final class ChatServerResource {
    1.52 +    public static void main(String... args) throws Exception {
    1.53 +        ResourceConfig rc = new ResourceConfig(ChatServerResource.class);
    1.54 +        GrizzlyHttpContainer c = ContainerFactory.createContainer(GrizzlyHttpContainer.class, rc);
    1.55 +        Closeable s = Launcher.showURL(
    1.56 +            "bck2brwsr", ChatClient.class.getClassLoader(),
    1.57 +            "/org/apidesign/bck2brwsr/demo/serverside/chat.html"
    1.58 +        );
    1.59 +        Launcher l = (Launcher) s;
    1.60 +        Callable<?> f = (Callable<?>) l;
    1.61 +        HttpServer server = (HttpServer) f.call();
    1.62 +        ServerConfiguration conf = server.getServerConfiguration();
    1.63 +        conf.addHttpHandler(c, "/chat");
    1.64 +        System.in.read();
    1.65 +        s.close();
    1.66 +    }
    1.67 +    
    1.68 +    private List<Message> msgs = new ArrayList<>();
    1.69 +    {
    1.70 +        Message welcome = new Message(Context.findDefault(Message.class));
    1.71 +        welcome.setUser("system");
    1.72 +        welcome.setComment("Welcome and enjoy!");
    1.73 +        msgs.add(welcome);
    1.74 +    }
    1.75 +    
    1.76 +    @Produces(MediaType.APPLICATION_JSON)
    1.77 +    @GET public Query getResources() {
    1.78 +        Query q = new Query(Context.findDefault(Query.class));
    1.79 +        q.getMessages().addAll(msgs);
    1.80 +        return q;
    1.81 +    }
    1.82 +    
    1.83 +    @Consumes(MediaType.APPLICATION_JSON)
    1.84 +    @PUT public void publish(Message msg) {
    1.85 +        if (msg != null) {
    1.86 +            msgs.add(msg);
    1.87 +        }
    1.88 +    }
    1.89 +}