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