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
     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 cz.xelfi.quoridor.webidor.*;
    30 import java.io.File;
    31 import java.io.FileInputStream;
    32 import java.io.FileNotFoundException;
    33 import java.io.FileOutputStream;
    34 import java.io.IOException;
    35 import java.util.Properties;
    36 import java.util.logging.Logger;
    37 import javax.ws.rs.GET;
    38 import javax.ws.rs.POST;
    39 import javax.ws.rs.Path;
    40 import javax.ws.rs.PathParam;
    41 import javax.ws.rs.Produces;
    42 import javax.ws.rs.QueryParam;
    43 import javax.ws.rs.core.MediaType;
    44 import javax.ws.rs.core.Response;
    45 
    46 /**
    47  *
    48  * @author Jaroslav Tulach <jtulach@netbeans.org>
    49  */
    50 public final class Users {
    51     private final Quoridor quoridor;
    52     private final File dir;
    53     private static final Logger LOG = Logger.getLogger(Users.class.getName());
    54 
    55     Users(File dir, Quoridor quoridor) {
    56         this.dir = dir;
    57         this.quoridor = quoridor;
    58         dir.mkdirs();
    59     }
    60 
    61     @GET
    62     @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
    63     public User getOwnInfo(
    64             @QueryParam("loginID") String loginId
    65     ) throws IOException {
    66         return getUserInfo(loginId, null);
    67     }
    68     @GET
    69     @Path("{id}")
    70     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    71     public 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             if (n.startsWith("permission.")) {
    84                 continue;
    85             }
    86             if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
    87                 continue;
    88             }
    89             user.addProperty(n, p.getProperty(n));
    90         }
    91         return user;
    92     }
    93 
    94     @POST
    95     @Path("{id}")
    96     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    97     public synchronized Response changeProperty(
    98         @QueryParam("loginID") String loginId,
    99         @PathParam("id") String id,
   100         @QueryParam("name") String name,
   101         @QueryParam("value") String value
   102     ) throws IOException {
   103         String myid = quoridor.isLoggedIn(loginId);
   104         if (!id.equals(myid) || name.startsWith("permission.")) {
   105             return Response.status(Response.Status.UNAUTHORIZED).build();
   106         }
   107 
   108         Properties p = getProp(myid);
   109         p.setProperty(name, value);
   110 
   111         File f = new File(dir, id);
   112         FileOutputStream os = new FileOutputStream(f);
   113         p.store(os, "");
   114         os.close();
   115         
   116         return Response.ok().entity(getUserInfo(loginId, id)).build();
   117     }
   118 
   119     private synchronized Properties getProp(String id) throws FileNotFoundException, IOException {
   120         Properties p = new Properties();
   121         if (id != null && id.length() > 0) {
   122             File f = new File(dir, id);
   123             if (f.exists()) {
   124                 FileInputStream is = new FileInputStream(f);
   125                 p.load(is);
   126                 is.close();
   127             }
   128         }
   129         return p;
   130     }
   131 }