webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Nov 2009 15:23:14 +0100
changeset 143 4eb88f05c207
child 144 cc04ede4cb5e
permissions -rw-r--r--
Support for properties associated with every user
     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.IOException;
    34 import java.util.ArrayList;
    35 import java.util.Collections;
    36 import java.util.List;
    37 import java.util.Properties;
    38 import java.util.logging.Logger;
    39 import javax.ws.rs.DefaultValue;
    40 import javax.ws.rs.GET;
    41 import javax.ws.rs.Path;
    42 import javax.ws.rs.PathParam;
    43 import javax.ws.rs.Produces;
    44 import javax.ws.rs.QueryParam;
    45 import javax.ws.rs.core.MediaType;
    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     @Path("{id}")
    64     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    65     public User getUserInfo(
    66         @QueryParam("loginID") String loginId,
    67         @PathParam("id") String id
    68     ) throws IOException {
    69         String  myid = quoridor.isLoggedIn(loginId);
    70         Properties myp = getProp(myid);
    71         Properties p = getProp(id);
    72         User user = new User(id);
    73         for (String n : p.stringPropertyNames()) {
    74             if (n.startsWith("permission.")) {
    75                 continue;
    76             }
    77             if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
    78                 continue;
    79             }
    80             user.addProperty(n, p.getProperty(n));
    81         }
    82         return user;
    83     }
    84 
    85     private Properties getProp(String id) throws FileNotFoundException, IOException {
    86         Properties p = new Properties();
    87         if (id != null && id.length() > 0) {
    88             File f = new File(dir, id);
    89             FileInputStream is = new FileInputStream(f);
    90             p.load(is);
    91             is.close();
    92         }
    93         return p;
    94     }
    95 }