webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 08 Nov 2009 09:54:32 +0100
changeset 146 0b889d9e4ee1
parent 145 ac9bd9be5263
child 171 524c7f359c4e
permissions -rw-r--r--
Allowing users to specify their preferred language
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@143
    83
            if (n.startsWith("permission.")) {
jaroslav@143
    84
                continue;
jaroslav@143
    85
            }
jaroslav@143
    86
            if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
jaroslav@143
    87
                continue;
jaroslav@143
    88
            }
jaroslav@143
    89
            user.addProperty(n, p.getProperty(n));
jaroslav@143
    90
        }
jaroslav@143
    91
        return user;
jaroslav@143
    92
    }
jaroslav@143
    93
jaroslav@144
    94
    @POST
jaroslav@144
    95
    @Path("{id}")
jaroslav@144
    96
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@144
    97
    public synchronized Response changeProperty(
jaroslav@144
    98
        @QueryParam("loginID") String loginId,
jaroslav@144
    99
        @PathParam("id") String id,
jaroslav@144
   100
        @QueryParam("name") String name,
jaroslav@144
   101
        @QueryParam("value") String value
jaroslav@144
   102
    ) throws IOException {
jaroslav@144
   103
        String myid = quoridor.isLoggedIn(loginId);
jaroslav@144
   104
        if (!id.equals(myid) || name.startsWith("permission.")) {
jaroslav@144
   105
            return Response.status(Response.Status.UNAUTHORIZED).build();
jaroslav@144
   106
        }
jaroslav@144
   107
jaroslav@144
   108
        Properties p = getProp(myid);
jaroslav@144
   109
        p.setProperty(name, value);
jaroslav@144
   110
jaroslav@144
   111
        File f = new File(dir, id);
jaroslav@144
   112
        FileOutputStream os = new FileOutputStream(f);
jaroslav@144
   113
        p.store(os, "");
jaroslav@144
   114
        os.close();
jaroslav@144
   115
        
jaroslav@144
   116
        return Response.ok().entity(getUserInfo(loginId, id)).build();
jaroslav@144
   117
    }
jaroslav@144
   118
jaroslav@144
   119
    private synchronized Properties getProp(String id) throws FileNotFoundException, IOException {
jaroslav@143
   120
        Properties p = new Properties();
jaroslav@143
   121
        if (id != null && id.length() > 0) {
jaroslav@143
   122
            File f = new File(dir, id);
jaroslav@145
   123
            if (f.exists()) {
jaroslav@145
   124
                FileInputStream is = new FileInputStream(f);
jaroslav@145
   125
                p.load(is);
jaroslav@145
   126
                is.close();
jaroslav@145
   127
            }
jaroslav@143
   128
        }
jaroslav@143
   129
        return p;
jaroslav@143
   130
    }
jaroslav@143
   131
}