diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java index 6bf5152..49d85e7 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java @@ -79,9 +79,10 @@ import org.jdom.Namespace; * } * @author etj + * @author eblondel */ public class RESTLayer { - private final Element layerElem; + protected final Element layerElem; public enum Type { VECTOR("VECTOR"), diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer21.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer21.java new file mode 100644 index 0000000..dd58926 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer21.java @@ -0,0 +1,178 @@ +package it.geosolutions.geoserver.rest.decoder; + +import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import java.util.ArrayList; +import java.util.List; + +import org.jdom.Element; + +/** + * Parse Layers returned as XML REST objects. Applicable to GS 2.1 for + * decoding: - AuthorityURLs - Identifiers - advertised property value + * + *

+ * This is the XML REST representation: + * + *

+ *  {@code
+ *  
+ *    tasmania_cities
+ *    /
+ *    VECTOR
+ *    
+ *        capitals
+ *        
+ *    
+ *    
+ *        tasmania_cities
+ *        
+ *    
+ *    true
+ *    true
+ *    true
+ *    
+ *        0
+ *        0
+ *    
+ *    
+ *    	
+ *    		[{"authority":"authority1","identifier":"identifier1"},]
+ *   	
+ *   	
+ *   		[{"name":"authority1","href":"http://www.authority1.org"},]
+ *   	
+ *   	true
+ *    
+ * 
+ *  }
+ * 
+ * + * @author eblondel + */ +public class RESTLayer21 extends RESTLayer{ + + + public RESTLayer21(Element layerElem) { + super(layerElem); + } + + public static RESTLayer21 build(String response) { + if(response == null) + return null; + + Element pb = JDOMBuilder.buildElement(response); + if(pb != null) + return new RESTLayer21(pb); + else + return null; + } + + /** + * Decodes the advertised property from the Geoserver Layer + * + */ + public boolean getAdvertised(){ + boolean advertised = true; + + final Element metadataRoot = layerElem.getChild("metadata"); + if(metadataRoot != null){ + final List metaElements = metadataRoot.getChildren(); + if(metaElements != null){ + for(Element el : metaElements){ + String key = el.getAttributeValue("key"); + if(key.matches("advertised")){ + advertised = Boolean.parseBoolean(el.getValue()); + } + } + } + } + return advertised; + } + + /** + * Decodes the list of AuthorityURLInfo from the GeoServer Layer + * + * @return the list of GSAuthorityURLInfoEncoder + */ + public List getEncodedAuthorityURLInfoList() { + List authorityURLList = null; + + final Element metadataRoot = layerElem.getChild("metadata"); + if (metadataRoot != null) { + final List metaElements = metadataRoot.getChildren(); + if (metaElements != null) { + for (Element element : metaElements) { + String key = element.getAttributeValue("key"); + if (key.matches("authorityURLs")) { + + String jsonStr = element.getValue(); + jsonStr = jsonStr.substring(2); + jsonStr = jsonStr.substring(0, + jsonStr.length() - 3); + + String[] items = jsonStr.split("\\}(,)\\{"); + authorityURLList = new ArrayList(items.length); + for (String item : items) { + String[] props = item.split(","); + + String[] kvp1 = props[0].split("\":"); + String name = kvp1[1].replace("\"", ""); + String[] kvp2 = props[1].split("\":"); + String href = kvp2[1].replace("\"", ""); + + authorityURLList + .add(new GSAuthorityURLInfoEncoder( + name, href)); + } + } + } + } + } + return authorityURLList; + } + + /** + * Decodes the list of IdentifierInfo from the GeoServer Layer + * + * @return the list of IdentifierInfoEncoder + */ + public List getEncodedIdentifierInfoList() { + List identifierList = null; + + final Element metadataRoot = layerElem.getChild("metadata"); + if (metadataRoot != null) { + final List metaElements = metadataRoot.getChildren(); + if (metaElements != null) { + for (Element element : metaElements) { + String key = element.getAttributeValue("key"); + if (key.matches("identifiers")) { + + String jsonStr = element.getValue(); + jsonStr = jsonStr.substring(2); + jsonStr = jsonStr.substring(0, + jsonStr.length() - 3); + + String[] items = jsonStr.split("\\}(,)\\{"); + identifierList = new ArrayList(items.length); + for (String item : items) { + String[] props = item.split(","); + + String[] kvp1 = props[0].split("\":"); + String authority = kvp1[1].replace("\"", ""); + String[] kvp2 = props[1].split("\":"); + String identifier = kvp2[1].replace("\"", ""); + + identifierList + .add(new GSIdentifierInfoEncoder( + authority, identifier)); + } + } + } + } + } + return identifierList; + } + +} diff --git a/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoder21Test.java b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoder21Test.java new file mode 100644 index 0000000..c31770a --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoder21Test.java @@ -0,0 +1,66 @@ +package it.geosolutions.geoserver.decoder; + +import it.geosolutions.geoserver.rest.decoder.RESTLayer; +import it.geosolutions.geoserver.rest.decoder.RESTLayer21; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import junit.framework.Assert; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.ClassPathResource; + +/** + * + * Apply Layer decoder tests on a GS 2.1 layer REST config + * + * @author eblondel + * + */ +public class LayerDecoder21Test{ + + RESTLayer21 layer; + + @Before + public void setUp() throws IOException{ + File layerFile = new ClassPathResource("testdata/layerExample21.xml").getFile(); + String layerString = FileUtils.readFileToString(layerFile); + layer = (RESTLayer21) RESTLayer21.build(layerString); + } + + @Test + public void testAdvertised(){ + Assert.assertEquals(true, layer.getAdvertised()); + } + + @Test + public void testAuthorityURLs() { + List authorityURLs = layer + .getEncodedAuthorityURLInfoList(); + System.out.println(authorityURLs.size()); + Assert.assertEquals("authority1", authorityURLs.get(0).getName()); + Assert.assertEquals("http://www.authority1.org", authorityURLs.get(0) + .getHref()); + Assert.assertEquals("authority2", authorityURLs.get(1).getName()); + Assert.assertEquals("http://www.authority2.org", authorityURLs.get(1) + .getHref()); + } + + @Test + public void testIdentifiers() { + List authorityURLs = layer + .getEncodedIdentifierInfoList(); + Assert.assertEquals("authority1", authorityURLs.get(0).getAuthority()); + Assert.assertEquals("identifier1", authorityURLs.get(0).getIdentifier()); + Assert.assertEquals("authority2", authorityURLs.get(1).getAuthority()); + Assert.assertEquals("identifier2", authorityURLs.get(1).getIdentifier()); + } + + +} \ No newline at end of file diff --git a/src/test/resources/testdata/layerExample21.xml b/src/test/resources/testdata/layerExample21.xml new file mode 100644 index 0000000..4a253a3 --- /dev/null +++ b/src/test/resources/testdata/layerExample21.xml @@ -0,0 +1,28 @@ + + tasmania_cities + / + VECTOR + + capitals + + + + tasmania_cities + + + true + true + true + + 0 + 0 + + + [{"authority":"authority1","identifier":"identifier1"},{"authority":"authority2","identifier":"identifier2"},] + [{"name":"authority1","href":"http://www.authority1.org"},{"name":"authority2","href":"http://www.authority2.org"},] + true + + \ No newline at end of file