webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Nov 2009 15:23:14 +0100
changeset 143 4eb88f05c207
parent 124 90371f3eb106
child 145 ac9bd9be5263
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 com.sun.jersey.api.container.httpserver.HttpServerFactory;
    30 import com.sun.jersey.api.core.PackagesResourceConfig;
    31 import com.sun.jersey.api.core.ResourceConfig;
    32 import com.sun.jersey.spi.resource.Singleton;
    33 import com.sun.net.httpserver.HttpServer;
    34 import java.io.File;
    35 import java.io.FileInputStream;
    36 import java.io.IOException;
    37 import java.net.ServerSocket;
    38 import java.util.HashMap;
    39 import java.util.Map;
    40 import java.util.Properties;
    41 import java.util.UUID;
    42 import javax.ws.rs.GET;
    43 import javax.ws.rs.PUT;
    44 import javax.ws.rs.Path;
    45 import javax.ws.rs.Produces;
    46 import javax.ws.rs.QueryParam;
    47 import javax.ws.rs.core.MediaType;
    48 
    49 /**
    50  *
    51  * @author Jaroslav Tulach <jtulach@netbeans.org>
    52  */
    53 @Path("/")
    54 @Singleton
    55 public final class Quoridor {
    56     private final File path;
    57     private Games games;
    58     private Users users;
    59     private final Map<UUID,String> loggedIn;
    60 
    61     public Quoridor() {
    62         final String prop = System.getProperty("quoridor.dir"); // NOI18N
    63         if (prop == null) {
    64             throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
    65         }
    66         path = new File(prop);
    67         path.mkdirs();
    68         loggedIn = new HashMap<UUID, String>();
    69     }
    70 
    71     @Path("games")
    72     public Games getGames() {
    73         if (games == null) {
    74             games = new Games(new File(path, "games"), this); // NOI18N
    75         }
    76         return games;
    77     }
    78 
    79     @Path("users")
    80     public Users getUsers() {
    81         if (users == null) {
    82             users = new Users(new File(path, "users"), this); // NOI18N
    83         }
    84         return users;
    85     }
    86 
    87     @Path("login")
    88     @PUT
    89     @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    90     public String login(
    91         @QueryParam("name") String name,
    92         @QueryParam("password") String password
    93     ) {
    94         File f = new File(path, "passwd"); // NOI18Nt
    95         Properties p = new Properties();
    96         try {
    97             p.load(new FileInputStream(f));
    98         } catch (IOException ex) {
    99             ex.printStackTrace();
   100         }
   101         if (name != null && password.equals(p.getProperty(name))) {
   102             UUID uuid = UUID.randomUUID();
   103             loggedIn.put(uuid, name);
   104             return uuid.toString();
   105         } else {
   106             return null;
   107         }
   108         
   109     }
   110 
   111     @Path("login")
   112     @GET
   113     @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   114     public String isLoggedIn(
   115         @QueryParam("id") String id
   116     ) {
   117         String ret = null;
   118         try {
   119             if (id != null) {
   120                 ret = loggedIn.get(UUID.fromString(id));
   121             }
   122         } catch (IllegalArgumentException ex) {
   123             // OK, happens for invalid ids
   124         }
   125         return ret == null ? "" : ret;
   126     }
   127 
   128     //
   129     // start the server
   130     //
   131 
   132     public static void main(String[] args) throws IOException, InterruptedException {
   133         int port = 9222;
   134         try {
   135             port = Integer.parseInt(args[0]);
   136         } catch (Exception ex) {
   137             // OK
   138         }
   139         HttpServer s = start(port);
   140         System.out.println(
   141             "Quoridor started at port " + port + "\n" + "Hit enter to stop it..."
   142         );
   143         if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
   144             System.out.println("Hit enter to stop it...");
   145             System.in.read();
   146         } else {
   147             synchronized (Quoridor.class) {
   148                 Quoridor.class.wait();
   149             }
   150         }
   151         System.in.read();
   152         s.stop(0);
   153         System.exit(0);
   154     }
   155 
   156     public static HttpServer start(int port) throws IOException {
   157         if (port == -1) {
   158             ServerSocket ss = new ServerSocket(0);
   159             port =ss.getLocalPort();
   160             ss.close();
   161         }
   162         final String baseUri = "http://localhost:" + port + "/";
   163 
   164         if (System.getProperty("quoridor.dir") == null) {
   165             File home = new File(System.getProperty("user.home"));
   166             File quoridor = new File(home, ".quoridor");
   167             System.setProperty("quoridor.dir", quoridor.getPath());
   168         }
   169 
   170         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
   171         HttpServer server = HttpServerFactory.create(baseUri, rc);
   172         server.start();
   173         return server;
   174     }
   175 
   176 }