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.json.JSONWithPadding;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.HashMap;
27 import java.util.Properties;
28 import java.util.UUID;
29 import javax.annotation.ManagedBean;
30 import javax.inject.Singleton;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.QueryParam;
36 import javax.ws.rs.WebApplicationException;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response.Status;
42 * @author Jaroslav Tulach <jtulach@netbeans.org>
47 public class Quoridor {
48 private final File path;
51 private final Map<UUID,String> loggedIn;
54 String prop = System.getProperty("quoridor.dir"); // NOI18N
56 prop = System.getProperty("user.dir") + File.separatorChar + ".quoridor"; // NOI18N
58 path = new File(prop);
60 loggedIn = new HashMap<UUID, String>();
61 games = new Games(new File(path, "games"), this); // NOI18N
62 users = new Users(new File(path, "users"), this); // NOI18N
66 public Games getGames() {
71 public Users getUsers() {
77 @Produces({ MediaType.TEXT_PLAIN })
79 @QueryParam("name") String name,
80 @QueryParam("password") String password
81 ) throws IOException {
82 File f = new File(path, "passwd"); // NOI18Nt
83 Properties p = new Properties();
85 p.load(new FileInputStream(f));
86 } catch (IOException ex) {
89 boolean loggedInOK = false;
90 if (name != null && password.equals(p.getProperty(name))) {
93 loggedInOK = getUsers().verifyPassword(name, password);
97 UUID uuid = UUID.randomUUID();
98 loggedIn.put(uuid, name);
99 return uuid.toString();
108 @Produces({ MediaType.TEXT_PLAIN })
109 public String isLoggedIn(
110 @QueryParam("id") String id
115 ret = loggedIn.get(UUID.fromString(id));
117 } catch (IllegalArgumentException ex) {
118 // OK, happens for invalid ids
120 return ret == null ? "" : ret;
125 @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
126 public JSONWithPadding loggedInInfo(
127 @QueryParam("callback") String callback,
128 @QueryParam("id") String id
129 ) throws IOException {
133 ret = loggedIn.get(UUID.fromString(id));
135 } catch (IllegalArgumentException ex) {
136 // OK, happens for invalid ids
139 throw new WebApplicationException(Status.UNAUTHORIZED);
141 return getUsers().getUserInfo(callback, id, ret);