freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Sep 2009 20:26:48 +0200
changeset 56 7c3794f5baa1
parent 55 830e0ba29c04
child 57 fa12b02023a0
permissions -rw-r--r--
Reload of the game page, warning messages, listing just game in progress
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@55
    43
import java.util.Map;
jaroslav@46
    44
import java.util.Properties;
jaroslav@50
    45
import java.util.concurrent.Callable;
jtulach@43
    46
import javax.ws.rs.DefaultValue;
jaroslav@47
    47
import javax.ws.rs.FormParam;
jtulach@41
    48
import javax.ws.rs.GET;
jaroslav@46
    49
import javax.ws.rs.POST;
jtulach@41
    50
import javax.ws.rs.Path;
jtulach@41
    51
import javax.ws.rs.PathParam;
jaroslav@46
    52
import javax.ws.rs.Produces;
jtulach@42
    53
import javax.ws.rs.QueryParam;
jtulach@42
    54
import javax.ws.rs.core.Context;
jtulach@42
    55
import javax.ws.rs.core.HttpHeaders;
jtulach@41
    56
import javax.ws.rs.core.MediaType;
jaroslav@46
    57
import javax.ws.rs.core.NewCookie;
jaroslav@46
    58
import javax.ws.rs.core.Response;
jaroslav@48
    59
import org.w3c.dom.Document;
jtulach@41
    60
jtulach@41
    61
/**
jtulach@41
    62
 *
jtulach@41
    63
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@41
    64
 */
jtulach@41
    65
@Path("/")
jtulach@41
    66
