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