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
jtulach@35
     1
/*
jtulach@35
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@35
     3
 *
jtulach@35
     4
 * The contents of this file are subject to the terms of either the GNU
jtulach@35
     5
 * General Public License Version 2 only ("GPL") or the Common
jtulach@35
     6
 * Development and Distribution License("CDDL") (collectively, the
jtulach@35
     7
 * "License"). You may not use this file except in compliance with the
jtulach@35
     8
 * License. You can obtain a copy of the License at
jtulach@35
     9
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@35
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@35
    11
 * specific language governing permissions and limitations under the
jtulach@35
    12
 * License.  When distributing the software, include this License Header
jtulach@35
    13
 * Notice in each file and include the License file at
jtulach@35
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@35
    15
 * particular file as subject to the "Classpath" exception as provided
jtulach@35
    16
 * by Sun in the GPL Version 2 section of the License file that
jtulach@35
    17
 * accompanied this code. If applicable, add the following below the
jtulach@35
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@35
    19
 * your own identifying information:
jtulach@35
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@35
    21
 *
jtulach@35
    22
 * Contributor(s):
jtulach@35
    23
 *
jtulach@35
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@35
    25
 */
jtulach@35
    26
jtulach@35
    27
package cz.xelfi.quoridor.webidor.resources;
jtulach@35
    28
jtulach@35
    29
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
jtulach@35
    30
import com.sun.jersey.api.core.PackagesResourceConfig;
jtulach@35
    31
import com.sun.jersey.api.core.ResourceConfig;
jtulach@35
    32
import com.sun.jersey.spi.resource.Singleton;
jtulach@35
    33
import com.sun.net.httpserver.HttpServer;
jtulach@37
    34
import java.io.File;
jtulach@82
    35
import java.io.FileInputStream;
jtulach@35
    36
import java.io.IOException;
jaroslav@52
    37
import java.net.ServerSocket;
jtulach@82
    38
import java.util.HashMap;
jtulach@82
    39
import java.util.Map;
jtulach@82
    40
import java.util.Properties;
jtulach@82
    41
import java.util.UUID;
jtulach@82
    42
import javax.ws.rs.GET;
jtulach@82
    43
import javax.ws.rs.PUT;
jtulach@35
    44
import javax.ws.rs.Path;
jtulach@82
    45
import javax.ws.rs.Produces;
jtulach@82
    46
import javax.ws.rs.QueryParam;
jtulach@82
    47
import javax.ws.rs.core.MediaType;
jtulach@35
    48
jtulach@35
    49
/**
jtulach@35
    50
 *
jtulach@35
    51
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@35
    52
 */
jaroslav@124
    53
@Path("/")
jtulach@35
    54
@Singleton
jtulach@35
    55
