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.webidor.resources;
29 import cz.xelfi.quoridor.webidor.*;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Properties;
39 import java.util.logging.Logger;
40 import javax.ws.rs.DefaultValue;
41 import javax.ws.rs.GET;
42 import javax.ws.rs.POST;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.PathParam;
45 import javax.ws.rs.Produces;
46 import javax.ws.rs.QueryParam;
47 import javax.ws.rs.core.MediaType;
48 import javax.ws.rs.core.Response;
52 * @author Jaroslav Tulach <jtulach@netbeans.org>
54 public final class Users {
55 private final Quoridor quoridor;
56 private final File dir;
57 private static final Logger LOG = Logger.getLogger(Users.class.getName());
59 Users(File dir, Quoridor quoridor) {
61 this.quoridor = quoridor;
66 @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
67 public User getOwnInfo(
68 @QueryParam("loginID") String loginId
69 ) throws IOException {
70 return getUserInfo(loginId, null);
74 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
75 public User getUserInfo(
76 @QueryParam("loginID") String loginId,
77 @PathParam("id") String id
78 ) throws IOException {
79 String myid = quoridor.isLoggedIn(loginId);
83 Properties myp = getProp(myid);
84 Properties p = getProp(id);
85 User user = new User(id);
86 for (String n : p.stringPropertyNames()) {
87 if (n.startsWith("permission.")) {
90 if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
93 user.addProperty(n, p.getProperty(n));
100 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
101 public synchronized Response changeProperty(
102 @QueryParam("loginID") String loginId,
103 @PathParam("id") String id,
104 @QueryParam("name") String name,
105 @QueryParam("value") String value
106 ) throws IOException {
107 String myid = quoridor.isLoggedIn(loginId);
108 if (!id.equals(myid) || name.startsWith("permission.")) {
109 return Response.status(Response.Status.UNAUTHORIZED).build();
112 Properties p = getProp(myid);
113 p.setProperty(name, value);
115 File f = new File(dir, id);
116 FileOutputStream os = new FileOutputStream(f);
120 return Response.ok().entity(getUserInfo(loginId, id)).build();
123 private synchronized Properties getProp(String id) throws FileNotFoundException, IOException {
124 Properties p = new Properties();
125 if (id != null && id.length() > 0) {
126 File f = new File(dir, id);
128 FileInputStream is = new FileInputStream(f);