jtulach@41
|
1 |
/*
|
jtulach@41
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@41
|
3 |
*
|
jtulach@41
|
4 |
* The contents of this file are subject to the terms of either the GNU
|
jtulach@41
|
5 |
* General Public License Version 2 only ("GPL") or the Common
|
jtulach@41
|
6 |
* Development and Distribution License("CDDL") (collectively, the
|
jtulach@41
|
7 |
* "License"). You may not use this file except in compliance with the
|
jtulach@41
|
8 |
* License. You can obtain a copy of the License at
|
jtulach@41
|
9 |
* http://www.netbeans.org/cddl-gplv2.html
|
jtulach@41
|
10 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jtulach@41
|
11 |
* specific language governing permissions and limitations under the
|
jtulach@41
|
12 |
* License. When distributing the software, include this License Header
|
jtulach@41
|
13 |
* Notice in each file and include the License file at
|
jtulach@41
|
14 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
jtulach@41
|
15 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@41
|
16 |
* by Sun in the GPL Version 2 section of the License file that
|
jtulach@41
|
17 |
* accompanied this code. If applicable, add the following below the
|
jtulach@41
|
18 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jtulach@41
|
19 |
* your own identifying information:
|
jtulach@41
|
20 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@41
|
21 |
*
|
jtulach@41
|
22 |
* Contributor(s):
|
jtulach@41
|
23 |
*
|
jtulach@41
|
24 |
* Portions Copyrighted 2009 Jaroslav Tulach
|
jtulach@41
|
25 |
*/
|
jtulach@41
|
26 |
|
jtulach@41
|
27 |
package cz.xelfi.quoridor.freemarkerdor;
|
jtulach@41
|
28 |
|
jtulach@41
|
29 |
import com.sun.jersey.api.client.Client;
|
jaroslav@56
|
30 |
import com.sun.jersey.api.client.UniformInterfaceException;
|
jtulach@41
|
31 |
import com.sun.jersey.api.client.WebResource;
|
jaroslav@146
|
32 |
import com.sun.jersey.api.client.config.DefaultClientConfig;
|
jtulach@41
|
33 |
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
|
jtulach@41
|
34 |
import com.sun.jersey.api.core.PackagesResourceConfig;
|
jtulach@41
|
35 |
import com.sun.jersey.api.core.ResourceConfig;
|
jtulach@41
|
36 |
import com.sun.jersey.api.view.Viewable;
|
jtulach@41
|
37 |
import com.sun.net.httpserver.HttpServer;
|
jaroslav@99
|
38 |
import cz.xelfi.quoridor.Board;
|
jaroslav@99
|
39 |
import cz.xelfi.quoridor.IllegalPositionException;
|
jaroslav@88
|
40 |
import java.io.IOException;
|
jaroslav@88
|
41 |
import java.io.InputStream;
|
jtulach@41
|
42 |
import java.net.URI;
|
jaroslav@55
|
43 |
import java.util.HashMap;
|
jaroslav@59
|
44 |
import java.util.Locale;
|
jaroslav@55
|
45 |
import java.util.Map;
|
jaroslav@59
|
46 |
import java.util.MissingResourceException;
|
jaroslav@88
|
47 |
import java.util.Properties;
|
jaroslav@59
|
48 |
import java.util.ResourceBundle;
|
jaroslav@50
|
49 |
import java.util.concurrent.Callable;
|
jtulach@43
|
50 |
import javax.ws.rs.DefaultValue;
|
jaroslav@47
|
51 |
import javax.ws.rs.FormParam;
|
jtulach@41
|
52 |
import javax.ws.rs.GET;
|
jaroslav@46
|
53 |
import javax.ws.rs.POST;
|
jtulach@41
|
54 |
import javax.ws.rs.Path;
|
jtulach@41
|
55 |
import javax.ws.rs.PathParam;
|
jaroslav@46
|
56 |
import javax.ws.rs.Produces;
|
jtulach@42
|
57 |
import javax.ws.rs.QueryParam;
|
jaroslav@129
|
58 |
import javax.ws.rs.core.CacheControl;
|
jtulach@42
|
59 |
import javax.ws.rs.core.Context;
|
jaroslav@101
|
60 |
import javax.ws.rs.core.Cookie;
|
jtulach@42
|
61 |
import javax.ws.rs.core.HttpHeaders;
|
jtulach@41
|
62 |
import javax.ws.rs.core.MediaType;
|
jaroslav@46
|
63 |
import javax.ws.rs.core.NewCookie;
|
jaroslav@46
|
64 |
import javax.ws.rs.core.Response;
|
jaroslav@101
|
65 |
import javax.ws.rs.core.Response.ResponseBuilder;
|
jaroslav@99
|
66 |
import org.openide.util.Exceptions;
|
jaroslav@48
|
67 |
import org.w3c.dom.Document;
|
jtulach@41
|
68 |
|
jtulach@41
|
69 |
/**
|
jtulach@41
|
70 |
*
|
jtulach@41
|
71 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@41
|
72 |
*/
|
jtulach@41
|
73 |
@Path("/")
|
jtulach@41
|
74 |
public final class UI {
|
jaroslav@88
|
75 |
private static final String version;
|
jaroslav@88
|
76 |
static {
|
jaroslav@88
|
77 |
Properties p = new Properties();
|
jaroslav@88
|
78 |
try {
|
jaroslav@88
|
79 |
InputStream is = FreemarkerProcessor.class.getResourceAsStream("/META-INF/maven/org.apidesign/freemarkerdor/pom.properties"); // NOI18N
|
jaroslav@88
|
80 |
if (is != null) {
|
jaroslav@88
|
81 |
p.load(is);
|
jaroslav@88
|
82 |
}
|
jaroslav@88
|
83 |
} catch (IOException ex) {
|
jaroslav@88
|
84 |
ex.printStackTrace();
|
jaroslav@88
|
85 |
}
|
jaroslav@88
|
86 |
version = p.getProperty("version", "unknown"); // NOI18N
|
jaroslav@88
|
87 |
}
|
jtulach@41
|
88 |
private static WebResource base;
|
jtulach@42
|
89 |
|
jtulach@42
|
90 |
@Context
|
jtulach@42
|
91 |
private HttpHeaders headers;
|
jaroslav@146
|
92 |
private UserInfo user;
|
jtulach@82
|
93 |
private String uuid;
|
jtulach@42
|
94 |
|
jtulach@41
|
95 |
public UI() {
|
jtulach@41
|
96 |
}
|
jtulach@41
|
97 |
|
jaroslav@46
|
98 |
private Viewable checkLogin() {
|
jaroslav@101
|
99 |
Cookie cookie = headers.getCookies().get("login");
|
jaroslav@101
|
100 |
if (cookie != null) {
|
jaroslav@101
|
101 |
String id = cookie.getValue();
|
jaroslav@146
|
102 |
UserInfo us;
|
jaroslav@85
|
103 |
try {
|
jaroslav@146
|
104 |
us = base.path("users").queryParam("loginID", id).
|
jaroslav@146
|
105 |
accept(MediaType.TEXT_XML).get(UserInfo.class);
|
jaroslav@146
|
106 |
} catch (Exception ex) {
|
jaroslav@85
|
107 |
ex.printStackTrace();
|
jaroslav@146
|
108 |
us = null;
|
jaroslav@85
|
109 |
}
|
jaroslav@146
|
110 |
if (us != null && us.getId().length() > 0) {
|
jtulach@83
|
111 |
user = us;
|
jtulach@83
|
112 |
uuid = id;
|
jtulach@83
|
113 |
return null;
|
jtulach@83
|
114 |
}
|
jaroslav@46
|
115 |
}
|
jaroslav@56
|
116 |
return viewable("login.fmt", null);
|
jaroslav@46
|
117 |
}
|
jaroslav@46
|
118 |
|
jaroslav@46
|
119 |
@POST
|
jaroslav@46
|
120 |
@Path("login")
|
jaroslav@46
|
121 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@46
|
122 |
public Response login(
|
jaroslav@47
|
123 |
@FormParam("name") String name, @FormParam("password") String password
|
jaroslav@46
|
124 |
) throws Exception {
|
jtulach@82
|
125 |
uuid = base.path("login").queryParam("name", name).queryParam("password", password).
|
jtulach@82
|
126 |
accept(MediaType.TEXT_PLAIN).put(String.class);
|
jtulach@82
|
127 |
if (uuid != null) {
|
jaroslav@146
|
128 |
user = new UserInfo(name);
|
jaroslav@101
|
129 |
NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false);
|
jaroslav@101
|
130 |
return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build();
|
jaroslav@46
|
131 |
} else {
|
jaroslav@56
|
132 |
Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
|
jaroslav@46
|
133 |
return Response.status(1).entity(v).build();
|
jaroslav@46
|
134 |
}
|
jaroslav@46
|
135 |
}
|
jaroslav@46
|
136 |
|
jtulach@41
|
137 |
@GET
|
jaroslav@46
|
138 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@129
|
139 |
public Response welcome(@QueryParam("maxItems") @DefaultValue("10") int maxItems) {
|
jaroslav@46
|
140 |
Viewable v = checkLogin();
|
jaroslav@129
|
141 |
ResponseBuilder resp = Response.ok();
|
jaroslav@129
|
142 |
if (v == null) {
|
jaroslav@129
|
143 |
v = welcomeImpl("maxItems", maxItems);
|
jtulach@42
|
144 |
}
|
jaroslav@129
|
145 |
CacheControl cc = new CacheControl();
|
jaroslav@129
|
146 |
cc.setNoCache(true);
|
jaroslav@129
|
147 |
resp.cacheControl(cc);
|
jaroslav@129
|
148 |
return resp.entity(v).build();
|
jtulach@41
|
149 |
}
|
jtulach@41
|
150 |
|
jaroslav@105
|
151 |
@GET
|
jaroslav@105
|
152 |
@Path("games/{id}.png")
|
jaroslav@105
|
153 |
@Produces("image/png")
|
jaroslav@129
|
154 |
public Response getBoardImage(
|
jaroslav@105
|
155 |
@PathParam("id") String id,
|
jaroslav@105
|
156 |
@QueryParam("fieldSize") @DefaultValue("50") int fieldSize,
|
jaroslav@105
|
157 |
@QueryParam("move") @DefaultValue("-1") int move
|
jaroslav@105
|
158 |
) throws IllegalPositionException {
|
jaroslav@105
|
159 |
WebResource wr = base.path("games").path(id);
|
jaroslav@105
|
160 |
if (move != -1) {
|
jaroslav@106
|
161 |
wr = wr.queryParam("move", "" + move);
|
jaroslav@105
|
162 |
}
|
jaroslav@105
|
163 |
String txt = wr.accept(MediaType.TEXT_PLAIN).get(String.class);
|
jaroslav@105
|
164 |
Board b = Board.valueOf(txt);
|
jaroslav@129
|
165 |
ResponseBuilder resp = Response.ok();
|
jaroslav@129
|
166 |
CacheControl cc = new CacheControl();
|
jaroslav@129
|
167 |
cc.setNoCache(true);
|
jaroslav@129
|
168 |
resp.cacheControl(cc);
|
jaroslav@129
|
169 |
return resp.entity(BoardImage.draw(b, fieldSize)).build();
|
jaroslav@105
|
170 |
}
|
jaroslav@105
|
171 |
|
jaroslav@105
|
172 |
|
jaroslav@101
|
173 |
private Response board(String id) {
|
jaroslav@100
|
174 |
return board(id, "", null);
|
jaroslav@95
|
175 |
}
|
jtulach@41
|
176 |
@GET
|
jtulach@43
|
177 |
@Path("games/{id}/")
|
jaroslav@46
|
178 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@101
|
179 |
public Response board(
|
jaroslav@100
|
180 |
@PathParam("id") String id,
|
jaroslav@100
|
181 |
@QueryParam("format") @DefaultValue("") String format,
|
jaroslav@100
|
182 |
@QueryParam("move") @DefaultValue("-1") String move
|
jaroslav@100
|
183 |
) {
|
jaroslav@100
|
184 |
return board(id, null, format, move);
|
jaroslav@56
|
185 |
}
|
jaroslav@101
|
186 |
private Response board(@PathParam("id") String id, String msg, String format, String m) {
|
jaroslav@46
|
187 |
Viewable v = checkLogin();
|
jaroslav@46
|
188 |
if (v != null) {
|
jaroslav@101
|
189 |
return Response.ok().entity(v).build();
|
jaroslav@46
|
190 |
}
|
jaroslav@100
|
191 |
int move;
|
jaroslav@100
|
192 |
try {
|
jaroslav@100
|
193 |
move = Integer.parseInt(m);
|
jaroslav@100
|
194 |
} catch (NumberFormatException ex) {
|
jaroslav@100
|
195 |
move = -1;
|
jaroslav@100
|
196 |
}
|
jaroslav@100
|
197 |
WebResource url = base.path("games").path(id);
|
jaroslav@100
|
198 |
if (move >= 0) {
|
jaroslav@100
|
199 |
url = url.queryParam("move", "" + move);
|
jaroslav@100
|
200 |
}
|
jaroslav@101
|
201 |
ResponseBuilder resp = Response.ok();
|
jaroslav@129
|
202 |
CacheControl cc = new CacheControl();
|
jaroslav@129
|
203 |
cc.setNoCache(true);
|
jaroslav@129
|
204 |
resp.cacheControl(cc);
|
jaroslav@101
|
205 |
Cookie cFormat = headers.getCookies().get("format");
|
jaroslav@101
|
206 |
if (format.length() == 0) {
|
jaroslav@101
|
207 |
if (cFormat != null) {
|
jaroslav@101
|
208 |
format = cFormat.getValue();
|
jaroslav@101
|
209 |
}
|
jaroslav@101
|
210 |
} else {
|
jaroslav@101
|
211 |
if (cFormat == null || !format.equals(cFormat.getValue())) {
|
jaroslav@101
|
212 |
resp.cookie(new NewCookie("format", format));
|
jaroslav@101
|
213 |
}
|
jaroslav@101
|
214 |
}
|
jaroslav@101
|
215 |
|
jaroslav@100
|
216 |
Document doc = url.accept(MediaType.TEXT_XML).get(Document.class);
|
jaroslav@99
|
217 |
Board b;
|
jaroslav@99
|
218 |
try {
|
jaroslav@99
|
219 |
b = Board.valueOf(doc.getElementsByTagName("board").item(0).getTextContent());
|
jaroslav@99
|
220 |
} catch (IllegalPositionException ex) {
|
jaroslav@99
|
221 |
Exceptions.printStackTrace(ex);
|
jaroslav@99
|
222 |
b = Board.empty();
|
jaroslav@99
|
223 |
}
|
jaroslav@101
|
224 |
v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b);
|
jaroslav@101
|
225 |
return resp.entity(v).build();
|
jtulach@42
|
226 |
}
|
jtulach@41
|
227 |
|
jtulach@42
|
228 |
@GET
|
jtulach@43
|
229 |
@Path("games/{id}/move")
|
jaroslav@46
|
230 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@101
|
231 |
public Response move(
|
jtulach@43
|
232 |
@PathParam("id") String id,
|
jtulach@43
|
233 |
@QueryParam("type") String type,
|
jtulach@43
|
234 |
@QueryParam("direction") String direction,
|
jtulach@43
|
235 |
@QueryParam("direction-next") @DefaultValue("") String directionNext,
|
jtulach@43
|
236 |
@QueryParam("column") @DefaultValue("") String column,
|
jtulach@43
|
237 |
@QueryParam("row") @DefaultValue("") String row
|
jtulach@54
|
238 |
) {
|
jaroslav@46
|
239 |
Viewable v = checkLogin();
|
jaroslav@46
|
240 |
if (v != null) {
|
jaroslav@101
|
241 |
return Response.ok().entity(v).build();
|
jaroslav@46
|
242 |
}
|
jtulach@82
|
243 |
WebResource wr = base.path("games").path(id).
|
jtulach@82
|
244 |
queryParam("loginID", uuid).
|
jaroslav@146
|
245 |
queryParam("player", user.getId());
|
jaroslav@56
|
246 |
try {
|
jtulach@75
|
247 |
if (type.equals("resign")) {
|
jtulach@75
|
248 |
wr.queryParam("move", "RESIGN").put();
|
jtulach@75
|
249 |
return board(id);
|
jtulach@75
|
250 |
}
|
jaroslav@56
|
251 |
if (type.equals("fence")) {
|
jaroslav@56
|
252 |
wr.queryParam("move", direction.charAt(0) + column + row).put();
|
jaroslav@56
|
253 |
return board(id);
|
jaroslav@56
|
254 |
}
|
jaroslav@56
|
255 |
if (type.equals("move")) {
|
jaroslav@56
|
256 |
wr.queryParam("move", direction + directionNext).put();
|
jaroslav@56
|
257 |
return board(id);
|
jaroslav@56
|
258 |
}
|
jaroslav@56
|
259 |
} catch (UniformInterfaceException ex) {
|
jaroslav@100
|
260 |
return board(id, "WRONG_MOVE", "-1");
|
jtulach@43
|
261 |
}
|
jtulach@43
|
262 |
return board(id);
|
jtulach@43
|
263 |
}
|
jtulach@43
|
264 |
|
jtulach@43
|
265 |
@GET
|
jaroslav@115
|
266 |
@Path("games/{id}/comment")
|
jaroslav@115
|
267 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@115
|
268 |
public Response comment(
|
jaroslav@115
|
269 |
@PathParam("id") String id,
|
jaroslav@115
|
270 |
@QueryParam("comment") String comment
|
jaroslav@115
|
271 |
) {
|
jaroslav@115
|
272 |
Viewable v = checkLogin();
|
jaroslav@115
|
273 |
if (v != null) {
|
jaroslav@115
|
274 |
return Response.ok().entity(v).build();
|
jaroslav@115
|
275 |
}
|
jaroslav@115
|
276 |
WebResource wr = base.path("games").path(id).
|
jaroslav@115
|
277 |
queryParam("loginID", uuid).
|
jaroslav@146
|
278 |
queryParam("player", user.getId()).
|
jaroslav@115
|
279 |
queryParam("comment", comment);
|
jaroslav@115
|
280 |
wr.put();
|
jaroslav@115
|
281 |
|
jaroslav@115
|
282 |
return board(id);
|
jaroslav@115
|
283 |
}
|
jaroslav@115
|
284 |
|
jaroslav@115
|
285 |
@GET
|
jtulach@42
|
286 |
@Path("games/create")
|
jaroslav@46
|
287 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@73
|
288 |
public Response create(
|
jtulach@42
|
289 |
@QueryParam("white") String white,
|
jtulach@42
|
290 |
@QueryParam("black") String black
|
jtulach@54
|
291 |
) {
|
jaroslav@46
|
292 |
Viewable v = checkLogin();
|
jaroslav@46
|
293 |
if (v != null) {
|
jaroslav@73
|
294 |
return Response.status(Response.Status.FORBIDDEN).entity(v).build();
|
jaroslav@46
|
295 |
}
|
jaroslav@57
|
296 |
|
jaroslav@146
|
297 |
if (user.getId().equals(white) || user.getId().equals(black)) {
|
jaroslav@57
|
298 |
Object obj =
|
jtulach@82
|
299 |
base.path("games").
|
jtulach@82
|
300 |
queryParam("loginID", uuid).
|
jtulach@82
|
301 |
queryParam("white", white).
|
jaroslav@73
|
302 |
queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class);
|
jaroslav@73
|
303 |
return Response.ok(welcomeImpl()).build();
|
jaroslav@57
|
304 |
} else {
|
jaroslav@73
|
305 |
return Response.status(Response.Status.NOT_FOUND).
|
jaroslav@146
|
306 |
entity(welcomeImpl("message", "You (" + user.getId() + ") must be white or black!")).build();
|
jaroslav@57
|
307 |
}
|
jtulach@41
|
308 |
}
|
jtulach@41
|
309 |
|
jaroslav@57
|
310 |
private Viewable welcomeImpl(Object... args) {
|
jaroslav@56
|
311 |
final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
|
jaroslav@57
|
312 |
return viewable("index.fmt", got, args);
|
jtulach@41
|
313 |
}
|
jtulach@41
|
314 |
|
jaroslav@146
|
315 |
|
jaroslav@146
|
316 |
@GET
|
jaroslav@146
|
317 |
@Path("options")
|
jaroslav@146
|
318 |
public Response changeOptions(
|
jaroslav@146
|
319 |
@QueryParam("email") String email,
|
jaroslav@146
|
320 |
@QueryParam("language") String language
|
jaroslav@146
|
321 |
) {
|
jaroslav@146
|
322 |
Viewable v = checkLogin();
|
jaroslav@146
|
323 |
if (v != null) {
|
jaroslav@146
|
324 |
return Response.status(Response.Status.FORBIDDEN).entity(v).build();
|
jaroslav@146
|
325 |
}
|
jaroslav@146
|
326 |
|
jaroslav@146
|
327 |
if (email != null) {
|
jaroslav@146
|
328 |
UserInfo ui = base.path("users/" + user.getId()).
|
jaroslav@146
|
329 |
queryParam("loginID", uuid).
|
jaroslav@146
|
330 |
queryParam("name", "email").
|
jaroslav@146
|
331 |
queryParam("value", email).accept(MediaType.TEXT_XML).post(UserInfo.class);
|
jaroslav@146
|
332 |
}
|
jaroslav@146
|
333 |
|
jaroslav@146
|
334 |
if (language != null) {
|
jaroslav@146
|
335 |
UserInfo ui = base.path("users/" + user.getId()).
|
jaroslav@146
|
336 |
queryParam("loginID", uuid).
|
jaroslav@146
|
337 |
queryParam("name", "language").
|
jaroslav@146
|
338 |
queryParam("value", language).
|
jaroslav@146
|
339 |
accept(MediaType.TEXT_XML).post(UserInfo.class);
|
jaroslav@146
|
340 |
}
|
jaroslav@146
|
341 |
|
jaroslav@146
|
342 |
return welcome(10);
|
jaroslav@146
|
343 |
}
|
jaroslav@146
|
344 |
|
jtulach@41
|
345 |
//
|
jtulach@41
|
346 |
// start the server
|
jtulach@41
|
347 |
//
|
jtulach@41
|
348 |
|
jtulach@41
|
349 |
public static void main(String[] args) throws Exception {
|
jaroslav@121
|
350 |
int port = 9333;
|
jaroslav@68
|
351 |
if (args.length > 1) {
|
jaroslav@68
|
352 |
port = Integer.parseInt(args[0]);
|
jaroslav@68
|
353 |
}
|
jaroslav@132
|
354 |
String remoteAPI = args.length >= 2 ? args[1] : null;
|
jaroslav@50
|
355 |
|
jaroslav@146
|
356 |
Locale.setDefault(Locale.ROOT);
|
jaroslav@68
|
357 |
|
jaroslav@121
|
358 |
Callable<Void> r = startServers(port, remoteAPI);
|
jaroslav@68
|
359 |
|
jaroslav@122
|
360 |
if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
|
jaroslav@68
|
361 |
System.out.println("Hit enter to stop it...");
|
jaroslav@68
|
362 |
System.in.read();
|
jaroslav@68
|
363 |
} else {
|
jaroslav@68
|
364 |
synchronized (UI.class) {
|
jaroslav@68
|
365 |
UI.class.wait();
|
jaroslav@68
|
366 |
}
|
jaroslav@68
|
367 |
}
|
jaroslav@50
|
368 |
r.call();
|
jaroslav@50
|
369 |
System.exit(0);
|
jaroslav@50
|
370 |
}
|
jaroslav@50
|
371 |
|
jaroslav@121
|
372 |
static Callable<Void> startServers(int port, String remoteAPI) throws Exception {
|
jaroslav@121
|
373 |
Client client = new Client();
|
jaroslav@72
|
374 |
|
jaroslav@121
|
375 |
final HttpServer apiServer;
|
jaroslav@121
|
376 |
if (remoteAPI == null) {
|
jaroslav@126
|
377 |
throw new IllegalArgumentException("Provide URL to API server"); // NOI18N
|
jaroslav@121
|
378 |
} else {
|
jaroslav@121
|
379 |
base = client.resource(new URI(remoteAPI));
|
jaroslav@121
|
380 |
apiServer = null;
|
jaroslav@73
|
381 |
}
|
jaroslav@72
|
382 |
|
jaroslav@72
|
383 |
ResourceConfig rc = new PackagesResourceConfig(
|
jaroslav@72
|
384 |
"cz.xelfi.quoridor.freemarkerdor"
|
jaroslav@72
|
385 |
);
|
jaroslav@72
|
386 |
|
jaroslav@72
|
387 |
final String baseUri = "http://localhost:" + port + "/";
|
jaroslav@72
|
388 |
final HttpServer server = HttpServerFactory.create(baseUri, rc);
|
jaroslav@72
|
389 |
server.start();
|
jaroslav@68
|
390 |
System.out.println("Quoridor started at port " + port);
|
jaroslav@50
|
391 |
|
jaroslav@50
|
392 |
return new Callable<Void>() {
|
jaroslav@50
|
393 |
public Void call() throws Exception {
|
jaroslav@121
|
394 |
if (apiServer != null) {
|
jaroslav@121
|
395 |
apiServer.stop(0);
|
jaroslav@121
|
396 |
}
|
jaroslav@72
|
397 |
server.stop(0);
|
jaroslav@50
|
398 |
return null;
|
jaroslav@50
|
399 |
}
|
jaroslav@50
|
400 |
};
|
jtulach@41
|
401 |
}
|
jtulach@41
|
402 |
|
jaroslav@56
|
403 |
private Viewable viewable(String page, Document doc, Object... more) {
|
jaroslav@59
|
404 |
ResourceBundle rb = null;
|
jaroslav@146
|
405 |
String lng = user == null ? null : user.getProperty("language"); // NOI18N
|
jaroslav@146
|
406 |
if (lng != null) {
|
jaroslav@146
|
407 |
try {
|
jaroslav@146
|
408 |
Locale l = new Locale(lng);
|
jaroslav@146
|
409 |
rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
|
jaroslav@146
|
410 |
} catch (MissingResourceException e) {
|
jaroslav@146
|
411 |
// OK
|
jaroslav@146
|
412 |
}
|
jaroslav@146
|
413 |
}
|
jaroslav@146
|
414 |
if (rb == null) {
|
jaroslav@146
|
415 |
for (Locale l : headers.getAcceptableLanguages()) {
|
jaroslav@146
|
416 |
try {
|
jaroslav@146
|
417 |
rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
|
jaroslav@146
|
418 |
} catch (MissingResourceException e) {
|
jaroslav@146
|
419 |
// OK
|
jaroslav@146
|
420 |
}
|
jaroslav@59
|
421 |
}
|
jaroslav@59
|
422 |
}
|
jaroslav@59
|
423 |
if (rb == null) {
|
jtulach@80
|
424 |
rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", Locale.ENGLISH);
|
jaroslav@59
|
425 |
}
|
jaroslav@59
|
426 |
|
jaroslav@56
|
427 |
Map<String,Object> map = new HashMap<String,Object>();
|
jaroslav@56
|
428 |
map.put("doc", doc);
|
jaroslav@146
|
429 |
if (user != null) {
|
jaroslav@146
|
430 |
map.put("user", user.getId());
|
jaroslav@146
|
431 |
map.put("email", user.getProperty("email"));
|
jaroslav@146
|
432 |
}
|
jaroslav@59
|
433 |
map.put("bundle", rb);
|
jtulach@78
|
434 |
map.put("now", System.currentTimeMillis());
|
jaroslav@88
|
435 |
map.put("version", version);
|
jaroslav@56
|
436 |
for (int i = 0; i < more.length; i += 2) {
|
jaroslav@56
|
437 |
map.put((String)more[i],more[i + 1]);
|
jaroslav@56
|
438 |
}
|
jaroslav@56
|
439 |
return new Viewable(page, map);
|
jaroslav@56
|
440 |
}
|
jaroslav@56
|
441 |
|
jtulach@41
|
442 |
}
|