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.WebResource;
31 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
32 import com.sun.jersey.api.core.PackagesResourceConfig;
33 import com.sun.jersey.api.core.ResourceConfig;
34 import com.sun.jersey.api.view.Viewable;
35 import com.sun.net.httpserver.HttpServer;
36 import cz.xelfi.quoridor.webidor.resources.Quoridor;
38 import java.io.FileInputStream;
39 import java.io.IOException;
41 import java.util.HashMap;
43 import java.util.Properties;
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;
75 private Viewable checkLogin() {
76 if (headers.getCookies().containsKey("login")) {
77 user = headers.getCookies().get("login").getValue();
80 return new Viewable("login.fmt", null);
85 @Produces(MediaType.TEXT_HTML)
86 public Response login(
87 @FormParam("name") String name, @FormParam("password") String password
89 File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd");
90 Properties p = new Properties();
92 p.load(new FileInputStream(f));
93 } catch (IOException ex) {
96 if (name != null && password.equals(p.getProperty(name))) {
97 return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build();
99 Viewable v = new Viewable("login.fmt", "Invalid name or password: " + name);
100 return Response.status(1).entity(v).build();
105 @Produces(MediaType.TEXT_HTML)
106 public Viewable welcome() {
107 Viewable v = checkLogin();
111 return welcomeImpl();
116 @Produces(MediaType.TEXT_HTML)
117 public Viewable board(@PathParam("id") String id) {
118 Viewable v = checkLogin();
122 Map<String,Object> map = new HashMap<String,Object>();
123 Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
125 map.put("user", user);
126 return new Viewable("game.fmt", map);
130 @Path("games/{id}/move")
131 @Produces(MediaType.TEXT_HTML)
132 public Viewable move(
133 @PathParam("id") String id,
134 @QueryParam("type") String type,
135 @QueryParam("direction") String direction,
136 @QueryParam("direction-next") @DefaultValue("") String directionNext,
137 @QueryParam("column") @DefaultValue("") String column,
138 @QueryParam("row") @DefaultValue("") String row
140 Viewable v = checkLogin();
144 WebResource wr = base.path("games").path(id).queryParam("player", user);
145 if (type.equals("fence")) {
146 wr.queryParam("move", direction.charAt(0) + column + row).put();
149 if (type.equals("move")) {
150 wr.queryParam("move", direction + directionNext).put();
157 @Path("games/create")
158 @Produces(MediaType.TEXT_HTML)
159 public Viewable create(
160 @QueryParam("white") String white,
161 @QueryParam("black") String black
163 Viewable v = checkLogin();
168 base.path("games").queryParam("white", white).
169 queryParam("black", black).post(Document.class);
173 private Viewable welcomeImpl() {
174 final Object got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
175 return new Viewable("index.fmt", got);
182 public static void main(String[] args) throws Exception {
183 Callable<Void> r = startServers();
190 static Callable<Void> startServers() throws Exception {
191 final HttpServer api = Quoridor.start(9998);
192 Client client = new Client();
193 base = client.resource(new URI("http://localhost:9998/api/"));
195 final HttpServer s = start(9997);
197 "Quoridor started at port 9997\n" + "Hit enter to stop it..."
200 return new Callable<Void>() {
201 public Void call() throws Exception {
209 static HttpServer start(int port) throws IOException {
210 final String baseUri = "http://localhost:" + port + "/";
211 ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.freemarkerdor");
212 HttpServer server = HttpServerFactory.create(baseUri, rc);