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@11
|
29 |
import java.util.IdentityHashMap;
|
jtulach@6
|
30 |
import java.util.List;
|
jtulach@11
|
31 |
import java.util.Map;
|
jtulach@6
|
32 |
import java.util.concurrent.Callable;
|
jtulach@11
|
33 |
import java.util.logging.Level;
|
jtulach@11
|
34 |
import java.util.logging.Logger;
|
jtulach@11
|
35 |
import javax.inject.Singleton;
|
jtulach@6
|
36 |
import javax.ws.rs.Consumes;
|
jtulach@11
|
37 |
import javax.ws.rs.DefaultValue;
|
jtulach@6
|
38 |
import javax.ws.rs.GET;
|
jtulach@6
|
39 |
import javax.ws.rs.PUT;
|
jtulach@6
|
40 |
import javax.ws.rs.Path;
|
jtulach@6
|
41 |
import javax.ws.rs.Produces;
|
jtulach@11
|
42 |
import javax.ws.rs.QueryParam;
|
jtulach@10
|
43 |
import javax.ws.rs.container.AsyncResponse;
|
jtulach@10
|
44 |
import javax.ws.rs.container.Suspended;
|
jtulach@6
|
45 |
import javax.ws.rs.core.MediaType;
|
jtulach@6
|
46 |
import net.java.html.json.Context;
|
jtulach@6
|
47 |
import org.apidesign.bck2brwsr.launcher.Launcher;
|
jtulach@6
|
48 |
import org.glassfish.grizzly.http.server.HttpServer;
|
jtulach@6
|
49 |
import org.glassfish.grizzly.http.server.ServerConfiguration;
|
jtulach@6
|
50 |
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer;
|
jtulach@6
|
51 |
import org.glassfish.jersey.server.ContainerFactory;
|
jtulach@6
|
52 |
import org.glassfish.jersey.server.ResourceConfig;
|
jtulach@6
|
53 |
|
jtulach@11
|
54 |
/** Server side of the chat application.*/
|
jtulach@11
|
55 |
@Path("/") @Singleton
|
jtulach@6
|
56 |
public final class ChatServerResource {
|
jtulach@11
|
57 |
private static final Logger LOG = Logger.getLogger(ChatServerResource.class.getName());
|
jtulach@11
|
58 |
|
jtulach@6
|
59 |
public static void main(String... args) throws Exception {
|
jtulach@6
|
60 |
ResourceConfig rc = new ResourceConfig(ChatServerResource.class);
|
jtulach@6
|
61 |
GrizzlyHttpContainer c = ContainerFactory.createContainer(GrizzlyHttpContainer.class, rc);
|
jtulach@6
|
62 |
Closeable s = Launcher.showURL(
|
jtulach@6
|
63 |
"bck2brwsr", ChatClient.class.getClassLoader(),
|
jtulach@6
|
64 |
"/org/apidesign/bck2brwsr/demo/serverside/chat.html"
|
jtulach@6
|
65 |
);
|
jtulach@6
|
66 |
Launcher l = (Launcher) s;
|
jtulach@6
|
67 |
Callable<?> f = (Callable<?>) l;
|
jtulach@6
|
68 |
HttpServer server = (HttpServer) f.call();
|
jtulach@6
|
69 |
ServerConfiguration conf = server.getServerConfiguration();
|
jtulach@6
|
70 |
conf.addHttpHandler(c, "/chat");
|
jtulach@6
|
71 |
System.in.read();
|
jtulach@6
|
72 |
s.close();
|
jtulach@6
|
73 |
}
|
jtulach@6
|
74 |
|
jtulach@6
|
75 |
private List<Message> msgs = new ArrayList<>();
|
jtulach@6
|
76 |
{
|
jtulach@6
|
77 |
Message welcome = new Message(Context.findDefault(Message.class));
|
jtulach@6
|
78 |
welcome.setUser("system");
|
jtulach@6
|
79 |
welcome.setComment("Welcome and enjoy!");
|
jtulach@8
|
80 |
welcome.setWhen(System.currentTimeMillis());
|
jtulach@6
|
81 |
msgs.add(welcome);
|
jtulach@6
|
82 |
}
|
jtulach@6
|
83 |
|
jtulach@11
|
84 |
private final Map<AsyncResponse, Long> awaiting = new IdentityHashMap<>();
|
jtulach@11
|
85 |
|
jtulach@6
|
86 |
@Produces(MediaType.APPLICATION_JSON)
|
jtulach@11
|
87 |
@GET public synchronized void getResources(
|
jtulach@11
|
88 |
@QueryParam("since") @DefaultValue("0") long since,
|
jtulach@11
|
89 |
@Suspended AsyncResponse ar
|
jtulach@11
|
90 |
) {
|
jtulach@11
|
91 |
Query q = new Query(Context.findDefault(ChatServerResource.class));
|
jtulach@11
|
92 |
for (Message m : msgs) {
|
jtulach@11
|
93 |
if (m.getWhen() >= since) {
|
jtulach@11
|
94 |
q.getMessages().add(m);
|
jtulach@11
|
95 |
}
|
jtulach@11
|
96 |
}
|
jtulach@11
|
97 |
if (!q.getMessages().isEmpty()) {
|
jtulach@11
|
98 |
ar.resume(q);
|
jtulach@11
|
99 |
} else {
|
jtulach@11
|
100 |
awaiting.put(ar, since);
|
jtulach@11
|
101 |
}
|
jtulach@6
|
102 |
}
|
jtulach@6
|
103 |
|
jtulach@11
|
104 |
private void handleAwaiting(long newest) {
|
jtulach@11
|
105 |
assert Thread.holdsLock(this);
|
jtulach@11
|
106 |
AGAIN: for (;;) {
|
jtulach@11
|
107 |
for (Map.Entry<AsyncResponse, Long> entry : awaiting.entrySet()) {
|
jtulach@11
|
108 |
AsyncResponse ar = entry.getKey();
|
jtulach@11
|
109 |
Long since = entry.getValue();
|
jtulach@11
|
110 |
if (since <= newest) {
|
jtulach@11
|
111 |
awaiting.remove(ar);
|
jtulach@11
|
112 |
getResources(since, ar);
|
jtulach@11
|
113 |
continue AGAIN;
|
jtulach@11
|
114 |
}
|
jtulach@11
|
115 |
}
|
jtulach@11
|
116 |
return;
|
jtulach@6
|
117 |
}
|
jtulach@6
|
118 |
}
|
jtulach@11
|
119 |
|
jtulach@11
|
120 |
@Path("addComment") @GET
|
jtulach@11
|
121 |
public synchronized Message publish(
|
jtulach@11
|
122 |
@QueryParam("user") String user,
|
jtulach@11
|
123 |
@QueryParam("comment") String comment
|
jtulach@11
|
124 |
) {
|
jtulach@11
|
125 |
Message msg = new Message(Context.findDefault(ChatServerResource.class));
|
jtulach@11
|
126 |
msg.setUser(user);
|
jtulach@11
|
127 |
msg.setComment(comment);
|
jtulach@11
|
128 |
msg.setWhen(System.currentTimeMillis());
|
jtulach@11
|
129 |
msgs.add(msg);
|
jtulach@11
|
130 |
handleAwaiting(msg.getWhen());
|
jtulach@11
|
131 |
return msg;
|
jtulach@11
|
132 |
}
|
jtulach@6
|
133 |
}
|