webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 13 Sep 2009 20:07:32 +0200
changeset 83 8dd8b041a3e1
parent 82 9ac7acee7d9f
child 85 3574b08dc3c3
permissions -rw-r--r--
Necessary fixes to make the system work at least a bit
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
 */
jtulach@41
    53
@Path("/api")
jtulach@35
    54
@Singleton
jtulach@35
    55
public final class Quoridor {
jtulach@37
    56
    private final File path;
jtulach@37
    57
    private Games games;
jtulach@82
    58
    private final Map<UUID,String> loggedIn;
jtulach@37
    59
jtulach@37
    60
    public Quoridor() {
jtulach@37
    61
        final String prop = System.getProperty("quoridor.dir"); // NOI18N
jtulach@37
    62
        if (prop == null) {
jtulach@37
    63
            throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
jtulach@37
    64
        }
jtulach@37
    65
        path = new File(prop);
jtulach@37
    66
        path.mkdirs();
jtulach@82
    67
        loggedIn = new HashMap<UUID, String>();
jtulach@37
    68
    }
jtulach@35
    69
jtulach@35
    70
    @Path("games")
jtulach@36
    71
    public Games getGames() {
jtulach@37
    72
        if (games == null) {
jtulach@82
    73
            games = new Games(new File(path, "games"), this); // NOI18N
jtulach@37
    74
        }
jtulach@35
    75
        return games;
jtulach@35
    76
    }
jtulach@35
    77
jtulach@82
    78
    @Path("login")
jtulach@82
    79
    @PUT
jtulach@82
    80
    @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jtulach@82
    81
    public String login(
jtulach@82
    82
        @QueryParam("name") String name,
jtulach@82
    83
        @QueryParam("password") String password
jtulach@82
    84
    ) {
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@82
    92
        if (name != null && password.equals(p.getProperty(name))) {
jtulach@82
    93
            UUID uuid = UUID.randomUUID();
jtulach@82
    94
            loggedIn.put(uuid, name);
jtulach@82
    95
            return uuid.toString();
jtulach@82
    96
        } else {
jtulach@82
    97
            return null;
jtulach@82
    98
        }
jtulach@82
    99
        
jtulach@82
   100
    }
jtulach@82
   101
jtulach@82
   102
    @Path("login")
jtulach@82
   103
    @GET
jtulach@82
   104
    @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jtulach@82
   105
    public String isLoggedIn(
jtulach@82
   106
        @QueryParam("id") String id
jtulach@82
   107
    ) {
jtulach@83
   108
        try {
jtulach@83
   109
            return id == null ? "" : loggedIn.get(UUID.fromString(id));
jtulach@83
   110
        } catch (IllegalArgumentException ex) {
jtulach@83
   111
            return "";
jtulach@83
   112
        }
jtulach@82
   113
    }
jtulach@82
   114
jtulach@35
   115
    //
jtulach@35
   116
    // start the server
jtulach@35
   117
    //
jtulach@35
   118
jtulach@35
   119
    public static void main(String[] args) throws IOException {
jtulach@41
   120
        HttpServer s = start(9998);
jtulach@41
   121
        System.out.println(
jtulach@41
   122
            "Quoridor started at port 9998\n" + "Hit enter to stop it..."
jtulach@41
   123
        );
jtulach@41
   124
        System.in.read();
jtulach@41
   125
        s.stop(0);
jtulach@41
   126
        System.exit(0);
jtulach@41
   127
    }
jtulach@35
   128
jtulach@41
   129
    public static HttpServer start(int port) throws IOException {
jaroslav@52
   130
        if (port == -1) {
jaroslav@52
   131
            ServerSocket ss = new ServerSocket(0);
jaroslav@52
   132
            port =ss.getLocalPort();
jaroslav@52
   133
            ss.close();
jaroslav@52
   134
        }
jtulach@41
   135
        final String baseUri = "http://localhost:" + port + "/";
jtulach@35
   136
jtulach@38
   137
        File home = new File(System.getProperty("user.home"));
jtulach@38
   138
        File quoridor = new File(home, ".quoridor");
jtulach@38
   139
jtulach@38
   140
        System.setProperty("quoridor.dir", quoridor.getPath());
jtulach@38
   141
jtulach@35
   142
        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
jtulach@41
   143
        HttpServer server = HttpServerFactory.create(baseUri, rc);
jtulach@41
   144
        server.start();
jtulach@41
   145
        return server;
jtulach@35
   146
    }
jtulach@35
   147
jtulach@35
   148
}