Show
Ignore:
Timestamp:
08/15/08 18:11:14 (5 months ago)
Author:
masuta
Message:

added support for posting layers in json format. see example file under /resources

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/geowebcache/src/main/java/org/geowebcache/layer/TileLayerResource.java

    r330 r333  
    66import java.io.Reader; 
    77import java.io.CharArrayReader; 
     8import java.io.StringReader; 
     9import java.io.StringWriter; 
    810 
    911import org.restlet.Context; 
     
    2224 
    2325import com.thoughtworks.xstream.*; 
     26import com.thoughtworks.xstream.io.HierarchicalStreamDriver; 
     27import com.thoughtworks.xstream.io.HierarchicalStreamReader; 
    2428import com.thoughtworks.xstream.io.xml.DomDriver; 
     29import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; 
     30import com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier; 
     31import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; 
    2532import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; 
    2633 
     
    226233                + getRequest().getHostRef().getHostIdentifier()); 
    227234        try{ 
    228             String xmlText = entity.getText(); 
     235            String xmlText = entity.getText();       
    229236            XStream xs = RESTDispatcher.getConfig().getConfiguredXStream(new XStream(new DomDriver())); 
    230             TileLayer tileLayer = (WMSLayer) xs.fromXML(xmlText); 
     237            TileLayer tileLayer = null; 
     238            if(entity.getMediaType().equals(MediaType.APPLICATION_XML)){ 
     239                tileLayer = (WMSLayer) xs.fromXML(xmlText); 
     240            } 
     241             
     242            /** 
     243             * deserializing a json string is more complicated. XStream does not natively 
     244             * support it. Rather it uses a JettisonMappedXmlDriver to convert to intermediate xml 
     245             * and then deserializes that into the desired object. At this time, there is a known issue  
     246             * with the Jettison driver involving elements that come after an array in the json string.  
     247             *  
     248             * http://jira.codehaus.org/browse/JETTISON-48 
     249             *  
     250             * The code below is a hack: it treats the json string as text, then converts it to the intermediate 
     251             * xml and then deserializes that into the tileLayer object.  
     252             *  
     253             *  
     254             */ 
     255            else if(entity.getMediaType().equals(MediaType.APPLICATION_JSON)){ 
     256                HierarchicalStreamDriver driver = new JettisonMappedXmlDriver(); 
     257                StringReader reader = new StringReader(xmlText); 
     258                HierarchicalStreamReader hsr = driver.createReader(reader); 
     259                StringWriter writer = new StringWriter(); 
     260                new HierarchicalStreamCopier().copy(hsr, new PrettyPrintWriter(writer)); 
     261                writer.close();  
     262                 
     263                XStream x = new XStream(new DomDriver()); 
     264                x.alias("wmslayer", WMSLayer.class); 
     265                x.aliasField("layer-name", TileLayer.class, "name"); 
     266                x.alias("grid", Grid.class); 
     267                 
     268                 
     269                tileLayer = (WMSLayer) x.fromXML(writer.toString()); 
     270            } 
     271             
    231272            //the layer we are posting to is null, so we need to create a new one 
    232273            if(currentLayer == null){