desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 13 Sep 2010 16:27:07 +0200
branchdesktop
changeset 260 8ebffb5aa10e
child 263 ac802aa234fc
permissions -rw-r--r--
Shows all current games
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 package cz.xelfi.quoridor.desktop.sample;
     7 
     8 import com.sun.jersey.api.client.Client;
     9 import com.sun.jersey.api.client.GenericType;
    10 import com.sun.jersey.api.client.WebResource;
    11 import cz.xelfi.quoridor.webidor.Game;
    12 import cz.xelfi.quoridor.webidor.GameId;
    13 import java.util.List;
    14 import javax.ws.rs.core.MediaType;
    15 import org.openide.util.Lookup;
    16 
    17 /** Class that connects to the server and answers queries.
    18  *
    19  * @author Jaroslav Tulach
    20  */
    21 abstract class Quoridor {
    22 
    23     public static Quoridor getDefault() {
    24         Quoridor q = Lookup.getDefault().lookup(Quoridor.class);
    25         return q == null ? new Impl() : q;
    26     }
    27 
    28     public abstract List<GameId> listGames();
    29     public abstract Game getGame(String id);
    30 
    31     private static class Impl extends Quoridor {
    32         private final WebResource wr;
    33 
    34         public Impl() {
    35             Client c = new Client();
    36             wr = c.resource("http://quoridor.xelfi.cz/api/");
    37         }
    38 
    39         @Override
    40         public List<GameId> listGames() {
    41             GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
    42             List<GameId> ids = wr.path("games").accept(MediaType.TEXT_XML).get(gType);
    43             return ids;
    44         }
    45 
    46 
    47         @Override
    48         public Game getGame(String id) {
    49             Game g = wr.path("games").path(id).accept(MediaType.TEXT_XML).get(Game.class);
    50             return g;
    51         }
    52 
    53     }
    54 }