freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 29 Nov 2009 09:12:55 +0100
changeset 156 e3d6e326eac1
parent 152 07e3bcb65c1d
child 168 a705dedc07d8
permissions -rw-r--r--
Automatically detecting connections from a phone and switching to small format
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;
jaroslav@99
    37
import cz.xelfi.quoridor.Board;
jaroslav@99
    38
import cz.xelfi.quoridor.IllegalPositionException;
jaroslav@88
    39
import java.io.IOException;
jaroslav@88
    40
import java.io.InputStream;
jtulach@41
    41
import java.net.URI;
jaroslav@55
    42
import java.util.HashMap;
jaroslav@156
    43
import java.util.List;
jaroslav@59
    44
import java.util.Locale;
jaroslav@55
    45
import java.util.Map;
jaroslav@59
    46
import java.util.MissingResourceException;
jaroslav@88
    47
import java.util.Properties;
jaroslav@59
    48
import java.util.ResourceBundle;
jaroslav@50
    49
import java.util.concurrent.Callable;
jtulach@43
    50
import javax.ws.rs.DefaultValue;
jaroslav@47
    51
import javax.ws.rs.FormParam;
jtulach@41
    52
import javax.ws.rs.GET;
jaroslav@46
    53
import javax.ws.rs.POST;
jtulach@41
    54
import javax.ws.rs.Path;
jtulach@41
    55
import javax.ws.rs.PathParam;
jaroslav@46
    56
import javax.ws.rs.Produces;
jtulach@42
    57
import javax.ws.rs.QueryParam;
jaroslav@129
    58
import javax.ws.rs.core.CacheControl;
jtulach@42
    59
import javax.ws.rs.core.Context;
jaroslav@101
    60
import javax.ws.rs.core.Cookie;
jtulach@42
    61
import javax.ws.rs.core.HttpHeaders;
jtulach@41
    62
import javax.ws.rs.core.MediaType;
jaroslav@46
    63
import javax.ws.rs.core.NewCookie;
jaroslav@46
    64
import javax.ws.rs.core.Response;
jaroslav@101
    65
import javax.ws.rs.core.Response.ResponseBuilder;
jaroslav@99
    66
import org.openide.util.Exceptions;
jaroslav@48
    67
import org.w3c.dom.Document;
jtulach@41
    68
jtulach@41
    69
/**
jtulach@41
    70
 *
jtulach@41
    71
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@41
    72
 */
jtulach@41
    73
@Path("/")
jtulach@41
    74
