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;
|
jaroslav@56
|
30 |
import com.sun.jersey.api.client.UniformInterfaceException;
|
jtulach@41
|
31 |
import com.sun.jersey.api.client.WebResource;
|
jtulach@41
|
32 |
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
|
jtulach@41
|
33 |
import com.sun.jersey.api.core.PackagesResourceConfig;
|
jtulach@41
|
34 |
import com.sun.jersey.api.core.ResourceConfig;
|
jtulach@41
|
35 |
import com.sun.jersey.api.view.Viewable;
|
jtulach@41
|
36 |
import com.sun.net.httpserver.HttpServer;
|
jaroslav@99
|
37 |
import cz.xelfi.quoridor.Board;
|
jaroslav@99
|
38 |
import cz.xelfi.quoridor.IllegalPositionException;
|
jaroslav@88
|
39 |
import java.io.IOException;
|
jaroslav@88
|
40 |
import java.io.InputStream;
|
jaroslav@179
|
41 |
import java.io.StringWriter;
|
jtulach@41
|
42 |
import java.net.URI;
|
jaroslav@172
|
43 |
import java.util.Date;
|
jaroslav@55
|
44 |
import java.util.HashMap;
|
jaroslav@156
|
45 |
import java.util.List;
|
jaroslav@59
|
46 |
import java.util.Locale;
|
jaroslav@55
|
47 |
import java.util.Map;
|
jaroslav@59
|
48 |
import java.util.MissingResourceException;
|
jaroslav@88
|
49 |
import java.util.Properties;
|
jaroslav@59
|
50 |
import java.util.ResourceBundle;
|
jaroslav@50
|
51 |
import java.util.concurrent.Callable;
|
jtulach@43
|
52 |
import javax.ws.rs.DefaultValue;
|
jaroslav@47
|
53 |
import javax.ws.rs.FormParam;
|
jtulach@41
|
54 |
import javax.ws.rs.GET;
|
jaroslav@46
|
55 |
import javax.ws.rs.POST;
|
jtulach@41
|
56 |
import javax.ws.rs.Path;
|
jtulach@41
|
57 |
import javax.ws.rs.PathParam;
|
jaroslav@46
|
58 |
import javax.ws.rs.Produces;
|
jtulach@42
|
59 |
import javax.ws.rs.QueryParam;
|
jaroslav@129
|
60 |
import javax.ws.rs.core.CacheControl;
|
jtulach@42
|
61 |
import javax.ws.rs.core.Context;
|
jaroslav@101
|
62 |
import javax.ws.rs.core.Cookie;
|
jtulach@42
|
63 |
import javax.ws.rs.core.HttpHeaders;
|
jtulach@41
|
64 |
import javax.ws.rs.core.MediaType;
|
jaroslav@46
|
65 |
import javax.ws.rs.core.NewCookie;
|
jaroslav@46
|
66 |
import javax.ws.rs.core.Response;
|
jaroslav@101
|
67 |
import javax.ws.rs.core.Response.ResponseBuilder;
|
jaroslav@99
|
68 |
import org.openide.util.Exceptions;
|
jaroslav@48
|
69 |
import org.w3c.dom.Document;
|
jtulach@41
|
70 |
|
jtulach@41
|
71 |
/**
|
jtulach@41
|
72 |
*
|
jtulach@41
|
73 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@41
|
74 |
*/
|
jtulach@41
|
75 |
@Path("/")
|
jtulach@41
|
76 |
public final class UI {
|
jaroslav@88
|
77 |
private static final String version;
|
jaroslav@88
|
78 |
static {
|
jaroslav@88
|
79 |
Properties p = new Properties();
|
jaroslav@88
|
80 |
try {
|
jaroslav@88
|
81 |
InputStream is = FreemarkerProcessor.class.getResourceAsStream("/META-INF/maven/org.apidesign/freemarkerdor/pom.properties"); // NOI18N
|
jaroslav@88
|
82 |
if (is != null) {
|
jaroslav@88
|
83 |
p.load(is);
|
jaroslav@88
|
84 |
}
|
jaroslav@88
|
85 |
} catch (IOException ex) {
|
jaroslav@88
|
86 |
ex.printStackTrace();
|
jaroslav@88
|
87 |
}
|
jaroslav@88
|
88 |
version = p.getProperty("version", "unknown"); // NOI18N
|
jaroslav@88
|
89 |
}
|
jtulach@41
|
90 |
private static WebResource base;
|
jaroslav@178
|
91 |
private static WebResource stat;
|
jtulach@42
|
92 |
|
jtulach@42
|
93 |
@Context
|
jtulach@42
|
94 |
private HttpHeaders headers;
|
jaroslav@146
|
95 |
private UserInfo user;
|
jtulach@82
|
96 |
private String uuid;
|
jtulach@42
|
97 |
|
jtulach@41
|
98 |
public UI() {
|
jtulach@41
|
99 |
}
|
jtulach@41
|
100 |
|
jaroslav@46
|
101 |
private Viewable checkLogin() {
|
jaroslav@101
|
102 |
Cookie cookie = headers.getCookies().get("login");
|
jaroslav@101
|
103 |
if (cookie != null) {
|
jaroslav@101
|
104 |
String id = cookie.getValue();
|
jaroslav@146
|
105 |
UserInfo us;
|
jaroslav@85
|
106 |
try {
|
jaroslav@146
|
107 |
us = base.path("users").queryParam("loginID", id).
|
jaroslav@146
|
108 |
accept(MediaType.TEXT_XML).get(UserInfo.class);
|
jaroslav@146
|
109 |
} catch (Exception ex) {
|
jaroslav@85
|
110 |
ex.printStackTrace();
|
jaroslav@146
|
111 |
us = null;
|
jaroslav@85
|
112 |
}
|
jaroslav@146
|
113 |
if (us != null && us.getId().length() > 0) {
|
jtulach@83
|
114 |
user = us;
|
jtulach@83
|
115 |
uuid = id;
|
jtulach@83
|
116 |
return null;
|
jtulach@83
|
117 |
}
|
jaroslav@46
|
118 |
}
|
jaroslav@56
|
119 |
return viewable("login.fmt", null);
|
jaroslav@46
|
120 |
}
|
jaroslav@46
|
121 |
|
jaroslav@46
|
122 |
@POST
|
jaroslav@46
|
123 |
@Path("login")
|
jaroslav@46
|
124 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@46
|
125 |
public Response login(
|
jaroslav@47
|
126 |
@FormParam("name") String name, @FormParam("password") String password
|
jaroslav@46
|
127 |
) throws Exception {
|
jtulach@82
|
128 |
uuid = base.path("login").queryParam("name", name).queryParam("password", password).
|
jtulach@82
|
129 |
accept(MediaType.TEXT_PLAIN).put(String.class);
|
jtulach@82
|
130 |
if (uuid != null) {
|
jaroslav@146
|
131 |
user = new UserInfo(name);
|
jaroslav@101
|
132 |
NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false);
|
jaroslav@101
|
133 |
return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build();
|
jaroslav@46
|
134 |
} else {
|
jaroslav@56
|
135 |
Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
|
jaroslav@46
|
136 |
return Response.status(1).entity(v).build();
|
jaroslav@46
|
137 |
}
|
jaroslav@46
|
138 |
}
|
jaroslav@46
|
139 |
|
jtulach@41
|
140 |
@GET
|
jaroslav@46
|
141 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@129
|
142 |
public Response welcome(@QueryParam("maxItems") @DefaultValue("10") int maxItems) {
|
jaroslav@46
|
143 |
Viewable v = checkLogin();
|
jaroslav@129
|
144 |
ResponseBuilder resp = Response.ok();
|
jaroslav@129
|
145 |
if (v == null) {
|
jaroslav@129
|
146 |
v = welcomeImpl("maxItems", maxItems);
|
jtulach@42
|
147 |
}
|
jaroslav@129
|
148 |
CacheControl cc = new CacheControl();
|
jaroslav@129
|
149 |
cc.setNoCache(true);
|
jaroslav@129
|
150 |
resp.cacheControl(cc);
|
jaroslav@129
|
151 |
return resp.entity(v).build();
|
jtulach@41
|
152 |
}
|
jtulach@41
|
153 |
|
jaroslav@105
|
154 |
@GET
|
jaroslav@105
|
155 |
@Path("games/{id}.png")
|
jaroslav@105
|
156 |
@Produces("image/png")
|
jaroslav@129
|
157 |
public Response getBoardImage(
|
jaroslav@105
|
158 |
@PathParam("id") String id,
|
jaroslav@105
|
159 |
@QueryParam("fieldSize") @DefaultValue("50") int fieldSize,
|
jaroslav@105
|
160 |
@QueryParam("move") @DefaultValue("-1") int move
|
jaroslav@105
|
161 |
) throws IllegalPositionException {
|
jaroslav@105
|
162 |
WebResource wr = base.path("games").path(id);
|
jaroslav@105
|
163 |
if (move != -1) {
|
jaroslav@106
|
164 |
wr = wr.queryParam("move", "" + move);
|
jaroslav@105
|
165 |
}
|
jaroslav@105
|
166 |
String txt = wr.accept(MediaType.TEXT_PLAIN).get(String.class);
|
jaroslav@105
|
167 |
Board b = Board.valueOf(txt);
|
jaroslav@178
|
168 |
// Board b = new Board(txt);
|
jaroslav@129
|
169 |
ResponseBuilder resp = Response.ok();
|
jaroslav@129
|
170 |
CacheControl cc = new CacheControl();
|
jaroslav@129
|
171 |
cc.setNoCache(true);
|
jaroslav@129
|
172 |
resp.cacheControl(cc);
|
jaroslav@129
|
173 |
return resp.entity(BoardImage.draw(b, fieldSize)).build();
|
jaroslav@105
|
174 |
}
|
jaroslav@105
|
175 |
|
jaroslav@105
|
176 |
|
jaroslav@101
|
177 |
private Response board(String id) {
|
jaroslav@100
|
178 |
return board(id, "", null);
|
jaroslav@95
|
179 |
}
|
jtulach@41
|
180 |
@GET
|
jtulach@43
|
181 |
@Path("games/{id}/")
|
jaroslav@46
|
182 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@101
|
183 |
public Response board(
|
jaroslav@100
|
184 |
@PathParam("id") String id,
|
jaroslav@100
|
185 |
@QueryParam("format") @DefaultValue("") String format,
|
jaroslav@100
|
186 |
@QueryParam("move") @DefaultValue("-1") String move
|
jaroslav@100
|
187 |
) {
|
jaroslav@100
|
188 |
return board(id, null, format, move);
|
jaroslav@56
|
189 |
}
|
jaroslav@101
|
190 |
private Response board(@PathParam("id") String id, String msg, String format, String m) {
|
jaroslav@46
|
191 |
Viewable v = checkLogin();
|
jaroslav@46
|
192 |
if (v != null) {
|
jaroslav@101
|
193 |
return Response.ok().entity(v).build();
|
jaroslav@46
|
194 |
}
|
jaroslav@100
|
195 |
int move;
|
jaroslav@100
|
196 |
try {
|
jaroslav@100
|
197 |
move = Integer.parseInt(m);
|
jaroslav@100
|
198 |
} catch (NumberFormatException ex) {
|
jaroslav@100
|
199 |
move = -1;
|
jaroslav@100
|
200 |
}
|
jtulach@170
|
201 |
WebResource url = base.path("games").queryParam("loginID", uuid).path(id);
|
jaroslav@100
|
202 |
if (move >= 0) {
|
jaroslav@100
|
203 |
url = url.queryParam("move", "" + move);
|
jaroslav@100
|
204 |
}
|
jaroslav@101
|
205 |
ResponseBuilder resp = Response.ok();
|
jaroslav@129
|
206 |
CacheControl cc = new CacheControl();
|
jaroslav@129
|
207 |
cc.setNoCache(true);
|
jaroslav@129
|
208 |
resp.cacheControl(cc);
|
jaroslav@101
|
209 |
Cookie cFormat = headers.getCookies().get("format");
|
jaroslav@101
|
210 |
if (format.length() == 0) {
|
jaroslav@101
|
211 |
if (cFormat != null) {
|
jaroslav@101
|
212 |
format = cFormat.getValue();
|
jaroslav@156
|
213 |
} else {
|
jaroslav@156
|
214 |
if (isMobile(headers)) {
|
jaroslav@156
|
215 |
format = "small";
|
jaroslav@156
|
216 |
}
|
jaroslav@101
|
217 |
}
|
jaroslav@101
|
218 |
} else {
|
jaroslav@101
|
219 |
if (cFormat == null || !format.equals(cFormat.getValue())) {
|
jaroslav@101
|
220 |
resp.cookie(new NewCookie("format", format));
|
jaroslav@101
|
221 |
}
|
jaroslav@101
|
222 |
}
|
jaroslav@101
|
223 |
|
jaroslav@100
|
224 |
Document doc = url.accept(MediaType.TEXT_XML).get(Document.class);
|
jaroslav@99
|
225 |
Board b;
|
jaroslav@178
|
226 |
String t = doc.getElementsByTagName("board").item(0).getTextContent();
|
jaroslav@99
|
227 |
try {
|
jaroslav@99
|
228 |
b = Board.valueOf(doc.getElementsByTagName("board").item(0).getTextContent());
|
jaroslav@178
|
229 |
} catch (Exception ex) {
|
jaroslav@178
|
230 |
// b = new Board(t);
|
jaroslav@178
|
231 |
// } catch (IllegalStateException ex) {
|
jaroslav@99
|
232 |
Exceptions.printStackTrace(ex);
|
jaroslav@99
|
233 |
b = Board.empty();
|
jaroslav@99
|
234 |
}
|
jaroslav@178
|
235 |
String bCode = null;
|
jaroslav@178
|
236 |
try{
|
jaroslav@179
|
237 |
bCode = stat.path("openings").path(b.getCode()+".check").queryParam("loginID", user.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
|
jaroslav@178
|
238 |
}catch(Exception e){
|
jaroslav@178
|
239 |
bCode = null;
|
jaroslav@178
|
240 |
}
|
jaroslav@178
|
241 |
if(bCode == null || "".equals(bCode))
|
jaroslav@179
|
242 |
v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture", boardToPicture(b));
|
jaroslav@178
|
243 |
else
|
jaroslav@179
|
244 |
v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture", boardToPicture(b),"bCode", bCode);
|
jaroslav@101
|
245 |
return resp.entity(v).build();
|
jtulach@42
|
246 |
}
|
jtulach@41
|
247 |
|
jaroslav@179
|
248 |
private static String boardToPicture(Board b) {
|
jaroslav@179
|
249 |
StringWriter w = new StringWriter();
|
jaroslav@179
|
250 |
try {
|
jaroslav@179
|
251 |
b.write(w);
|
jaroslav@179
|
252 |
} catch (IOException ex) {
|
jaroslav@179
|
253 |
return ex.toString();
|
jaroslav@179
|
254 |
}
|
jaroslav@179
|
255 |
return w.toString();
|
jaroslav@179
|
256 |
}
|
jaroslav@179
|
257 |
|
jtulach@42
|
258 |
@GET
|
jtulach@43
|
259 |
@Path("games/{id}/move")
|
jaroslav@46
|
260 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@101
|
261 |
public Response move(
|
jtulach@43
|
262 |
@PathParam("id") String id,
|
jtulach@43
|
263 |
@QueryParam("type") String type,
|
jtulach@43
|
264 |
@QueryParam("direction") String direction,
|
jtulach@43
|
265 |
@QueryParam("direction-next") @DefaultValue("") String directionNext,
|
jtulach@43
|
266 |
@QueryParam("column") @DefaultValue("") String column,
|
jtulach@43
|
267 |
@QueryParam("row") @DefaultValue("") String row
|
jtulach@54
|
268 |
) {
|
jaroslav@46
|
269 |
Viewable v = checkLogin();
|
jaroslav@46
|
270 |
if (v != null) {
|
jaroslav@101
|
271 |
return Response.ok().entity(v).build();
|
jaroslav@46
|
272 |
}
|
jtulach@82
|
273 |
WebResource wr = base.path("games").path(id).
|
jtulach@82
|
274 |
queryParam("loginID", uuid).
|
jaroslav@146
|
275 |
queryParam("player", user.getId());
|
jaroslav@56
|
276 |
try {
|
jtulach@75
|
277 |
if (type.equals("resign")) {
|
jtulach@75
|
278 |
wr.queryParam("move", "RESIGN").put();
|
jtulach@75
|
279 |
return board(id);
|
jtulach@75
|
280 |
}
|
jaroslav@56
|
281 |
if (type.equals("fence")) {
|
jaroslav@56
|
282 |
wr.queryParam("move", direction.charAt(0) + column + row).put();
|
jaroslav@56
|
283 |
return board(id);
|
jaroslav@56
|
284 |
}
|
jaroslav@56
|
285 |
if (type.equals("move")) {
|
jaroslav@56
|
286 |
wr.queryParam("move", direction + directionNext).put();
|
jaroslav@56
|
287 |
return board(id);
|
jaroslav@56
|
288 |
}
|
jaroslav@56
|
289 |
} catch (UniformInterfaceException ex) {
|
jaroslav@100
|
290 |
return board(id, "WRONG_MOVE", "-1");
|
jtulach@43
|
291 |
}
|
jtulach@43
|
292 |
return board(id);
|
jtulach@43
|
293 |
}
|
jtulach@43
|
294 |
|
jtulach@43
|
295 |
@GET
|
jaroslav@115
|
296 |
@Path("games/{id}/comment")
|
jaroslav@115
|
297 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@115
|
298 |
public Response comment(
|
jaroslav@115
|
299 |
@PathParam("id") String id,
|
jaroslav@115
|
300 |
@QueryParam("comment") String comment
|
jaroslav@115
|
301 |
) {
|
jaroslav@115
|
302 |
Viewable v = checkLogin();
|
jaroslav@115
|
303 |
if (v != null) {
|
jaroslav@115
|
304 |
return Response.ok().entity(v).build();
|
jaroslav@115
|
305 |
}
|
jaroslav@115
|
306 |
WebResource wr = base.path("games").path(id).
|
jaroslav@115
|
307 |
queryParam("loginID", uuid).
|
jaroslav@146
|
308 |
queryParam("player", user.getId()).
|
jaroslav@115
|
309 |
queryParam("comment", comment);
|
jaroslav@115
|
310 |
wr.put();
|
jaroslav@115
|
311 |
|
jaroslav@115
|
312 |
return board(id);
|
jaroslav@115
|
313 |
}
|
jaroslav@115
|
314 |
|
jaroslav@115
|
315 |
@GET
|
jtulach@42
|
316 |
@Path("games/create")
|
jaroslav@46
|
317 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@73
|
318 |
public Response create(
|
jtulach@42
|
319 |
@QueryParam("white") String white,
|
jtulach@42
|
320 |
@QueryParam("black") String black
|
jtulach@54
|
321 |
) {
|
jaroslav@46
|
322 |
Viewable v = checkLogin();
|
jaroslav@46
|
323 |
if (v != null) {
|
jaroslav@73
|
324 |
return Response.status(Response.Status.FORBIDDEN).entity(v).build();
|
jaroslav@46
|
325 |
}
|
jaroslav@57
|
326 |
|
jaroslav@146
|
327 |
if (user.getId().equals(white) || user.getId().equals(black)) {
|
jaroslav@57
|
328 |
Object obj =
|
jtulach@82
|
329 |
base.path("games").
|
jtulach@82
|
330 |
queryParam("loginID", uuid).
|
jtulach@82
|
331 |
queryParam("white", white).
|
jaroslav@73
|
332 |
queryParam("black", black).accept(MediaType.TEXT_XML).post(Document.class);
|
jaroslav@73
|
333 |
return Response.ok(welcomeImpl()).build();
|
jaroslav@57
|
334 |
} else {
|
jaroslav@73
|
335 |
return Response.status(Response.Status.NOT_FOUND).
|
jaroslav@146
|
336 |
entity(welcomeImpl("message", "You (" + user.getId() + ") must be white or black!")).build();
|
jaroslav@57
|
337 |
}
|
jtulach@41
|
338 |
}
|
jtulach@41
|
339 |
|
jaroslav@57
|
340 |
private Viewable welcomeImpl(Object... args) {
|
jtulach@168
|
341 |
final Document got = base.path("games").queryParam("loginID", uuid).accept(MediaType.TEXT_XML).get(Document.class);
|
jaroslav@57
|
342 |
return viewable("index.fmt", got, args);
|
jtulach@41
|
343 |
}
|
jtulach@41
|
344 |
|
jaroslav@146
|
345 |
|
jaroslav@146
|
346 |
@GET
|
jaroslav@146
|
347 |
@Path("options")
|
jaroslav@146
|
348 |
public Response changeOptions(
|
jaroslav@146
|
349 |
@QueryParam("email") String email,
|
jaroslav@146
|
350 |
@QueryParam("language") String language
|
jaroslav@146
|
351 |
) {
|
jaroslav@146
|
352 |
Viewable v = checkLogin();
|
jaroslav@146
|
353 |
if (v != null) {
|
jaroslav@146
|
354 |
return Response.status(Response.Status.FORBIDDEN).entity(v).build();
|
jaroslav@146
|
355 |
}
|
jaroslav@146
|
356 |
|
jaroslav@146
|
357 |
if (email != null) {
|
jaroslav@146
|
358 |
UserInfo ui = base.path("users/" + user.getId()).
|
jaroslav@146
|
359 |
queryParam("loginID", uuid).
|
jaroslav@146
|
360 |
queryParam("name", "email").
|
jaroslav@146
|
361 |
queryParam("value", email).accept(MediaType.TEXT_XML).post(UserInfo.class);
|
jaroslav@146
|
362 |
}
|
jaroslav@146
|
363 |
|
jaroslav@146
|
364 |
if (language != null) {
|
jaroslav@146
|
365 |
UserInfo ui = base.path("users/" + user.getId()).
|
jaroslav@146
|
366 |
queryParam("loginID", uuid).
|
jaroslav@146
|
367 |
queryParam("name", "language").
|
jaroslav@146
|
368 |
queryParam("value", language).
|
jaroslav@146
|
369 |
accept(MediaType.TEXT_XML).post(UserInfo.class);
|
jaroslav@146
|
370 |
}
|
jaroslav@146
|
371 |
|
jaroslav@146
|
372 |
return welcome(10);
|
jaroslav@146
|
373 |
}
|
jaroslav@178
|
374 |
|
jaroslav@178
|
375 |
@GET
|
jaroslav@178
|
376 |
@Path("elo")
|
jaroslav@178
|
377 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@178
|
378 |
public Response getEloList(){
|
jaroslav@178
|
379 |
Viewable v = checkLogin();
|
jaroslav@178
|
380 |
if (v != null) {
|
jaroslav@178
|
381 |
return Response.status(Response.Status.FORBIDDEN).entity(v).build();
|
jaroslav@178
|
382 |
}
|
jaroslav@178
|
383 |
final Document got = stat.path("elo").path("list").accept(MediaType.TEXT_XML).get(Document.class);
|
jaroslav@178
|
384 |
return Response.ok(viewable("elo.fmt", got)).build();
|
jaroslav@178
|
385 |
}
|
jaroslav@146
|
386 |
|
jaroslav@178
|
387 |
@GET
|
jaroslav@178
|
388 |
@Path("openings")
|
jaroslav@178
|
389 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@178
|
390 |
public Response getOpeningRoot(){
|
jaroslav@178
|
391 |
return getOpeningNode("ROOT");
|
jaroslav@178
|
392 |
}
|
jaroslav@178
|
393 |
|
jaroslav@178
|
394 |
@GET
|
jaroslav@178
|
395 |
@Path("openings/{code}")
|
jaroslav@178
|
396 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@178
|
397 |
public Response getOpeningNode(@PathParam("code") String code){
|
jaroslav@178
|
398 |
Viewable v = checkLogin();
|
jaroslav@178
|
399 |
if (v != null) {
|
jaroslav@178
|
400 |
return Response.status(Response.Status.FORBIDDEN).entity(v).build();
|
jaroslav@178
|
401 |
}
|
jaroslav@178
|
402 |
final Document got = stat.path("openings").path(code).queryParam("loginID", user.getId()).accept(MediaType.TEXT_XML).get(Document.class);
|
jaroslav@178
|
403 |
Board b;
|
jaroslav@178
|
404 |
try {
|
jaroslav@178
|
405 |
b = Board.valueOf(got.getElementsByTagName("nodeCode").item(0).getTextContent());
|
jaroslav@178
|
406 |
} catch (Exception ex) {
|
jaroslav@178
|
407 |
Exceptions.printStackTrace(ex);
|
jaroslav@178
|
408 |
b = Board.empty();
|
jaroslav@178
|
409 |
}
|
jaroslav@178
|
410 |
return Response.ok(viewable("openings.fmt", got, "whitefences",b.getPlayers().get(0).getFences(),"blackfences",b.getPlayers().get(1).getFences())).build();
|
jaroslav@178
|
411 |
}
|
jaroslav@178
|
412 |
|
jaroslav@178
|
413 |
@GET
|
jaroslav@178
|
414 |
@Path("openings/{code}/{status}")
|
jaroslav@178
|
415 |
@Produces(MediaType.TEXT_HTML)
|
jaroslav@178
|
416 |
public Response getOpeningNodeGames(@PathParam("code") String code, @PathParam("status") String status){
|
jaroslav@178
|
417 |
Viewable v = checkLogin();
|
jaroslav@178
|
418 |
if (v != null) {
|
jaroslav@178
|
419 |
return Response.status(Response.Status.FORBIDDEN).entity(v).build();
|
jaroslav@178
|
420 |
}
|
jaroslav@178
|
421 |
final Document got = stat.path("openings").path(code).path(status).queryParam("loginID", user.getId()).accept(MediaType.TEXT_XML).get(Document.class);
|
jaroslav@178
|
422 |
return Response.ok(viewable("opening_games.fmt", got,"code",code,"color",status)).build();
|
jaroslav@178
|
423 |
}
|
jaroslav@178
|
424 |
|
jaroslav@178
|
425 |
@GET
|
jaroslav@178
|
426 |
@Path("openings/{code}.png")
|
jaroslav@178
|
427 |
@Produces("image/png")
|
jaroslav@178
|
428 |
public Response getOpeningBoardImage(
|
jaroslav@178
|
429 |
@PathParam("code") String code,
|
jaroslav@178
|
430 |
@QueryParam("fieldSize") @DefaultValue("40") int fieldSize
|
jaroslav@178
|
431 |
) throws IllegalPositionException {
|
jaroslav@179
|
432 |
Board b = Board.valueOf(code);
|
jaroslav@178
|
433 |
ResponseBuilder resp = Response.ok();
|
jaroslav@178
|
434 |
CacheControl cc = new CacheControl();
|
jaroslav@178
|
435 |
cc.setNoCache(true);
|
jaroslav@178
|
436 |
resp.cacheControl(cc);
|
jaroslav@178
|
437 |
return resp.entity(BoardImage.draw(b, fieldSize)).build();
|
jaroslav@178
|
438 |
}
|
jaroslav@178
|
439 |
|
jtulach@41
|
440 |
//
|
jtulach@41
|
441 |
// start the server
|
jtulach@41
|
442 |
//
|
jtulach@41
|
443 |
|
jtulach@41
|
444 |
public static void main(String[] args) throws Exception {
|
jaroslav@121
|
445 |
int port = 9333;
|
jaroslav@68
|
446 |
if (args.length > 1) {
|
jaroslav@68
|
447 |
port = Integer.parseInt(args[0]);
|
jaroslav@68
|
448 |
}
|
jaroslav@132
|
449 |
String remoteAPI = args.length >= 2 ? args[1] : null;
|
jaroslav@50
|
450 |
|
jaroslav@146
|
451 |
Locale.setDefault(Locale.ROOT);
|
jaroslav@68
|
452 |
|
jaroslav@121
|
453 |
Callable<Void> r = startServers(port, remoteAPI);
|
jaroslav@68
|
454 |
|
jaroslav@122
|
455 |
if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
|
jaroslav@68
|
456 |
System.out.println("Hit enter to stop it...");
|
jaroslav@68
|
457 |
System.in.read();
|
jaroslav@68
|
458 |
} else {
|
jaroslav@68
|
459 |
synchronized (UI.class) {
|
jaroslav@68
|
460 |
UI.class.wait();
|
jaroslav@68
|
461 |
}
|
jaroslav@68
|
462 |
}
|
jaroslav@50
|
463 |
r.call();
|
jaroslav@50
|
464 |
System.exit(0);
|
jaroslav@50
|
465 |
}
|
jaroslav@50
|
466 |
|
jaroslav@121
|
467 |
static Callable<Void> startServers(int port, String remoteAPI) throws Exception {
|
jaroslav@121
|
468 |
Client client = new Client();
|
jaroslav@178
|
469 |
Client client1 = new Client();
|
jaroslav@72
|
470 |
|
jaroslav@121
|
471 |
final HttpServer apiServer;
|
jaroslav@121
|
472 |
if (remoteAPI == null) {
|
jaroslav@126
|
473 |
throw new IllegalArgumentException("Provide URL to API server"); // NOI18N
|
jaroslav@121
|
474 |
} else {
|
jaroslav@121
|
475 |
base = client.resource(new URI(remoteAPI));
|
jaroslav@178
|
476 |
stat = client1.resource(new URI("http://localhost:9444"));
|
jaroslav@121
|
477 |
apiServer = null;
|
jaroslav@73
|
478 |
}
|
jaroslav@72
|
479 |
|
jaroslav@72
|
480 |
ResourceConfig rc = new PackagesResourceConfig(
|
jaroslav@72
|
481 |
"cz.xelfi.quoridor.freemarkerdor"
|
jaroslav@72
|
482 |
);
|
jaroslav@72
|
483 |
|
jaroslav@72
|
484 |
final String baseUri = "http://localhost:" + port + "/";
|
jaroslav@72
|
485 |
final HttpServer server = HttpServerFactory.create(baseUri, rc);
|
jaroslav@72
|
486 |
server.start();
|
jaroslav@68
|
487 |
System.out.println("Quoridor started at port " + port);
|
jaroslav@50
|
488 |
|
jaroslav@50
|
489 |
return new Callable<Void>() {
|
jaroslav@50
|
490 |
public Void call() throws Exception {
|
jaroslav@121
|
491 |
if (apiServer != null) {
|
jaroslav@121
|
492 |
apiServer.stop(0);
|
jaroslav@121
|
493 |
}
|
jaroslav@72
|
494 |
server.stop(0);
|
jaroslav@50
|
495 |
return null;
|
jaroslav@50
|
496 |
}
|
jaroslav@50
|
497 |
};
|
jtulach@41
|
498 |
}
|
jtulach@41
|
499 |
|
jaroslav@56
|
500 |
private Viewable viewable(String page, Document doc, Object... more) {
|
jaroslav@59
|
501 |
ResourceBundle rb = null;
|
jaroslav@146
|
502 |
String lng = user == null ? null : user.getProperty("language"); // NOI18N
|
jaroslav@172
|
503 |
Locale locale = null;
|
jaroslav@146
|
504 |
if (lng != null) {
|
jaroslav@146
|
505 |
try {
|
jaroslav@146
|
506 |
Locale l = new Locale(lng);
|
jaroslav@146
|
507 |
rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
|
jaroslav@172
|
508 |
locale = l;
|
jaroslav@146
|
509 |
} catch (MissingResourceException e) {
|
jaroslav@146
|
510 |
// OK
|
jaroslav@146
|
511 |
}
|
jaroslav@146
|
512 |
}
|
jaroslav@146
|
513 |
if (rb == null) {
|
jaroslav@146
|
514 |
for (Locale l : headers.getAcceptableLanguages()) {
|
jaroslav@146
|
515 |
try {
|
jaroslav@146
|
516 |
rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", l);
|
jaroslav@172
|
517 |
locale = l;
|
jaroslav@172
|
518 |
break;
|
jaroslav@146
|
519 |
} catch (MissingResourceException e) {
|
jaroslav@146
|
520 |
// OK
|
jaroslav@146
|
521 |
}
|
jaroslav@59
|
522 |
}
|
jaroslav@59
|
523 |
}
|
jaroslav@59
|
524 |
if (rb == null) {
|
jtulach@80
|
525 |
rb = ResourceBundle.getBundle("cz.xelfi.quoridor.freemarkerdor.UI.Bundle", Locale.ENGLISH);
|
jaroslav@172
|
526 |
locale = Locale.ENGLISH;
|
jaroslav@59
|
527 |
}
|
jaroslav@59
|
528 |
|
jaroslav@56
|
529 |
Map<String,Object> map = new HashMap<String,Object>();
|
jaroslav@172
|
530 |
class ConvertToDate extends HashMap<Object,Object> {
|
jaroslav@172
|
531 |
@Override
|
jaroslav@172
|
532 |
public Object get(Object o) {
|
jaroslav@172
|
533 |
long time = Long.parseLong(o.toString());
|
jaroslav@172
|
534 |
return new Date(time);
|
jaroslav@172
|
535 |
}
|
jaroslav@172
|
536 |
}
|
jaroslav@172
|
537 |
|
jaroslav@172
|
538 |
map.put("locale", locale.toString());
|
jaroslav@56
|
539 |
map.put("doc", doc);
|
jaroslav@146
|
540 |
if (user != null) {
|
jaroslav@146
|
541 |
map.put("user", user.getId());
|
jaroslav@146
|
542 |
map.put("email", user.getProperty("email"));
|
jaroslav@146
|
543 |
}
|
jaroslav@59
|
544 |
map.put("bundle", rb);
|
jaroslav@172
|
545 |
map.put("toDate", new ConvertToDate());
|
jtulach@78
|
546 |
map.put("now", System.currentTimeMillis());
|
jaroslav@88
|
547 |
map.put("version", version);
|
jaroslav@56
|
548 |
for (int i = 0; i < more.length; i += 2) {
|
jaroslav@56
|
549 |
map.put((String)more[i],more[i + 1]);
|
jaroslav@56
|
550 |
}
|
jaroslav@56
|
551 |
return new Viewable(page, map);
|
jaroslav@56
|
552 |
}
|
jaroslav@56
|
553 |
|
jaroslav@156
|
554 |
|
jaroslav@156
|
555 |
private static boolean isMobile(HttpHeaders headers) {
|
jaroslav@156
|
556 |
final String[] keywords = {
|
jaroslav@156
|
557 |
"Profile/MIDP",
|
jaroslav@156
|
558 |
};
|
jaroslav@156
|
559 |
List<String> agent = headers.getRequestHeader(HttpHeaders.USER_AGENT);
|
jaroslav@156
|
560 |
if (agent != null) {
|
jaroslav@156
|
561 |
for (String a : agent) {
|
jaroslav@156
|
562 |
for (String k : keywords) {
|
jaroslav@156
|
563 |
if (a.contains(k)) {
|
jaroslav@156
|
564 |
return true;
|
jaroslav@156
|
565 |
}
|
jaroslav@156
|
566 |
}
|
jaroslav@156
|
567 |
}
|
jaroslav@156
|
568 |
}
|
jaroslav@156
|
569 |
return false;
|
jaroslav@156
|
570 |
}
|
jaroslav@156
|
571 |
|
jtulach@41
|
572 |
}
|