# HG changeset patch # User Jaroslav Tulach # Date 1253380672 -7200 # Node ID c7244cdd143e0d610f0cec797df129b3321e013b # Parent 8b899ed24f9f7e9299469218cb687f08c8b26ccd Remember the selection of format thru out the session diff -r 8b899ed24f9f -r c7244cdd143e freemarkerdor/pom.xml --- a/freemarkerdor/pom.xml Sat Sep 19 14:38:29 2009 +0200 +++ b/freemarkerdor/pom.xml Sat Sep 19 19:17:52 2009 +0200 @@ -10,7 +10,7 @@ org.apidesign freemarkerdor freemarkerdor - 1.15 + 1.16 http://maven.apache.org diff -r 8b899ed24f9f -r c7244cdd143e freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sat Sep 19 14:38:29 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sat Sep 19 19:17:52 2009 +0200 @@ -56,10 +56,12 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; +import javax.ws.rs.core.Cookie; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; import org.openide.util.Exceptions; import org.w3c.dom.Document; @@ -93,8 +95,9 @@ } private Viewable checkLogin() { - if (headers.getCookies().containsKey("login")) { - String id = headers.getCookies().get("login").getValue(); + Cookie cookie = headers.getCookies().get("login"); + if (cookie != null) { + String id = cookie.getValue(); String us; try { us = base.path("login").queryParam("id", id). @@ -122,7 +125,8 @@ accept(MediaType.TEXT_PLAIN).put(String.class); if (uuid != null) { user = name; - return Response.ok().cookie(new NewCookie("login", uuid)).entity(viewable("login.fmt", null)).build(); + NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false); + return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build(); } else { Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name); return Response.status(1).entity(v).build(); @@ -139,23 +143,23 @@ return welcomeImpl(); } - private Viewable board(String id) { + private Response board(String id) { return board(id, "", null); } @GET @Path("games/{id}/") @Produces(MediaType.TEXT_HTML) - public Viewable board( + public Response board( @PathParam("id") String id, @QueryParam("format") @DefaultValue("") String format, @QueryParam("move") @DefaultValue("-1") String move ) { return board(id, null, format, move); } - private Viewable board(@PathParam("id") String id, String msg, String format, String m) { + private Response board(@PathParam("id") String id, String msg, String format, String m) { Viewable v = checkLogin(); if (v != null) { - return v; + return Response.ok().entity(v).build(); } int move; try { @@ -167,6 +171,18 @@ if (move >= 0) { url = url.queryParam("move", "" + move); } + ResponseBuilder resp = Response.ok(); + Cookie cFormat = headers.getCookies().get("format"); + if (format.length() == 0) { + if (cFormat != null) { + format = cFormat.getValue(); + } + } else { + if (cFormat == null || !format.equals(cFormat.getValue())) { + resp.cookie(new NewCookie("format", format)); + } + } + Document doc = url.accept(MediaType.TEXT_XML).get(Document.class); Board b; try { @@ -175,13 +191,14 @@ Exceptions.printStackTrace(ex); b = Board.empty(); } - return viewable("game.fmt", doc, "message", msg, "format", format, "board", b); + v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b); + return resp.entity(v).build(); } @GET @Path("games/{id}/move") @Produces(MediaType.TEXT_HTML) - public Viewable move( + public Response move( @PathParam("id") String id, @QueryParam("type") String type, @QueryParam("direction") String direction, @@ -191,7 +208,7 @@ ) { Viewable v = checkLogin(); if (v != null) { - return v; + return Response.ok().entity(v).build(); } WebResource wr = base.path("games").path(id). queryParam("loginID", uuid).