public final class UI {
jaroslav@88
    75
    private static final String version;
jaroslav@88
    76
    static {
jaroslav@88
    77
        Properties p = new Properties();
jaroslav@88
    78
        try {
jaroslav@88
    79
            InputStream is = FreemarkerProcessor.class.getResourceAsStream("/META-INF/maven/org.apidesign/freemarkerdor/pom.properties"); // NOI18N
jaroslav@88
    80
            if (is != null) {
jaroslav@88
    81
                p.load(is);
jaroslav@88
    82
            }
jaroslav@88
    83
        } catch (IOException ex) {
jaroslav@88
    84
            ex.printStackTrace();
jaroslav@88
    85
        }
jaroslav@88
    86
        version = p.getProperty("version", "unknown"); // NOI18N
jaroslav@88
    87
    }
jtulach@41
    88
    private static WebResource base;
jtulach@42
    89
jtulach@42
    90
    @Context
jtulach@42
    91
    private HttpHeaders headers;
jaroslav@146
    92
    private UserInfo user;
jtulach@82
    93
    private String uuid;
jtulach@42
    94
jtulach@41
    95
    public UI() {
jtulach@41
    96
    }
jtulach@41
    97
jaroslav@46
    98
    private Viewable checkLogin() {
jaroslav@101
    99
        Cookie cookie = headers.getCookies().get("login");
jaroslav@101
   100
        if (cookie != null) {
jaroslav@101
   101
            String id = cookie.getValue();
jaroslav@146
   102
            UserInfo us;
jaroslav@85
   103
            try {
jaroslav@146
   104
                us = base.path("users").queryParam("loginID", id).
jaroslav@146
   105
                    accept(MediaType.TEXT_XML).get(UserInfo.class);
jaroslav@146
   106
            } catch (Exception ex) {
jaroslav@85
   107
                ex.printStackTrace();
jaroslav@146
   108
                us = null;
jaroslav@85
   109
            }
jaroslav@146
   110
            if (us != null && us.getId().length() > 0) {
jtulach@83
   111
                user = us;
jtulach@83
   112
                uuid = id;
jtulach@83
   113
                return null;
jtulach@83
   114
            }
jaroslav@46
   115
        }
jaroslav@56
   116
        return viewable("login.fmt", null);
jaroslav@46
   117
    }
jaroslav@46
   118
jaroslav@46
   119
    @POST
jaroslav@46
   120
    @Path("login")
jaroslav@46
   121
    @Produces(MediaType.TEXT_HTML)
jaroslav@46
   122
    public Response login(
jaroslav@47
   123
        @FormParam("name") String name, @FormParam("password") String password
jaroslav@46
   124
    ) throws Exception {
jtulach@82
   125
        uuid = base.path("login").queryParam("name", name).queryParam("password", password).
jtulach@82
   126
            accept(MediaType.TEXT_PLAIN).put(String.class);
jtulach@82
   127
        if (uuid != null) {
jaroslav@146
   128
            user = new UserInfo(name);
jaroslav@101
   129
            NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false);
jaroslav@101
   130
            return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build();
jaroslav@46
   131
        } else {
jaroslav@56
   132
            Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
jaroslav@46
   133
            return Response.status(1).entity(v).build();
jaroslav@46
   134
        }
jaroslav@46
   135
    }
jaroslav@46
   136
jtulach@41
   137
    @GET
jaroslav@46
   138
    @Produces(MediaType.TEXT_HTML)
jaroslav@129
   139
    public Response welcome(@QueryParam("maxItems") @DefaultValue("10") int maxItems) {
jaroslav@46
   140
        Viewable v = checkLogin();
jaroslav@129
   141
        ResponseBuilder resp = Response.ok();
jaroslav@129
   142
        if (v == null) {
jaroslav@129
   143
            v = welcomeImpl("maxItems", maxItems);
jtulach@42
   144
        }
jaroslav@129
   145
        CacheControl cc = new CacheControl();
jaroslav@129
   146
        cc.setNoCache(true);
jaroslav@129
   147
        resp.cacheControl(cc);
jaroslav@129
   148
        return resp.entity(v).build();
jtulach@41
   149
    }
jtulach@41
   150
jaroslav@105
   151
    @GET
jaroslav@105
   152
    @Path("games/{id}.png")
jaroslav@105
   153
    @Produces("image/png")
jaroslav@129
   154
    public Response getBoardImage(
jaroslav@105
   155
        @PathParam("id") String id,
jaroslav@105
   156
        @QueryParam("fieldSize") @DefaultValue("50") int fieldSize,
jaroslav@105
   157
        @QueryParam("move") @DefaultValue("-1") int move
jaroslav@105
   158
    ) throws IllegalPositionException {
jaroslav@105
   159
        WebResource wr = base.path("games").path(id);
jaroslav@105
   160
        if (move != -1) {
jaroslav@106
   161
            wr = wr.queryParam("move", "" + move);
jaroslav@105
   162
        }
jaroslav@105
   163
        String txt = wr.accept(MediaType.TEXT_PLAIN).get(String.class);
jaroslav@105
   164
        Board b = Board.valueOf(txt);
jaroslav@129
   165
        ResponseBuilder resp = Response.ok();
jaroslav@129
   166
        CacheControl cc = new CacheControl();
jaroslav@129
   167
        cc.setNoCache(true);
jaroslav@129
   168
        resp.cacheControl(cc);
jaroslav@129
   169
        return resp.entity(BoardImage.draw(b, fieldSize)).build();
jaroslav@105
   170
    }
