# HG changeset patch # User Jaroslav Tulach # Date 1257603794 -3600 # Node ID 4eb88f05c20718bc5426276ed2936b2670d51cf4 # Parent 62e85a5ecffaf6bedc3f1b619ecb7eac8f079fd4 Support for properties associated with every user diff -r 62e85a5ecffa -r 4eb88f05c207 webidor/pom.xml --- a/webidor/pom.xml Thu Nov 05 13:34:36 2009 +0100 +++ b/webidor/pom.xml Sat Nov 07 15:23:14 2009 +0100 @@ -1,130 +1,130 @@ - - - 4.0.0 - - all-quoridor - org.apidesign - 1.0 - - org.apidesign - webidor - jar - 1.7 - webidor server - http://maven.apache.org - - - maven2-repository.dev.java.net - Java.net Repository for Maven - http://download.java.net/maven/2/ - default - - - maven-repository.dev.java.net - Java.net Maven 1 Repository (legacy) - http://download.java.net/maven/1 - legacy - - - - - - junit - junit - 4.5 - test - - - com.sun.jersey - jersey-core - 1.1.0-ea - - - com.sun.jersey - jersey-server - 1.1.0-ea - - - com.sun.jersey - jersey-json - 1.1.0-ea - jar - - - jaxb-api - javax.xml.bind - - - stax-api - stax - - - - - com.sun.jersey.test.framework - jersey-test-framework - 1.1.0-ea - test - - - org.apidesign - quoridor - 1.0 - jar - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0.2 - - 1.5 - 1.5 - - - - org.codehaus.mojo - exec-maven-plugin - - cz.xelfi.quoridor.webidor.resources.Quoridor - - - - maven-assembly-plugin - 2.2-beta-2 - - - create-executable-jar - package - - single - - - - all-zip.xml - - webidor-${version} - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - - true - lib/ - cz.xelfi.quoridor.webidor.resources.Quoridor - - - - - - - - - + + + 4.0.0 + + all-quoridor + org.apidesign + 1.0 + + org.apidesign + webidor + jar + 1.8 + webidor server + http://maven.apache.org + + + maven2-repository.dev.java.net + Java.net Repository for Maven + http://download.java.net/maven/2/ + default + + + maven-repository.dev.java.net + Java.net Maven 1 Repository (legacy) + http://download.java.net/maven/1 + legacy + + + + + + junit + junit + 4.5 + test + + + com.sun.jersey + jersey-core + 1.1.0-ea + + + com.sun.jersey + jersey-server + 1.1.0-ea + + + com.sun.jersey + jersey-json + 1.1.0-ea + jar + + + jaxb-api + javax.xml.bind + + + stax-api + stax + + + + + com.sun.jersey.test.framework + jersey-test-framework + 1.1.0-ea + test + + + org.apidesign + quoridor + 1.0 + jar + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + + + + org.codehaus.mojo + exec-maven-plugin + + cz.xelfi.quoridor.webidor.resources.Quoridor + + + + maven-assembly-plugin + 2.2-beta-2 + + + create-executable-jar + package + + single + + + + all-zip.xml + + webidor-${version} + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + lib/ + cz.xelfi.quoridor.webidor.resources.Quoridor + + + + + + + + + diff -r 62e85a5ecffa -r 4eb88f05c207 webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java Sat Nov 07 15:23:14 2009 +0100 @@ -0,0 +1,94 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import java.util.ArrayList; +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; + +/** + * + * @author Jaroslav Tulach + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.NONE) +public final class User extends Object { + @XmlAttribute + private String id; + @XmlElement(name="property") + private List properties; + + User() { + } + + public User(String id) { + this.id = id; + } + + public void addProperty(String name, String value) { + if (properties == null) { + properties = new ArrayList(); + } + properties.add(new Property(name, value)); + } + + public String getProperty(String name) { + if (properties == null) { + return null; + } + for (Property p : properties) { + if (p.name.equals(name)) { + return p.value; + } + } + return null; + } + + public String getId() { + return id; + } + + public static final class Property { + @XmlAttribute + private String name; + @XmlValue + private String value; + + private Property() { + } + + Property(String name, String value) { + this.name = name; + this.value = value; + } + } +} diff -r 62e85a5ecffa -r 4eb88f05c207 webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Thu Nov 05 13:34:36 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Sat Nov 07 15:23:14 2009 +0100 @@ -55,6 +55,7 @@ public final class Quoridor { private final File path; private Games games; + private Users users; private final Map loggedIn; public Quoridor() { @@ -75,6 +76,14 @@ return games; } + @Path("users") + public Users getUsers() { + if (users == null) { + users = new Users(new File(path, "users"), this); // NOI18N + } + return users; + } + @Path("login") @PUT @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML }) diff -r 62e85a5ecffa -r 4eb88f05c207 webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java Sat Nov 07 15:23:14 2009 +0100 @@ -0,0 +1,95 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor.resources; + +import cz.xelfi.quoridor.webidor.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Properties; +import java.util.logging.Logger; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +/** + * + * @author Jaroslav Tulach + */ +public final class Users { + private final Quoridor quoridor; + private final File dir; + private static final Logger LOG = Logger.getLogger(Users.class.getName()); + + Users(File dir, Quoridor quoridor) { + this.dir = dir; + this.quoridor = quoridor; + dir.mkdirs(); + } + + @GET + @Path("{id}") + @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML }) + public User getUserInfo( + @QueryParam("loginID") String loginId, + @PathParam("id") String id + ) throws IOException { + String myid = quoridor.isLoggedIn(loginId); + Properties myp = getProp(myid); + Properties p = getProp(id); + User user = new User(id); + for (String n : p.stringPropertyNames()) { + if (n.startsWith("permission.")) { + continue; + } + if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) { + continue; + } + user.addProperty(n, p.getProperty(n)); + } + return user; + } + + private Properties getProp(String id) throws FileNotFoundException, IOException { + Properties p = new Properties(); + if (id != null && id.length() > 0) { + File f = new File(dir, id); + FileInputStream is = new FileInputStream(f); + p.load(is); + is.close(); + } + return p; + } +} diff -r 62e85a5ecffa -r 4eb88f05c207 webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java Sat Nov 07 15:23:14 2009 +0100 @@ -0,0 +1,127 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import com.sun.jersey.api.client.GenericType; +import com.sun.jersey.test.framework.JerseyTest; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.List; +import java.util.Properties; +import javax.ws.rs.core.MediaType; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Jaroslav Tulach + */ +public class UsersTest extends JerseyTest { + private File dir; + + public UsersTest() throws Exception { + super("cz.xelfi.quoridor.webidor.resources"); + } + + @Override + public void setUp() throws Exception { + dir = File.createTempFile("quoridor", ".dir"); + dir.delete(); + System.setProperty("quoridor.dir", dir.getPath()); + dir.mkdirs(); + File passwd = new File(dir, "passwd"); + FileOutputStream os = new FileOutputStream(passwd); + os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8")); + os.close(); + super.setUp(); + } + + @Override + public void tearDown() throws Exception { + deleteRec(dir); + } + + static void deleteRec(File dir) throws IOException { + if (dir == null) { + return; + } + File[] arr = dir.listFiles(); + if (arr != null) { + for (File f : arr) { + deleteRec(f); + } + } + dir.delete(); + } + + @Test public void testListUsers() throws Exception { + File usersDir = new File(dir, "users"); + usersDir.mkdirs(); + File fJarda = new File(usersDir, "Jarda"); + { + Properties p = new Properties(); + p.setProperty("email", "jar@da.cz"); + p.setProperty("permission.email", "true"); + p.store(new FileOutputStream(fJarda), ""); + } + File fJirka = new File(usersDir, "Jirka"); + { + Properties p = new Properties(); + p.setProperty("email", "jir@ka.cz"); + p.store(new FileOutputStream(fJirka), ""); + } + + String logJarda = webResource.path("login"). + queryParam("name", "Jarda"). + queryParam("password", "heslo"). + accept(MediaType.TEXT_PLAIN). + put(String.class); + String logJirka = webResource.path("login"). + queryParam("name", "Jirka"). + queryParam("password", "pesko"). + accept(MediaType.TEXT_PLAIN). + put(String.class); + + User uJirka = webResource.path("users/Jirka").accept(MediaType.TEXT_XML).get(User.class); + assertEquals("Jirka", uJirka.getId()); + assertNull("Cannot get email without login", uJirka.getProperty("email")); + + uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class); + assertEquals("Jirka", uJirka.getId()); + assertEquals("Email for ownself is OK", "jir@ka.cz", uJirka.getProperty("email")); + + uJirka = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(User.class); + assertEquals("Jirka", uJirka.getId()); + assertEquals("Jarda has permission for property email", "jir@ka.cz", uJirka.getProperty("email")); + + String txt = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(String.class); + if (!txt.contains("jir@ka.cz")) { + fail(txt); + } + } +}