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.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;
60 * @author Jaroslav Tulach <jtulach@netbeans.org>
63 public final class UI {
64 private static WebResource base;
67 private Request request;
69 private HttpHeaders headers;
75 public Viewable welcome() throws JSONException {
76 if (headers.getCookies().containsKey("login")) {
79 Object obj = getJson(base.path("games"));
80 return new Viewable("index.fmt", obj);
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);
92 public Viewable create(
93 @QueryParam("white") String white,
94 @QueryParam("black") String black
95 ) throws JSONException {
97 base.path("games").queryParam("white", white).
98 queryParam("black", black).post(JSONObject.class)
100 Map<?,?> map = (Map<?,?>)obj;
101 String id = (String)map.get("id");
106 private static Object getJson(WebResource res) throws JSONException {
107 return convert(res.accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class));
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)));
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)));
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/"));
141 HttpServer s = start(9997);
143 "Quoridor started at port 9997\n" + "Hit enter to stop it..."
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);