jaroslav@105
   171
jaroslav@105
   172
jaroslav@101
   173
    private Response board(String id) {
jaroslav@100
   174
        return board(id, "", null);
jaroslav@95
   175
    }
jtulach@41
   176
    @GET
jtulach@43
   177
    @Path("games/{id}/")
jaroslav@46
   178
    @Produces(MediaType.TEXT_HTML)
jaroslav@101
   179
    public Response board(
jaroslav@100
   180
        @PathParam("id") String id,
jaroslav@100
   181
        @QueryParam("format") @DefaultValue("") String format,
jaroslav@100
   182
        @QueryParam("move") @DefaultValue("-1") String move
jaroslav@100
   183
    ) {
jaroslav@100
   184
        return board(id, null, format, move);
jaroslav@56
   185
    }
jaroslav@101
   186
    private Response board(@PathParam("id") String id, String msg, String format, String m) {
jaroslav@46
   187
        Viewable v = checkLogin();
jaroslav@46
   188
        if (v != null) {
jaroslav@101
   189
            return Response.ok().entity(v).build();
jaroslav@46
   190
        }
jaroslav@100
   191
        int move;
jaroslav@100
   192
        try {
jaroslav@100
   193
            move = Integer.parseInt(m);
jaroslav@100
   194
        } catch (NumberFormatException ex) {
jaroslav@100
   195
            move = -1;
jaroslav@100
   196
        }
jaroslav@100
   197
        WebResource url = base.path("games").path(id);
jaroslav@100
   198
        if (move >= 0) {
jaroslav@100
   199
            url = url.queryParam("move", "" + move);
jaroslav@100
   200
        }
jaroslav@101
   201
        ResponseBuilder resp = Response.ok();
jaroslav@129
   202
        CacheControl cc = new CacheControl();
jaroslav@129
   203
        cc.setNoCache(true);
jaroslav@129
   204
        resp.cacheControl(cc);
jaroslav@101
   205
        Cookie cFormat = headers.getCookies().get("format");
jaroslav@101
   206
        if (format.length() == 0) {
jaroslav@101
   207
            if (cFormat != null) {
jaroslav@101
   208
                format = cFormat.getValue();
jaroslav@156
   209
            } else {
jaroslav@156
   210
                if (isMobile(headers)) {
jaroslav@156
   211
                    format = "small";
jaroslav@156
   212
                }
jaroslav@101
   213
            }
jaroslav@101
   214
        } else {
jaroslav@101
   215
            if (cFormat == null || !format.equals(cFormat.getValue())) {
jaroslav@101
   216
                resp.cookie(new NewCookie("format", format));
jaroslav@101
   217
            }
jaroslav@101
   218
        }
jaroslav@101
   219
jaroslav@100
   220
        Document doc = url.accept(MediaType.TEXT_XML).get(Document.class);
jaroslav@99
   221
        Board b;
jaroslav@99
   222
        try {
jaroslav@99
   223
            b = Board.valueOf(doc.getElementsByTagName("board").item(0).getTextContent());
jaroslav@99
   224
        } catch (IllegalPositionException ex) {
jaroslav@99
   225
            Exceptions.printStackTrace(ex);
jaroslav@99
   226
            b = Board.empty();
jaroslav@99
   227
        }
jaroslav@101
   228
        v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b);
jaroslav@101
   229
        return resp.entity(v).build();
jtulach@42
   230
    }
jtulach@41
   231
jtulach@42
   232
    @GET
jtulach@43
   233
    @Path("games/{id}/move")
jaroslav@46
   234
    @Produces(MediaType.TEXT_HTML)
