Remember the selection of format thru out the session
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Sep 2009 19:17:52 +0200
changeset 101c7244cdd143e
parent 100 8b899ed24f9f
child 102 bda5bd82b435
Remember the selection of format thru out the session
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
     1.1 --- a/freemarkerdor/pom.xml	Sat Sep 19 14:38:29 2009 +0200
     1.2 +++ b/freemarkerdor/pom.xml	Sat Sep 19 19:17:52 2009 +0200
     1.3 @@ -10,7 +10,7 @@
     1.4    <groupId>org.apidesign</groupId>
     1.5    <artifactId>freemarkerdor</artifactId>
     1.6    <name>freemarkerdor</name>
     1.7 -  <version>1.15</version>
     1.8 +  <version>1.16</version>
     1.9    <url>http://maven.apache.org</url>
    1.10    <dependencies>
    1.11      <dependency>
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sat Sep 19 14:38:29 2009 +0200
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sat Sep 19 19:17:52 2009 +0200
     2.3 @@ -56,10 +56,12 @@
     2.4  import javax.ws.rs.Produces;
     2.5  import javax.ws.rs.QueryParam;
     2.6  import javax.ws.rs.core.Context;
     2.7 +import javax.ws.rs.core.Cookie;
     2.8  import javax.ws.rs.core.HttpHeaders;
     2.9  import javax.ws.rs.core.MediaType;
    2.10  import javax.ws.rs.core.NewCookie;
    2.11  import javax.ws.rs.core.Response;
    2.12 +import javax.ws.rs.core.Response.ResponseBuilder;
    2.13  import org.openide.util.Exceptions;
    2.14  import org.w3c.dom.Document;
    2.15  
    2.16 @@ -93,8 +95,9 @@
    2.17      }
    2.18  
    2.19      private Viewable checkLogin() {
    2.20 -        if (headers.getCookies().containsKey("login")) {
    2.21 -            String id = headers.getCookies().get("login").getValue();
    2.22 +        Cookie cookie = headers.getCookies().get("login");
    2.23 +        if (cookie != null) {
    2.24 +            String id = cookie.getValue();
    2.25              String us;
    2.26              try {
    2.27                  us = base.path("login").queryParam("id", id).
    2.28 @@ -122,7 +125,8 @@
    2.29              accept(MediaType.TEXT_PLAIN).put(String.class);
    2.30          if (uuid != null) {
    2.31              user = name;
    2.32 -            return Response.ok().cookie(new NewCookie("login", uuid)).entity(viewable("login.fmt", null)).build();
    2.33 +            NewCookie nc = new NewCookie("login", uuid, null, null, null, 3600 * 24 * 7, false);
    2.34 +            return Response.ok().cookie(nc).entity(viewable("login.fmt", null)).build();
    2.35          } else {
    2.36              Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
    2.37              return Response.status(1).entity(v).build();
    2.38 @@ -139,23 +143,23 @@
    2.39          return welcomeImpl();
    2.40      }
    2.41  
    2.42 -    private Viewable board(String id) {
    2.43 +    private Response board(String id) {
    2.44          return board(id, "", null);
    2.45      }
    2.46      @GET
    2.47      @Path("games/{id}/")
    2.48      @Produces(MediaType.TEXT_HTML)
    2.49 -    public Viewable board(
    2.50 +    public Response board(
    2.51          @PathParam("id") String id,
    2.52          @QueryParam("format") @DefaultValue("") String format,
    2.53          @QueryParam("move") @DefaultValue("-1") String move
    2.54      ) {
    2.55          return board(id, null, format, move);
    2.56      }
    2.57 -    private Viewable board(@PathParam("id") String id, String msg, String format, String m) {
    2.58 +    private Response board(@PathParam("id") String id, String msg, String format, String m) {
    2.59          Viewable v = checkLogin();
    2.60          if (v != null) {
    2.61 -            return v;
    2.62 +            return Response.ok().entity(v).build();
    2.63          }
    2.64          int move;
    2.65          try {
    2.66 @@ -167,6 +171,18 @@
    2.67          if (move >= 0) {
    2.68              url = url.queryParam("move", "" + move);
    2.69          }
    2.70 +        ResponseBuilder resp = Response.ok();
    2.71 +        Cookie cFormat = headers.getCookies().get("format");
    2.72 +        if (format.length() == 0) {
    2.73 +            if (cFormat != null) {
    2.74 +                format = cFormat.getValue();
    2.75 +            }
    2.76 +        } else {
    2.77 +            if (cFormat == null || !format.equals(cFormat.getValue())) {
    2.78 +                resp.cookie(new NewCookie("format", format));
    2.79 +            }
    2.80 +        }
    2.81 +
    2.82          Document doc = url.accept(MediaType.TEXT_XML).get(Document.class);
    2.83          Board b;
    2.84          try {
    2.85 @@ -175,13 +191,14 @@
    2.86              Exceptions.printStackTrace(ex);
    2.87              b = Board.empty();
    2.88          }
    2.89 -        return viewable("game.fmt", doc, "message", msg, "format", format, "board", b);
    2.90 +        v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b);
    2.91 +        return resp.entity(v).build();
    2.92      }
    2.93  
    2.94      @GET
    2.95      @Path("games/{id}/move")
    2.96      @Produces(MediaType.TEXT_HTML)
    2.97 -    public Viewable move(
    2.98 +    public Response move(
    2.99          @PathParam("id") String id,
   2.100          @QueryParam("type") String type,
   2.101          @QueryParam("direction") String direction,
   2.102 @@ -191,7 +208,7 @@
   2.103      ) {
   2.104          Viewable v = checkLogin();
   2.105          if (v != null) {
   2.106 -            return v;
   2.107 +            return Response.ok().entity(v).build();
   2.108          }
   2.109          WebResource wr = base.path("games").path(id).
   2.110              queryParam("loginID", uuid).