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