# HG changeset patch # User Jaroslav Tulach # Date 1251554153 -7200 # Node ID 71e4cf307c93cb1b639766b12f6ff1292dfe7724 # Parent ba37a52fef86f21541290d0a921e33c24b4a85e8 Support for logging in diff -r ba37a52fef86 -r 71e4cf307c93 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Wed Aug 19 17:58:05 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sat Aug 29 15:55:53 2009 +0200 @@ -32,9 +32,10 @@ import com.sun.jersey.api.core.PackagesResourceConfig; import com.sun.jersey.api.core.ResourceConfig; import com.sun.jersey.api.view.Viewable; -import com.sun.jersey.spi.resource.Singleton; import com.sun.net.httpserver.HttpServer; import cz.xelfi.quoridor.webidor.resources.Quoridor; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.net.URI; import java.util.ArrayList; @@ -42,16 +43,19 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Properties; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Request; +import javax.ws.rs.core.NewCookie; +import javax.ws.rs.core.Response; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; @@ -65,31 +69,64 @@ private static WebResource base; @Context - private Request request; - @Context private HttpHeaders headers; + private String user; public UI() { } + private Viewable checkLogin() { + if (headers.getCookies().containsKey("login")) { + user = headers.getCookies().get("login").getValue(); + return null; + } + return new Viewable("login.fmt", null); + } + + @POST + @Path("login") + @Produces(MediaType.TEXT_HTML) + public Response login( + @QueryParam("name") String name, @QueryParam("password") String password + ) throws Exception { + File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd"); + Properties p = new Properties(); + p.load(new FileInputStream(f)); + if (name != null && password.equals(p.getProperty(name))) { + return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build(); + } else { + Viewable v = new Viewable("login.fmt", "Invalid name or password: " + name); + return Response.status(1).entity(v).build(); + } + } + @GET + @Produces(MediaType.TEXT_HTML) public Viewable welcome() throws JSONException { - if (headers.getCookies().containsKey("login")) { + Viewable v = checkLogin(); + if (v != null) { + return v; } - - Object obj = getJson(base.path("games")); - return new Viewable("index.fmt", obj); + return welcomeImpl(); } @GET @Path("games/{id}/") + @Produces(MediaType.TEXT_HTML) public Viewable board(@PathParam("id") String id) throws JSONException { - Object obj = convert(base.path("games").path(id).accept(MediaType.TEXT_PLAIN_TYPE).get(String.class)); + Viewable v = checkLogin(); + if (v != null) { + return v; + } + Map obj = (Map)convert(base.path("games").path(id).accept(MediaType.APPLICATION_JSON_TYPE).get(JSONObject.class)); + + return new Viewable("game.fmt", obj); } @GET @Path("games/{id}/move") + @Produces(MediaType.TEXT_HTML) public Viewable move( @PathParam("id") String id, @QueryParam("type") String type, @@ -98,12 +135,17 @@ @QueryParam("column") @DefaultValue("") String column, @QueryParam("row") @DefaultValue("") String row ) throws JSONException { + Viewable v = checkLogin(); + if (v != null) { + return v; + } + WebResource wr = base.path("games").path(id).queryParam("player", user); if (type.equals("fence")) { - base.path("games").path(id).queryParam("move", direction.charAt(0) + column + row).post(); + wr.queryParam("move", direction.charAt(0) + column + row).put(); return board(id); } if (type.equals("move")) { - base.path("games").path(id).queryParam("move", direction + directionNext).post(); + wr.queryParam("move", direction + directionNext).put(); return board(id); } return board(id); @@ -111,10 +153,15 @@ @GET @Path("games/create") + @Produces(MediaType.TEXT_HTML) public Viewable create( @QueryParam("white") String white, @QueryParam("black") String black ) throws JSONException { + Viewable v = checkLogin(); + if (v != null) { + return v; + } Object obj = convert( base.path("games").queryParam("white", white). queryParam("black", black).post(JSONObject.class) @@ -177,4 +224,9 @@ return server; } + private Viewable welcomeImpl() throws JSONException { + Object obj = getJson(base.path("games")); + return new Viewable("index.fmt", obj); + } + } diff -r ba37a52fef86 -r 71e4cf307c93 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/login.fmt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/login.fmt Sat Aug 29 15:55:53 2009 +0200 @@ -0,0 +1,19 @@ + + + + + + + +

Quoridor Community Server

+

Login

+ + ${model!""} + +
+ Name: + Password: + +
+ + \ No newline at end of file diff -r ba37a52fef86 -r 71e4cf307c93 webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Wed Aug 19 17:58:05 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Sat Aug 29 15:55:53 2009 +0200 @@ -38,7 +38,10 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.ws.rs.GET; @@ -49,6 +52,7 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; +import org.codehaus.jettison.json.JSONObject; /** * @@ -102,6 +106,20 @@ return g.getBoard().toString(); } + @GET + @Path("{id}") + @Produces(MediaType.APPLICATION_JSON) + public Object getBoardInfo(@PathParam("id") String id) { + Game g = findGame(id); + if (g == null) { + throw new IllegalArgumentException("Unknown game " + id); + } + Map data = new HashMap(); + data.put("board", g.getBoard().toString()); + data.put("game", g); + return data; + } + @PUT @Path("{id}") @Produces(MediaType.APPLICATION_JSON) diff -r ba37a52fef86 -r 71e4cf307c93 webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java Wed Aug 19 17:58:05 2009 +0200 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java Sat Aug 29 15:55:53 2009 +0200 @@ -38,6 +38,9 @@ import java.io.FileReader; import java.io.IOException; import java.util.List; +import java.util.Map; +import javax.ws.rs.core.MediaType; +import org.codehaus.jettison.json.JSONObject; import org.junit.Test; import static org.junit.Assert.*; @@ -146,6 +149,17 @@ assertEquals(7, board.getPlayers().get(1).getRow()); assertEquals(Move.NORTH, read.getGames().get(0).getMoves().get(0)); assertEquals(Move.SOUTH, read.getGames().get(0).getMoves().get(1)); + + class GMap extends GenericType>{} + JSONObject map = webResource.path("games").path(s.getId()).accept(MediaType.APPLICATION_JSON).get(JSONObject.class); + assertNotNull("Map really returned", map); + String txtBoard = (String) map.get("board"); + assertNotNull("Contains its textual form", txtBoard); + assertEquals("It is same as text of our game", board.toString(), txtBoard); + + Object og = map.getJSONObject("game"); + assertTrue("Instance of JSON: " + og, og instanceof JSONObject); + JSONObject jg = (JSONObject)og; } }