webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 25 Apr 2010 21:41:00 +0200
changeset 239 a47345ebbdd7
parent 189 6245e1b634aa
child 264 d60370059c3c
permissions -rw-r--r--
Password can be stored in user properties
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor.resources;
    28 
    29 import com.sun.jersey.api.json.JSONWithPadding;
    30 import cz.xelfi.quoridor.webidor.*;
    31 import java.io.File;
    32 import java.io.FileInputStream;
    33 import java.io.FileNotFoundException;
    34 import java.io.FileOutputStream;
    35 import java.io.IOException;
    36 import java.util.Properties;
    37 import java.util.logging.Logger;
    38 import javax.ws.rs.GET;
    39 import javax.ws.rs.POST;
    40 import javax.ws.rs.Path;
    41 import javax.ws.rs.PathParam;
    42 import javax.ws.rs.Produces;
    43 import javax.ws.rs.QueryParam;
    44 import javax.ws.rs.core.MediaType;
    45 import javax.ws.rs.core.Response;
    46 
    47 /**
    48  *
    49  * @author Jaroslav Tulach <jtulach@netbeans.org>
    50  */
    51 public final class Users {
    52     private final Quoridor quoridor;
    53     private final File dir;
    54     private static final Logger LOG = Logger.getLogger(Users.class.getName());
    55 
    56     Users(File dir, Quoridor quoridor) {
    57         this.dir = dir;
    58         this.quoridor = quoridor;
    59         dir.mkdirs();
    60     }
    61 
    62     @GET
    63     @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    64     public JSONWithPadding getOwnInfo(
    65         @QueryParam("callback") String callback,
    66         @QueryParam("loginID") String loginId
    67     ) throws IOException {
    68         return getUserInfo(callback, loginId, null);
    69     }
    70 
    71     User getUserInfo(
    72         @QueryParam("loginID") String loginId,
    73         @PathParam("id") String id
    74     ) throws IOException {
    75         String  myid = quoridor.isLoggedIn(loginId);
    76         if (id == null) {
    77             id = myid;
    78         }
    79         Properties myp = getProp(myid);
    80         Properties p = getProp(id);
    81         User user = new User(id);
    82         for (String n : p.stringPropertyNames()) {
    83             final String prefix = "permission.";
    84             if (n.startsWith(prefix)) {
    85                 if ("true".equals(p.getProperty(n))) {
    86                     user.addPermission(n.substring(prefix.length()));
    87                 }
    88                 continue;
    89             }
    90             if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
    91                 continue;
    92             }
    93             user.addProperty(n, p.getProperty(n));
    94         }
    95         return user;
    96     }
    97 
    98     @GET
    99     @Path("{id}")
   100     @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   101     public JSONWithPadding getUserInfo(
   102         @QueryParam("callback") String callback,
   103         @QueryParam("loginID") String loginId,
   104         @PathParam("id") String id
   105     ) throws IOException {
   106         return new JSONWithPadding(getUserInfo(loginId, id), callback);
   107     }
   108 
   109     @POST
   110     @Path("{id}")
   111     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   112     public synchronized Response changeProperty(
   113         @QueryParam("loginID") String loginId,
   114         @PathParam("id") String id,
   115         @QueryParam("name") String name,
   116         @QueryParam("value") String value
   117     ) throws IOException {
   118         String myid = quoridor.isLoggedIn(loginId);
   119         if (!id.equals(myid) || name.startsWith("permission.")) {
   120             return Response.status(Response.Status.UNAUTHORIZED).build();
   121         }
   122 
   123         Properties p = getProp(myid);
   124         p.setProperty(name, value);
   125 
   126         File f = new File(dir, id);
   127         FileOutputStream os = new FileOutputStream(f);
   128         p.store(os, "");
   129         os.close();
   130         
   131         return Response.ok().entity(getUserInfo(null, loginId, id).getJsonSource()).build();
   132     }
   133 
   134     final boolean verifyPassword(String id, String passwd) throws IOException {
   135         Properties p = getProp(id);
   136         if (p != null) {
   137             return passwd.equals(p.getProperty("passwd"));
   138         }
   139         return false;
   140     }
   141 
   142     private synchronized Properties getProp(String id) throws FileNotFoundException, IOException {
   143         Properties p = new Properties();
   144         if (id != null && id.length() > 0) {
   145             File f = new File(dir, id);
   146             if (f.exists()) {
   147                 FileInputStream is = new FileInputStream(f);
   148                 p.load(is);
   149                 is.close();
   150             }
   151         }
   152         return p;
   153     }
   154 }