webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 17 Jul 2011 15:40:50 +0200
branchglassfish
changeset 284 90be53f96e0c
parent 282 40fc213a7f43
permissions -rw-r--r--
webidor rewritten to run as glassfish application
     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 
    19 package cz.xelfi.quoridor.webidor.resources;
    20 
    21 import com.sun.jersey.api.json.JSONWithPadding;
    22 import java.io.File;
    23 import java.io.FileInputStream;
    24 import java.io.IOException;
    25 import java.util.HashMap;
    26 import java.util.Map;
    27 import java.util.Properties;
    28 import java.util.UUID;
    29 import javax.annotation.ManagedBean;
    30 import javax.inject.Singleton;
    31 import javax.ws.rs.GET;
    32 import javax.ws.rs.PUT;
    33 import javax.ws.rs.Path;
    34 import javax.ws.rs.Produces;
    35 import javax.ws.rs.QueryParam;
    36 import javax.ws.rs.WebApplicationException;
    37 import javax.ws.rs.core.MediaType;
    38 import javax.ws.rs.core.Response.Status;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 @Path("/")
    45 @Singleton
    46 @ManagedBean
    47 public class Quoridor {
    48     private final File path;
    49     private Games games;
    50     private Users users;
    51     private final Map<UUID,String> loggedIn;
    52 
    53     public Quoridor() {
    54         String prop = System.getProperty("quoridor.dir"); // NOI18N
    55         if (prop == null) {
    56             prop = System.getProperty("user.dir") + File.separatorChar + ".quoridor"; // NOI18N
    57         }
    58         path = new File(prop);
    59         path.mkdirs();
    60         loggedIn = new HashMap<UUID, String>();
    61         games = new Games(new File(path, "games"), this); // NOI18N
    62         users = new Users(new File(path, "users"), this); // NOI18N
    63     }
    64 
    65     @Path("games")
    66     public Games getGames() {
    67         return games;
    68     }
    69 
    70     @Path("users")
    71     public Users getUsers() {
    72         return users;
    73     }
    74 
    75     @Path("login")
    76     @PUT
    77     @Produces({ MediaType.TEXT_PLAIN })
    78     public String login(
    79         @QueryParam("name") String name,
    80         @QueryParam("password") String password
    81     ) throws IOException {
    82         File f = new File(path, "passwd"); // NOI18Nt
    83         Properties p = new Properties();
    84         try {
    85             p.load(new FileInputStream(f));
    86         } catch (IOException ex) {
    87             ex.printStackTrace();
    88         }
    89         boolean loggedInOK = false;
    90         if (name != null && password.equals(p.getProperty(name))) {
    91             loggedInOK = true;
    92         } else {
    93             loggedInOK = getUsers().verifyPassword(name, password);
    94         }
    95 
    96         if (loggedInOK) {
    97             UUID uuid = UUID.randomUUID();
    98             loggedIn.put(uuid, name);
    99             return uuid.toString();
   100         } else {
   101             return null;
   102         }
   103         
   104     }
   105 
   106     @Path("login")
   107     @GET
   108     @Produces({ MediaType.TEXT_PLAIN })
   109     public String isLoggedIn(
   110         @QueryParam("id") String id
   111     ) {
   112         String ret = null;
   113         try {
   114             if (id != null) {
   115                 ret = loggedIn.get(UUID.fromString(id));
   116             }
   117         } catch (IllegalArgumentException ex) {
   118             // OK, happens for invalid ids
   119         }
   120         return ret == null ? "" : ret;
   121     }
   122 
   123     @Path("login")
   124     @GET
   125     @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   126     public JSONWithPadding loggedInInfo(
   127         @QueryParam("callback") String callback,
   128         @QueryParam("id") String id
   129     ) throws IOException {
   130         String ret = null;
   131         try {
   132             if (id != null) {
   133                 ret = loggedIn.get(UUID.fromString(id));
   134             }
   135         } catch (IllegalArgumentException ex) {
   136             // OK, happens for invalid ids
   137         }
   138         if (ret == null) {
   139             throw new WebApplicationException(Status.UNAUTHORIZED);
   140         } else {
   141             return getUsers().getUserInfo(callback, id, ret);
   142         }
   143     }
   144 }