webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 12 Jul 2009 13:59:07 +0200
changeset 35 2e85dd878f04
child 36 d5ccf73ebbe5
permissions -rw-r--r--
Converting the webidor into Jersey based REST server
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor.resources;
    28 
    29 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
    30 import com.sun.jersey.api.core.PackagesResourceConfig;
    31 import com.sun.jersey.api.core.ResourceConfig;
    32 import cz.xelfi.quoridor.IllegalPositionException;
    33 import cz.xelfi.quoridor.webidor.*;
    34 import com.sun.jersey.spi.resource.Singleton;
    35 import com.sun.net.httpserver.HttpServer;
    36 import cz.xelfi.quoridor.Move;
    37 import java.io.IOException;
    38 import java.util.ArrayList;
    39 import java.util.HashMap;
    40 import java.util.List;
    41 import java.util.Map;
    42 import javax.ws.rs.GET;
    43 import javax.ws.rs.POST;
    44 import javax.ws.rs.PUT;
    45 import javax.ws.rs.Path;
    46 import javax.ws.rs.PathParam;
    47 import javax.ws.rs.Produces;
    48 import javax.ws.rs.QueryParam;
    49 import javax.ws.rs.core.MediaType;
    50 
    51 /**
    52  *
    53  * @author Jaroslav Tulach <jtulach@netbeans.org>
    54  */
    55 @Path("/")
    56 @Singleton
    57 public final class Quoridor {
    58     private List<Game> games = new ArrayList<Game>();
    59 
    60     @POST
    61     @Path("games")
    62     public Game createGame(@QueryParam("white") String user1, @QueryParam("black") String user2) {
    63         Game g = new Game(user1, user2);
    64         games.add(g);
    65         return g;
    66     }
    67 
    68     @PUT
    69     @Path("games/{id}")
    70     public Game applyMove(@PathParam("id") String id, @QueryParam("player") String player, @QueryParam("move") String move)
    71     throws IllegalPositionException {
    72         Game g = findGame(id);
    73         if (g == null) {
    74             throw new IllegalArgumentException("Unknown game " + id);
    75         }
    76         Move m = Move.valueOf(move);
    77         g.apply(player, m);
    78         return g;
    79     }
    80 
    81     @GET
    82     @Produces(MediaType.APPLICATION_JSON)
    83     @Path("games")
    84     public List<Game> getGames() {
    85         return games;
    86     }
    87 
    88     private Game findGame(String id) {
    89         for (Game g : games) {
    90             if (g.getId().equals(id)) {
    91                 return g;
    92             }
    93         }
    94         return null;
    95     }
    96 
    97 
    98     //
    99     // start the server
   100     //
   101 
   102     public static void main(String[] args) throws IOException {
   103 
   104         final String baseUri = "http://localhost:9998/";
   105         final Map<String, String> initParams = new HashMap<String, String>();
   106 
   107         initParams.put("com.sun.jersey.config.property.packages",
   108                 "cz.xelfi.quoridor.webidor.resources");
   109 
   110         System.out.println("Starting HttpServer...");
   111         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
   112         HttpServer threadSelector = HttpServerFactory.create(baseUri, rc);
   113         threadSelector.start();
   114         System.out.println(String.format(
   115                     "Jersey app started with WADL available at %sapplication.wadl\n" +
   116             "Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
   117             System.in.read();
   118             threadSelector.stop(0);
   119             System.exit(0);
   120     }
   121 
   122 }