# HG changeset patch # User Jaroslav Tulach # Date 1251751487 -7200 # Node ID f041b6570ff9c1bb8143b731192e4c2b1c46a2d0 # Parent ccc325a936ccbc433344b092ec3ebfa093685792 Removing anything related to JSON from the HTML layer, using natural freemarker's W3C DOM processing capabilities diff -r ccc325a936cc -r f041b6570ff9 .hgignore --- a/.hgignore Sun Aug 30 16:15:37 2009 +0200 +++ b/.hgignore Mon Aug 31 22:44:47 2009 +0200 @@ -1,3 +1,4 @@ +target/.* .*/target/.* .*orig$ .*~$ diff -r ccc325a936cc -r f041b6570ff9 freemarkerdor/pom.xml --- a/freemarkerdor/pom.xml Sun Aug 30 16:15:37 2009 +0200 +++ b/freemarkerdor/pom.xml Mon Aug 31 22:44:47 2009 +0200 @@ -46,6 +46,11 @@ 4.5 test + + freemarker + freemarker + 2.3.8 + @@ -65,3 +70,8 @@ + + + + + diff -r ccc325a936cc -r f041b6570ff9 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java Sun Aug 30 16:15:37 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/FreemarkerProcessor.java Mon Aug 31 22:44:47 2009 +0200 @@ -41,6 +41,7 @@ import javax.script.ScriptException; import javax.ws.rs.ext.Provider; import org.openide.filesystems.FileUtil; +import org.w3c.dom.Document; /** * @@ -68,7 +69,11 @@ if (model instanceof Map) { bind.putAll((Map)model); } - bind.put("model", model); + if (model instanceof Document) { + bind.put("doc", model); + } else { + bind.put("model", model); + } Writer w = new OutputStreamWriter(out); diff -r ccc325a936cc -r f041b6570ff9 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sun Aug 30 16:15:37 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Mon Aug 31 22:44:47 2009 +0200 @@ -38,11 +38,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.net.URI; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; import java.util.Properties; import java.util.concurrent.Callable; import javax.ws.rs.DefaultValue; @@ -58,9 +53,6 @@ import javax.ws.rs.core.MediaType; 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; import org.w3c.dom.Document; /** @@ -94,7 +86,11 @@ ) 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)); + try { + p.load(new FileInputStream(f)); + } catch (IOException ex) { + ex.printStackTrace(); + } if (name != null && password.equals(p.getProperty(name))) { return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build(); } else { @@ -105,7 +101,7 @@ @GET @Produces(MediaType.TEXT_HTML) - public Viewable welcome() throws JSONException { + public Viewable welcome() { Viewable v = checkLogin(); if (v != null) { return v; @@ -116,15 +112,13 @@ @GET @Path("games/{id}/") @Produces(MediaType.TEXT_HTML) - public Viewable board(@PathParam("id") String id) throws JSONException { + public Viewable board(@PathParam("id") String id) { 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); + Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class); + return new Viewable("game.fmt", doc); } @GET @@ -137,7 +131,7 @@ @QueryParam("direction-next") @DefaultValue("") String directionNext, @QueryParam("column") @DefaultValue("") String column, @QueryParam("row") @DefaultValue("") String row - ) throws JSONException { + ) { Viewable v = checkLogin(); if (v != null) { return v; @@ -160,41 +154,20 @@ public Viewable create( @QueryParam("white") String white, @QueryParam("black") String black - ) throws JSONException { + ) { Viewable v = checkLogin(); if (v != null) { return v; } - Object obj = convert( + Object obj = base.path("games").queryParam("white", white). - queryParam("black", black).post(JSONObject.class) - ); - Map map = (Map)obj; - String id = (String)map.get("id"); - return board(id); + queryParam("black", black).post(Document.class); + return welcome(); } - - private static Object convert(Object obj) throws JSONException { - if (obj instanceof JSONArray) { - JSONArray arr = (JSONArray)obj; - final int length = arr.length(); - List res = new ArrayList(length); - for (int i = 0; i < length; i++) { - res.add(convert(arr.get(i))); - } - return res; - } else if (obj instanceof JSONObject) { - JSONObject json = (JSONObject)obj; - Map map = new HashMap(json.length() * 2 / 3); - for (Iterator it = json.keys(); it.hasNext();) { - String key = (String)it.next(); - map.put(key, convert(json.get(key))); - } - return map; - } else { - return obj; - } + private Viewable welcomeImpl() { + final Object got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class); + return new Viewable("index.fmt", got); } // @@ -236,10 +209,4 @@ return server; } - private Viewable welcomeImpl() throws JSONException { - final Object got = base.path("games").accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class); - List obj = (List)convert(got); - return new Viewable("index.fmt", obj); - } - } diff -r ccc325a936cc -r f041b6570ff9 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt Sun Aug 30 16:15:37 2009 +0200 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt Mon Aug 31 22:44:47 2009 +0200 @@ -6,7 +6,7 @@

