webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 239 a47345ebbdd7
child 282 40fc213a7f43
permissions -rw-r--r--
Changing headers to GPLv3
jtulach@35
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@35
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jtulach@35
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jtulach@35
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jtulach@35
    17
 */
jtulach@35
    18
jtulach@35
    19
package cz.xelfi.quoridor.webidor.resources;
jtulach@35
    20
jtulach@35
    21
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
jtulach@35
    22
import com.sun.jersey.api.core.PackagesResourceConfig;
jtulach@35
    23
import com.sun.jersey.api.core.ResourceConfig;
jaroslav@189
    24
import com.sun.jersey.api.json.JSONWithPadding;
jtulach@35
    25
import com.sun.jersey.spi.resource.Singleton;
jtulach@35
    26
import com.sun.net.httpserver.HttpServer;
jaroslav@145
    27
import cz.xelfi.quoridor.webidor.User;
jtulach@37
    28
import java.io.File;
jtulach@82
    29
import java.io.FileInputStream;
jtulach@35
    30
import java.io.IOException;
jaroslav@52
    31
import java.net.ServerSocket;
jtulach@82
    32
import java.util.HashMap;
jtulach@82
    33
import java.util.Map;
jtulach@82
    34
import java.util.Properties;
jtulach@82
    35
import java.util.UUID;
jtulach@82
    36
import javax.ws.rs.GET;
jtulach@82
    37
import javax.ws.rs.PUT;
jtulach@35
    38
import javax.ws.rs.Path;
jtulach@82
    39
import javax.ws.rs.Produces;
jtulach@82
    40
import javax.ws.rs.QueryParam;
jaroslav@189
    41
import javax.ws.rs.WebApplicationException;
jtulach@82
    42
import javax.ws.rs.core.MediaType;
jaroslav@189
    43
import javax.ws.rs.core.Response.Status;
jtulach@35
    44
jtulach@35
    45
/**
jtulach@35
    46
 *
jtulach@35
    47
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@35
    48
 */
jaroslav@124
    49
@Path("/")
jtulach@35
    50
@Singleton
jtulach@35
    51
