2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.freemarkerdor;
29 import com.sun.jersey.api.client.Client;
30 import com.sun.jersey.api.client.UniformInterfaceException;
31 import com.sun.jersey.api.client.WebResource;
32 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
33 import com.sun.jersey.api.core.PackagesResourceConfig;
34 import com.sun.jersey.api.core.ResourceConfig;
35 import com.sun.jersey.api.view.Viewable;
36 import com.sun.net.httpserver.HttpServer;
39 import java.util.HashMap;
40 import java.util.Locale;
42 import java.util.MissingResourceException;
43 import java.util.ResourceBundle;
44 import java.util.concurrent.Callable;
45 import javax.ws.rs.DefaultValue;
46 import javax.ws.rs.FormParam;
47 import javax.ws.rs.GET;
48 import javax.ws.rs.POST;
49 import javax.ws.rs.Path;
50 import javax.ws.rs.PathParam;
51 import javax.ws.rs.Produces;
52 import javax.ws.rs.QueryParam;
53 import javax.ws.rs.core.Context;
54 import javax.ws.rs.core.HttpHeaders;
55 import javax.ws.rs.core.MediaType;
56 import javax.ws.rs.core.NewCookie;
57 import javax.ws.rs.core.Response;
58 import org.w3c.dom.Document;
62 * @author Jaroslav Tulach <jtulach@netbeans.org>
65 public final class UI {
66 private static WebResource base;
69 private HttpHeaders headers;
76 private Viewable checkLogin() {
77 if (headers.getCookies().containsKey("login")) {
78 String id = headers.getCookies().get("login").getValue();
81 us = base.path("login").queryParam("id", id).
82 accept(MediaType.TEXT_PLAIN).get(String.class);
83 } catch (UniformInterfaceException ex) {
87 if (us.length() > 0) {
93 return viewable("login.fmt", null);
98 @Produces(MediaType.TEXT_HTML)
99 public Response login(
100 @FormParam("name") String name, @FormParam("password") String password
102 uuid = base.path("login").queryParam("name", name).queryParam("password", password).
103 accept(MediaType.TEXT_PLAIN).put(String.class);
106 return Response.ok().cookie(new NewCookie("login", uuid)).entity(viewable("login.fmt", null)).build();
108 Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
109 return Response.status(1).entity(v).build();
114 @Produces(MediaType.TEXT_HTML)
115 public Viewable welcome() {
116 Viewable v = checkLogin();
120 return welcomeImpl();
125 @Produces(MediaType.TEXT_HTML)
126 public Viewable board(@PathParam("id") String id) {
127 return board(id, null);
129 private Viewable board(@PathParam("id") String id, String msg) {
130 Viewable v = checkLogin();
134 Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
135 return viewable("game.fmt", doc, "message", msg);
139 @Path("games/{id}/move")
140 @Produces(MediaType.TEXT_HTML)
141 public Viewable move(
142 @PathParam("id") String id,
143 @QueryParam("type") String type,
144 @QueryParam("direction") String direction,
145 @QueryParam("direction-next") @DefaultValue("") String directionNext,
146 @QueryParam("column") @DefaultValue("") String column,
147 @QueryParam("row") @DefaultValue("") String row
149 Viewable v = checkLogin();
153 WebResource wr = base.path("games").path(id).
154 queryParam("loginID", uuid).
155 queryParam("player", user);
157 if (type.equals("resign")) {
158 wr.queryParam("move", "RESIGN").put();
161 if (type.equals("fence")) {
162 wr.queryParam("move", direction.charAt(0) + column + row).put();
165 if (type.equals("move")) {
166 wr.queryParam("move", direction + directionNext).put();
169 } catch (UniformInterfaceException ex) {
170 return board(id, "WRONG_MOVE");
176 @Path("games/create")
177 @Produces(MediaType.TEXT_HTML)
178 public Response create(
179 @QueryParam("white") String white,
180 @QueryParam("black") String black
182 Viewable v = checkLogin();
184 return Response.status(Response.Status.FORBIDDEN).entity(v).build();
187 if (user.equals(white) || user.equals(black)) {
190 queryParam("loginID", uuid).
191 queryParam("white", white).
192 queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class);
193 return Response.ok(welcomeImpl()).build();
195 return Response.status(Response.Status.NOT_FOUND).
196 entity(welcomeImpl("message", "You (" + user + ") must be white or black!")).build();
200 private Viewable welcomeImpl(Object... args) {
201 final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
202 return viewable("index.fmt", got, args);
209 public static void main(String[] args) throws Exception {
211 if (args.length > 1) {
212 port = Integer.parseInt(args[0]);
216 Callable<Void> r = startServers(port);
218 if (args.length > 2 && args[1].equals("--kill")) {
219 System.out.println("Hit enter to stop it...");
222 synchronized (UI.class) {
230 static Callable<Void> startServers(int port) throws Exception {
232 if (System.getProperty("quoridor.dir") == null) {
233 File home = new File(System.getProperty("user.home"));
234 File quoridor = new File(home, ".quoridor");
235 System.setProperty("quoridor.dir", quoridor.getPath());
238 ResourceConfig rc = new PackagesResourceConfig(
239 "cz.xelfi.quoridor.webidor",
240 "cz.xelfi.quoridor.freemarkerdor"
243 Client client = new Client();
244 base = client.resource(new URI("http://localhost:" + port + "/api/"));
246 final String baseUri = "http://localhost:" + port + "/";
247 final HttpServer server = HttpServerFactory.create(baseUri, rc);
249 System.out.println("Quoridor started at port " + port);
251 return new Callable<Void>() {
252 public Void call() throws Exception {
259 private Viewable viewable(String page, Document doc, Object... more) {
260 ResourceBundle rb = null;
261 for (Locale l : headers.getAcceptableLanguages()) {
263 rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
264 } catch (MissingResourceException e) {
269 rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", Locale.ENGLISH);
272 Map<String,Object> map = new HashMap<String,Object>();
274 map.put("user", user);
275 map.put("bundle", rb);
276 map.put("now", System.currentTimeMillis());
277 for (int i = 0; i < more.length; i += 2) {
278 map.put((String)more[i],more[i + 1]);
280 return new Viewable(page, map);