jaroslav@101
   235
    public Response move(
jtulach@43
   236
        @PathParam("id") String id,
jtulach@43
   237
        @QueryParam("type") String type,
jtulach@43
   238
        @QueryParam("direction") String direction,
jtulach@43
   239
        @QueryParam("direction-next") @DefaultValue("") String directionNext,
jtulach@43
   240
        @QueryParam("column") @DefaultValue("") String column,
jtulach@43
   241
        @QueryParam("row") @DefaultValue("") String row
jtulach@54
   242
    ) {
jaroslav@46
   243
        Viewable v = checkLogin();
jaroslav@46
   244
        if (v != null) {
jaroslav@101
   245
            return Response.ok().entity(v).build();
jaroslav@46
   246
        }
jtulach@82
   247
        WebResource wr = base.path("games").path(id).
jtulach@82
   248
            queryParam("loginID", uuid).
jaroslav@146
   249
            queryParam("player", user.getId());
jaroslav@56
   250
        try {
jtulach@75
   251
            if (type.equals("resign")) {
jtulach@75
   252
                wr.queryParam("move", "RESIGN").put();
jtulach@75
   253
                return board(id);
jtulach@75
   254
            }
jaroslav@56
   255
            if (type.equals("fence")) {
jaroslav@56
   256
                wr.queryParam("move", direction.charAt(0) + column + row).put();
jaroslav@56
   257
                return board(id);
jaroslav@56
   258
            }
jaroslav@56
   259
            if (type.equals("move")) {
jaroslav@56
   260
                wr.queryParam("move", direction + directionNext).put();
jaroslav@56
   261
                return board(id);
jaroslav@56
   262
            }
jaroslav@56
   263
        } catch (UniformInterfaceException ex) {
jaroslav@100
   264
            return board(id, "WRONG_MOVE", "-1");
jtulach@43
   265
        }
jtulach@43
   266
        return board(id);
jtulach@43
   267
    }
jtulach@43
   268
jtulach@43
   269
    @GET
jaroslav@115
   270
    @Path("games/{id}/comment")
jaroslav@115
   271
    @Produces(MediaType.TEXT_HTML)
jaroslav@115
   272
    public Response comment(
jaroslav@115
   273
        @PathParam("id") String id,
jaroslav@115
   274
        @QueryParam("comment") String comment
jaroslav@115
   275
    ) {
jaroslav@115
   276
        Viewable v = checkLogin();
jaroslav@115
   277
        if (v != null) {
jaroslav@115
   278
            return Response.ok().entity(v).build();
jaroslav@115
   279
        }
jaroslav@115
   280
        WebResource wr = base.path("games").path(id).
jaroslav@115
   281
            queryParam("loginID", uuid).
jaroslav@146
   282
            queryParam("player", user.getId()).
jaroslav@115
   283
            queryParam("comment", comment);
jaroslav@115
   284
        wr.put();
jaroslav@115
   285
        
jaroslav@115
   286
        return board(id);
jaroslav@115
   287
    }
jaroslav@115
   288
jaroslav@115
   289
    @GET
jtulach@42
   290
    @Path("games/create")
jaroslav@46
   291
    @Produces(MediaType.TEXT_HTML)
jaroslav@73
   292
    public Response create(
jtulach@42
   293
        @QueryParam("white") String white,
jtulach@42
   294
        @QueryParam("black") String black
jtulach@54
   295
    ) {
jaroslav@46
   296
        Viewable v = checkLogin();
jaroslav@46
   297
        if (v != null) {
jaroslav@73
   298
            return Response.status(Response.Status.FORBIDDEN).entity(v).build();
jaroslav@46
   299
        }
jaroslav@57
   300
jaroslav@146
   301
        if (user.getId().equals(white) || user.getId().equals(black)) {
jaroslav@57
   302
            Object obj =
jtulach@82
   303
                base.path("games").
jtulach@82
   304
                queryParam("loginID", uuid).
jtulach@82
   305
                queryParam("white", white).
jaroslav@73
   306
                queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class);
jaroslav@73
   307
            return Response.ok(welcomeImpl()).build();
jaroslav@57
   308
        } else {
jaroslav@73
   309
            return Response.status(Response.Status.NOT_FOUND).
jaroslav@146
   310
                entity(welcomeImpl("message", "You (" + user.getId() + ") must be white or black!")).build();
jaroslav@57
   311
        }
jtulach@41
   312
    }