public final class Quoridor {
jtulach@37
    52
    private final File path;
jtulach@37
    53
    private Games games;
jaroslav@143
    54
    private Users users;
jtulach@82
    55
    private final Map<UUID,String> loggedIn;
jtulach@37
    56
jtulach@37
    57
    public Quoridor() {
jtulach@37
    58
        final String prop = System.getProperty("quoridor.dir"); // NOI18N
jtulach@37
    59
        if (prop == null) {
jtulach@37
    60
            throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
jtulach@37
    61
        }
jtulach@37
    62
        path = new File(prop);
jtulach@37
    63
        path.mkdirs();
jtulach@82
    64
        loggedIn = new HashMap<UUID, String>();
jaroslav@189
    65
        games = new Games(new File(path, "games"), this); // NOI18N
jaroslav@189
    66
        users = new Users(new File(path, "users"), this); // NOI18N
jtulach@37
    67
    }
jtulach@35
    68
jtulach@35
    69
    @Path("games")
jtulach@36
    70
    public Games getGames() {
jtulach@35
    71
        return games;
jtulach@35
    72
    }
jtulach@35
    73
jaroslav@143
    74
    @Path("users")
jaroslav@143
    75
    public Users getUsers() {
jaroslav@143
    76
        return users;
jaroslav@143
    77
    }
jaroslav@143
    78
jtulach@82
    79
    @Path("login")
jtulach@82
    80
    @PUT
jaroslav@145
    81
    @Produces({ MediaType.TEXT_PLAIN })
jtulach@82
    82
    public String login(
jtulach@82
    83
        @QueryParam("name") String name,
jtulach@82
    84
        @QueryParam("password") String password
jtulach@239
    85
    ) throws IOException {
jtulach@82
    86
        File f = new File(path, "passwd"); // NOI18Nt
jtulach@82
    87
        Properties p = new Properties();
jtulach@82
    88
        try {
jtulach@82
    89
            p.load(new FileInputStream(f));
jtulach@82
    90
        } catch (IOException ex) {
jtulach@82
    91
            ex.printStackTrace();
jtulach@82
    92
        }
jtulach@239
    93
        boolean loggedInOK = false;
jtulach@82
    94
        if (name != null && password.equals(p.getProperty(name))) {
jtulach@239
    95
            loggedInOK = true;
jtulach@239
    96
        } else {
jtulach@239
    97
            loggedInOK = getUsers().verifyPassword(name, password);
jtulach@239
    98
        }
jtulach@239
    99
jtulach@239
   100
        if (loggedInOK) {
jtulach@82
   101
            UUID uuid = UUID.randomUUID();
jtulach@82
   102
            loggedIn.put(uuid, name);
jtulach@82
   103
            return uuid.toString();
jtulach@82
   104
        } else {
jtulach@82
   105
            return null;
jtulach@82
   106
        }
jtulach@82
   107
        
jtulach@82
   108
    }
jtulach@82
   109
jtulach@82
   110
    @Path("login")
jtulach@82
   111
    @GET
jaroslav@145
   112
    @Produces({ MediaType.TEXT_PLAIN })
jtulach@82
   113
    public String isLoggedIn(
jtulach@82
   114
        @QueryParam("id") String id
jtulach@82
   115
    ) {
jaroslav@85
   116
        String ret = null;
jtulach@83
   117
        try {
jaroslav@85
   118
            if (id != null) {
jaroslav@85
   119
                ret = loggedIn.get(UUID.fromString(id));
jaroslav@85
   120
            }
jtulach@83
   121
        } catch (IllegalArgumentException ex) {
jaroslav@85
   122
            // OK, happens for invalid ids
jtulach@83
   123
        }
jaroslav@85
   124
        return ret == null ? "" : ret;
jtulach@82
   125
    }
jtulach@82
   126
jaroslav@145
   127
    @Path("login")
jaroslav@145
   128
    @GET
jaroslav@189
   129
    @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@189
   130
    public JSONWithPadding loggedInInfo(
jaroslav@189
   131
        @QueryParam("callback") String callback,
jaroslav@145
   132
        @QueryParam("id") String id
jaroslav@145
   133
    ) throws IOException {
jaroslav@145
   134
        String ret = null;
jaroslav@145
   135
        try {
jaroslav@145
   136
            if (id != null) {
jaroslav@145
   137
                ret = loggedIn.get(UUID.fromString(id));
jaroslav@145
   138
            }
jaroslav@145
   139
        } catch (IllegalArgumentException ex) {
jaroslav@145
   140
            // OK, happens for invalid ids
jaroslav@145
   141
        }
jaroslav@189
   142
        if (ret == null) {
jaroslav@189
   143
            throw new WebApplicationException(Status.UNAUTHORIZED);
jaroslav@189
   144
        } else {
jaroslav@189
   145
            return getUsers().getUserInfo(callback, id, ret);
jaroslav@189
   146
        }
jaroslav@145
   147
    }
jaroslav@145
   148
jtulach@35
   149
    //
jtulach@35
   150
    // start the server
jtulach@35
   151
    //
jtulach@35
   152
jaroslav@123
   153
    public static void main(String[] args) throws IOException, InterruptedException {
jaroslav@121
   154
        int port = 9222;
jaroslav@121
   155
        try {
jaroslav@121
   156
            port = Integer.parseInt(args[0]);
jaroslav@121
   157
        } catch (Exception ex) {
jaroslav@121
   158
            // OK
jaroslav@121
   159
        }
jaroslav@121
   160
        HttpServer s = start(port);
jtulach@41
   161
        System.out.println(
jaroslav@121
   162
            "Quoridor started at port " + port + "\n" + "Hit enter to stop it..."
jtulach@41
   163
        );
jaroslav@123
   164
        if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
jaroslav@123
   165
            System.out.println("Hit enter to stop it...");
jaroslav@123
   166
            System.in.read();
jaroslav@123
   167
        } else {
jaroslav@123
   168
            synchronized (Quoridor.class) {
jaroslav@123
   169
                Quoridor.class.wait();
jaroslav@123
   170
            }
jaroslav@123
   171
        }
jtulach@41
   172
        System.in.read();
jtulach@41
   173
        s.stop(0);
jtulach@41
   174
        System.exit(0);
jtulach@41
   175
    }
jtulach@35
   176
jtulach@41
   177
    public static HttpServer start(int port) throws IOException {
jaroslav@52
   178
        if (port == -1) {
jaroslav@52
   179
            ServerSocket ss = new ServerSocket(0);
jaroslav@52
   180
            port =ss.getLocalPort();
jaroslav@52
   181
            ss.close();
jaroslav@52
   182
        }
jtulach@41
   183
        final String baseUri = "http://localhost:" + port + "/";
jtulach@35
   184
jaroslav@121
   185
        if (System.getProperty("quoridor.dir") == null) {
jaroslav@121
   186
            File home = new File(System.getProperty("user.home"));
jaroslav@121
   187
            File quoridor = new File(home, ".quoridor");
jaroslav@121
   188
            System.setProperty("quoridor.dir", quoridor.getPath());
jaroslav@121
   189
        }
jtulach@38
   190
jtulach@35
   191
        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
jtulach@41
   192
        HttpServer server = HttpServerFactory.create(baseUri, rc);
jtulach@41
   193
        server.start();
jtulach@41
   194
        return server;
jtulach@35
   195
    }
jtulach@35
   196
jtulach@35
   197
}