webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Dec 2009 07:59:16 +0100
changeset 171 524c7f359c4e
parent 146 0b889d9e4ee1
child 189 6245e1b634aa
permissions -rw-r--r--
Adding support for 'permission.games' to allow special roles to enlist all the available games
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@144
    33
import java.io.FileOutputStream;
jaroslav@143
    34
import java.io.IOException;
jaroslav@143
    35
import java.util.Properties;
jaroslav@143
    36
import java.util.logging.Logger;
jaroslav@143
    37
import javax.ws.rs.GET;
jaroslav@144
    38
import javax.ws.rs.POST;
jaroslav@143
    39
import javax.ws.rs.Path;
jaroslav@143
    40
import javax.ws.rs.PathParam;
jaroslav@143
    41
import javax.ws.rs.Produces;
jaroslav@143
    42
import javax.ws.rs.QueryParam;
jaroslav@143
    43
import javax.ws.rs.core.MediaType;
jaroslav@144
    44
import javax.ws.rs.core.Response;
jaroslav@143
    45
jaroslav@143
    46
/**
jaroslav@143
    47
 *
jaroslav@143
    48
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@143
    49
 */
jaroslav@143
    50
public final class Users {
jaroslav@143
    51
    private final Quoridor quoridor;
jaroslav@143
    52
    private final File dir;
jaroslav@143
    53
    private static final Logger LOG = Logger.getLogger(Users.class.getName());
jaroslav@143
    54
jaroslav@143
    55
    Users(File dir, Quoridor quoridor) {
jaroslav@143
    56
        this.dir = dir;
jaroslav@143
    57
        this.quoridor = quoridor;
jaroslav@143
    58
        dir.mkdirs();
jaroslav@143
    59
    }
jaroslav@143
    60
jaroslav@143
    61
    @GET
jaroslav@145
    62
    @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
jaroslav@145
    63
    public User getOwnInfo(
jaroslav@145
    64
            @QueryParam("loginID") String loginId
jaroslav@145
    65
    ) throws IOException {
jaroslav@145
    66
        return getUserInfo(loginId, null);
jaroslav@145
    67
    }
jaroslav@145
    68
    @GET
jaroslav@143
    69
    @Path("{id}")
jaroslav@143
    70
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@143
    71
    public User getUserInfo(
jaroslav@143
    72
        @QueryParam("loginID") String loginId,
jaroslav@143
    73
        @PathParam("id") String id
jaroslav@143
    74
    ) throws IOException {
jaroslav@143
    75
        String  myid = quoridor.isLoggedIn(loginId);
jaroslav@145
    76
        if (id == null) {
jaroslav@145
    77
            id = myid;
jaroslav@145
    78
        }
jaroslav@143
    79
        Properties myp = getProp(myid);
jaroslav@143
    80
        Properties p = getProp(id);
jaroslav@143
    81
        User user = new User(id);
jaroslav@143
    82
        for (String n : p.stringPropertyNames()) {
jaroslav@171
    83
            final String prefix = "permission.";
jaroslav@171
    84
            if (n.startsWith(prefix)) {
jaroslav@171
    85
                if ("true".equals(p.getProperty(n))) {
jaroslav@171
    86
                    user.addPermission(n.substring(prefix.length()));
jaroslav@171
    87
                }
jaroslav@143
    88
                continue;
jaroslav@143
    89
            }
jaroslav@143
    90
            if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
jaroslav@143
    91
                continue;
jaroslav@143
    92
            }
jaroslav@143
    93
            user.addProperty(n, p.getProperty(n));
jaroslav@143
    94
        }
jaroslav@143
    95
        return user;
jaroslav@143
    96
    }
jaroslav@143
    97
jaroslav@144
    98
    @POST
jaroslav@144
    99
    @Path("{id}")
jaroslav@144
   100
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@144
   101
    public synchronized Response changeProperty(
jaroslav@144
   102
        @QueryParam("loginID") String loginId,
jaroslav@144
   103
        @PathParam("id") String id,
jaroslav@144
   104
        @QueryParam("name") String name,
jaroslav@144
   105
        @QueryParam("value") String value
jaroslav@144
   106
    ) throws IOException {
jaroslav@144
   107
        String myid = quoridor.isLoggedIn(loginId);
jaroslav@144
   108
        if (!id.equals(myid) || name.startsWith("permission.")) {
jaroslav@144
   109
            return Response.status(Response.Status.UNAUTHORIZED).build();
jaroslav@144
   110
        }
jaroslav@144
   111
jaroslav@144
   112
        Properties p = getProp(myid);
jaroslav@144
   113
        p.setProperty(name, value);
jaroslav@144
   114
jaroslav@144
   115
        File f = new File(dir, id);
jaroslav@144
   116
        FileOutputStream os = new FileOutputStream(f);
jaroslav@144
   117
        p.store(os, "");
jaroslav@144
   118
        os.close();
jaroslav@144
   119
        
jaroslav@144
   120
        return Response.ok().entity(getUserInfo(loginId, id)).build();
jaroslav@144
   121
    }
jaroslav@144
   122
jaroslav@144
   123
    private synchronized Properties getProp(String id) throws FileNotFoundException, IOException {
jaroslav@143
   124
        Properties p = new Properties();
jaroslav@143
   125
        if (id != null && id.length() > 0) {
jaroslav@143
   126
            File f = new File(dir, id);
jaroslav@145
   127
            if (f.exists()) {
jaroslav@145
   128
                FileInputStream is = new FileInputStream(f);
jaroslav@145
   129
                p.load(is);
jaroslav@145
   130
                is.close();
jaroslav@145
   131
            }
jaroslav@143
   132
        }
jaroslav@143
   133
        return p;
jaroslav@143
   134
    }
jaroslav@143
   135
}