freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Sep 2009 23:02:23 +0200
changeset 59 fd7e8e705b75
parent 57 fa12b02023a0
child 68 33cf89760fab
permissions -rw-r--r--
Localization of the game
jtulach@41
     1
/*
jtulach@41
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@41
     3
 *
jtulach@41
     4
 * The contents of this file are subject to the terms of either the GNU
jtulach@41
     5
 * General Public License Version 2 only ("GPL") or the Common
jtulach@41
     6
 * Development and Distribution License("CDDL") (collectively, the
jtulach@41
     7
 * "License"). You may not use this file except in compliance with the
jtulach@41
     8
 * License. You can obtain a copy of the License at
jtulach@41
     9
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@41
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@41
    11
 * specific language governing permissions and limitations under the
jtulach@41
    12
 * License.  When distributing the software, include this License Header
jtulach@41
    13
 * Notice in each file and include the License file at
jtulach@41
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@41
    15
 * particular file as subject to the "Classpath" exception as provided
jtulach@41
    16
 * by Sun in the GPL Version 2 section of the License file that
jtulach@41
    17
 * accompanied this code. If applicable, add the following below the
jtulach@41
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@41
    19
 * your own identifying information:
jtulach@41
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@41
    21
 *
jtulach@41
    22
 * Contributor(s):
jtulach@41
    23
 *
jtulach@41
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@41
    25
 */
jtulach@41
    26
jtulach@41
    27
package cz.xelfi.quoridor.freemarkerdor;
jtulach@41
    28
jtulach@41
    29
import com.sun.jersey.api.client.Client;
jaroslav@56
    30
import com.sun.jersey.api.client.UniformInterfaceException;
jtulach@41
    31
import com.sun.jersey.api.client.WebResource;
jtulach@41
    32
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
jtulach@41
    33
import com.sun.jersey.api.core.PackagesResourceConfig;
jtulach@41
    34
import com.sun.jersey.api.core.ResourceConfig;
jtulach@41
    35
import com.sun.jersey.api.view.Viewable;
jtulach@41
    36
import com.sun.net.httpserver.HttpServer;
jtulach@41
    37
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jaroslav@46
    38
import java.io.File;
jaroslav@46
    39
import java.io.FileInputStream;
jtulach@41
    40
import java.io.IOException;
jtulach@41
    41
import java.net.URI;
jaroslav@55
    42
import java.util.HashMap;
jaroslav@59
    43
import java.util.Locale;
jaroslav@55
    44
import java.util.Map;
jaroslav@59
    45
import java.util.MissingResourceException;
jaroslav@46
    46
import java.util.Properties;
jaroslav@59
    47
import java.util.ResourceBundle;
jaroslav@50
    48
import java.util.concurrent.Callable;
jtulach@43
    49
import javax.ws.rs.DefaultValue;
jaroslav@47
    50
import javax.ws.rs.FormParam;
jtulach@41
    51
import javax.ws.rs.GET;
jaroslav@46
    52
import javax.ws.rs.POST;
jtulach@41
    53
import javax.ws.rs.Path;
jtulach@41
    54
import javax.ws.rs.PathParam;
jaroslav@46
    55
import javax.ws.rs.Produces;
jtulach@42
    56
import javax.ws.rs.QueryParam;
jtulach@42
    57
import javax.ws.rs.core.Context;
jtulach@42
    58
import javax.ws.rs.core.HttpHeaders;
jtulach@41
    59
import javax.ws.rs.core.MediaType;
jaroslav@46
    60
import javax.ws.rs.core.NewCookie;
jaroslav@46
    61
import javax.ws.rs.core.Response;
jaroslav@48
    62
import org.w3c.dom.Document;
jtulach@41
    63
jtulach@41
    64
/**
jtulach@41
    65
 *
jtulach@41
    66
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@41
    67
 */
jtulach@41
    68
@Path("/")
jtulach@41
    69
