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