# HG changeset patch # User Jaroslav Tulach # Date 1248784504 -7200 # Node ID d5ccf73ebbe51220e4e4e0384110c8ca4d3fc93c # Parent 2e85dd878f04772b878dc4986028dd675483db3e Separating Games related operations to own class Games diff -r 2e85dd878f04 -r d5ccf73ebbe5 webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Tue Jul 28 14:35:04 2009 +0200 @@ -0,0 +1,118 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor.resources; + +import com.sun.jersey.api.container.httpserver.HttpServerFactory; +import com.sun.jersey.api.core.PackagesResourceConfig; +import com.sun.jersey.api.core.ResourceConfig; +import cz.xelfi.quoridor.IllegalPositionException; +import cz.xelfi.quoridor.webidor.*; +import com.sun.jersey.spi.resource.Singleton; +import com.sun.net.httpserver.HttpServer; +import cz.xelfi.quoridor.Move; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +/** + * + * @author Jaroslav Tulach + */ +public final class Games { + private List games = new ArrayList(); + + @POST + public Game createGame(@QueryParam("white") String user1, @QueryParam("black") String user2) { + Game g = new Game(user1, user2); + games.add(g); + return g; + } + + @PUT + @Path("{id}") + public Game applyMove(@PathParam("id") String id, @QueryParam("player") String player, @QueryParam("move") String move) + throws IllegalPositionException { + Game g = findGame(id); + if (g == null) { + throw new IllegalArgumentException("Unknown game " + id); + } + Move m = Move.valueOf(move); + g.apply(player, m); + return g; + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public List getGames() { + return games; + } + + private Game findGame(String id) { + for (Game g : games) { + if (g.getId().equals(id)) { + return g; + } + } + return null; + } + + + // + // start the server + // + + public static void main(String[] args) throws IOException { + + final String baseUri = "http://localhost:9998/"; + final Map initParams = new HashMap(); + + initParams.put("com.sun.jersey.config.property.packages", + "cz.xelfi.quoridor.webidor.resources"); + + System.out.println("Starting HttpServer..."); + ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor"); + HttpServer threadSelector = HttpServerFactory.create(baseUri, rc); + threadSelector.start(); + System.out.println(String.format( + "Jersey app started with WADL available at %sapplication.wadl\n" + + "Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri)); + System.in.read(); + threadSelector.stop(0); + System.exit(0); + } + +} diff -r 2e85dd878f04 -r d5ccf73ebbe5 webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Sun Jul 12 13:59:07 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Tue Jul 28 14:35:04 2009 +0200 @@ -55,46 +55,13 @@ @Path("/") @Singleton public final class Quoridor { - private List games = new ArrayList(); + private Games games = new Games(); - @POST @Path("games") - public Game createGame(@QueryParam("white") String user1, @QueryParam("black") String user2) { - Game g = new Game(user1, user2); - games.add(g); - return g; - } - - @PUT - @Path("games/{id}") - public Game applyMove(@PathParam("id") String id, @QueryParam("player") String player, @QueryParam("move") String move) - throws IllegalPositionException { - Game g = findGame(id); - if (g == null) { - throw new IllegalArgumentException("Unknown game " + id); - } - Move m = Move.valueOf(move); - g.apply(player, m); - return g; - } - - @GET - @Produces(MediaType.APPLICATION_JSON) - @Path("games") - public List getGames() { + public Games getGames() { return games; } - private Game findGame(String id) { - for (Game g : games) { - if (g.getId().equals(id)) { - return g; - } - } - return null; - } - - // // start the server //