public final class UI {
jtulach@41
    70
    private static WebResource base;
jtulach@42
    71
jtulach@42
    72
    @Context
jtulach@42
    73
    private HttpHeaders headers;
jaroslav@46
    74
    private String user;
jtulach@42
    75
jtulach@41
    76
    public UI() {
jtulach@41
    77
    }
jtulach@41
    78
jaroslav@46
    79
    private Viewable checkLogin() {
jaroslav@46
    80
        if (headers.getCookies().containsKey("login")) {
jaroslav@46
    81
            user = headers.getCookies().get("login").getValue();
jaroslav@46
    82
            return null;
jaroslav@46
    83
        }
jaroslav@56
    84
        return viewable("login.fmt", null);
jaroslav@46
    85
    }
jaroslav@46
    86
jaroslav@46
    87
    @POST
jaroslav@46
    88
    @Path("login")
jaroslav@46
    89
    @Produces(MediaType.TEXT_HTML)
jaroslav@46
    90
    public Response login(
jaroslav@47
    91
        @FormParam("name") String name, @FormParam("password") String password
jaroslav@46
    92
    ) throws Exception {
jaroslav@46
    93
        File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd");
jaroslav@46
    94
        Properties p = new Properties();
jtulach@54
    95
        try {
jtulach@54
    96
            p.load(new FileInputStream(f));
jtulach@54
    97
        } catch (IOException ex) {
jtulach@54
    98
            ex.printStackTrace();
jtulach@54
    99
        }
jaroslav@46
   100
        if (name != null && password.equals(p.getProperty(name))) {
jaroslav@46
   101
            return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build();
jaroslav@46
   102
        } else {
jaroslav@56
   103
            Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
jaroslav@46
   104
            return Response.status(1).entity(v).build();
jaroslav@46
   105
        }
jaroslav@46
   106
    }
jaroslav@46
   107
jtulach@41
   108
    @GET
jaroslav@46
   109
    @Produces(MediaType.TEXT_HTML)
jtulach@54
   110
    public Viewable welcome() {
jaroslav@46
   111
        Viewable v = checkLogin();
jaroslav@46
   112
        if (v != null) {
jaroslav@46
   113
            return v;
jtulach@42
   114
        }
jaroslav@46
   115
        return welcomeImpl();
jtulach@41
   116
    }
jtulach@41
   117
jtulach@41
   118
    @GET
jtulach@43
   119
    @Path("games/{id}/")
jaroslav@46
   120
    @Produces(MediaType.TEXT_HTML)
jtulach@54
   121
    public Viewable board(@PathParam("id") String id) {
jaroslav@56
   122
        return board(id, null);
jaroslav@56
   123
    }
jaroslav@56
   124
    private Viewable board(@PathParam("id") String id, String msg) {
jaroslav@46
   125
        Viewable v = checkLogin();
jaroslav@46
   126
        if (v != null) {
jaroslav@46
   127
            return v;
jaroslav@46
   128
        }
jtulach@54
   129
        Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
jaroslav@56
   130
        return viewable("game.fmt", doc, "message", msg);
jtulach@42
   131
    }
jtulach@41
   132
jtulach@42
   133
    @GET
jtulach@43
   134
    @Path("games/{id}/move")
jaroslav@46
   135
    @Produces(MediaType.TEXT_HTML)
jtulach@43
   136
    public Viewable move(
jtulach@43
   137
        @PathParam("id") String id,
jtulach@43
   138
        @QueryParam("type") String type,
jtulach@43
   139
        @QueryParam("direction") String direction,
jtulach@43
   140
        @QueryParam("direction-next") @DefaultValue("") String directionNext,
jtulach@43
   141
        @QueryParam("column") @DefaultValue("") String column,
jtulach@43
   142
        @QueryParam("row") @DefaultValue("") String row
jtulach@54
   143
    ) {
jaroslav@46
   144
        Viewable v = checkLogin();
jaroslav@46
   145
        if (v != null) {
jaroslav@46
   146
            return v;
jaroslav@46
   147
        }
jaroslav@46
   148
        WebResource wr = base.path("games").path(id).queryParam("player", user);
jaroslav@56
   149
        try {
jaroslav@56
   150
            if (type.equals("fence")) {
jaroslav@56
   151
                wr.queryParam("move", direction.charAt(0) + column + row).put();
jaroslav@56
   152
                return board(id);
jaroslav@56
   153
            }
jaroslav@56
   154
            if (type.equals("move")) {
jaroslav@56
   155
                wr.queryParam("move", direction + directionNext).put();
jaroslav@56
   156
                return board(id);
jaroslav@56
   157
            }
jaroslav@56
   158
        } catch (UniformInterfaceException ex) {
jaroslav@56
   159
            return board(id, "WRONG_MOVE");
jtulach@43
   160
        }
jtulach@43
   161
        return board(id);
jtulach@43
   162
    }
jtulach@43
   163
jtulach@43
   164
    @GET
jtulach@42
   165
    @Path("games/create")
jaroslav@46
   166
    @Produces(MediaType.TEXT_HTML)
jtulach@42
   167
    public Viewable create(
jtulach@42
   168
        @QueryParam("white") String white,
jtulach@42
   169
        @QueryParam("black") String black
jtulach@54
   170
    ) {
jaroslav@46
   171
        Viewable v = checkLogin();
jaroslav@46
   172
        if (v != null) {
jaroslav@46
   173
            return v;
jaroslav@46
   174
        }
jaroslav@57
   175
jaroslav@57
   176
        if (user.equals(white) || user.equals(black)) {
jaroslav@57
   177
            Object obj =
jaroslav@57
   178
                base.path("games").queryParam("white", white).
jaroslav@57
   179
                queryParam("black", black).post(Document.class);
jaroslav@57
   180
            return welcomeImpl();
jaroslav@57
   181
        } else {
jaroslav@57
   182
            return welcomeImpl("message", "You (" + user + ") must be white or black!");
jaroslav@57
   183
        }
jtulach@41
   184
    }
jtulach@41
   185
jaroslav@57
   186
    private Viewable welcomeImpl(Object... args) {
jaroslav@56
   187
        final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
jaroslav@57
   188
        return viewable("index.fmt", got, args);
jtulach@41
   189
    }
jtulach@41
   190
jtulach@41
   191
    //
jtulach@41
   192
    // start the server
jtulach@41
   193
    //
jtulach@41
   194
jtulach@41
   195
    public static void main(String[] args) throws Exception {
jaroslav@50
   196
        Callable<Void> r = startServers();
jaroslav@50
   197
jaroslav@50
   198
        System.in.read();
jaroslav@50
   199
        r.call();
jaroslav@50
   200
        System.exit(0);
jaroslav@50
   201
    }
jaroslav@50
   202
jaroslav@50
   203
    static Callable<Void> startServers() throws Exception {
jaroslav@50
   204
        final HttpServer api = Quoridor.start(9998);
jtulach@41
   205
        Client client = new Client();
jtulach@41
   206
        base = client.resource(new URI("http://localhost:9998/api/"));
jtulach@41
   207
jaroslav@50
   208
        final HttpServer s = start(9997);
jtulach@41
   209
        System.out.println(
jtulach@41
   210
            "Quoridor started at port 9997\n" + "Hit enter to stop it..."
jtulach@41
   211
        );
jaroslav@50
   212
jaroslav@50
   213
        return new Callable<Void>() {
jaroslav@50
   214
            public Void call() throws Exception {
jaroslav@50
   215
                s.stop(0);
jaroslav@50
   216
                api.stop(0);
jaroslav@50
   217
                return null;
jaroslav@50
   218
            }
jaroslav@50
   219
        };
jtulach@41
   220
    }
jtulach@41
   221
jtulach@41
   222
    static HttpServer start(int port) throws IOException {
jtulach@41
   223
        final String baseUri = "http://localhost:" + port + "/";
jtulach@41
   224
        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.freemarkerdor");
jtulach@41
   225
        HttpServer server = HttpServerFactory.create(baseUri, rc);
jtulach@41
   226
        server.start();
jtulach@41
   227
        return server;
jtulach@41
   228
    }
jtulach@41
   229
jaroslav@56
   230
    private Viewable viewable(String page, Document doc, Object... more) {
jaroslav@59
   231
        String bundle = page.split("\\.")[0];
jaroslav@59
   232
jaroslav@59
   233
        ResourceBundle rb = null;
jaroslav@59
   234
        for (Locale l : headers.getAcceptableLanguages()) {
jaroslav@59
   235
            try {
jaroslav@59
   236
                rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI." + bundle, l);
jaroslav@59
   237
            } catch (MissingResourceException e) {
jaroslav@59
   238
                // OK
jaroslav@59
   239
            }
jaroslav@59
   240
        }
jaroslav@59
   241
        if (rb == null) {
jaroslav@59
   242
            rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI." + bundle, Locale.ENGLISH);
jaroslav@59
   243
        }
jaroslav@59
   244
jaroslav@56
   245
        Map<String,Object> map = new HashMap<String,Object>();
jaroslav@56
   246
        map.put("doc", doc);
jaroslav@56
   247
        map.put("user", user);
jaroslav@59
   248
        map.put("bundle", rb);
jaroslav@56
   249
        for (int i = 0; i < more.length; i += 2) {
jaroslav@56
   250
            map.put((String)more[i],more[i + 1]);
jaroslav@56
   251
        }
jaroslav@56
   252
        return new Viewable(page, map);
jaroslav@56
   253
    }
jaroslav@56
   254
jtulach@41
   255
}