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