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