webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
changeset 143 4eb88f05c207
child 144 cc04ede4cb5e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java	Sat Nov 07 15:23:14 2009 +0100
     1.3 @@ -0,0 +1,95 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.webidor.resources;
    1.31 +
    1.32 +import cz.xelfi.quoridor.webidor.*;
    1.33 +import java.io.File;
    1.34 +import java.io.FileInputStream;
    1.35 +import java.io.FileNotFoundException;
    1.36 +import java.io.IOException;
    1.37 +import java.util.ArrayList;
    1.38 +import java.util.Collections;
    1.39 +import java.util.List;
    1.40 +import java.util.Properties;
    1.41 +import java.util.logging.Logger;
    1.42 +import javax.ws.rs.DefaultValue;
    1.43 +import javax.ws.rs.GET;
    1.44 +import javax.ws.rs.Path;
    1.45 +import javax.ws.rs.PathParam;
    1.46 +import javax.ws.rs.Produces;
    1.47 +import javax.ws.rs.QueryParam;
    1.48 +import javax.ws.rs.core.MediaType;
    1.49 +
    1.50 +/**
    1.51 + *
    1.52 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.53 + */
    1.54 +public final class Users {
    1.55 +    private final Quoridor quoridor;
    1.56 +    private final File dir;
    1.57 +    private static final Logger LOG = Logger.getLogger(Users.class.getName());
    1.58 +
    1.59 +    Users(File dir, Quoridor quoridor) {
    1.60 +        this.dir = dir;
    1.61 +        this.quoridor = quoridor;
    1.62 +        dir.mkdirs();
    1.63 +    }
    1.64 +
    1.65 +    @GET
    1.66 +    @Path("{id}")
    1.67 +    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.68 +    public User getUserInfo(
    1.69 +        @QueryParam("loginID") String loginId,
    1.70 +        @PathParam("id") String id
    1.71 +    ) throws IOException {
    1.72 +        String  myid = quoridor.isLoggedIn(loginId);
    1.73 +        Properties myp = getProp(myid);
    1.74 +        Properties p = getProp(id);
    1.75 +        User user = new User(id);
    1.76 +        for (String n : p.stringPropertyNames()) {
    1.77 +            if (n.startsWith("permission.")) {
    1.78 +                continue;
    1.79 +            }
    1.80 +            if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
    1.81 +                continue;
    1.82 +            }
    1.83 +            user.addProperty(n, p.getProperty(n));
    1.84 +        }
    1.85 +        return user;
    1.86 +    }
    1.87 +
    1.88 +    private Properties getProp(String id) throws FileNotFoundException, IOException {
    1.89 +        Properties p = new Properties();
    1.90 +        if (id != null && id.length() > 0) {
    1.91 +            File f = new File(dir, id);
    1.92 +            FileInputStream is = new FileInputStream(f);
    1.93 +            p.load(is);
    1.94 +            is.close();
    1.95 +        }
    1.96 +        return p;
    1.97 +    }
    1.98 +}