Changeset 333

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

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

Location:
trunk/geowebcache/src/main
Files:
1 added
5 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){ 
  • trunk/geowebcache/src/main/java/org/geowebcache/seeder/SeedResource.java

    r331 r333  
    9999        try { 
    100100            String text = entity.getText(); 
     101             
    101102            XStream xs = new XStream(new DomDriver()); 
    102103                xs.alias("seedRequest", SeedRequest.class); 
     
    106107            xs.alias("zoomstart", Integer.class); 
    107108            xs.alias("zoomstop", Integer.class); 
     109             
    108110            SeedRequest rq = null;  
     111             
    109112            if(entity.getMediaType().equals(MediaType.APPLICATION_XML)){ 
    110113                rq = (SeedRequest) xs.fromXML(text); 
    111114            } 
     115             
     116            /** 
     117             * deserializing a json string is more complicated. XStream does not natively 
     118             * support it. Rather it uses a JettisonMappedXmlDriver to convert to intermediate xml 
     119             * and then deserializes that into the desired object. At this time, there is a known issue  
     120             * with the Jettison driver involving elements that come after an array in the json string.  
     121             *  
     122             * http://jira.codehaus.org/browse/JETTISON-48 
     123             *  
     124             * The code below is a hack: it treats the json string as text, then converts it to the intermediate 
     125             * xml and then deserializes that into the SeedRequest object.  
     126             *  
     127             *  
     128             */ 
     129           
    112130            else if(entity.getMediaType().equals(MediaType.APPLICATION_JSON)){ 
    113131                HierarchicalStreamDriver driver = new JettisonMappedXmlDriver(); 
  • trunk/geowebcache/src/main/java/org/geowebcache/util/XMLConfiguration.java

    r331 r333  
    3535import org.geowebcache.layer.wms.WMSLayer; 
    3636import org.geowebcache.GeoWebCacheException; 
    37 import org.geowebcache.cache.CacheFactory; 
    3837import org.geowebcache.cache.*; 
    3938import org.geowebcache.layer.Grid; 
     
    133132    public XStream getConfiguredXStream(XStream xstream) { 
    134133        XStream xs = xstream; 
    135  
     134        xs.setMode(XStream.NO_REFERENCES); 
     135         
    136136        xs.alias("layer", TileLayer.class); 
    137137        xs.alias("wmslayer", WMSLayer.class); 
    138138        xs.aliasField("layer-name", TileLayer.class, "name"); 
     139        xs.alias("grids", new ArrayList<Grid>().getClass()); 
    139140        xs.alias("grid", Grid.class); 
    140         xs.alias("format", String.class); 
    141  
     141        xs.aliasType("format", String.class); 
     142        xs.alias("mimeFormats", new ArrayList<String>().getClass()); 
     143        xs.aliasType("WMSurl", String.class); 
     144        xs.aliasType("errormime", String.class); 
     145        xs.alias("metaWidthHeight", new int[1].getClass()); 
     146        xs.alias("width", Integer.class); 
     147        xs.alias("height", Integer.class); 
     148        xs.aliasType("version", String.class); 
     149        xs.alias("tiled", boolean.class); 
     150        xs.alias("transparent", boolean.class); 
     151        xs.alias("debugheaders", boolean.class); 
     152         
     153         
    142154        return xs; 
    143155    } 
  • trunk/geowebcache/src/main/resources/geowebcache.xml

    r330 r333  
    33             <layer-name>topp:states</layer-name> 
    44             <mimeFormats> 
    5                  <format>image/png</format> 
    6                  <format>image/jpeg</format> 
     5                 <string>image/png</string> 
     6                 <string>image/jpeg</string> 
    77              </mimeFormats> 
    88              <grids> 
  • trunk/geowebcache/src/main/resources/layerTest.xml

    r319 r333  
    33             <layer-name>my:newTest</layer-name> 
    44             <mimeFormats> 
    5                  <format>image/jpeg</format> 
     5                 <string>image/jpeg</string> 
    66              </mimeFormats> 
    77              <grids>