desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java
branchdesktop
changeset 263 ac802aa234fc
parent 260 8ebffb5aa10e
child 268 13fe01081e23
     1.1 --- a/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java	Mon Sep 13 16:27:07 2010 +0200
     1.2 +++ b/desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java	Tue Sep 14 08:30:37 2010 +0200
     1.3 @@ -7,10 +7,13 @@
     1.4  
     1.5  import com.sun.jersey.api.client.Client;
     1.6  import com.sun.jersey.api.client.GenericType;
     1.7 +import com.sun.jersey.api.client.UniformInterfaceException;
     1.8  import com.sun.jersey.api.client.WebResource;
     1.9  import cz.xelfi.quoridor.webidor.Game;
    1.10  import cz.xelfi.quoridor.webidor.GameId;
    1.11  import java.util.List;
    1.12 +import java.util.logging.Level;
    1.13 +import java.util.logging.Logger;
    1.14  import javax.ws.rs.core.MediaType;
    1.15  import org.openide.util.Lookup;
    1.16  
    1.17 @@ -19,17 +22,28 @@
    1.18   * @author Jaroslav Tulach
    1.19   */
    1.20  abstract class Quoridor {
    1.21 +    private static Quoridor q;
    1.22  
    1.23      public static Quoridor getDefault() {
    1.24 -        Quoridor q = Lookup.getDefault().lookup(Quoridor.class);
    1.25 -        return q == null ? new Impl() : q;
    1.26 +        if (q != null) {
    1.27 +            return q;
    1.28 +        }
    1.29 +        q = Lookup.getDefault().lookup(Quoridor.class);
    1.30 +        if (q == null) {
    1.31 +            q = new Impl();
    1.32 +        }
    1.33 +        return q;
    1.34      }
    1.35  
    1.36      public abstract List<GameId> listGames();
    1.37      public abstract Game getGame(String id);
    1.38  
    1.39 +    public abstract boolean login(String login, String password);
    1.40 +
    1.41      private static class Impl extends Quoridor {
    1.42          private final WebResource wr;
    1.43 +        private String id = "";
    1.44 +        private static final Logger LOG = Logger.getLogger(Impl.class.getName());
    1.45  
    1.46          public Impl() {
    1.47              Client c = new Client();
    1.48 @@ -39,16 +53,30 @@
    1.49          @Override
    1.50          public List<GameId> listGames() {
    1.51              GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
    1.52 -            List<GameId> ids = wr.path("games").accept(MediaType.TEXT_XML).get(gType);
    1.53 +            List<GameId> ids = wr.path("games").queryParam("loginID", id).
    1.54 +                accept(MediaType.TEXT_XML).get(gType);
    1.55              return ids;
    1.56          }
    1.57  
    1.58  
    1.59          @Override
    1.60          public Game getGame(String id) {
    1.61 -            Game g = wr.path("games").path(id).accept(MediaType.TEXT_XML).get(Game.class);
    1.62 +            Game g = wr.path("games").path(id).queryParam("loginID", id).
    1.63 +                accept(MediaType.TEXT_XML).get(Game.class);
    1.64              return g;
    1.65          }
    1.66  
    1.67 +        @Override
    1.68 +        public boolean login(String login, String password) {
    1.69 +            try {
    1.70 +                id = wr.path("login").queryParam("name", login).
    1.71 +                    queryParam("password", password).put(String.class);
    1.72 +                return id != null;
    1.73 +            } catch (UniformInterfaceException ex) {
    1.74 +                LOG.log(Level.INFO, "Cannot login", ex);
    1.75 +                return false;
    1.76 +            }
    1.77 +        }
    1.78 +
    1.79      }
    1.80  }