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