desktop/desktop-sample/src/main/java/cz/xelfi/quoridor/desktop/sample/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 09:58:04 +0200
branchdesktop
changeset 268 13fe01081e23
parent 263 ac802aa234fc
child 269 430ab68846fa
permissions -rw-r--r--
Updating licenses in desktop modules
jaroslav@268
     1
/**
jaroslav@268
     2
 * Quoridor server and related libraries
jaroslav@268
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@268
     4
 *
jaroslav@268
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@268
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@268
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@268
     8
 *
jaroslav@268
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@268
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@268
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@268
    12
 * GNU General Public License for more details.
jaroslav@268
    13
 *
jaroslav@268
    14
 * You should have received a copy of the GNU General Public License
jaroslav@268
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@268
    16
 * If not, see http://www.gnu.org/licenses/.
jaroslav@260
    17
 */
jaroslav@260
    18
jaroslav@260
    19
package cz.xelfi.quoridor.desktop.sample;
jaroslav@260
    20
jaroslav@260
    21
import com.sun.jersey.api.client.Client;
jaroslav@260
    22
import com.sun.jersey.api.client.GenericType;
jaroslav@263
    23
import com.sun.jersey.api.client.UniformInterfaceException;
jaroslav@260
    24
import com.sun.jersey.api.client.WebResource;
jaroslav@260
    25
import cz.xelfi.quoridor.webidor.Game;
jaroslav@260
    26
import cz.xelfi.quoridor.webidor.GameId;
jaroslav@260
    27
import java.util.List;
jaroslav@263
    28
import java.util.logging.Level;
jaroslav@263
    29
import java.util.logging.Logger;
jaroslav@260
    30
import javax.ws.rs.core.MediaType;
jaroslav@260
    31
import org.openide.util.Lookup;
jaroslav@260
    32
jaroslav@260
    33
/** Class that connects to the server and answers queries.
jaroslav@260
    34
 *
jaroslav@260
    35
 * @author Jaroslav Tulach
jaroslav@260
    36
 */
jaroslav@260
    37
abstract class Quoridor {
jaroslav@263
    38
    private static Quoridor q;
jaroslav@260
    39
jaroslav@260
    40
    public static Quoridor getDefault() {
jaroslav@263
    41
        if (q != null) {
jaroslav@263
    42
            return q;
jaroslav@263
    43
        }
jaroslav@263
    44
        q = Lookup.getDefault().lookup(Quoridor.class);
jaroslav@263
    45
        if (q == null) {
jaroslav@263
    46
            q = new Impl();
jaroslav@263
    47
        }
jaroslav@263
    48
        return q;
jaroslav@260
    49
    }
jaroslav@260
    50
jaroslav@260
    51
    public abstract List<GameId> listGames();
jaroslav@260
    52
    public abstract Game getGame(String id);
jaroslav@260
    53
jaroslav@263
    54
    public abstract boolean login(String login, String password);
jaroslav@263
    55
jaroslav@260
    56
    private static class Impl extends Quoridor {
jaroslav@260
    57
        private final WebResource wr;
jaroslav@263
    58
        private String id = "";
jaroslav@263
    59
        private static final Logger LOG = Logger.getLogger(Impl.class.getName());
jaroslav@260
    60
jaroslav@260
    61
        public Impl() {
jaroslav@260
    62
            Client c = new Client();
jaroslav@260
    63
            wr = c.resource("http://quoridor.xelfi.cz/api/");
jaroslav@260
    64
        }
jaroslav@260
    65
jaroslav@260
    66
        @Override
jaroslav@260
    67
        public List<GameId> listGames() {
jaroslav@260
    68
            GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
jaroslav@263
    69
            List<GameId> ids = wr.path("games").queryParam("loginID", id).
jaroslav@263
    70
                accept(MediaType.TEXT_XML).get(gType);
jaroslav@260
    71
            return ids;
jaroslav@260
    72
        }
jaroslav@260
    73
jaroslav@260
    74
jaroslav@260
    75
        @Override
jaroslav@260
    76
        public Game getGame(String id) {
jaroslav@263
    77
            Game g = wr.path("games").path(id).queryParam("loginID", id).
jaroslav@263
    78
                accept(MediaType.TEXT_XML).get(Game.class);
jaroslav@260
    79
            return g;
jaroslav@260
    80
        }
jaroslav@260
    81
jaroslav@263
    82
        @Override
jaroslav@263
    83
        public boolean login(String login, String password) {
jaroslav@263
    84
            try {
jaroslav@263
    85
                id = wr.path("login").queryParam("name", login).
jaroslav@263
    86
                    queryParam("password", password).put(String.class);
jaroslav@263
    87
                return id != null;
jaroslav@263
    88
            } catch (UniformInterfaceException ex) {
jaroslav@263
    89
                LOG.log(Level.INFO, "Cannot login", ex);
jaroslav@263
    90
                return false;
jaroslav@263
    91
            }
jaroslav@263
    92
        }
jaroslav@263
    93
jaroslav@260
    94
    }
jaroslav@260
    95
}