public final class UI {
jtulach@41
    67
    private static WebResource base;
jtulach@42
    68
jtulach@42
    69
    @Context
jtulach@42
    70
    private HttpHeaders headers;
jaroslav@46
    71
    private String user;
jtulach@42
    72
jtulach@41
    73
    public UI() {
jtulach@41
    74
    }
jtulach@41
    75
jaroslav@46
    76
    private Viewable checkLogin() {
jaroslav@46
    77
        if (headers.getCookies().containsKey("login")) {
jaroslav@46
    78
            user = headers.getCookies().get("login").getValue();
jaroslav@46
    79
            return null;
jaroslav@46
    80
        }
jaroslav@56
    81
        return viewable("login.fmt", null);
jaroslav@46
    82
    }
jaroslav@46
    83
jaroslav@46
    84
    @POST
jaroslav@46
    85
    @Path("login")
jaroslav@46
    86
    @Produces(MediaType.TEXT_HTML)
jaroslav@46
    87
    public Response login(
jaroslav@47
    88
        @FormParam("name") String name, @FormParam("password") String password
jaroslav@46
    89
    ) throws Exception {
jaroslav@46
    90
        File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd");
jaroslav@46
    91
        Properties p = new Properties();
jtulach@54
    92
        try {
jtulach@54
    93
            p.load(new FileInputStream(f));
jtulach@54
    94
        } catch (IOException ex) {
jtulach@54
    95
            ex.printStackTrace();
jtulach@54
    96
        }
jaroslav@46
    97
        if (name != null && password.equals(p.getProperty(name))) {
jaroslav@46
    98
            return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build();
jaroslav@46
    99
        } else {
jaroslav@56
   100
            Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
jaroslav@46
   101
            return Response.status(1).entity(v).build();
jaroslav@46
   102
        }
jaroslav@46
   103
    }
jaroslav@46
   104
jtulach@41
   105
    @GET
jaroslav@46
   106
    @Produces(MediaType.TEXT_HTML)
jtulach@54
   107
    public Viewable welcome() {
jaroslav@46
   108
        Viewable v = checkLogin();
jaroslav@46
   109
        if (v != null) {
jaroslav@46
   110
            return v;
jtulach@42
   111
        }
jaroslav@46
   112
        return welcomeImpl();
jtulach@41
   113
    }
jtulach@41
   114
jtulach@41
   115
    @GET
jtulach@43
   116
    @Path("games/{id}/")
jaroslav@46
   117
    @Produces(MediaType.TEXT_HTML)
jtulach@54
   118
    public Viewable board(@PathParam("id") String id) {
jaroslav@56
   119
        return board(id, null);
jaroslav@56
   120
    }
jaroslav@56
   121
    private Viewable board(@PathParam("id") String id, String msg) {
jaroslav@46
   122
        Viewable v = checkLogin();
jaroslav@46
   123
        if (v != null) {
jaroslav@46
   124
            return v;
jaroslav@46
   125
        }
jtulach@54
   126
        Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
jaroslav@56
   127
        return viewable("game.fmt", doc, "message", msg);
jtulach@42
   128
    }
jtulach@41
   129
jtulach@42
   130
    @GET
jtulach@43
   131
    @Path("games/{id}/move")
jaroslav@46
   132
    @Produces(MediaType.TEXT_HTML)
jtulach@43
   133
    public Viewable move(
jtulach@43
   134
        @PathParam("id") String id,
jtulach@43
   135
        @QueryParam("type") String type,
jtulach@43
   136
        @QueryParam("direction") String direction,
jtulach@43
   137
        @QueryParam("direction-next") @DefaultValue("") String directionNext,
jtulach@43
   138
        @QueryParam("column") @DefaultValue("") String column,
jtulach@43
   139
        @QueryParam("row") @DefaultValue("") String row
jtulach@54
   140
    ) {
jaroslav@46
   141
        Viewable v = checkLogin();
jaroslav@46
   142
        if (v != null) {
jaroslav@46
   143
            return v;
jaroslav@46
   144
        }
jaroslav@46
   145
        WebResource wr = base.path("games").path(id).queryParam("player", user);
jaroslav@56
   146
        try {
jaroslav@56
   147
            if (type.equals("fence")) {
jaroslav@56
   148
                wr.queryParam("move", direction.charAt(0) + column + row).put();
jaroslav@56
   149
                return board(id);
jaroslav@56
   150
            }
jaroslav@56
   151
            if (type.equals("move")) {
jaroslav@56
   152
                wr.queryParam("move", direction + directionNext).put();
jaroslav@56
   153
                return board(id);
jaroslav@56
   154
            }
jaroslav@56
   155
        } catch (UniformInterfaceException ex) {
jaroslav@56
   156
            return board(id, "WRONG_MOVE");
jtulach@43
   157
        }
jtulach@43
   158
        return board(id);
jtulach@43
   159
    }
jtulach@43
   160
jtulach@43
   161
    @GET
jtulach@42
   162
    @Path("games/create")
jaroslav@46
   163
    @Produces(MediaType.TEXT_HTML)
jtulach@42
   164
    public Viewable create(
jtulach@42
   165
        @QueryParam("white") String white,
jtulach@42
   166
        @QueryParam("black") String black
jtulach@54
   167
    ) {
jaroslav@46
   168
        Viewable v = checkLogin();
jaroslav@46
   169
        if (v != null) {
jaroslav@46
   170
            return v;
jaroslav@46
   171
        }
jtulach@54
   172
        Object obj =
jtulach@42
   173
            base.path("games").queryParam("white", white).
jtulach@54
   174
            queryParam("black", black).post(Document.class);
jtulach@54
   175
        return welcome();
jtulach@41
   176
    }
jtulach@41
   177
jtulach@54
   178
    private Viewable welcomeImpl() {
jaroslav@56
   179
        final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
jaroslav@56
   180
        return viewable("index.fmt", got);
jtulach@41
   181
    }
jtulach@41
   182
jtulach@41
   183
    //
jtulach@41
   184
    // start the server
jtulach@41
   185
    //
jtulach@41
   186
jtulach@41
   187
    public static void main(String[] args) throws Exception {
jaroslav@50
   188
        Callable<Void> r = startServers();
jaroslav@50
   189
jaroslav@50
   190
        System.in.read();
jaroslav@50
   191
        r.call();
jaroslav@50
   192
        System.exit(0);
jaroslav@50
   193
    }
jaroslav@50
   194
jaroslav@50
   195
    static Callable<Void> startServers() throws Exception {
jaroslav@50
   196
        final HttpServer api = Quoridor.start(9998);
jtulach@41
   197
        Client client = new Client();
jtulach@41
   198
        base = client.resource(new URI("http://localhost:9998/api/"));
jtulach@41
   199
jaroslav@50
   200
        final HttpServer s = start(9997);
jtulach@41
   201
        System.out.println(
jtulach@41
   202
            "Quoridor started at port 9997\n" + "Hit enter to stop it..."
jtulach@41
   203
        );
jaroslav@50
   204
jaroslav@50
   205
        return new Callable<Void>() {
jaroslav@50
   206
            public Void call() throws Exception {
jaroslav@50
   207
                s.stop(0);
jaroslav@50
   208
                api.stop(0);
jaroslav@50
   209
                return null;
jaroslav@50
   210
            }
jaroslav@50
   211
        };
jtulach@41
   212
    }
jtulach@41
   213
jtulach@41
   214
    static HttpServer start(int port) throws IOException {
jtulach@41
   215
        final String baseUri = "http://localhost:" + port + "/";
jtulach@41
   216
        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.freemarkerdor");
jtulach@41
   217
        HttpServer server = HttpServerFactory.create(baseUri, rc);
jtulach@41
   218
        server.start();
jtulach@41
   219
        return server;
jtulach@41
   220
    }
jtulach@41
   221
jaroslav@56
   222
    private Viewable viewable(String page, Document doc, Object... more) {
jaroslav@56
   223
        Map<String,Object> map = new HashMap<String,Object>();
jaroslav@56
   224
        map.put("doc", doc);
jaroslav@56
   225
        map.put("user", user);
jaroslav@56
   226
        for (int i = 0; i < more.length; i += 2) {
jaroslav@56
   227
            map.put((String)more[i],more[i + 1]);
jaroslav@56
   228
        }
jaroslav@56
   229
        return new Viewable(page, map);
jaroslav@56
   230
    }
jaroslav@56
   231
jtulach@41
   232
}