webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Nov 2009 15:23:14 +0100
changeset 143 4eb88f05c207
child 144 cc04ede4cb5e
permissions -rw-r--r--
Support for properties associated with every user
jaroslav@143
     1
/*
jaroslav@143
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@143
     3
 *
jaroslav@143
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@143
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@143
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@143
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@143
     8
 * License. You can obtain a copy of the License at
jaroslav@143
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@143
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@143
    11
 * specific language governing permissions and limitations under the
jaroslav@143
    12
 * License.  When distributing the software, include this License Header
jaroslav@143
    13
 * Notice in each file and include the License file at
jaroslav@143
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@143
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@143
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@143
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@143
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@143
    19
 * your own identifying information:
jaroslav@143
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@143
    21
 *
jaroslav@143
    22
 * Contributor(s):
jaroslav@143
    23
 *
jaroslav@143
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@143
    25
 */
jaroslav@143
    26
jaroslav@143
    27
package cz.xelfi.quoridor.webidor.resources;
jaroslav@143
    28
jaroslav@143
    29
import cz.xelfi.quoridor.webidor.*;
jaroslav@143
    30
import java.io.File;
jaroslav@143
    31
import java.io.FileInputStream;
jaroslav@143
    32
import java.io.FileNotFoundException;
jaroslav@143
    33
import java.io.IOException;
jaroslav@143
    34
import java.util.ArrayList;
jaroslav@143
    35
import java.util.Collections;
jaroslav@143
    36
import java.util.List;
jaroslav@143
    37
import java.util.Properties;
jaroslav@143
    38
import java.util.logging.Logger;
jaroslav@143
    39
import javax.ws.rs.DefaultValue;
jaroslav@143
    40
import javax.ws.rs.GET;
jaroslav@143
    41
import javax.ws.rs.Path;
jaroslav@143
    42
import javax.ws.rs.PathParam;
jaroslav@143
    43
import javax.ws.rs.Produces;
jaroslav@143
    44
import javax.ws.rs.QueryParam;
jaroslav@143
    45
import javax.ws.rs.core.MediaType;
jaroslav@143
    46
jaroslav@143
    47
/**
jaroslav@143
    48
 *
jaroslav@143
    49
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@143
    50
 */
jaroslav@143
    51
public final class Users {
jaroslav@143
    52
    private final Quoridor quoridor;
jaroslav@143
    53
    private final File dir;
jaroslav@143
    54
    private static final Logger LOG = Logger.getLogger(Users.class.getName());
jaroslav@143
    55
jaroslav@143
    56
    Users(File dir, Quoridor quoridor) {
jaroslav@143
    57
        this.dir = dir;
jaroslav@143
    58
        this.quoridor = quoridor;
jaroslav@143
    59
        dir.mkdirs();
jaroslav@143
    60
    }
jaroslav@143
    61
jaroslav@143
    62
    @GET
jaroslav@143
    63
    @Path("{id}")
jaroslav@143
    64
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@143
    65
    public User getUserInfo(
jaroslav@143
    66
        @QueryParam("loginID") String loginId,
jaroslav@143
    67
        @PathParam("id") String id
jaroslav@143
    68
    ) throws IOException {
jaroslav@143
    69
        String  myid = quoridor.isLoggedIn(loginId);
jaroslav@143
    70
        Properties myp = getProp(myid);
jaroslav@143
    71
        Properties p = getProp(id);
jaroslav@143
    72
        User user = new User(id);
jaroslav@143
    73
        for (String n : p.stringPropertyNames()) {
jaroslav@143
    74
            if (n.startsWith("permission.")) {
jaroslav@143
    75
                continue;
jaroslav@143
    76
            }
jaroslav@143
    77
            if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
jaroslav@143
    78
                continue;
jaroslav@143
    79
            }
jaroslav@143
    80
            user.addProperty(n, p.getProperty(n));
jaroslav@143
    81
        }
jaroslav@143
    82
        return user;
jaroslav@143
    83
    }
jaroslav@143
    84
jaroslav@143
    85
    private Properties getProp(String id) throws FileNotFoundException, IOException {
jaroslav@143
    86
        Properties p = new Properties();
jaroslav@143
    87
        if (id != null && id.length() > 0) {
jaroslav@143
    88
            File f = new File(dir, id);
jaroslav@143
    89
            FileInputStream is = new FileInputStream(f);
jaroslav@143
    90
            p.load(is);
jaroslav@143
    91
            is.close();
jaroslav@143
    92
        }
jaroslav@143
    93
        return p;
jaroslav@143
    94
    }
jaroslav@143
    95
}