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 011873e..49d85e7 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java @@ -25,7 +25,16 @@ package it.geosolutions.geoserver.rest.decoder; +import java.util.ArrayList; +import java.util.List; + import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.AuthorityURLInfo; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.IdentifierInfo; +import it.geosolutions.geoserver.rest.encoder.metadatalink.GSMetadataLinkInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.metadatalink.ResourceMetadataLinkInfo; import org.jdom.Element; import org.jdom.Namespace; @@ -49,16 +58,31 @@ import org.jdom.Namespace; true + true + true 0 0 + + + authority1 + http://www.authority1.org + + + + + authority1 + identifier1 + + * } * @author etj + * @author eblondel */ public class RESTLayer { - private final Element layerElem; + protected final Element layerElem; public enum Type { VECTOR("VECTOR"), @@ -97,6 +121,18 @@ public class RESTLayer { this.layerElem = layerElem; } + public boolean getEnabled(){ + return Boolean.parseBoolean(layerElem.getChildText("enabled")); + } + + public boolean getQueryable(){ + return Boolean.parseBoolean(layerElem.getChildText("queryable")); + } + + public boolean getAdvertised(){ + return Boolean.parseBoolean(layerElem.getChildText("advertised")); + } + public String getName() { return layerElem.getChildText("name"); } @@ -164,6 +200,62 @@ public class RESTLayer { Element atom = resource.getChild("link", Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom")); return atom.getAttributeValue("href"); } + + + /** + * Decodes the list of AuthorityURLInfo from the GeoServer Layer + * + * @return the list of GSAuthorityURLInfoEncoder + */ + public List getEncodedAuthorityURLInfoList() { + List authorityURLList = null; + + final Element authorityURLsRoot = layerElem.getChild("authorityURLs"); + if (authorityURLsRoot != null) { + final List authorityURLs = authorityURLsRoot.getChildren(); + if (authorityURLs != null) { + authorityURLList = new ArrayList( + authorityURLs.size()); + for (Element authorityURL : authorityURLs) { + final GSAuthorityURLInfoEncoder authEnc = new GSAuthorityURLInfoEncoder(); + authEnc.setName(authorityURL + .getChildText(AuthorityURLInfo.name.name())); + authEnc.setHref(authorityURL + .getChildText(AuthorityURLInfo.href.name())); + authorityURLList.add(authEnc); + } + } + } + return authorityURLList; + } + + /** + * Decodes the list of IdentifierInfo from the GeoServer Layer + * + * @return the list of IdentifierInfoEncoder + */ + public List getEncodedIdentifierInfoList() { + List idList = null; + + final Element idRoot = layerElem.getChild("identifiers"); + if (idRoot != null) { + final List identifiers = idRoot.getChildren(); + if (identifiers != null) { + idList = new ArrayList( + identifiers.size()); + for (Element identifier : identifiers) { + final GSIdentifierInfoEncoder idEnc = new GSIdentifierInfoEncoder(); + idEnc.setAuthority(identifier + .getChildText(IdentifierInfo.authority.name())); + idEnc.setIdentifier(identifier + .getChildText(IdentifierInfo.identifier.name())); + idList.add(idEnc); + } + } + } + return idList; + } + // protected double getLatLonEdge(String edge) { // Element resource = layerElem.getChild("resource"); 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/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java index 48dc583..f78ccac 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java @@ -25,15 +25,19 @@ package it.geosolutions.geoserver.rest.encoder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder; import org.jdom.Element; import org.jdom.filter.Filter; /** + * Layer encoder for Geoserver >= 2.2 * * @author ETj (etj at geo-solutions.it) * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com * * The layer encoder is enabled by default * @@ -41,12 +45,19 @@ import org.jdom.filter.Filter; public class GSLayerEncoder extends PropertyXMLEncoder { public final static String STYLES = "styles"; + public final static String AUTHORITY_URLS="authorityURLs"; + public final static String IDENTIFIERS="identifiers"; + final private Element stylesEncoder = new Element(STYLES); + final private Element authorityURLListEncoder = new Element(AUTHORITY_URLS); + final private Element identifierListEncoder = new Element(IDENTIFIERS); public GSLayerEncoder() { super("layer"); addEnabled(); addContent(stylesEncoder); + addContent(authorityURLListEncoder); + addContent(identifierListEncoder); } /** @@ -167,4 +178,58 @@ public class GSLayerEncoder extends PropertyXMLEncoder { } })).size() == 0 ? false : true; } + + /** + * + * @param advertised + * true if the layer should be advertised + */ + public void setAdvertised(boolean advertised) { + if (advertised) + set("advertised", "true"); + else + set("advertised", "false"); + } + + /** + * Add an authorityURLInfo to the GeoServer layer + * + * @param authorityURLInfo + */ + public void addAuthorityURL(GSAuthorityURLInfoEncoder authorityURLInfo) { + authorityURLListEncoder.addContent(authorityURLInfo.getRoot()); + } + + /** + * Deletes a AuthorityURLInfo from the list using the authorityURL + * (AuthorityURLInfo href) + * + * @param authorityURL + * @return true if something is removed, false otherwise + */ + public boolean delAuthorityURL(final String authorityURL) { + return (authorityURLListEncoder.removeContent(GSAuthorityURLInfoEncoder + .getFilterByHref(authorityURL))).size() == 0 ? false : true; + } + + /** + * Add an identifierInfo to the GeoServer layer + * + * @param identifierInfo + */ + public void addIdentifier(GSIdentifierInfoEncoder identifierInfo) { + identifierListEncoder.addContent(identifierInfo.getRoot()); + } + + /** + * Deletes a IdentifierInfo from the list using the authority name + * (IdentifierInfo authority) + * + * @param authority + * @return true if something is removed, false otherwise + */ + public boolean delIdentifier(final String authority) { + return (identifierListEncoder.removeContent(GSIdentifierInfoEncoder + .getFilterByHref(authority))).size() == 0 ? false : true; + } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java new file mode 100644 index 0000000..5daf3f6 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java @@ -0,0 +1,188 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package it.geosolutions.geoserver.rest.encoder; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import it.geosolutions.geoserver.rest.encoder.authorityurl.AuthorityURLInfo; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.IdentifierInfo; +import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder; +import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; + +/** + * Layer encoder for Geoserver = 2.1 + * + * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com + * + * The layer encoder is enabled by default + * + */ +public class GSLayerEncoder21 extends GSLayerEncoder { + + + public final static String METADATA = "metadata"; + final private GSMetadataEncoder metadata = new GSMetadataEncoder(); + public Map authorityURLList; + public Map identifierList; + + private class GSMetadataEncoder extends NestedElementEncoder{ + public GSMetadataEncoder() { + super(METADATA); + } + } + + public GSLayerEncoder21() { + super(); + addContent(metadata.getRoot()); + addAdvertised(); + } + + /** + * @param key + * @param dimensionInfo + */ + protected void addMetadata(String key, XmlElement dimensionInfo) { + metadata.add(key, dimensionInfo.getRoot()); + } + + /** + * advertise the layer + */ + protected void addAdvertised(){ + metadata.add("advertised", "true"); + } + + /** + * + * @param advertised true if the layer should be advertised + */ + public void setAdvertised(boolean advertised){ + if(advertised){ + metadata.add("advertised", "true"); + }else{ + metadata.add("advertised", "false"); + } + } + + /** + * Add an authorityURLInfo to the GeoServer layer + * + * @param authorityURLInfo + */ + public void addAuthorityURL(GSAuthorityURLInfoEncoder authorityURLInfo){ + if(authorityURLList == null){ + authorityURLList = new HashMap(); + } + authorityURLList.put(authorityURLInfo.getHref(), authorityURLInfo.getName()); + String jsonStr = ""; + for(Entry entry : authorityURLList.entrySet()){ + jsonStr += "{"+ + "\""+AuthorityURLInfo.name.name()+"\":\""+entry.getValue()+"\","+ + "\""+AuthorityURLInfo.href.name()+"\":\""+entry.getKey()+"\""+ + "},"; + } + metadata.set("authorityURLs", "["+jsonStr+"]"); + } + + + /** + * Deletes a AuthorityURLInfo from the list using the authorityURL + * (AuthorityURLInfo href) + * + * @param authorityURL + * @return true if something is removed, false otherwise + */ + public boolean delAuthorityURL(final String authorityURL){ + boolean delete = false; + if(!(authorityURLList == null || authorityURLList.isEmpty())){ + if(authorityURLList.containsKey(authorityURL)){ + identifierList.remove(authorityURL); + String jsonStr = ""; + for(Entry entry : identifierList.entrySet()){ + jsonStr += "{"+ + "\""+AuthorityURLInfo.name.name()+"\":\""+entry.getValue()+"\","+ + "\""+AuthorityURLInfo.href.name()+"\":\""+entry.getKey()+"\""+ + "},"; + } + metadata.set("identifiers", "["+jsonStr+"]"); + delete = true; + } + } + return delete; + } + + /** + * Add an identifierInfo to the GeoServer layer + * + * @param identifierInfo + */ + public void addIdentifier(GSIdentifierInfoEncoder identifierInfo){ + if(identifierList == null){ + identifierList = new HashMap(); + } + identifierList.put(identifierInfo.getAuthority(), identifierInfo.getIdentifier()); + String jsonStr = ""; + for(Entry entry : identifierList.entrySet()){ + jsonStr += "{"+ + "\""+IdentifierInfo.authority.name()+"\":\""+entry.getKey()+"\","+ + "\""+IdentifierInfo.identifier.name()+"\":\""+entry.getValue()+"\""+ + "},"; + } + metadata.set("identifiers", "["+jsonStr+"]"); + } + + /** + * Deletes a IdentifierInfo from the list using the authority + * name (IdentifierInfo authority) + * + * @param authority + * @return true if something is removed, false otherwise + */ + public boolean delIdentifier(final String authority){ + boolean delete = false; + if(!(identifierList == null || identifierList.isEmpty())){ + if(identifierList.containsKey(authority)){ + identifierList.remove(authority); + String jsonStr = ""; + for(Entry entry : identifierList.entrySet()){ + jsonStr += "{"+ + "\""+IdentifierInfo.authority.name()+"\":\""+entry.getKey()+"\","+ + "\""+IdentifierInfo.identifier.name()+"\":\""+entry.getValue()+"\""+ + "},"; + } + metadata.set("identifiers", "["+jsonStr+"]"); + delete = true; + } + } + return delete; + } + + +} diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/AuthorityURLInfo.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/AuthorityURLInfo.java new file mode 100644 index 0000000..402655a --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/AuthorityURLInfo.java @@ -0,0 +1,37 @@ +/* +* GeoServer-Manager - Simple Manager Library for GeoServer +* +* Copyright (C) 2007,2011 GeoSolutions S.A.S. +* http://www.geo-solutions.it +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +package it.geosolutions.geoserver.rest.encoder.authorityurl; + +/** + * Enumeration of layer authorityURL members + * + * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | + * emmanuel.blondel@fao.org + * + */ +public enum AuthorityURLInfo { + + name, href; +} \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoder.java new file mode 100644 index 0000000..f75ba24 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoder.java @@ -0,0 +1,215 @@ +/* +* GeoServer-Manager - Simple Manager Library for GeoServer +* +* Copyright (C) 2007,2011 GeoSolutions S.A.S. +* http://www.geo-solutions.it +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +package it.geosolutions.geoserver.rest.encoder.authorityurl; + +import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; + +import org.jdom.Element; +import org.jdom.filter.Filter; + +/** +* GSAuthorityURLInfoEncoder - encodes an authorityURL for a given GeoServer +* layer as follows: +*
+* {@code
+* final GSAuthorityURLInfoEncoder ae = new GSAuthorityURLInfoEncoder();
+* ae.setName("an authority");
+* ae.setHref("http://www.organization.org");
+* }
+* 
+* For this example, the XML output is: +*
+* {@code
+* 
+*   an authority
+*   http://www.organization.org
+* 
+* }
+* 
+* +* @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | +* emmanuel.blondel@fao.org +* +*/ +public class GSAuthorityURLInfoEncoder extends XmlElement { + + /** + * A class to filter the AuthorityURL by href + * + * + */ + private static class filterByHref implements Filter { + + final private String key; + + public filterByHref(String href) { + this.key = href; + } + + private static final long serialVersionUID = 1L; + + public boolean matches(Object obj) { + Element el = ((Element) obj).getChild(AuthorityURLInfo.href + .toString()); + if (el != null && el.getTextTrim().equals(key)) { + return true; + } + return false; + } + } + + /** + * Get a Filter using the AuthorityURLInfo href (authorityURL) + * + * @param href + * @return the filter + */ + public static Filter getFilterByHref(String href) { + return new filterByHref(href); + } + + /** + * Constructs a new GSAuthorityURLInfoEncoder + * + */ + public GSAuthorityURLInfoEncoder() { + super("AuthorityURL"); + } + + /** + * Constructs quickly an AuthorityURL info + * + * @param name + * (required) + * @param href + * (required) + */ + public GSAuthorityURLInfoEncoder(String name, String href) { + super("AuthorityURL"); + this.setup(name, href); + } + + /** + * Set-up quickly an AuthorityURL info + * + * @param name + * @param href + */ + protected void setup(String name, String href) { + set(AuthorityURLInfo.name.name(), name); + set(AuthorityURLInfo.href.name(), href); + } + + /** + * Set an AuthorityURLInfo member (name, href) + * + * @param type + * @param value + */ + protected void setMember(AuthorityURLInfo type, String value) { + set(type.toString(), value); + } + + /** + * Set the name + * + * @param name + */ + public void setName(String name) { + this.setMember(AuthorityURLInfo.name, name); + } + + /** + * Set the href + * + * @param href + */ + public void setHref(String href) { + this.setMember(AuthorityURLInfo.href, href); + } + + /** + * Deletes an AuthorityURLInfo member + * + * @param type + * @return true if the AuthorityURLInfo member is removed + */ + protected boolean delMember(AuthorityURLInfo type) { + return ElementUtils.remove(this.getRoot(), + this.getRoot().getChild(type.toString())); + } + + /** + * Deletes the authority name + * + * @return true if removed + */ + public boolean delName() { + return this.delMember(AuthorityURLInfo.name); + } + + /** + * Deletes the href + * + * @return true if removed + */ + public boolean delHref() { + return this.delMember(AuthorityURLInfo.href); + } + + /** + * Get the value of the AuthorityURLInfo member + * + * @param type + * @return the value of the AuthorityURLInfo member + */ + protected String getMember(AuthorityURLInfo type) { + Element el = this.getRoot().getChild(type.toString()); + if (el != null) + return el.getTextTrim(); + else + return null; + } + + /** + * Get the name + * + * @return + */ + public String getName() { + return this.getMember(AuthorityURLInfo.name); + } + + /** + * Get the href + * + * @return + */ + public String getHref() { + return this.getMember(AuthorityURLInfo.href); + } + +} \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoder.java new file mode 100644 index 0000000..23ca90c --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoder.java @@ -0,0 +1,215 @@ +/* +* GeoServer-Manager - Simple Manager Library for GeoServer +* +* Copyright (C) 2007,2011 GeoSolutions S.A.S. +* http://www.geo-solutions.it +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +package it.geosolutions.geoserver.rest.encoder.identifier; + +import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; + +import org.jdom.Element; +import org.jdom.filter.Filter; + +/** +* GSIdentifierInfoEncoder - encodes an Identifier for a given GeoServer +* layer as follows: +*
+* {@code
+* final GSIdentifierInfoEncoder ie = new GSIdentifierInfoEncoder();
+* ie.setAuthority("an authority");
+* ie.setIdentifier("an identifier");
+* }
+* 
+* For this example, the XML output is: +*
+* {@code
+* 
+*   an authority
+*   an identifier
+* 
+* }
+* 
+* +* @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | +* emmanuel.blondel@fao.org +* +*/ +public class GSIdentifierInfoEncoder extends XmlElement { + + /** + * A class to filter the Idenfiers by authority + * + * + */ + private static class filterByAuthority implements Filter { + + final private String key; + + public filterByAuthority(String authority) { + this.key = authority; + } + + private static final long serialVersionUID = 1L; + + public boolean matches(Object obj) { + Element el = ((Element) obj).getChild(IdentifierInfo.authority + .toString()); + if (el != null && el.getTextTrim().equals(key)) { + return true; + } + return false; + } + } + + /** + * Get a Filter using the IdentifierInfo authority + * + * @param authority + * @return the filter + */ + public static Filter getFilterByHref(String authority) { + return new filterByAuthority(authority); + } + + /** + * Constructs a new GSIdentifierInfoEncoder + * + */ + public GSIdentifierInfoEncoder() { + super("Identifier"); + } + + /** + * Constructs quickly an Identifier info + * + * @param authority + * (required) + * @param identifier + * (required) + */ + public GSIdentifierInfoEncoder(String authority, String identifier) { + super("Identifier"); + this.setup(authority, identifier); + } + + /** + * Set-up quickly an Identifier info + * + * @param authority + * @param identifier + */ + protected void setup(String authority, String identifier) { + set(IdentifierInfo.authority.name(), authority); + set(IdentifierInfo.identifier.name(), identifier); + } + + /** + * Set an IdentifierInfo member (authority, identifier) + * + * @param type + * @param value + */ + protected void setMember(IdentifierInfo type, String value) { + set(type.toString(), value); + } + + /** + * Set the authority + * + * @param authority + */ + public void setAuthority(String authority) { + this.setMember(IdentifierInfo.authority, authority); + } + + /** + * Set the identifier + * + * @param identifier + */ + public void setIdentifier(String identifier) { + this.setMember(IdentifierInfo.identifier, identifier); + } + + /** + * Deletes an IdentifierInfo member + * + * @param type + * @return true if the IdentifierInfo member is removed + */ + protected boolean delMember(IdentifierInfo type) { + return ElementUtils.remove(this.getRoot(), + this.getRoot().getChild(type.toString())); + } + + /** + * Deletes the authority + * + * @return true if removed + */ + public boolean delAuthority() { + return this.delMember(IdentifierInfo.authority); + } + + /** + * Deletes the identifier + * + * @return true if removed + */ + public boolean delIdentifier() { + return this.delMember(IdentifierInfo.identifier); + } + + /** + * Get the value of the IdentifierInfo member + * + * @param type + * @return the value of the IdentifierInfo member + */ + protected String getMember(IdentifierInfo type) { + Element el = this.getRoot().getChild(type.toString()); + if (el != null) + return el.getTextTrim(); + else + return null; + } + + /** + * Get the authority + * + * @return + */ + public String getAuthority() { + return this.getMember(IdentifierInfo.authority); + } + + /** + * Get the identifier + * + * @return + */ + public String getIdentifier() { + return this.getMember(IdentifierInfo.identifier); + } + +} \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/IdentifierInfo.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/IdentifierInfo.java new file mode 100644 index 0000000..63f4deb --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/IdentifierInfo.java @@ -0,0 +1,37 @@ +/* +* GeoServer-Manager - Simple Manager Library for GeoServer +* +* Copyright (C) 2007,2011 GeoSolutions S.A.S. +* http://www.geo-solutions.it +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +package it.geosolutions.geoserver.rest.encoder.identifier; + +/** +* Enumeration of layer authorityURL members +* +* @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | +* emmanuel.blondel@fao.org +* +*/ +public enum IdentifierInfo { + + authority, identifier; +} \ No newline at end of file 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/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java new file mode 100644 index 0000000..7962769 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java @@ -0,0 +1,98 @@ +package it.geosolutions.geoserver.decoder; + +import it.geosolutions.geoserver.rest.decoder.RESTLayer; +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; + +/** + * + * @author eblondel + * + */ +public class LayerDecoderTest { + + RESTLayer layer; + + @Before + public void setUp() throws IOException{ + File layerFile = new ClassPathResource("testdata/layerExample.xml").getFile(); + String layerString = FileUtils.readFileToString(layerFile); + layer = RESTLayer.build(layerString); + } + + @Test + public void testEnabled(){ + Assert.assertEquals(true, layer.getEnabled()); + } + + @Test + public void testQueryable(){ + Assert.assertEquals(true, layer.getQueryable()); + } + + @Test + public void testAdvertised(){ + Assert.assertEquals(true, layer.getAdvertised()); + } + + @Test + public void testName() { + Assert.assertEquals("tasmania_cities", layer.getName()); + } + + @Test + public void testTypeString() { + Assert.assertEquals("VECTOR", layer.getTypeString()); + } + + @Test + public void testType(){ + Assert.assertEquals(RESTLayer.Type.VECTOR, layer.getType()); + } + + @Test + public void testDefaultStyle() { + Assert.assertEquals("capitals", layer.getDefaultStyle()); + } + + @Test + public void testResourceUrl() { + Assert.assertEquals( + "http://localhost:8080/geoserver/rest/workspaces/topp/datastores/taz_shapes/featuretypes/tasmania_cities.xml", + layer.getResourceUrl()); + } + + @Test + public void testAuthorityURLs() { + List authorityURLs = layer + .getEncodedAuthorityURLInfoList(); + 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()); + } + +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java new file mode 100644 index 0000000..c88e351 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2007 - 2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * GPLv3 + Classpath exception + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package it.geosolutions.geoserver.rest.encoder; + +import java.util.ArrayList; +import java.util.List; + +import it.geosolutions.geoserver.rest.encoder.authorityurl.AuthorityURLInfo; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.IdentifierInfo; +import junit.framework.Assert; + +import org.jdom.Element; +import org.junit.Before; +import org.junit.Test; + +/** + * GSLayerEncoder21Test + * + * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | + * emmanuel.blondel@fao.org + */ +public class GSLayerEncoder21Test { + + GSLayerEncoder21 layerEncoder; + + @Before + public void setup() { + layerEncoder = new GSLayerEncoder21(); + layerEncoder.setAdvertised(true); + layerEncoder.addAuthorityURL(new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org")); + layerEncoder.addIdentifier(new GSIdentifierInfoEncoder("authority1", + "identifier1")); + layerEncoder.addAuthorityURL(new GSAuthorityURLInfoEncoder( + "authority2", "http://www.authority2.org")); + layerEncoder.addIdentifier(new GSIdentifierInfoEncoder("authority2", + "identifier2")); + } + + + @Test + public void testMetadata(){ + List metaElements = layerEncoder.getRoot().getChild("metadata").getChildren(); + for(Element el : metaElements){ + String key = el.getAttributeValue("key"); + + if(key.matches("advertised")){ + Assert.assertEquals(true, + Boolean.parseBoolean(el.getValue())); + + }else if(key.matches("authorityURLs")){ + String jsonStr = el.getValue(); + jsonStr = jsonStr.substring(2); + jsonStr = jsonStr.substring(0, + jsonStr.length() - 3); + + String[] items = jsonStr.split("\\}(,)\\{"); + + String[] props1 = items[0].split(","); + String[] kvp1_1 = props1[0].split("\":"); + String[] kvp1_2 = props1[1].split("\":"); + Assert.assertEquals(AuthorityURLInfo.name.name(), kvp1_1[0].replace("\"", "")); + Assert.assertEquals("authority1", kvp1_1[1].replace("\"", "")); + Assert.assertEquals(AuthorityURLInfo.href.name(), kvp1_2[0].replace("\"", "")); + Assert.assertEquals("http://www.authority1.org", kvp1_2[1].replace("\"", "")); + + String[] props2 = items[1].split(","); + String[] kvp2_1 = props2[0].split("\":"); + String[] kvp2_2 = props2[1].split("\":"); + Assert.assertEquals(AuthorityURLInfo.name.name(), kvp2_1[0].replace("\"", "")); + Assert.assertEquals("authority2", kvp2_1[1].replace("\"", "")); + Assert.assertEquals(AuthorityURLInfo.href.name(), kvp2_2[0].replace("\"", "")); + Assert.assertEquals("http://www.authority2.org", kvp2_2[1].replace("\"", "")); + + + }else if(key.matches("identifiers")){ + String jsonStr = el.getValue(); + jsonStr = jsonStr.substring(2); + jsonStr = jsonStr.substring(0, + jsonStr.length() - 3); + + String[] items = jsonStr.split("\\}(,)\\{"); + + String[] props1 = items[0].split(","); + String[] kvp1_1 = props1[0].split("\":"); + String[] kvp1_2 = props1[1].split("\":"); + Assert.assertEquals(IdentifierInfo.authority.name(), kvp1_1[0].replace("\"", "")); + Assert.assertEquals("authority2", kvp1_1[1].replace("\"", "")); + Assert.assertEquals(IdentifierInfo.identifier.name(), kvp1_2[0].replace("\"", "")); + Assert.assertEquals("identifier2", kvp1_2[1].replace("\"", "")); + + String[] props2 = items[1].split(","); + String[] kvp2_1 = props2[0].split("\":"); + String[] kvp2_2 = props2[1].split("\":"); + Assert.assertEquals(IdentifierInfo.authority.name(), kvp2_1[0].replace("\"", "")); + Assert.assertEquals("authority1", kvp2_1[1].replace("\"", "")); + Assert.assertEquals(IdentifierInfo.identifier.name(), kvp2_2[0].replace("\"", "")); + Assert.assertEquals("identifier1", kvp2_2[1].replace("\"", "")); + + } + } + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoderTest.java index 8774bb1..1d71e58 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoderTest.java @@ -19,6 +19,8 @@ */ package it.geosolutions.geoserver.rest.encoder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; import junit.framework.Assert; import org.jdom.Element; @@ -40,9 +42,16 @@ public class GSLayerEncoderTest { layerEncoder = new GSLayerEncoder(); layerEncoder.setEnabled(true); layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); + layerEncoder.setDefaultStyle("point"); layerEncoder.addStyle("additional_style1"); layerEncoder.addStyle("additional_style2"); + + layerEncoder.addAuthorityURL(new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org")); + layerEncoder.addIdentifier(new GSIdentifierInfoEncoder("authority1", + "identifier1")); } @Test @@ -55,6 +64,10 @@ public class GSLayerEncoderTest { true, Boolean.parseBoolean(layerEncoder.getRoot() .getChild("queryable").getValue())); + Assert.assertEquals( + true, + Boolean.parseBoolean(layerEncoder.getRoot() + .getChild("advertised").getValue())); } @Test @@ -81,5 +94,21 @@ public class GSLayerEncoderTest { Assert.assertEquals("additional_style2", ((Element) layerEncoder .getRoot().getChild("styles").getChildren().get(0)).getText()); } - + + @Test + public void testAuthorityURL(){ + Element el = (Element) layerEncoder.getRoot().getChild("authorityURLs") + .getChildren().get(0); + Assert.assertEquals("authority1", el.getChild("name").getValue()); + Assert.assertEquals("http://www.authority1.org", el.getChild("href") + .getValue()); + } + + @Test + public void testIdentifier(){ + Element el = (Element) layerEncoder.getRoot().getChild("identifiers") + .getChildren().get(0); + Assert.assertEquals("authority1", el.getChild("authority").getValue()); + Assert.assertEquals("identifier1", el.getChild("identifier").getValue()); + } } diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoderTest.java new file mode 100644 index 0000000..03803a8 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoderTest.java @@ -0,0 +1,49 @@ +/* +* Copyright (C) 2007 - 2011 GeoSolutions S.A.S. +* http://www.geo-solutions.it +* +* GPLv3 + Classpath exception +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ +package it.geosolutions.geoserver.rest.encoder.authorityurl; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * + * @author eblondel + * + */ +public class GSAuthorityURLInfoEncoderTest { + + @Test + public void authorityURLInfoTest() { + GSAuthorityURLInfoEncoder encoder = new GSAuthorityURLInfoEncoder(); + encoder.setup("authority1", "http://www.authority1.org"); + + Assert.assertEquals("authority1", encoder.getName()); + Assert.assertEquals("http://www.authority1.org", encoder.getHref()); + + Assert.assertTrue(encoder.delHref()); + Assert.assertNull(encoder.getHref()); + + encoder.setName("authority2"); + encoder.setHref("http://www.authority2.org"); + Assert.assertEquals("authority2", encoder.getName()); + Assert.assertEquals("http://www.authority2.org", encoder.getHref()); + } +} \ No newline at end of file diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java index 491bf0a..d67fd69 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java @@ -23,8 +23,12 @@ import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; import it.geosolutions.geoserver.rest.GeoserverRESTTest; import it.geosolutions.geoserver.rest.decoder.RESTLayer; import it.geosolutions.geoserver.rest.decoder.RESTResource; +import it.geosolutions.geoserver.rest.decoder.about.GSVersionDecoder; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; +import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder21; import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.Presentation; import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder; @@ -73,6 +77,7 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { String layerName = "cities"; GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder(); + fte.setNativeName(layerName); fte.setName(layerName + "_NEW"); fte.setTitle("title"); // fte.addKeyword("TODO"); @@ -86,38 +91,61 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { "http://www.organization.org/metadata1"); fte.addMetadataLinkInfo(metadatalink); - GSLayerEncoder layerEncoder = new GSLayerEncoder(); - layerEncoder.setEnabled(true); - layerEncoder.setQueryable(true); + GSLayerEncoder layerEncoder = null; + if (!GSVersionDecoder.VERSION.getVersion(VERSION).equals( + GSVersionDecoder.VERSION.UNRECOGNIZED)) { + layerEncoder = new GSLayerEncoder(); + } else if (GSVersionDecoder.VERSION.getVersion(VERSION).equals( + GSVersionDecoder.VERSION.UNRECOGNIZED)) { + layerEncoder = new GSLayerEncoder21(); + } + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); - layerEncoder.setDefaultStyle("point"); - layerEncoder.addStyle("point2"); - layerEncoder.addStyle("point3"); + layerEncoder.setDefaultStyle("point"); + layerEncoder.addStyle("point2"); + layerEncoder.addStyle("point3"); - publisher.createWorkspace(DEFAULT_WS); + // authorityURL + GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org"); + layerEncoder.addAuthorityURL(authorityURL); - File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + // identifier + GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( + "authority1", "identifier1"); + layerEncoder.addIdentifier(identifier); - // test insert - boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile); - assertTrue("publish() failed", published); - assertTrue(existsLayer(layerName)); + publisher.createWorkspace(DEFAULT_WS); - publisher.publishStyle(new File(new ClassPathResource("testdata").getFile(), - "default_point.sld")); + File zipFile = new ClassPathResource("testdata/resttestshp.zip") + .getFile(); - // optionally select the attributes to publish - RESTLayer layer = reader.getLayer(layerName); - RESTResource resource = reader.getResource(layer); - List attrs = resource.getEncodedAttributeList(); - assertNotNull(attrs); - for (GSAttributeEncoder enc : attrs) { - fte.setAttribute(enc); - } + // test insert + boolean published = publisher.publishShp(DEFAULT_WS, storeName, + layerName, zipFile); + assertTrue("publish() failed", published); + assertTrue(existsLayer(layerName)); + + publisher.publishStyle(new File(new ClassPathResource("testdata") + .getFile(), "default_point.sld")); + + // optionally select the attributes to publish + RESTLayer layer = reader.getLayer(layerName); + RESTResource resource = reader.getResource(layer); + List attrs = resource.getEncodedAttributeList(); + assertNotNull(attrs); + for (GSAttributeEncoder enc : attrs) { + fte.setAttribute(enc); + } + + assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, + layerEncoder)); - assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); } + @Test public void testFeatureTypeEncoder() { diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoderTest.java new file mode 100644 index 0000000..18a2e00 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoderTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2007 - 2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * GPLv3 + Classpath exception + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package it.geosolutions.geoserver.rest.encoder.identifier; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * + * @author eblondel + * + */ +public class GSIdentifierInfoEncoderTest { + + @Test + public void identifierInfoTest() { + GSIdentifierInfoEncoder encoder = new GSIdentifierInfoEncoder(); + encoder.setup("authority1", "identifier1"); + + Assert.assertEquals("authority1", encoder.getAuthority()); + Assert.assertEquals("identifier1", encoder.getIdentifier()); + + Assert.assertTrue(encoder.delIdentifier()); + Assert.assertNull(encoder.getIdentifier()); + + encoder.setAuthority("authority2"); + encoder.setIdentifier("identifier2"); + Assert.assertEquals("authority2", encoder.getAuthority()); + Assert.assertEquals("identifier2", encoder.getIdentifier()); + + } +} \ No newline at end of file diff --git a/src/test/resources/testdata/layerExample.xml b/src/test/resources/testdata/layerExample.xml new file mode 100644 index 0000000..af98cfa --- /dev/null +++ b/src/test/resources/testdata/layerExample.xml @@ -0,0 +1,40 @@ + + tasmania_cities + / + VECTOR + + capitals + + + + tasmania_cities + + + true + true + true + + 0 + 0 + + + + authority1 + http://www.authority1.org + + + authority2 + http://www.authority2.org + + + + + authority1 + identifier1 + + + authority2 + identifier2 + + + \ 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