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