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/.
19 package cz.xelfi.quoridor.webidor.resources;
21 import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
22 import com.sun.jersey.api.core.PackagesResourceConfig;
23 import com.sun.jersey.api.core.ResourceConfig;
24 import com.sun.jersey.api.json.JSONWithPadding;
25 import com.sun.jersey.spi.resource.Singleton;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.net.ServerSocket;
30 import java.util.HashMap;
32 import java.util.Properties;
33 import java.util.UUID;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.QueryParam;
39 import javax.ws.rs.WebApplicationException;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response.Status;
42 import org.glassfish.grizzly.http.server.HttpServer;
46 * @author Jaroslav Tulach <jtulach@netbeans.org>
50 public final class Quoridor {
51 private final File path;
54 private final Map<UUID,String> loggedIn;
57 final String prop = System.getProperty("quoridor.dir"); // NOI18N
59 throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
61 path = new File(prop);
63 loggedIn = new HashMap<UUID, String>();
64 games = new Games(new File(path, "games"), this); // NOI18N
65 users = new Users(new File(path, "users"), this); // NOI18N
69 public Games getGames() {
74 public Users getUsers() {
80 @Produces({ MediaType.TEXT_PLAIN })
82 @QueryParam("name") String name,
83 @QueryParam("password") String password
84 ) throws IOException {
85 File f = new File(path, "passwd"); // NOI18Nt
86 Properties p = new Properties();
88 p.load(new FileInputStream(f));
89 } catch (IOException ex) {
92 boolean loggedInOK = false;
93 if (name != null && password.equals(p.getProperty(name))) {
96 loggedInOK = getUsers().verifyPassword(name, password);
100 UUID uuid = UUID.randomUUID();
101 loggedIn.put(uuid, name);
102 return uuid.toString();
111 @Produces({ MediaType.TEXT_PLAIN })
112 public String isLoggedIn(
113 @QueryParam("id") String id
118 ret = loggedIn.get(UUID.fromString(id));
120 } catch (IllegalArgumentException ex) {
121 // OK, happens for invalid ids
123 return ret == null ? "" : ret;
128 @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
129 public JSONWithPadding loggedInInfo(
130 @QueryParam("callback") String callback,
131 @QueryParam("id") String id
132 ) throws IOException {
136 ret = loggedIn.get(UUID.fromString(id));
138 } catch (IllegalArgumentException ex) {
139 // OK, happens for invalid ids
142 throw new WebApplicationException(Status.UNAUTHORIZED);
144 return getUsers().getUserInfo(callback, id, ret);
152 public static void main(String[] args) throws IOException, InterruptedException {
155 port = Integer.parseInt(args[0]);
156 } catch (Exception ex) {
159 HttpServer s = start(port);
161 "Quoridor started at port " + port + "\n" + "Hit enter to stop it..."
163 if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
164 System.out.println("Hit enter to stop it...");
167 synchronized (Quoridor.class) {
168 Quoridor.class.wait();
176 public static HttpServer start(int port) throws IOException {
178 ServerSocket ss = new ServerSocket(0);
179 port =ss.getLocalPort();
182 final String baseUri = "http://localhost:" + port + "/";
184 if (System.getProperty("quoridor.dir") == null) {
185 File home = new File(System.getProperty("user.home"));
186 File quoridor = new File(home, ".quoridor");
187 System.setProperty("quoridor.dir", quoridor.getPath());
190 ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
191 HttpServer server = GrizzlyServerFactory.createHttpServer(baseUri, rc);