freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
changeset 46 71e4cf307c93
parent 44 85a0fad6b3c2
child 47 2b6c104e6a59
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Tue Aug 11 15:18:43 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  }