2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
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]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.freemarkerdor;
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;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
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;
55 * @author Jaroslav Tulach <jtulach@netbeans.org>
59 public final class UI {
60 private static WebResource base;
66 public Viewable welcome() throws JSONException {
67 Object obj = getJson(base.path("games"));
68 return new Viewable("index.fmt", obj);
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));
76 System.err.println("obj: " + obj);
77 return new Viewable("game.fmt", obj);
81 private static Object getJson(WebResource res) throws JSONException {
82 return convert(res.accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class));
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)));
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)));
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/"));
116 HttpServer s = start(9997);
118 "Quoridor started at port 9997\n" + "Hit enter to stop it..."
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);