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