1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Tue Aug 11 14:26:49 2009 +0200
1.3 @@ -0,0 +1,133 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.freemarkerdor;
1.31 +
1.32 +import com.sun.jersey.api.client.Client;
1.33 +import com.sun.jersey.api.client.WebResource;
1.34 +import com.sun.jersey.api.container.httpserver.HttpServerFactory;
1.35 +import com.sun.jersey.api.core.PackagesResourceConfig;
1.36 +import com.sun.jersey.api.core.ResourceConfig;
1.37 +import com.sun.jersey.api.view.Viewable;
1.38 +import com.sun.jersey.spi.resource.Singleton;
1.39 +import com.sun.net.httpserver.HttpServer;
1.40 +import cz.xelfi.quoridor.webidor.resources.Quoridor;
1.41 +import java.io.IOException;
1.42 +import java.net.URI;
1.43 +import java.util.ArrayList;
1.44 +import java.util.HashMap;
1.45 +import java.util.Iterator;
1.46 +import java.util.List;
1.47 +import java.util.Map;
1.48 +import javax.ws.rs.GET;
1.49 +import javax.ws.rs.Path;
1.50 +import javax.ws.rs.PathParam;
1.51 +import javax.ws.rs.core.MediaType;
1.52 +import org.codehaus.jettison.json.JSONArray;
1.53 +import org.codehaus.jettison.json.JSONException;
1.54 +import org.codehaus.jettison.json.JSONObject;
1.55 +
1.56 +/**
1.57 + *
1.58 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.59 + */
1.60 +@Path("/")
1.61 +@Singleton
1.62 +public final class UI {
1.63 + private static WebResource base;
1.64 + public UI() {
1.65 + }
1.66 +
1.67 + @GET
1.68 + @Path("/")
1.69 + public Viewable welcome() throws JSONException {
1.70 + Object obj = getJson(base.path("games"));
1.71 + return new Viewable("index.fmt", obj);
1.72 + }
1.73 +
1.74 + @GET
1.75 + @Path("games/{id}")
1.76 + public Viewable board(@PathParam("id") String id) throws JSONException {
1.77 + Object obj = convert(base.path("games").path(id).accept(MediaType.TEXT_PLAIN_TYPE).get(String.class));
1.78 +
1.79 + System.err.println("obj: " + obj);
1.80 + return new Viewable("game.fmt", obj);
1.81 + }
1.82 +
1.83 +
1.84 + private static Object getJson(WebResource res) throws JSONException {
1.85 + return convert(res.accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class));
1.86 + }
1.87 +
1.88 + private static Object convert(Object obj) throws JSONException {
1.89 + if (obj instanceof JSONArray) {
1.90 + JSONArray arr = (JSONArray)obj;
1.91 + final int length = arr.length();
1.92 + List<Object> res = new ArrayList<Object>(length);
1.93 + for (int i = 0; i < length; i++) {
1.94 + res.add(convert(arr.get(i)));
1.95 + }
1.96 + return res;
1.97 + } else if (obj instanceof JSONObject) {
1.98 + JSONObject json = (JSONObject)obj;
1.99 + Map<Object,Object> map = new HashMap<Object,Object>(json.length() * 2 / 3);
1.100 + for (Iterator it = json.keys(); it.hasNext();) {
1.101 + String key = (String)it.next();
1.102 + map.put(key, convert(json.get(key)));
1.103 + }
1.104 + return map;
1.105 + } else {
1.106 + return obj;
1.107 + }
1.108 + }
1.109 +
1.110 + //
1.111 + // start the server
1.112 + //
1.113 +
1.114 + public static void main(String[] args) throws Exception {
1.115 + HttpServer api = Quoridor.start(9998);
1.116 + Client client = new Client();
1.117 + base = client.resource(new URI("http://localhost:9998/api/"));
1.118 +
1.119 + HttpServer s = start(9997);
1.120 + System.out.println(
1.121 + "Quoridor started at port 9997\n" + "Hit enter to stop it..."
1.122 + );
1.123 + System.in.read();
1.124 + s.stop(0);
1.125 + System.exit(0);
1.126 + }
1.127 +
1.128 + static HttpServer start(int port) throws IOException {
1.129 + final String baseUri = "http://localhost:" + port + "/";
1.130 + ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.freemarkerdor");
1.131 + HttpServer server = HttpServerFactory.create(baseUri, rc);
1.132 + server.start();
1.133 + return server;
1.134 + }
1.135 +
1.136 +}