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/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..11ebef6 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java @@ -0,0 +1,95 @@ +/* + * 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.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")); + } + + + @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 content = el.getValue(); + content = content.substring(2); + content = content.substring(0, content.length()-2); + String[] props = content.split(","); + for(String prop : props){ + String[] kvp = prop.split(":"); + if(kvp[0].matches(AuthorityURLInfo.name.name())){ + Assert.assertEquals("authority1", kvp[1]); + }else if(kvp[0].matches(AuthorityURLInfo.href.name())){ + Assert.assertEquals("http://www.authority1.org", kvp[1]); + } + } + + }else if(key.matches("identifiers")){ + String content = el.getValue(); + content = content.substring(2); + content = content.substring(0, content.length()-2); + String[] props = content.split(","); + for(String prop : props){ + String[] kvp = prop.split(":"); + if(kvp[0].matches(IdentifierInfo.authority.name())){ + Assert.assertEquals("authority1", kvp[1]); + }else if(kvp[0].matches(IdentifierInfo.identifier.name())){ + Assert.assertEquals("identifier1", kvp[1]); + } + } + } + } + } +} 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 d4ee4f2..1da7fc8 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 @@ -24,6 +24,7 @@ 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.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; @@ -133,6 +134,64 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); } + + @Test + public void testIntegration_21() throws IOException { + + if (!enabled()) + return; + deleteAll(); + + GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW); + + String storeName = "resttestshp"; + String layerName = "cities"; + + GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder(); + fte.setNativeName(layerName); + fte.setName(layerName + "_NEW2"); + fte.setTitle("title"); + fte.setNativeCRS("EPSG:4326"); + fte.setDescription("desc"); + fte.setEnabled(true); + + //metadataLink + GSMetadataLinkInfoEncoder metadatalink = new GSMetadataLinkInfoEncoder( + "text/xml", "ISO19115:2003", + "http://www.organization.org/metadata1"); + fte.addMetadataLinkInfo(metadatalink); + + //use of GSLayerEncoder specific to GS 2.1 + GSLayerEncoder21 layerEncoder = new GSLayerEncoder21(); + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); + + layerEncoder.setDefaultStyle("point"); + layerEncoder.addStyle("point2"); + layerEncoder.addStyle("point3"); + + // authorityURL + GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org"); + GSAuthorityURLInfoEncoder authorityURL2 = new GSAuthorityURLInfoEncoder( + "authority2", "http://www.authority2.org"); + layerEncoder.addAuthorityURL(authorityURL); + layerEncoder.addAuthorityURL(authorityURL2); + + // identifier + GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( + "authority1", "identifier1"); + GSIdentifierInfoEncoder identifier2 = new GSIdentifierInfoEncoder( + "authority2", "identifier2"); + layerEncoder.addIdentifier(identifier); + layerEncoder.addIdentifier(identifier2); + + publisher.createWorkspace(DEFAULT_WS); + assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); + } + + @Test public void testFeatureTypeEncoder() {