freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 11 Aug 2009 15:18:43 +0200
changeset 44 85a0fad6b3c2
parent 43 58b8db5faf2c
child 46 71e4cf307c93
permissions -rw-r--r--
Simple UI for submitting moves (without implementation)
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.jersey.spi.resource.Singleton;
jtulach@41
    36
import com.sun.net.httpserver.HttpServer;
jtulach@41
    37
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jtulach@41
    38
import java.io.IOException;
jtulach@41
    39
import java.net.URI;
jtulach@41
    40
import java.util.ArrayList;
jtulach@41
    41
import java.util.HashMap;
jtulach@41
    42
import java.util.Iterator;
jtulach@41
    43
import java.util.List;
jtulach@41
    44
import java.util.Map;
jtulach@43
    45
import javax.ws.rs.DefaultValue;
jtulach@41
    46
import javax.ws.rs.GET;
jtulach@41
    47
import javax.ws.rs.Path;
jtulach@41
    48
import javax.ws.rs.PathParam;
jtulach@42
    49
import javax.ws.rs.QueryParam;
jtulach@42
    50
import javax.ws.rs.core.CacheControl;
jtulach@42
    51
import javax.ws.rs.core.Context;
jtulach@42
    52
import javax.ws.rs.core.HttpHeaders;
jtulach@41
    53
import javax.ws.rs.core.MediaType;
jtulach@42
    54
import javax.ws.rs.core.Request;
jtulach@41
    55
import org.codehaus.jettison.json.JSONArray;
jtulach@41
    56
import org.codehaus.jettison.json.JSONException;
jtulach@41
    57
import org.codehaus.jettison.json.JSONObject;
jtulach@41
    58
jtulach@41
    59
/**
jtulach@41
    60
 *
jtulach@41
    61
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@41
    62
 */
jtulach@41
    63
@Path("/")
jtulach@41
    64
public final class UI {
jtulach@41
    65
    private static WebResource base;
jtulach@42
    66
jtulach@42
    67
    @Context
jtulach@42
    68
    private Request request;
jtulach@42
    69
    @Context
jtulach@42
    70
    private HttpHeaders headers;
jtulach@42
    71
jtulach@41
    72
    public UI() {
jtulach@41
    73
    }
jtulach@41
    74
jtulach@41
    75
    @GET
jtulach@41
    76
    public Viewable welcome() throws JSONException {
jtulach@42
    77
        if (headers.getCookies().containsKey("login")) {
jtulach@42
    78
        }
jtulach@42
    79
jtulach@41
    80
        Object obj = getJson(base.path("games"));
jtulach@41
    81
        return new Viewable("index.fmt", obj);
jtulach@41
    82
    }
jtulach@41
    83
jtulach@41
    84
    @GET
jtulach@43
    85
    @Path("games/{id}/")
jtulach@41
    86
    public Viewable board(@PathParam("id") String id) throws JSONException {
jtulach@41
    87
        Object obj = convert(base.path("games").path(id).accept(MediaType.TEXT_PLAIN_TYPE).get(String.class));
jtulach@42
    88
        return new Viewable("game.fmt", obj);
jtulach@42
    89
    }
jtulach@41
    90
jtulach@42
    91
    @GET
jtulach@43
    92
    @Path("games/{id}/move")
jtulach@43
    93
    public Viewable move(
jtulach@43
    94
        @PathParam("id") String id,
jtulach@43
    95
        @QueryParam("type") String type,
jtulach@43
    96
        @QueryParam("direction") String direction,
jtulach@43
    97
        @QueryParam("direction-next") @DefaultValue("") String directionNext,
jtulach@43
    98
        @QueryParam("column") @DefaultValue("") String column,
jtulach@43
    99
        @QueryParam("row") @DefaultValue("") String row
jtulach@43
   100
    ) throws JSONException {
jtulach@43
   101
        if (type.equals("fence")) {
jtulach@43
   102
            base.path("games").path(id).queryParam("move", direction.charAt(0) + column + row).post();
jtulach@43
   103
            return board(id);
jtulach@43
   104
        }
jtulach@43
   105
        if (type.equals("move")) {
jtulach@44
   106
            base.path("games").path(id).queryParam("move", direction + directionNext).post();
jtulach@43
   107
            return board(id);
jtulach@43
   108
        }
jtulach@43
   109
        return board(id);
jtulach@43
   110
    }
jtulach@43
   111
jtulach@43
   112
    @GET
jtulach@42
   113
    @Path("games/create")
jtulach@42
   114
    public Viewable create(
jtulach@42
   115
        @QueryParam("white") String white,
jtulach@42
   116
        @QueryParam("black") String black
jtulach@42
   117
    ) throws JSONException {
jtulach@42
   118
        Object obj = convert(
jtulach@42
   119
            base.path("games").queryParam("white", white).
jtulach@42
   120
            queryParam("black", black).post(JSONObject.class)
jtulach@42
   121
        );
jtulach@42
   122
        Map<?,?> map = (Map<?,?>)obj;
jtulach@42
   123
        String id = (String)map.get("id");
jtulach@42
   124
        return board(id);
jtulach@41
   125
    }
jtulach@41
   126
jtulach@41
   127
jtulach@41
   128
    private static Object getJson(WebResource res) throws JSONException {
jtulach@41
   129
        return convert(res.accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class));
jtulach@41
   130
    }
