freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 11 Aug 2009 14:26:49 +0200
changeset 41 c94f68ddef59
child 42 c5726abc1218
permissions -rw-r--r--
Simple, freemarker based textual UI
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.freemarkerdor;
    28 
    29 import com.sun.jersey.api.client.Client;
    30 import com.sun.jersey.api.client.WebResource;
    31 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
    32 import com.sun.jersey.api.core.PackagesResourceConfig;
    33 import com.sun.jersey.api.core.ResourceConfig;
    34 import com.sun.jersey.api.view.Viewable;
    35 import com.sun.jersey.spi.resource.Singleton;
    36 import com.sun.net.httpserver.HttpServer;
    37 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    38 import java.io.IOException;
    39 import java.net.URI;
    40 import java.util.ArrayList;
    41 import java.util.HashMap;
    42 import java.util.Iterator;
    43 import java.util.List;
    44 import java.util.Map;
    45 import javax.ws.rs.GET;
    46 import javax.ws.rs.Path;
    47 import javax.ws.rs.PathParam;
    48 import javax.ws.rs.core.MediaType;
    49 import org.codehaus.jettison.json.JSONArray;
    50 import org.codehaus.jettison.json.JSONException;
    51 import org.codehaus.jettison.json.JSONObject;
    52 
    53 /**
    54  *
    55  * @author Jaroslav Tulach <jtulach@netbeans.org>
    56  */
    57 @Path("/")
    58 @Singleton
    59 public final class UI {
    60     private static WebResource base;
    61     public UI() {
    62     }
    63 
    64     @GET
    65     @Path("/")
    66     public Viewable welcome() throws JSONException {
    67         Object obj = getJson(base.path("games"));
    68         return new Viewable("index.fmt", obj);
    69     }
    70 
    71     @GET
    72     @Path("games/{id}")
    73     public Viewable board(@PathParam("id") String id) throws JSONException {
    74         Object obj = convert(base.path("games").path(id).accept(MediaType.TEXT_PLAIN_TYPE).get(String.class));
    75 
    76         System.err.println("obj: " + obj);
    77         return new Viewable("game.fmt", obj);
    78     }
    79 
    80 
    81     private static Object getJson(WebResource res) throws JSONException {
    82         return convert(res.accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class));
    83     }
    84 
    85     private static Object convert(Object obj) throws JSONException {
    86         if (obj instanceof JSONArray) {
    87             JSONArray arr = (JSONArray)obj;
    88             final int length = arr.length();
    89             List<Object> res = new ArrayList<Object>(length);
    90             for (int i = 0; i < length; i++) {
    91                 res.add(convert(arr.get(i)));
    92             }
    93             return res;
    94         } else if (obj instanceof JSONObject) {
    95             JSONObject json = (JSONObject)obj;
    96             Map<Object,Object> map = new HashMap<Object,Object>(json.length() * 2 / 3);
    97             for (Iterator it = json.keys(); it.hasNext();) {
    98                 String key = (String)it.next();
    99                 map.put(key, convert(json.get(key)));
   100             }
   101             return map;
   102         } else {
   103             return obj;
   104         }
   105     }
   106 
   107     //
   108     // start the server
   109     //
   110 
   111     public static void main(String[] args) throws Exception {
   112         HttpServer api = Quoridor.start(9998);
   113         Client client = new Client();
   114         base = client.resource(new URI("http://localhost:9998/api/"));
   115 
   116         HttpServer s = start(9997);
   117         System.out.println(
   118             "Quoridor started at port 9997\n" + "Hit enter to stop it..."
   119         );
   120         System.in.read();
   121         s.stop(0);
   122         System.exit(0);
   123     }
   124 
   125     static HttpServer start(int port) throws IOException {
   126         final String baseUri = "http://localhost:" + port + "/";
   127         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.freemarkerdor");
   128         HttpServer server = HttpServerFactory.create(baseUri, rc);
   129         server.start();
   130         return server;
   131     }
   132 
   133 }