2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
18 package cz.xelfi.quoridor.freemarkerdor;
20 import com.sun.jersey.api.client.Client;
21 import com.sun.jersey.api.client.UniformInterfaceException;
22 import com.sun.jersey.api.client.WebResource;
23 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
24 import com.sun.jersey.api.core.PackagesResourceConfig;
25 import com.sun.jersey.api.core.ResourceConfig;
26 import com.sun.jersey.api.view.Viewable;
27 import com.sun.net.httpserver.HttpServer;
28 import cz.xelfi.quoridor.Board;
29 import cz.xelfi.quoridor.IllegalPositionException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.StringWriter;
34 import java.text.MessageFormat;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Date;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Locale;
42 import java.util.MissingResourceException;
43 import java.util.Properties;
44 import java.util.ResourceBundle;
45 import java.util.concurrent.Callable;
46 import javax.ws.rs.DefaultValue;
47 import javax.ws.rs.FormParam;
48 import javax.ws.rs.GET;
49 import javax.ws.rs.POST;
50 import javax.ws.rs.Path;
51 import javax.ws.rs.PathParam;
52 import javax.ws.rs.Produces;
53 import javax.ws.rs.QueryParam;
54 import javax.ws.rs.core.CacheControl;
55 import javax.ws.rs.core.Context;
56 import javax.ws.rs.core.Cookie;
57 import javax.ws.rs.core.HttpHeaders;
58 import javax.ws.rs.core.MediaType;
59 import javax.ws.rs.core.NewCookie;
60 import javax.ws.rs.core.Response;
61 import javax.ws.rs.core.Response.ResponseBuilder;
62 import org.openide.util.Exceptions;
63 import org.w3c.dom.Document;
67 * @author Jaroslav Tulach <jtulach@netbeans.org>
70 public final class UI {
71 private static final String version;
73 Properties p = new Properties();
75 InputStream is = FreemarkerProcessor.class.getResourceAsStream("/META-INF/maven/cz.xelfi.quoridor/freemarkerdor/pom.properties"); // NOI18N
79 } catch (IOException ex) {
82 version = p.getProperty("version", "unknown"); // NOI18N
84 private static WebResource base;
85 private static WebResource stat;
86 private static WebResource web;
87 private static Requests requests;
90 private HttpHeaders headers;
91 private UserInfo user;
97 private String login() {
98 Cookie cookie = headers.getCookies().get("login");
100 return cookie.getValue();
105 private Viewable checkLogin() {
110 us = base.path("users").queryParam("loginID", id).
111 accept(MediaType.TEXT_XML).get(UserInfo.class);
112 } catch (Exception ex) {
113 ex.printStackTrace();
116 if (us != null && us.getId().length() > 0) {
122 return viewable("login.fmt", null);
127 @Produces(MediaType.TEXT_HTML)
128 public Response login(
129 @FormParam("name") String name, @FormParam("password") String password
131 uuid = base.path("login").queryParam("name", name).queryParam("password", password).
132 accept(MediaType.TEXT_PLAIN).put(String.class);
134 user = new UserInfo(name);
135 NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false);
136 return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build();
138 Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
139 return Response.status(1).entity(v).build();
144 @Produces(MediaType.TEXT_HTML)
145 public Response welcome(@QueryParam("maxItems") @DefaultValue("10") int maxItems) {
146 Viewable v = checkLogin();
147 ResponseBuilder resp = Response.ok();
149 v = welcomeImpl("maxItems", maxItems);
151 CacheControl cc = new CacheControl();
153 resp.cacheControl(cc);
154 return resp.entity(v).build();
158 @Path("games/{id}.png")
159 @Produces("image/png")
160 public Response getBoardImage(
161 @PathParam("id") String id,
162 @QueryParam("fieldSize") @DefaultValue("50") int fieldSize,
163 @QueryParam("move") @DefaultValue("-1") int move
164 ) throws IllegalPositionException {
165 WebResource wr = base.path("games").path(id);
167 wr = wr.queryParam("move", "" + move);
169 String txt = wr.accept(MediaType.TEXT_PLAIN).get(String.class);
170 Board b = Board.valueOf(txt);
171 // Board b = new Board(txt);
172 ResponseBuilder resp = Response.ok();
173 CacheControl cc = new CacheControl();
175 resp.cacheControl(cc);
176 return resp.entity(BoardImage.draw(b, fieldSize)).build();
180 private Response board(String id) {
181 return board(id, "", null);
185 @Produces(MediaType.TEXT_HTML)
186 public Response board(
187 @PathParam("id") String id,
188 @QueryParam("format") @DefaultValue("") String format,
189 @QueryParam("move") @DefaultValue("-1") String move
191 return board(id, null, format, move);
193 private Response board(@PathParam("id") String id, String msg, String format, String m) {
194 Viewable v = checkLogin();
196 return Response.ok().entity(v).build();
200 move = Integer.parseInt(m);
201 } catch (NumberFormatException ex) {
204 WebResource url = base.path("games").queryParam("loginID", uuid).path(id);
206 url = url.queryParam("move", "" + move);
208 ResponseBuilder resp = Response.ok();
209 CacheControl cc = new CacheControl();
211 resp.cacheControl(cc);
212 Cookie cFormat = headers.getCookies().get("format");
213 if (format.length() == 0) {
214 if (cFormat != null) {
215 format = cFormat.getValue();
217 if (isMobile(headers)) {
222 if (cFormat == null || !format.equals(cFormat.getValue())) {
223 resp.cookie(new NewCookie("format", format));
227 Document doc = url.accept(MediaType.TEXT_XML).get(Document.class);
229 String t = doc.getElementsByTagName("board").item(0).getTextContent();
231 b = Board.valueOf(doc.getElementsByTagName("board").item(0).getTextContent());
232 } catch (Exception ex) {
234 // } catch (IllegalStateException ex) {
235 Exceptions.printStackTrace(ex);
240 bCode = stat.path("openings").path(b.getCode()+".check").queryParam("loginID", user.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
244 if(bCode == null || "".equals(bCode))
245 v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture", boardToPicture(b));
247 v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture", boardToPicture(b),"bCode", bCode);
248 return resp.entity(v).build();
251 private static String boardToPicture(Board b) {
252 StringWriter w = new StringWriter();
255 } catch (IOException ex) {
256 return ex.toString();
262 @Path("games/{id}/move")
263 @Produces(MediaType.TEXT_HTML)
264 public Response move(
265 @PathParam("id") String id,
266 @QueryParam("type") String type,
267 @QueryParam("direction") String direction,
268 @QueryParam("direction-next") @DefaultValue("") String directionNext,
269 @QueryParam("column") @DefaultValue("") String column,
270 @QueryParam("row") @DefaultValue("") String row
272 Viewable v = checkLogin();
274 return Response.ok().entity(v).build();
276 WebResource wr = base.path("games").path(id).
277 queryParam("loginID", uuid).
278 queryParam("player", user.getId());
280 if (type.equals("resign")) {
281 wr.queryParam("move", "RESIGN").put();
284 if (type.equals("fence")) {
285 wr.queryParam("move", direction.charAt(0) + column + row).put();
288 if (type.equals("move")) {
289 wr.queryParam("move", direction + directionNext).put();
292 } catch (UniformInterfaceException ex) {
293 return board(id, "WRONG_MOVE", "-1");
299 @Path("games/{id}/comment")
300 @Produces(MediaType.TEXT_HTML)
301 public Response comment(
302 @PathParam("id") String id,
303 @QueryParam("comment") String comment
305 Viewable v = checkLogin();
307 return Response.ok().entity(v).build();
309 WebResource wr = base.path("games").path(id).
310 queryParam("loginID", uuid).
311 queryParam("player", user.getId()).
312 queryParam("comment", comment);
319 @Path("games/create")
320 @Produces(MediaType.TEXT_HTML)
321 public Response create(
322 @QueryParam("white") String white,
323 @QueryParam("black") String black
325 Viewable v = checkLogin();
327 return Response.status(Response.Status.FORBIDDEN).entity(v).build();
330 if (user.getId().equals(white) || user.getId().equals(black)) {
333 queryParam("loginID", uuid).
334 queryParam("white", white).
335 queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class);
336 return Response.ok(welcomeImpl()).build();
338 return Response.status(Response.Status.NOT_FOUND).
339 entity(welcomeImpl("message", "You (" + user.getId() + ") must be white or black!")).build();
343 private Viewable welcomeImpl(Object... args) {
344 final Document got = base.path("games").queryParam("loginID", uuid).accept(MediaType.TEXT_XML).get(Document.class);
345 return viewable("index.fmt", got, args);
349 public Requests getRequests() {
350 if (requests == null) {
351 requests = new Requests(web.path("requests"));
359 public Response changeOptions(
360 @QueryParam("email") String email,
361 @QueryParam("language") String language,
362 @QueryParam("verified") String verified
363 ) throws IOException {
364 Viewable v = checkLogin();
366 return Response.status(Response.Status.FORBIDDEN).entity(v).build();
370 if (getRequests().isVerified(verified)) {
371 UserInfo ui = base.path("users/" + user.getId()).
372 queryParam("loginID", uuid).
373 queryParam("name", "email").
374 queryParam("value", email).accept(MediaType.TEXT_XML).post(UserInfo.class);
377 request = web.path("options").queryParam("name", "email").queryParam("email", email);
378 URI callback = getRequests().register(login(), request);
380 ResourceBundle rb = bundle(null);
381 String subject = rb.getString("MSG_ChangeEmailSubject");
382 String text = MessageFormat.format(rb.getString("MSG_ChangeEmailText"), user.getId(), callback);
383 EmailService.getDefault().sendEmail(email, subject, text);
384 return Response.ok(viewable("email.fmt", null)).build();
389 if (language != null) {
390 UserInfo ui = base.path("users/" + user.getId()).
391 queryParam("loginID", uuid).
392 queryParam("name", "language").
393 queryParam("value", language).
394 accept(MediaType.TEXT_XML).post(UserInfo.class);
402 @Produces(MediaType.TEXT_HTML)
403 public Response getEloList(
404 @QueryParam("historyId") @DefaultValue("0") Integer historyId){
405 Viewable v = checkLogin();
407 return Response.status(Response.Status.FORBIDDEN).entity(v).build();
409 final Document got = stat.path("elo").path("list").path(historyId.toString()).accept(MediaType.TEXT_XML).get(Document.class);
410 return Response.ok(viewable("elo.fmt", got, "historyId", historyId)).build();
415 @Produces(MediaType.TEXT_HTML)
416 public Response getOpeningRoot(){
417 return getOpeningNode("ROOT");
421 @Path("openings/{code}")
422 @Produces(MediaType.TEXT_HTML)
423 public Response getOpeningNode(@PathParam("code") String code){
424 Viewable v = checkLogin();
426 return Response.status(Response.Status.FORBIDDEN).entity(v).build();
428 final Document got = stat.path("openings").path(code).queryParam("loginID", user.getId()).accept(MediaType.TEXT_XML).get(Document.class);
431 b = Board.valueOf(got.getElementsByTagName("nodeCode").item(0).getTextContent());
432 } catch (Exception ex) {
433 Exceptions.printStackTrace(ex);
436 return Response.ok(viewable("openings.fmt", got, "whitefences",b.getPlayers().get(0).getFences(),"blackfences",b.getPlayers().get(1).getFences())).build();
440 @Path("openings/{code}/{status}")
441 @Produces(MediaType.TEXT_HTML)
442 public Response getOpeningNodeGames(@PathParam("code") String code, @PathParam("status") String status){
443 Viewable v = checkLogin();
445 return Response.status(Response.Status.FORBIDDEN).entity(v).build();
447 final Document got = stat.path("openings").path(code).path(status).queryParam("loginID", user.getId()).accept(MediaType.TEXT_XML).get(Document.class);
448 return Response.ok(viewable("opening_games.fmt", got,"code",code,"color",status)).build();
452 @Path("openings/{code}.png")
453 @Produces("image/png")
454 public Response getOpeningBoardImage(
455 @PathParam("code") String code,
456 @QueryParam("fieldSize") @DefaultValue("40") int fieldSize
457 ) throws IllegalPositionException {
458 Board b = Board.valueOf(code);
459 ResponseBuilder resp = Response.ok();
460 CacheControl cc = new CacheControl();
462 resp.cacheControl(cc);
463 return resp.entity(BoardImage.draw(b, fieldSize)).build();
470 public static void main(String[] params) throws Exception {
471 List<String> args = new ArrayList<String>(Arrays.asList(params));
473 String publicURL = null;
474 if (args.size() >= 2 && args.get(0).equals("--url")) {
475 publicURL = args.get(1);
481 if (args.size() > 1) {
482 port = Integer.parseInt(args.get(0));
484 String remoteAPI = args.size() >= 2 ? args.get(1) : null;
485 String remoteStatistics = args.size() >= 3 ? args.get(2) : null;
487 Locale.setDefault(Locale.ROOT);
489 Callable<Void> r = startServers(port, remoteAPI, remoteStatistics, publicURL);
491 if (args.size() < 3 || !args.get(args.size() - 1).equals("--kill")) {
492 System.out.println("Hit enter to stop it...");
495 synchronized (UI.class) {
503 static Callable<Void> startServers(int port, String remoteAPI, String remoteStatistics, String publicURL) throws Exception {
504 Client client = new Client();
505 Client client1 = new Client();
507 final HttpServer apiServer;
508 if (remoteAPI == null) {
509 throw new IllegalArgumentException("Provide URL to API server"); // NOI18N
511 base = client.resource(new URI(remoteAPI));
515 if (remoteStatistics != null) {
516 stat = client1.resource(new URI(remoteStatistics));
518 stat = client1.resource(new URI("http://localhost:9444"));
521 ResourceConfig rc = new PackagesResourceConfig(
522 "cz.xelfi.quoridor.freemarkerdor"
525 final String baseUri = "http://localhost:" + port + "/";
526 if (publicURL == null) {
529 final HttpServer server = HttpServerFactory.create(baseUri, rc);
530 Client c3 = new Client();
531 web = c3.resource(publicURL);
533 System.out.println("Quoridor started at port " + port);
535 return new Callable<Void>() {
536 public Void call() throws Exception {
537 if (apiServer != null) {
546 private ResourceBundle bundle(Locale[] locale) {
547 ResourceBundle rb = null;
548 String lng = user == null ? null : user.getProperty("language"); // NOI18N
551 Locale l = new Locale(lng);
552 rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
553 if (locale != null) {
556 } catch (MissingResourceException e) {
561 for (Locale l : headers.getAcceptableLanguages()) {
563 rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
564 if (locale != null) {
568 } catch (MissingResourceException e) {
574 rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", Locale.ENGLISH);
575 if (locale != null) {
576 locale[0] = Locale.ENGLISH;
582 private Viewable viewable(String page, Document doc, Object... more) {
583 Locale[] locale = new Locale[1];
584 ResourceBundle rb = bundle(locale);
586 Map<String,Object> map = new HashMap<String,Object>();
587 class ConvertToDate extends HashMap<Object,Object> {
589 public Object get(Object o) {
590 long time = Long.parseLong(o.toString());
591 return new Date(time);
595 map.put("locale", locale[0].toString());
598 map.put("user", user.getId());
599 map.put("email", user.getProperty("email"));
601 map.put("bundle", rb);
602 map.put("toDate", new ConvertToDate());
603 map.put("now", System.currentTimeMillis());
604 map.put("version", version);
605 for (int i = 0; i < more.length; i += 2) {
606 map.put((String)more[i],more[i + 1]);
608 return new Viewable(page, map);
612 private static boolean isMobile(HttpHeaders headers) {
613 final String[] keywords = {
616 List<String> agent = headers.getRequestHeader(HttpHeaders.USER_AGENT);
618 for (String a : agent) {
619 for (String k : keywords) {