jtulach@41
   131
jtulach@41
   132
    private static Object convert(Object obj) throws JSONException {
jtulach@41
   133
        if (obj instanceof JSONArray) {
jtulach@41
   134
            JSONArray arr = (JSONArray)obj;
jtulach@41
   135
            final int length = arr.length();
jtulach@41
   136
            List<Object> res = new ArrayList<Object>(length);
jtulach@41
   137
            for (int i = 0; i < length; i++) {
jtulach@41
   138
                res.add(convert(arr.get(i)));
jtulach@41
   139
            }
jtulach@41
   140
            return res;
jtulach@41
   141
        } else if (obj instanceof JSONObject) {
jtulach@41
   142
            JSONObject json = (JSONObject)obj;
jtulach@41
   143
            Map<Object,Object> map = new HashMap<Object,Object>(json.length() * 2 / 3);
jtulach@41
   144
            for (Iterator it = json.keys(); it.hasNext();) {
jtulach@41
   145
                String key = (String)it.next();
jtulach@41
   146
                map.put(key, convert(json.get(key)));
jtulach@41
   147
            }
jtulach@41
   148
            return map;
jtulach@41
   149
        } else {
jtulach@41
   150
            return obj;
jtulach@41
   151
        }
jtulach@41
   152
    }
jtulach@41
   153
jtulach@41
   154
    //
jtulach@41
   155
    // start the server
jtulach@41
   156
    //
jtulach@41
   157
jtulach@41
   158
    public static void main(String[] args) throws Exception {
jtulach@41
   159
        HttpServer api = Quoridor.start(9998);
jtulach@41
   160
        Client client = new Client();
jtulach@41
   161
        base = client.resource(new URI("http://localhost:9998/api/"));
jtulach@41
   162
jtulach@41
   163
        HttpServer s = start(9997);
jtulach@41
   164
        System.out.println(
jtulach@41
   165
            "Quoridor started at port 9997\n" + "Hit enter to stop it..."
jtulach@41
   166
        );
jtulach@41
   167
        System.in.read();
jtulach@41
   168
        s.stop(0);
jtulach@41
   169
        System.exit(0);
jtulach@41
   170
    }
jtulach@41
   171
jtulach@41
   172
    static HttpServer start(int port) throws IOException {
jtulach@41
   173
        final String baseUri = "http://localhost:" + port + "/";
jtulach@41
   174
        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.freemarkerdor");
jtulach@41
   175
        HttpServer server = HttpServerFactory.create(baseUri, rc);
jtulach@41
   176
        server.start();
jtulach@41
   177
        return server;
jtulach@41
   178
    }
jtulach@41
   179
jtulach@41
   180
}