webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 239 a47345ebbdd7
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.webidor.resources;
    19 
    20 import com.sun.jersey.api.json.JSONWithPadding;
    21 import cz.xelfi.quoridor.webidor.*;
    22 import java.io.File;
    23 import java.io.FileInputStream;
    24 import java.io.FileNotFoundException;
    25 import java.io.FileOutputStream;
    26 import java.io.IOException;
    27 import java.util.Properties;
    28 import java.util.logging.Logger;
    29 import javax.ws.rs.GET;
    30 import javax.ws.rs.POST;
    31 import javax.ws.rs.Path;
    32 import javax.ws.rs.PathParam;
    33 import javax.ws.rs.Produces;
    34 import javax.ws.rs.QueryParam;
    35 import javax.ws.rs.core.MediaType;
    36 import javax.ws.rs.core.Response;
    37 
    38 /**
    39  *
    40  * @author Jaroslav Tulach <jtulach@netbeans.org>
    41  */
    42 public final class Users {
    43     private final Quoridor quoridor;
    44     private final File dir;
    45     private static final Logger LOG = Logger.getLogger(Users.class.getName());
    46 
    47     Users(File dir, Quoridor quoridor) {
    48         this.dir = dir;
    49         this.quoridor = quoridor;
    50         dir.mkdirs();
    51     }
    52 
    53     @GET
    54     @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    55     public JSONWithPadding getOwnInfo(
    56         @QueryParam("callback") String callback,
    57         @QueryParam("loginID") String loginId
    58     ) throws IOException {
    59         return getUserInfo(callback, loginId, null);
    60     }
    61 
    62     User getUserInfo(
    63         @QueryParam("loginID") String loginId,
    64         @PathParam("id") String id
    65     ) throws IOException {
    66         String  myid = quoridor.isLoggedIn(loginId);
    67         if (id == null) {
    68             id = myid;
    69         }
    70         Properties myp = getProp(myid);
    71         Properties p = getProp(id);
    72         User user = new User(id);
    73         for (String n : p.stringPropertyNames()) {
    74             final String prefix = "permission.";
    75             if (n.startsWith(prefix)) {
    76                 if ("true".equals(p.getProperty(n))) {
    77                     user.addPermission(n.substring(prefix.length()));
    78                 }
    79                 continue;
    80             }
    81             if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
    82                 continue;
    83             }
    84             user.addProperty(n, p.getProperty(n));
    85         }
    86         return user;
    87     }
    88 
    89     @GET
    90     @Path("{id}")
    91     @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    92     public JSONWithPadding getUserInfo(
    93         @QueryParam("callback") String callback,
    94         @QueryParam("loginID") String loginId,
    95         @PathParam("id") String id
    96     ) throws IOException {
    97         return new JSONWithPadding(getUserInfo(loginId, id), callback);
    98     }
    99 
   100     @POST
   101     @Path("{id}")
   102     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   103     public synchronized Response changeProperty(
   104         @QueryParam("loginID") String loginId,
   105         @PathParam("id") String id,
   106         @QueryParam("name") String name,
   107         @QueryParam("value") String value
   108     ) throws IOException {
   109         String myid = quoridor.isLoggedIn(loginId);
   110         if (!id.equals(myid) || name.startsWith("permission.")) {
   111             return Response.status(Response.Status.UNAUTHORIZED).build();
   112         }
   113 
   114         Properties p = getProp(myid);
   115         p.setProperty(name, value);
   116 
   117         File f = new File(dir, id);
   118         FileOutputStream os = new FileOutputStream(f);
   119         p.store(os, "");
   120         os.close();
   121         
   122         return Response.ok().entity(getUserInfo(null, loginId, id).getJsonSource()).build();
   123     }
   124 
   125     final boolean verifyPassword(String id, String passwd) throws IOException {
   126         Properties p = getProp(id);
   127         if (p != null) {
   128             return passwd.equals(p.getProperty("passwd"));
   129         }
   130         return false;
   131     }
   132 
   133     private synchronized Properties getProp(String id) throws FileNotFoundException, IOException {
   134         Properties p = new Properties();
   135         if (id != null && id.length() > 0) {
   136             File f = new File(dir, id);
   137             if (f.exists()) {
   138                 FileInputStream is = new FileInputStream(f);
   139                 p.load(is);
   140                 is.close();
   141             }
   142         }
   143         return p;
   144     }
   145 }