Separating Games related operations to own class Games
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 28 Jul 2009 14:35:04 +0200
changeset 36d5ccf73ebbe5
parent 35 2e85dd878f04
child 37 782d925cb5a1
Separating Games related operations to own class Games
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Tue Jul 28 14:35:04 2009 +0200
     1.3 @@ -0,0 +1,118 @@
     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 +public final class Games {
    1.59 +    private List<Game> games = new ArrayList<Game>();
    1.60 +
    1.61 +    @POST
    1.62 +    public Game createGame(@QueryParam("white") String user1, @QueryParam("black") String user2) {
    1.63 +        Game g = new Game(user1, user2);
    1.64 +        games.add(g);
    1.65 +        return g;
    1.66 +    }
    1.67 +
    1.68 +    @PUT
    1.69 +    @Path("{id}")
    1.70 +    public Game applyMove(@PathParam("id") String id, @QueryParam("player") String player, @QueryParam("move") String move)
    1.71 +    throws IllegalPositionException {
    1.72 +        Game g = findGame(id);
    1.73 +        if (g == null) {
    1.74 +            throw new IllegalArgumentException("Unknown game " + id);
    1.75 +        }
    1.76 +        Move m = Move.valueOf(move);
    1.77 +        g.apply(player, m);
    1.78 +        return g;
    1.79 +    }
    1.80 +
    1.81 +    @GET
    1.82 +    @Produces(MediaType.APPLICATION_JSON)
    1.83 +    public List<Game> getGames() {
    1.84 +        return games;
    1.85 +    }
    1.86 +
    1.87 +    private Game findGame(String id) {
    1.88 +        for (Game g : games) {
    1.89 +            if (g.getId().equals(id)) {
    1.90 +                return g;
    1.91 +            }
    1.92 +        }
    1.93 +        return null;
    1.94 +    }
    1.95 +
    1.96 +
    1.97 +    //
    1.98 +    // start the server
    1.99 +    //
   1.100 +
   1.101 +    public static void main(String[] args) throws IOException {
   1.102 +
   1.103 +        final String baseUri = "http://localhost:9998/";
   1.104 +        final Map<String, String> initParams = new HashMap<String, String>();
   1.105 +
   1.106 +        initParams.put("com.sun.jersey.config.property.packages",
   1.107 +                "cz.xelfi.quoridor.webidor.resources");
   1.108 +
   1.109 +        System.out.println("Starting HttpServer...");
   1.110 +        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
   1.111 +        HttpServer threadSelector = HttpServerFactory.create(baseUri, rc);
   1.112 +        threadSelector.start();
   1.113 +        System.out.println(String.format(
   1.114 +                    "Jersey app started with WADL available at %sapplication.wadl\n" +
   1.115 +            "Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
   1.116 +            System.in.read();
   1.117 +            threadSelector.stop(0);
   1.118 +            System.exit(0);
   1.119 +    }
   1.120 +
   1.121 +}
     2.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sun Jul 12 13:59:07 2009 +0200
     2.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Tue Jul 28 14:35:04 2009 +0200
     2.3 @@ -55,46 +55,13 @@
     2.4  @Path("/")
     2.5  @Singleton
     2.6  public final class Quoridor {
     2.7 -    private List<Game> games = new ArrayList<Game>();
     2.8 +    private Games games = new Games();
     2.9  
    2.10 -    @POST
    2.11      @Path("games")
    2.12 -    public Game createGame(@QueryParam("white") String user1, @QueryParam("black") String user2) {
    2.13 -        Game g = new Game(user1, user2);
    2.14 -        games.add(g);
    2.15 -        return g;
    2.16 -    }
    2.17 -
    2.18 -    @PUT
    2.19 -    @Path("games/{id}")
    2.20 -    public Game applyMove(@PathParam("id") String id, @QueryParam("player") String player, @QueryParam("move") String move)
    2.21 -    throws IllegalPositionException {
    2.22 -        Game g = findGame(id);
    2.23 -        if (g == null) {
    2.24 -            throw new IllegalArgumentException("Unknown game " + id);
    2.25 -        }
    2.26 -        Move m = Move.valueOf(move);
    2.27 -        g.apply(player, m);
    2.28 -        return g;
    2.29 -    }
    2.30 -
    2.31 -    @GET
    2.32 -    @Produces(MediaType.APPLICATION_JSON)
    2.33 -    @Path("games")
    2.34 -    public List<Game> getGames() {
    2.35 +    public Games getGames() {
    2.36          return games;
    2.37      }
    2.38  
    2.39 -    private Game findGame(String id) {
    2.40 -        for (Game g : games) {
    2.41 -            if (g.getId().equals(id)) {
    2.42 -                return g;
    2.43 -            }
    2.44 -        }
    2.45 -        return null;
    2.46 -    }
    2.47 -
    2.48 -
    2.49      //
    2.50      // start the server
    2.51      //