More tests to check behaviour of "UI", querying for text/xml in create new game page
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;
37 import cz.xelfi.quoridor.webidor.resources.Quoridor;
39 import java.io.FileInputStream;
40 import java.io.IOException;
42 import java.util.HashMap;
43 import java.util.Locale;
45 import java.util.MissingResourceException;
46 import java.util.Properties;
47 import java.util.ResourceBundle;
48 import java.util.concurrent.Callable;
49 import javax.ws.rs.DefaultValue;
50 import javax.ws.rs.FormParam;
51 import javax.ws.rs.GET;
52 import javax.ws.rs.POST;
53 import javax.ws.rs.Path;
54 import javax.ws.rs.PathParam;
55 import javax.ws.rs.Produces;
56 import javax.ws.rs.QueryParam;
57 import javax.ws.rs.core.Context;
58 import javax.ws.rs.core.HttpHeaders;
59 import javax.ws.rs.core.MediaType;
60 import javax.ws.rs.core.NewCookie;
61 import javax.ws.rs.core.Response;
62 import org.w3c.dom.Document;
66 * @author Jaroslav Tulach <jtulach@netbeans.org>
69 public final class UI {
70 private static WebResource base;
73 private HttpHeaders headers;
79 private Viewable checkLogin() {
80 if (headers.getCookies().containsKey("login")) {
81 user = headers.getCookies().get("login").getValue();
84 return viewable("login.fmt", null);
89 @Produces(MediaType.TEXT_HTML)
90 public Response login(
91 @FormParam("name") String name, @FormParam("password") String password
93 File f = new File(new File(System.getProperty("quoridor.dir")), "passwd");
94 Properties p = new Properties();
96 p.load(new FileInputStream(f));
97 } catch (IOException ex) {
100 if (name != null && password.equals(p.getProperty(name))) {
102 return Response.ok().cookie(new NewCookie("login", name)).entity(viewable("login.fmt", null)).build();
104 Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
105 return Response.status(1).entity(v).build();
110 @Produces(MediaType.TEXT_HTML)
111 public Viewable welcome() {
112 Viewable v = checkLogin();
116 return welcomeImpl();
121 @Produces(MediaType.TEXT_HTML)
122 public Viewable board(@PathParam("id") String id) {
123 return board(id, null);
125 private Viewable board(@PathParam("id") String id, String msg) {
126 Viewable v = checkLogin();
130 Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
131 return viewable("game.fmt", doc, "message", msg);
135 @Path("games/{id}/move")
136 @Produces(MediaType.TEXT_HTML)
137 public Viewable move(
138 @PathParam("id") String id,
139 @QueryParam("type") String type,
140 @QueryParam("direction") String direction,
141 @QueryParam("direction-next") @DefaultValue("") String directionNext,
142 @QueryParam("column") @DefaultValue("") String column,
143 @QueryParam("row") @DefaultValue("") String row
145 Viewable v = checkLogin();
149 WebResource wr = base.path("games").path(id).queryParam("player", user);
151 if (type.equals("fence")) {
152 wr.queryParam("move", direction.charAt(0) + column + row).put();
155 if (type.equals("move")) {
156 wr.queryParam("move", direction + directionNext).put();
159 } catch (UniformInterfaceException ex) {
160 return board(id, "WRONG_MOVE");
166 @Path("games/create")
167 @Produces(MediaType.TEXT_HTML)
168 public Response create(
169 @QueryParam("white") String white,
170 @QueryParam("black") String black
172 Viewable v = checkLogin();
174 return Response.status(Response.Status.FORBIDDEN).entity(v).build();
177 if (user.equals(white) || user.equals(black)) {
179 base.path("games").queryParam("white", white).
180 queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class);
181 return Response.ok(welcomeImpl()).build();
183 return Response.status(Response.Status.NOT_FOUND).
184 entity(welcomeImpl("message", "You (" + user + ") must be white or black!")).build();
188 private Viewable welcomeImpl(Object... args) {
189 final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
190 return viewable("index.fmt", got, args);
197 public static void main(String[] args) throws Exception {
199 if (args.length > 1) {
200 port = Integer.parseInt(args[0]);
204 Callable<Void> r = startServers(port);
206 if (args.length > 2 && args[1].equals("--kill")) {
207 System.out.println("Hit enter to stop it...");
210 synchronized (UI.class) {
218 static Callable<Void> startServers(int port) throws Exception {
220 if (System.getProperty("quoridor.dir") == null) {
221 File home = new File(System.getProperty("user.home"));
222 File quoridor = new File(home, ".quoridor");
223 System.setProperty("quoridor.dir", quoridor.getPath());
226 ResourceConfig rc = new PackagesResourceConfig(
227 "cz.xelfi.quoridor.webidor",
228 "cz.xelfi.quoridor.freemarkerdor"
231 Client client = new Client();
232 base = client.resource(new URI("http://localhost:" + port + "/api/"));
234 final String baseUri = "http://localhost:" + port + "/";
235 final HttpServer server = HttpServerFactory.create(baseUri, rc);
237 System.out.println("Quoridor started at port " + port);
239 return new Callable<Void>() {
240 public Void call() throws Exception {
247 private Viewable viewable(String page, Document doc, Object... more) {
248 String bundle = page.split("\\.")[0];
250 ResourceBundle rb = null;
251 for (Locale l : headers.getAcceptableLanguages()) {
253 rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI." + bundle, l);
254 } catch (MissingResourceException e) {
259 rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI." + bundle, Locale.ENGLISH);
262 Map<String,Object> map = new HashMap<String,Object>();
264 map.put("user", user);
265 map.put("bundle", rb);
266 for (int i = 0; i < more.length; i += 2) {
267 map.put((String)more[i],more[i + 1]);
269 return new Viewable(page, map);