jtulach@41
   313
jaroslav@57
   314
    private Viewable welcomeImpl(Object... args) {
jaroslav@56
   315
        final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
jaroslav@57
   316
        return viewable("index.fmt", got, args);
jtulach@41
   317
    }
jtulach@41
   318
jaroslav@146
   319
jaroslav@146
   320
    @GET
jaroslav@146
   321
    @Path("options")
jaroslav@146
   322
    public Response changeOptions(
jaroslav@146
   323
        @QueryParam("email") String email,
jaroslav@146
   324
        @QueryParam("language") String language
jaroslav@146
   325
    ) {
jaroslav@146
   326
        Viewable v = checkLogin();
jaroslav@146
   327
        if (v != null) {
jaroslav@146
   328
            return Response.status(Response.Status.FORBIDDEN).entity(v).build();
jaroslav@146
   329
        }
jaroslav@146
   330
jaroslav@146
   331
        if (email != null) {
jaroslav@146
   332
            UserInfo ui = base.path("users/" + user.getId()).
jaroslav@146
   333
                queryParam("loginID", uuid).
jaroslav@146
   334
                queryParam("name", "email").
jaroslav@146
   335
                queryParam("value", email).accept(MediaType.TEXT_XML).post(UserInfo.class);
jaroslav@146
   336
        }
jaroslav@146
   337
jaroslav@146
   338
        if (language != null) {
jaroslav@146
   339
            UserInfo ui = base.path("users/" + user.getId()).
jaroslav@146
   340
                queryParam("loginID", uuid).
jaroslav@146
   341
                queryParam("name", "language").
jaroslav@146
   342
                queryParam("value", language).
jaroslav@146
   343
                accept(MediaType.TEXT_XML).post(UserInfo.class);
jaroslav@146
   344
        }
jaroslav@146
   345
jaroslav@146
   346
        return welcome(10);
jaroslav@146
   347
    }
jaroslav@146
   348
    
jtulach@41
   349
    //
jtulach@41
   350
    // start the server
jtulach@41
   351
    //
jtulach@41
   352
jtulach@41
   353
    public static void main(String[] args) throws Exception {
jaroslav@121
   354
        int port = 9333;
jaroslav@68
   355
        if (args.length > 1) {
jaroslav@68
   356
            port = Integer.parseInt(args[0]);
jaroslav@68
   357
        }
jaroslav@132
   358
        String remoteAPI = args.length >= 2 ? args[1] : null;
jaroslav@50
   359
jaroslav@146
   360
        Locale.setDefault(Locale.ROOT);
jaroslav@68
   361
jaroslav@121
   362
        Callable<Void> r = startServers(port, remoteAPI);
jaroslav@68
   363
jaroslav@122
   364
        if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
jaroslav@68
   365
            System.out.println("Hit enter to stop it...");
jaroslav@68
   366
            System.in.read();
jaroslav@68
   367
        } else {
jaroslav@68
   368
            synchronized (UI.class) {
jaroslav@68
   369
                UI.class.wait();
jaroslav@68
   370
            }
jaroslav@68
   371
        }
jaroslav@50
   372
        r.call();
jaroslav@50
   373
        System.exit(0);
jaroslav@50
   374
    }
jaroslav@50
   375