public final class Quoridor {
jtulach@37
    56
    private final File path;
jtulach@37
    57
    private Games games;
jaroslav@143
    58
    private Users users;
jtulach@82
    59
    private final Map<UUID,String> loggedIn;
jtulach@37
    60
jtulach@37
    61
    public Quoridor() {
jtulach@37
    62
        final String prop = System.getProperty("quoridor.dir"); // NOI18N
jtulach@37
    63
        if (prop == null) {
jtulach@37
    64
            throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
jtulach@37
    65
        }
jtulach@37
    66
        path = new File(prop);
jtulach@37
    67
        path.mkdirs();
jtulach@82
    68
        loggedIn = new HashMap<UUID, String>();
jtulach@37
    69
    }
jtulach@35
    70
jtulach@35
    71
    @Path("games")
jtulach@36
    72
    public Games getGames() {
jtulach@37
    73
        if (games == null) {
jtulach@82
    74
            games = new Games(new File(path, "games"), this); // NOI18N
jtulach@37
    75
        }
jtulach@35
    76
        return games;
jtulach@35
    77
    }
jtulach@35
    78
jaroslav@143
    79
    @Path("users")
jaroslav@143
    80
    public Users getUsers() {
jaroslav@143
    81
        if (users == null) {
jaroslav@143
    82
            users = new Users(new File(path, "users"), this); // NOI18N
jaroslav@143
    83
        }
jaroslav@143
    84
        return users;
jaroslav@143
    85
    }
jaroslav@143
    86
jtulach@82
    87
    @Path("login")
jtulach@82
    88
    @PUT
jtulach@82
    89
    @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jtulach@82
    90
    public String login(
jtulach@82
    91
        @QueryParam("name") String name,
jtulach@82
    92
        @QueryParam("password") String password
jtulach@82
    93
    ) {
jtulach@82
    94
        File f = new File(path, "passwd"); // NOI18Nt
jtulach@82
    95
        Properties p = new Properties();
jtulach@82
    96
        try {
jtulach@82
    97
            p.load(new FileInputStream(f));
jtulach@82
    98
        } catch (IOException ex) {
jtulach@82
    99
            ex.printStackTrace();
jtulach@82
   100
        }
jtulach@82
   101
        if (name != null && password.equals(p.getProperty(name))) {
jtulach@82
   102
            UUID uuid = UUID.randomUUID();
jtulach@82
   103
            loggedIn.put(uuid, name);
jtulach@82
   104
            return uuid.toString();
jtulach@82
   105
        } else {
jtulach@82
   106
            return null;
jtulach@82
   107
        }
jtulach@82
   108
        
jtulach@82
   109
    }
jtulach@82
   110
jtulach@82
   111
    @Path("login")
jtulach@82
   112
    @GET
jtulach@82
   113
    @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jtulach@82
   114
    public String isLoggedIn(
jtulach@82
   115
        @QueryParam("id") String id
jtulach@82
   116
    ) {
jaroslav@85
   117
        String ret = null;
jtulach@83
   118
        try {
jaroslav@85
   119
            if (id != null) {
jaroslav@85
   120
                ret = loggedIn.get(UUID.fromString(id));
jaroslav@85
   121
            }
jtulach@83
   122
        } catch (IllegalArgumentException ex) {
jaroslav@85
   123
            // OK, happens for invalid ids
jtulach@83
   124
        }
jaroslav@85
   125
        return ret == null ? "" : ret;
jtulach@82
   126
    }
jtulach@82
   127
jtulach@35
   128
    //
jtulach@35
   129
    // start the server
jtulach@35
   130
    //
jtulach@35
   131
jaroslav@123
   132
    public static void main(String[] args) throws IOException, InterruptedException {
jaroslav@121
   133
        int port = 9222;
jaroslav@121
   134
        try {
jaroslav@121
   135
            port = Integer.parseInt(args[0]);
jaroslav@121
   136
        } catch (Exception ex) {
jaroslav@121
   137
            // OK
jaroslav@121
   138
        }
jaroslav@121
   139
        HttpServer s = start(port);
jtulach@41
   140
        System.out.println(
jaroslav@121
   141
            "Quoridor started at port " + port + "\n" + "Hit enter to stop it..."
jtulach@41
   142
        );
jaroslav@123
   143
        if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
jaroslav@123
   144
            System.out.println("Hit enter to stop it...");
jaroslav@123
   145
            System.in.read();
jaroslav@123
   146
        } else {
jaroslav@123
   147
            synchronized (Quoridor.class) {
jaroslav@123
   148
                Quoridor.class.wait();
jaroslav@123
   149
            }
jaroslav@123
   150
        }
jtulach@41
   151
        System.in.read();
jtulach@41
   152
        s.stop(0);
jtulach@41
   153
        System.exit(0);
jtulach@41
   154
    }
jtulach@35
   155
jtulach@41
   156
    public static HttpServer start(int port) throws IOException {
jaroslav@52
   157
        if (port == -1) {
jaroslav@52
   158
            ServerSocket ss = new ServerSocket(0);
jaroslav@52
   159
            port =ss.getLocalPort();
jaroslav@52
   160
            ss.close();
jaroslav@52
   161
        }
jtulach@41
   162
        final String baseUri = "http://localhost:" + port + "/";
jtulach@35
   163
jaroslav@121
   164
        if (System.getProperty("quoridor.dir") == null) {
jaroslav@121
   165
            File home = new File(System.getProperty("user.home"));
jaroslav@121
   166
            File quoridor = new File(home, ".quoridor");
jaroslav@121
   167
            System.setProperty("quoridor.dir", quoridor.getPath());
jaroslav@121
   168
        }
jtulach@38
   169
jtulach@35
   170
        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
jtulach@41
   171
        HttpServer server = HttpServerFactory.create(baseUri, rc);
jtulach@41
   172
        server.start();
jtulach@41
   173
        return server;
jtulach@35
   174
    }
jtulach@35
   175
jtulach@35
   176
}