Support for logging in
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Aug 2009 15:55:53 +0200
changeset 4671e4cf307c93
parent 45 ba37a52fef86
child 47 2b6c104e6a59
Support for logging in
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/login.fmt
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Wed Aug 19 17:58:05 2009 +0200
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sat Aug 29 15:55:53 2009 +0200
     1.3 @@ -32,9 +32,10 @@
     1.4  import com.sun.jersey.api.core.PackagesResourceConfig;
     1.5  import com.sun.jersey.api.core.ResourceConfig;
     1.6  import com.sun.jersey.api.view.Viewable;
     1.7 -import com.sun.jersey.spi.resource.Singleton;
     1.8  import com.sun.net.httpserver.HttpServer;
     1.9  import cz.xelfi.quoridor.webidor.resources.Quoridor;
    1.10 +import java.io.File;
    1.11 +import java.io.FileInputStream;
    1.12  import java.io.IOException;
    1.13  import java.net.URI;
    1.14  import java.util.ArrayList;
    1.15 @@ -42,16 +43,19 @@
    1.16  import java.util.Iterator;
    1.17  import java.util.List;
    1.18  import java.util.Map;
    1.19 +import java.util.Properties;
    1.20  import javax.ws.rs.DefaultValue;
    1.21  import javax.ws.rs.GET;
    1.22 +import javax.ws.rs.POST;
    1.23  import javax.ws.rs.Path;
    1.24  import javax.ws.rs.PathParam;
    1.25 +import javax.ws.rs.Produces;
    1.26  import javax.ws.rs.QueryParam;
    1.27 -import javax.ws.rs.core.CacheControl;
    1.28  import javax.ws.rs.core.Context;
    1.29  import javax.ws.rs.core.HttpHeaders;
    1.30  import javax.ws.rs.core.MediaType;
    1.31 -import javax.ws.rs.core.Request;
    1.32 +import javax.ws.rs.core.NewCookie;
    1.33 +import javax.ws.rs.core.Response;
    1.34  import org.codehaus.jettison.json.JSONArray;
    1.35  import org.codehaus.jettison.json.JSONException;
    1.36  import org.codehaus.jettison.json.JSONObject;
    1.37 @@ -65,31 +69,64 @@
    1.38      private static WebResource base;
    1.39  
    1.40      @Context
    1.41 -    private Request request;
    1.42 -    @Context
    1.43      private HttpHeaders headers;
    1.44 +    private String user;
    1.45  
    1.46      public UI() {
    1.47      }
    1.48  
    1.49 +    private Viewable checkLogin() {
    1.50 +        if (headers.getCookies().containsKey("login")) {
    1.51 +            user = headers.getCookies().get("login").getValue();
    1.52 +            return null;
    1.53 +        }
    1.54 +        return new Viewable("login.fmt", null);
    1.55 +    }
    1.56 +
    1.57 +    @POST
    1.58 +    @Path("login")
    1.59 +    @Produces(MediaType.TEXT_HTML)
    1.60 +    public Response login(
    1.61 +        @QueryParam("name") String name, @QueryParam("password") String password
    1.62 +    ) throws Exception {
    1.63 +        File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd");
    1.64 +        Properties p = new Properties();
    1.65 +        p.load(new FileInputStream(f));
    1.66 +        if (name != null && password.equals(p.getProperty(name))) {
    1.67 +            return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build();
    1.68 +        } else {
    1.69 +            Viewable v = new Viewable("login.fmt", "Invalid name or password: " + name);
    1.70 +            return Response.status(1).entity(v).build();
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74      @GET
    1.75 +    @Produces(MediaType.TEXT_HTML)
    1.76      public Viewable welcome() throws JSONException {
    1.77 -        if (headers.getCookies().containsKey("login")) {
    1.78 +        Viewable v = checkLogin();
    1.79 +        if (v != null) {
    1.80 +            return v;
    1.81          }
    1.82 -
    1.83 -        Object obj = getJson(base.path("games"));
    1.84 -        return new Viewable("index.fmt", obj);
    1.85 +        return welcomeImpl();
    1.86      }
    1.87  
    1.88      @GET
    1.89      @Path("games/{id}/")
    1.90 +    @Produces(MediaType.TEXT_HTML)
    1.91      public Viewable board(@PathParam("id") String id) throws JSONException {
    1.92 -        Object obj = convert(base.path("games").path(id).accept(MediaType.TEXT_PLAIN_TYPE).get(String.class));
    1.93 +        Viewable v = checkLogin();
    1.94 +        if (v != null) {
    1.95 +            return v;
    1.96 +        }
    1.97 +        Map<?,?> obj = (Map<?,?>)convert(base.path("games").path(id).accept(MediaType.APPLICATION_JSON_TYPE).get(JSONObject.class));
    1.98 +
    1.99 +
   1.100          return new Viewable("game.fmt", obj);
   1.101      }
   1.102  
   1.103      @GET
   1.104      @Path("games/{id}/move")
   1.105 +    @Produces(MediaType.TEXT_HTML)
   1.106      public Viewable move(
   1.107          @PathParam("id") String id,
   1.108          @QueryParam("type") String type,
   1.109 @@ -98,12 +135,17 @@
   1.110          @QueryParam("column") @DefaultValue("") String column,
   1.111          @QueryParam("row") @DefaultValue("") String row
   1.112      ) throws JSONException {
   1.113 +        Viewable v = checkLogin();
   1.114 +        if (v != null) {
   1.115 +            return v;
   1.116 +        }
   1.117 +        WebResource wr = base.path("games").path(id).queryParam("player", user);
   1.118          if (type.equals("fence")) {
   1.119 -            base.path("games").path(id).queryParam("move", direction.charAt(0) + column + row).post();
   1.120 +            wr.queryParam("move", direction.charAt(0) + column + row).put();
   1.121              return board(id);
   1.122          }
   1.123          if (type.equals("move")) {
   1.124 -            base.path("games").path(id).queryParam("move", direction + directionNext).post();
   1.125 +            wr.queryParam("move", direction + directionNext).put();
   1.126              return board(id);
   1.127          }
   1.128          return board(id);
   1.129 @@ -111,10 +153,15 @@
   1.130  
   1.131      @GET
   1.132      @Path("games/create")
   1.133 +    @Produces(MediaType.TEXT_HTML)
   1.134      public Viewable create(
   1.135          @QueryParam("white") String white,
   1.136          @QueryParam("black") String black
   1.137      ) throws JSONException {
   1.138 +        Viewable v = checkLogin();
   1.139 +        if (v != null) {
   1.140 +            return v;
   1.141 +        }
   1.142          Object obj = convert(
   1.143              base.path("games").queryParam("white", white).
   1.144              queryParam("black", black).post(JSONObject.class)
   1.145 @@ -177,4 +224,9 @@
   1.146          return server;
   1.147      }
   1.148  
   1.149 +    private Viewable welcomeImpl() throws JSONException {
   1.150 +        Object obj = getJson(base.path("games"));
   1.151 +        return new Viewable("index.fmt", obj);
   1.152 +    }
   1.153 +
   1.154  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/login.fmt	Sat Aug 29 15:55:53 2009 +0200
     2.3 @@ -0,0 +1,19 @@
     2.4 +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     2.5 +<html>
     2.6 +  <head>
     2.7 +    <title></title>
     2.8 +    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     2.9 +  </head>
    2.10 +  <body>
    2.11 +      <h1>Quoridor Community Server</h1>
    2.12 +      <h2>Login</h2>
    2.13 +
    2.14 +      <b>${model!""}</b>
    2.15 +
    2.16 +      <form action="login" method="post">
    2.17 +            Name: <input type="text" name="name"/>
    2.18 +            Password: <input type="password" name="password"/>
    2.19 +            <input type="submit" value="Login!"/>
    2.20 +      </form>
    2.21 +  </body>
    2.22 +</html>
    2.23 \ No newline at end of file
     3.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Wed Aug 19 17:58:05 2009 +0200
     3.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sat Aug 29 15:55:53 2009 +0200
     3.3 @@ -38,7 +38,10 @@
     3.4  import java.io.PrintWriter;
     3.5  import java.util.ArrayList;
     3.6  import java.util.Arrays;
     3.7 +import java.util.HashMap;
     3.8  import java.util.List;
     3.9 +import java.util.Map;
    3.10 +import java.util.TreeMap;
    3.11  import java.util.logging.Level;
    3.12  import java.util.logging.Logger;
    3.13  import javax.ws.rs.GET;
    3.14 @@ -49,6 +52,7 @@
    3.15  import javax.ws.rs.Produces;
    3.16  import javax.ws.rs.QueryParam;
    3.17  import javax.ws.rs.core.MediaType;
    3.18 +import org.codehaus.jettison.json.JSONObject;
    3.19  
    3.20  /**
    3.21   *
    3.22 @@ -102,6 +106,20 @@
    3.23          return g.getBoard().toString();
    3.24      }
    3.25  
    3.26 +    @GET
    3.27 +    @Path("{id}")
    3.28 +    @Produces(MediaType.APPLICATION_JSON)
    3.29 +    public Object getBoardInfo(@PathParam("id") String id) {
    3.30 +        Game g = findGame(id);
    3.31 +        if (g == null) {
    3.32 +            throw new IllegalArgumentException("Unknown game " + id);
    3.33 +        }
    3.34 +        Map<String,Object> data = new HashMap<String, Object>();
    3.35 +        data.put("board", g.getBoard().toString());
    3.36 +        data.put("game", g);
    3.37 +        return data;
    3.38 +    }
    3.39 +
    3.40      @PUT
    3.41      @Path("{id}")
    3.42      @Produces(MediaType.APPLICATION_JSON)
     4.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Wed Aug 19 17:58:05 2009 +0200
     4.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Sat Aug 29 15:55:53 2009 +0200
     4.3 @@ -38,6 +38,9 @@
     4.4  import java.io.FileReader;
     4.5  import java.io.IOException;
     4.6  import java.util.List;
     4.7 +import java.util.Map;
     4.8 +import javax.ws.rs.core.MediaType;
     4.9 +import org.codehaus.jettison.json.JSONObject;
    4.10  import org.junit.Test;
    4.11  import static org.junit.Assert.*;
    4.12  
    4.13 @@ -146,6 +149,17 @@
    4.14          assertEquals(7, board.getPlayers().get(1).getRow());
    4.15          assertEquals(Move.NORTH, read.getGames().get(0).getMoves().get(0));
    4.16          assertEquals(Move.SOUTH, read.getGames().get(0).getMoves().get(1));
    4.17 +
    4.18 +        class GMap extends GenericType<Map<String,Object>>{}
    4.19 +        JSONObject map = webResource.path("games").path(s.getId()).accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
    4.20 +        assertNotNull("Map really returned", map);
    4.21 +        String txtBoard = (String) map.get("board");
    4.22 +        assertNotNull("Contains its textual form", txtBoard);
    4.23 +        assertEquals("It is same as text of our game", board.toString(), txtBoard);
    4.24 +
    4.25 +        Object og = map.getJSONObject("game");
    4.26 +        assertTrue("Instance of JSON: " + og, og instanceof JSONObject);
    4.27 +        JSONObject jg = (JSONObject)og;
    4.28      }
    4.29  
    4.30  }