jaroslav@121
   376
    static Callable<Void> startServers(int port, String remoteAPI) throws Exception {
jaroslav@121
   377
        Client client = new Client();
jaroslav@72
   378
jaroslav@121
   379
        final HttpServer apiServer;
jaroslav@121
   380
        if (remoteAPI == null) {
jaroslav@126
   381
            throw new IllegalArgumentException("Provide URL to API server"); // NOI18N
jaroslav@121
   382
        } else {
jaroslav@121
   383
            base = client.resource(new URI(remoteAPI));
jaroslav@121
   384
            apiServer = null;
jaroslav@73
   385
        }
jaroslav@72
   386
jaroslav@72
   387
        ResourceConfig rc = new PackagesResourceConfig(
jaroslav@72
   388
            "cz.xelfi.quoridor.freemarkerdor"
jaroslav@72
   389
        );
jaroslav@72
   390
jaroslav@72
   391
        final String baseUri = "http://localhost:" + port + "/";
jaroslav@72
   392
        final HttpServer server = HttpServerFactory.create(baseUri, rc);
jaroslav@72
   393
        server.start();
jaroslav@68
   394
        System.out.println("Quoridor started at port " + port);
jaroslav@50
   395
jaroslav@50
   396
        return new Callable<Void>() {
jaroslav@50
   397
            public Void call() throws Exception {
jaroslav@121
   398
                if (apiServer != null) {
jaroslav@121
   399
                    apiServer.stop(0);
jaroslav@121
   400
                }
jaroslav@72
   401
                server.stop(0);
jaroslav@50
   402
                return null;
jaroslav@50
   403
            }
jaroslav@50
   404
        };
jtulach@41
   405
    }
jtulach@41
   406
jaroslav@56
   407
    private Viewable viewable(String page, Document doc, Object... more) {
jaroslav@59
   408
        ResourceBundle rb = null;
jaroslav@146
   409
        String lng = user == null ? null : user.getProperty("language"); // NOI18N
jaroslav@146
   410
        if (lng != null) {
jaroslav@146
   411
                try {
jaroslav@146
   412
                    Locale l = new Locale(lng);
jaroslav@146
   413
                    rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
jaroslav@146
   414
                } catch (MissingResourceException e) {
jaroslav@146
   415
                    // OK
jaroslav@146
   416
                }
jaroslav@146
   417
        }
jaroslav@146
   418
        if (rb == null) {
jaroslav@146
   419
            for (Locale l : headers.getAcceptableLanguages()) {
jaroslav@146
   420
                try {
jaroslav@146
   421
                    rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
jaroslav@146
   422
                } catch (MissingResourceException e) {
jaroslav@146
   423
                    // OK
jaroslav@146
   424
                }
jaroslav@59
   425
            }
jaroslav@59
   426
        }
jaroslav@59
   427
        if (rb == null) {
jtulach@80
   428
            rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", Locale.ENGLISH);
jaroslav@59
   429
        }
jaroslav@59
   430
jaroslav@56
   431
        Map<String,Object> map = new HashMap<String,Object>();
jaroslav@56
   432
        map.put("doc", doc);
jaroslav@146
   433
        if (user != null) {
jaroslav@146
   434
            map.put("user", user.getId());
jaroslav@146
   435
            map.put("email", user.getProperty("email"));
jaroslav@146
   436
        }
jaroslav@59
   437
        map.put("bundle", rb);
jtulach@78
   438
        map.put("now", System.currentTimeMillis());
jaroslav@88
   439
        map.put("version", version);
jaroslav@56
   440
        for (int i = 0; i < more.length; i += 2) {
jaroslav@56
   441
            map.put((String)more[i],more[i + 1]);
jaroslav@56
   442
        }
jaroslav@56
   443
        return new Viewable(page, map);
jaroslav@56
   444
    }
jaroslav@56
   445
jaroslav@156
   446
jaroslav@156
   447
    private static boolean isMobile(HttpHeaders headers) {
jaroslav@156
   448
        final String[] keywords = {
jaroslav@156
   449
            "Profile/MIDP",
jaroslav@156
   450
        };
jaroslav@156
   451
        List<String> agent = headers.getRequestHeader(HttpHeaders.USER_AGENT);
jaroslav@156
   452
        if (agent != null) {
jaroslav@156
   453
            for (String a : agent) {
jaroslav@156
   454
                for (String k : keywords) {
jaroslav@156
   455
                    if (a.contains(k)) {
jaroslav@156
   456
                        return true;
jaroslav@156
   457
                    }
jaroslav@156
   458
                }
jaroslav@156
   459
            }
jaroslav@156
   460
        }
jaroslav@156
   461
        return false;
jaroslav@156
   462
    }
jaroslav@156
   463
jtulach@41
   464
}