freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
changeset 54 f041b6570ff9
parent 50 1cce50d16bb5
child 55 830e0ba29c04
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Aug 30 14:56:32 2009 +0200
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Mon Aug 31 22:44:47 2009 +0200
     1.3 @@ -38,11 +38,6 @@
     1.4  import java.io.FileInputStream;
     1.5  import java.io.IOException;
     1.6  import java.net.URI;
     1.7 -import java.util.ArrayList;
     1.8 -import java.util.HashMap;
     1.9 -import java.util.Iterator;
    1.10 -import java.util.List;
    1.11 -import java.util.Map;
    1.12  import java.util.Properties;
    1.13  import java.util.concurrent.Callable;
    1.14  import javax.ws.rs.DefaultValue;
    1.15 @@ -58,9 +53,6 @@
    1.16  import javax.ws.rs.core.MediaType;
    1.17  import javax.ws.rs.core.NewCookie;
    1.18  import javax.ws.rs.core.Response;
    1.19 -import org.codehaus.jettison.json.JSONArray;
    1.20 -import org.codehaus.jettison.json.JSONException;
    1.21 -import org.codehaus.jettison.json.JSONObject;
    1.22  import org.w3c.dom.Document;
    1.23  
    1.24  /**
    1.25 @@ -94,7 +86,11 @@
    1.26      ) throws Exception {
    1.27          File f = new File(new File(new File(System.getProperty("user.home")), ".quoridor"), "passwd");
    1.28          Properties p = new Properties();
    1.29 -        p.load(new FileInputStream(f));
    1.30 +        try {
    1.31 +            p.load(new FileInputStream(f));
    1.32 +        } catch (IOException ex) {
    1.33 +            ex.printStackTrace();
    1.34 +        }
    1.35          if (name != null && password.equals(p.getProperty(name))) {
    1.36              return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build();
    1.37          } else {
    1.38 @@ -105,7 +101,7 @@
    1.39  
    1.40      @GET
    1.41      @Produces(MediaType.TEXT_HTML)
    1.42 -    public Viewable welcome() throws JSONException {
    1.43 +    public Viewable welcome() {
    1.44          Viewable v = checkLogin();
    1.45          if (v != null) {
    1.46              return v;
    1.47 @@ -116,15 +112,13 @@
    1.48      @GET
    1.49      @Path("games/{id}/")
    1.50      @Produces(MediaType.TEXT_HTML)
    1.51 -    public Viewable board(@PathParam("id") String id) throws JSONException {
    1.52 +    public Viewable board(@PathParam("id") String id) {
    1.53          Viewable v = checkLogin();
    1.54          if (v != null) {
    1.55              return v;
    1.56          }
    1.57 -        Map<?,?> obj = (Map<?,?>)convert(base.path("games").path(id).accept(MediaType.APPLICATION_JSON_TYPE).get(JSONObject.class));
    1.58 -
    1.59 -
    1.60 -        return new Viewable("game.fmt", obj);
    1.61 +        Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
    1.62 +        return new Viewable("game.fmt", doc);
    1.63      }
    1.64  
    1.65      @GET
    1.66 @@ -137,7 +131,7 @@
    1.67          @QueryParam("direction-next") @DefaultValue("") String directionNext,
    1.68          @QueryParam("column") @DefaultValue("") String column,
    1.69          @QueryParam("row") @DefaultValue("") String row
    1.70 -    ) throws JSONException {
    1.71 +    ) {
    1.72          Viewable v = checkLogin();
    1.73          if (v != null) {
    1.74              return v;
    1.75 @@ -160,41 +154,20 @@
    1.76      public Viewable create(
    1.77          @QueryParam("white") String white,
    1.78          @QueryParam("black") String black
    1.79 -    ) throws JSONException {
    1.80 +    ) {
    1.81          Viewable v = checkLogin();
    1.82          if (v != null) {
    1.83              return v;
    1.84          }
    1.85 -        Object obj = convert(
    1.86 +        Object obj =
    1.87              base.path("games").queryParam("white", white).
    1.88 -            queryParam("black", black).post(JSONObject.class)
    1.89 -        );
    1.90 -        Map<?,?> map = (Map<?,?>)obj;
    1.91 -        String id = (String)map.get("id");
    1.92 -        return board(id);
    1.93 +            queryParam("black", black).post(Document.class);
    1.94 +        return welcome();
    1.95      }
    1.96  
    1.97 -
    1.98 -    private static Object convert(Object obj) throws JSONException {
    1.99 -        if (obj instanceof JSONArray) {
   1.100 -            JSONArray arr = (JSONArray)obj;
   1.101 -            final int length = arr.length();
   1.102 -            List<Object> res = new ArrayList<Object>(length);
   1.103 -            for (int i = 0; i < length; i++) {
   1.104 -                res.add(convert(arr.get(i)));
   1.105 -            }
   1.106 -            return res;
   1.107 -        } else if (obj instanceof JSONObject) {
   1.108 -            JSONObject json = (JSONObject)obj;
   1.109 -            Map<Object,Object> map = new HashMap<Object,Object>(json.length() * 2 / 3);
   1.110 -            for (Iterator it = json.keys(); it.hasNext();) {
   1.111 -                String key = (String)it.next();
   1.112 -                map.put(key, convert(json.get(key)));
   1.113 -            }
   1.114 -            return map;
   1.115 -        } else {
   1.116 -            return obj;
   1.117 -        }
   1.118 +    private Viewable welcomeImpl() {
   1.119 +        final Object got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
   1.120 +        return new Viewable("index.fmt", got);
   1.121      }
   1.122  
   1.123      //
   1.124 @@ -236,10 +209,4 @@
   1.125          return server;
   1.126      }
   1.127  
   1.128 -    private Viewable welcomeImpl() throws JSONException {
   1.129 -        final Object got = base.path("games").accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class);
   1.130 -        List<?> obj = (List<?>)convert(got);
   1.131 -        return new Viewable("index.fmt", obj);
   1.132 -    }
   1.133 -
   1.134  }