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