desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
6 package cz.xelfi.quoridor.desktop.sample;
8 import com.sun.jersey.api.client.Client;
9 import com.sun.jersey.api.client.GenericType;
10 import com.sun.jersey.api.client.UniformInterfaceException;
11 import com.sun.jersey.api.client.WebResource;
12 import cz.xelfi.quoridor.webidor.Game;
13 import cz.xelfi.quoridor.webidor.GameId;
14 import java.util.List;
15 import java.util.logging.Level;
16 import java.util.logging.Logger;
17 import javax.ws.rs.core.MediaType;
18 import org.openide.util.Lookup;
20 /** Class that connects to the server and answers queries.
22 * @author Jaroslav Tulach
24 abstract class Quoridor {
25 private static Quoridor q;
27 public static Quoridor getDefault() {
31 q = Lookup.getDefault().lookup(Quoridor.class);
38 public abstract List<GameId> listGames();
39 public abstract Game getGame(String id);
41 public abstract boolean login(String login, String password);
43 private static class Impl extends Quoridor {
44 private final WebResource wr;
45 private String id = "";
46 private static final Logger LOG = Logger.getLogger(Impl.class.getName());
49 Client c = new Client();
50 wr = c.resource("http://quoridor.xelfi.cz/api/");
54 public List<GameId> listGames() {
55 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
56 List<GameId> ids = wr.path("games").queryParam("loginID", id).
57 accept(MediaType.TEXT_XML).get(gType);
63 public Game getGame(String id) {
64 Game g = wr.path("games").path(id).queryParam("loginID", id).
65 accept(MediaType.TEXT_XML).get(Game.class);
70 public boolean login(String login, String password) {
72 id = wr.path("login").queryParam("name", login).
73 queryParam("password", password).put(String.class);
75 } catch (UniformInterfaceException ex) {
76 LOG.log(Level.INFO, "Cannot login", ex);