desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:30:37 +0200
branchdesktop
changeset 263 ac802aa234fc
parent 260 8ebffb5aa10e
child 268 13fe01081e23
permissions -rw-r--r--
Login panel
     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.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;
    19 
    20 /** Class that connects to the server and answers queries.
    21  *
    22  * @author Jaroslav Tulach
    23  */
    24 abstract class Quoridor {
    25     private static Quoridor q;
    26 
    27     public static Quoridor getDefault() {
    28         if (q != null) {
    29             return q;
    30         }
    31         q = Lookup.getDefault().lookup(Quoridor.class);
    32         if (q == null) {
    33             q = new Impl();
    34         }
    35         return q;
    36     }
    37 
    38     public abstract List<GameId> listGames();
    39     public abstract Game getGame(String id);
    40 
    41     public abstract boolean login(String login, String password);
    42 
    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());
    47 
    48         public Impl() {
    49             Client c = new Client();
    50             wr = c.resource("http://quoridor.xelfi.cz/api/");
    51         }
    52 
    53         @Override
    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);
    58             return ids;
    59         }
    60 
    61 
    62         @Override
    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);
    66             return g;
    67         }
    68 
    69         @Override
    70         public boolean login(String login, String password) {
    71             try {
    72                 id = wr.path("login").queryParam("name", login).
    73                     queryParam("password", password).put(String.class);
    74                 return id != null;
    75             } catch (UniformInterfaceException ex) {
    76                 LOG.log(Level.INFO, "Cannot login", ex);
    77                 return false;
    78             }
    79         }
    80 
    81     }
    82 }