1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Sun Jul 12 13:59:07 2009 +0200
1.3 @@ -0,0 +1,122 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.webidor.resources;
1.31 +
1.32 +import com.sun.jersey.api.container.httpserver.HttpServerFactory;
1.33 +import com.sun.jersey.api.core.PackagesResourceConfig;
1.34 +import com.sun.jersey.api.core.ResourceConfig;
1.35 +import cz.xelfi.quoridor.IllegalPositionException;
1.36 +import cz.xelfi.quoridor.webidor.*;
1.37 +import com.sun.jersey.spi.resource.Singleton;
1.38 +import com.sun.net.httpserver.HttpServer;
1.39 +import cz.xelfi.quoridor.Move;
1.40 +import java.io.IOException;
1.41 +import java.util.ArrayList;
1.42 +import java.util.HashMap;
1.43 +import java.util.List;
1.44 +import java.util.Map;
1.45 +import javax.ws.rs.GET;
1.46 +import javax.ws.rs.POST;
1.47 +import javax.ws.rs.PUT;
1.48 +import javax.ws.rs.Path;
1.49 +import javax.ws.rs.PathParam;
1.50 +import javax.ws.rs.Produces;
1.51 +import javax.ws.rs.QueryParam;
1.52 +import javax.ws.rs.core.MediaType;
1.53 +
1.54 +/**
1.55 + *
1.56 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.57 + */
1.58 +@Path("/")
1.59 +@Singleton
1.60 +public final class Quoridor {
1.61 + private List<Game> games = new ArrayList<Game>();
1.62 +
1.63 + @POST
1.64 + @Path("games")
1.65 + public Game createGame(@QueryParam("white") String user1, @QueryParam("black") String user2) {
1.66 + Game g = new Game(user1, user2);
1.67 + games.add(g);
1.68 + return g;
1.69 + }
1.70 +
1.71 + @PUT
1.72 + @Path("games/{id}")
1.73 + public Game applyMove(@PathParam("id") String id, @QueryParam("player") String player, @QueryParam("move") String move)
1.74 + throws IllegalPositionException {
1.75 + Game g = findGame(id);
1.76 + if (g == null) {
1.77 + throw new IllegalArgumentException("Unknown game " + id);
1.78 + }
1.79 + Move m = Move.valueOf(move);
1.80 + g.apply(player, m);
1.81 + return g;
1.82 + }
1.83 +
1.84 + @GET
1.85 + @Produces(MediaType.APPLICATION_JSON)
1.86 + @Path("games")
1.87 + public List<Game> getGames() {
1.88 + return games;
1.89 + }
1.90 +
1.91 + private Game findGame(String id) {
1.92 + for (Game g : games) {
1.93 + if (g.getId().equals(id)) {
1.94 + return g;
1.95 + }
1.96 + }
1.97 + return null;
1.98 + }
1.99 +
1.100 +
1.101 + //
1.102 + // start the server
1.103 + //
1.104 +
1.105 + public static void main(String[] args) throws IOException {
1.106 +
1.107 + final String baseUri = "http://localhost:9998/";
1.108 + final Map<String, String> initParams = new HashMap<String, String>();
1.109 +
1.110 + initParams.put("com.sun.jersey.config.property.packages",
1.111 + "cz.xelfi.quoridor.webidor.resources");
1.112 +
1.113 + System.out.println("Starting HttpServer...");
1.114 + ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
1.115 + HttpServer threadSelector = HttpServerFactory.create(baseUri, rc);
1.116 + threadSelector.start();
1.117 + System.out.println(String.format(
1.118 + "Jersey app started with WADL available at %sapplication.wadl\n" +
1.119 + "Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
1.120 + System.in.read();
1.121 + threadSelector.stop(0);
1.122 + System.exit(0);
1.123 + }
1.124 +
1.125 +}