webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 18 Oct 2009 15:48:47 +0200
changeset 124 90371f3eb106
parent 123 4529cb22ff7d
child 143 4eb88f05c207
permissions -rw-r--r--
Removing /api prefix from the webidor server
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;
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
    ) {
jaroslav@85
   108
        String ret = null;
jtulach@83
   109
        try {
jaroslav@85
   110
            if (id != null) {
jaroslav@85
   111
                ret = loggedIn.get(UUID.fromString(id));
jaroslav@85
   112
            }
jtulach@83
   113
        } catch (IllegalArgumentException ex) {
jaroslav@85
   114
            // OK, happens for invalid ids
jtulach@83
   115
        }
jaroslav@85
   116
        return ret == null ? "" : ret;
jtulach@82
   117
    }
jtulach@82
   118
jtulach@35
   119
    //
jtulach@35
   120
    // start the server
jtulach@35
   121
    //
jtulach@35
   122
jaroslav@123
   123
    public static void main(String[] args) throws IOException, InterruptedException {
jaroslav@121
   124
        int port = 9222;
jaroslav@121
   125
        try {
jaroslav@121
   126
            port = Integer.parseInt(args[0]);
jaroslav@121
   127
        } catch (Exception ex) {
jaroslav@121
   128
            // OK
jaroslav@121
   129
        }
jaroslav@121
   130
        HttpServer s = start(port);
jtulach@41
   131
        System.out.println(
jaroslav@121
   132
            "Quoridor started at port " + port + "\n" + "Hit enter to stop it..."
jtulach@41
   133
        );
jaroslav@123
   134
        if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
jaroslav@123
   135
            System.out.println("Hit enter to stop it...");
jaroslav@123
   136
            System.in.read();
jaroslav@123
   137
        } else {
jaroslav@123
   138
            synchronized (Quoridor.class) {
jaroslav@123
   139
                Quoridor.class.wait();
jaroslav@123
   140
            }
jaroslav@123
   141
        }
jtulach@41
   142
        System.in.read();
jtulach@41
   143
        s.stop(0);
jtulach@41
   144
        System.exit(0);
jtulach@41
   145
    }
jtulach@35
   146
jtulach@41
   147
    public static HttpServer start(int port) throws IOException {
jaroslav@52
   148
        if (port == -1) {
jaroslav@52
   149
            ServerSocket ss = new ServerSocket(0);
jaroslav@52
   150
            port =ss.getLocalPort();
jaroslav@52
   151
            ss.close();
jaroslav@52
   152
        }
jtulach@41
   153
        final String baseUri = "http://localhost:" + port + "/";
jtulach@35
   154
jaroslav@121
   155
        if (System.getProperty("quoridor.dir") == null) {
jaroslav@121
   156
            File home = new File(System.getProperty("user.home"));
jaroslav@121
   157
            File quoridor = new File(home, ".quoridor");
jaroslav@121
   158
            System.setProperty("quoridor.dir", quoridor.getPath());
jaroslav@121
   159
        }
jtulach@38
   160
jtulach@35
   161
        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
jtulach@41
   162
        HttpServer server = HttpServerFactory.create(baseUri, rc);
jtulach@41
   163
        server.start();
jtulach@41
   164
        return server;
jtulach@35
   165
    }
jtulach@35
   166
jtulach@35
   167
}