Game

-

${id.white} vs. ${id.black}

+

${doc.game.id.@white} vs. ${doc.game.id.@black}

-
${board}
+
${doc.game.board}
\ No newline at end of file diff -r ccc325a936cc -r f041b6570ff9 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt Sun Aug 30 16:15:37 2009 +0200 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt Mon Aug 31 22:44:47 2009 +0200 @@ -6,10 +6,11 @@

Quoridor Community Server

-
    - <#list model as item> -
  1. ${item.white} vs. ${item.black} board
  2. + <#list doc.gameIds.* as g> +
  3. + ${g.@white} vs. ${g.@black} board +
diff -r ccc325a936cc -r f041b6570ff9 freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Sun Aug 30 16:15:37 2009 +0200 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Mon Aug 31 22:44:47 2009 +0200 @@ -104,6 +104,9 @@ if (res.indexOf("action=\"login\"") == -1) { fail("Wrong index.html:\n" + res); } + if (res.toLowerCase().indexOf("error") != -1) { + fail("There was an error:\n" + res); + } res = webResource.cookie(Cookie.valueOf("login=jarda")).accept("text/html").get(String.class); if (res.indexOf("action=\"games/create\"") == -1) { fail(res); diff -r ccc325a936cc -r f041b6570ff9 webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Sun Aug 30 16:15:37 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Mon Aug 31 22:44:47 2009 +0200 @@ -49,7 +49,7 @@ public final class Game extends Object { @XmlElement private GameId id; - @XmlAttribute + @XmlElement @XmlJavaTypeAdapter(BoardAdapter.class) private Board board; @XmlElement diff -r ccc325a936cc -r f041b6570ff9 webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java Sun Aug 30 16:15:37 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java Mon Aug 31 22:44:47 2009 +0200 @@ -49,7 +49,7 @@ private Date started; @XmlAttribute private GameResult result; - @XmlID + @XmlID @XmlAttribute private String id; GameId() { diff -r ccc325a936cc -r f041b6570ff9 webidor/src/main/java/cz/xelfi/quoridor/webidor/W3CDocumentReader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/W3CDocumentReader.java Mon Aug 31 22:44:47 2009 +0200 @@ -0,0 +1,46 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package cz.xelfi.quoridor.webidor; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import javax.ws.rs.Consumes; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.Provider; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +/** + * + * @author Jaroslav Tulach + */ +@Provider +@Consumes(MediaType.TEXT_XML) +public class W3CDocumentReader implements javax.ws.rs.ext.MessageBodyReader { + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return type == Document.class; + } + + public Document readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + return builder.parse(entityStream); + } catch (ParserConfigurationException ex) { + throw new WebApplicationException(ex); + } catch (SAXException ex) { + throw new WebApplicationException(ex); + } + } + +} diff -r ccc325a936cc -r f041b6570ff9 webidor/src/main/resources/META-INF/services/javax.ws.rs.ext.MessageBodyReader --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/main/resources/META-INF/services/javax.ws.rs.ext.MessageBodyReader Mon Aug 31 22:44:47 2009 +0200 @@ -0,0 +1,1 @@ +cz.xelfi.quoridor.webidor.W3CDocumentReader diff -r ccc325a936cc -r f041b6570ff9 webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java Sun Aug 30 16:15:37 2009 +0200 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java Mon Aug 31 22:44:47 2009 +0200 @@ -40,6 +40,7 @@ import java.util.Map; import javax.ws.rs.core.MediaType; import org.junit.Test; +import org.w3c.dom.Document; import static org.junit.Assert.*; /** @@ -100,6 +101,10 @@ GenericType> gType = new GenericType>() {}; + Document doc = webResource.path("games").accept("text/xml").get(Document.class); + assertNotNull("Can read games in form of XML", doc); + assertEquals("Name is correct", "gameIds", doc.getDocumentElement().getNodeName()); + assertEquals("One child", 1, doc.getDocumentElement().getChildNodes().getLength()); List games = webResource.path("games").accept("application/json").get(gType); assertEquals("One game", 1, games.size()); assertEquals("Same white", "Jarda", games.get(0).getWhite());