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