#62 MetadataLink support (1.5.x branch) - add encoder/decoder & tests

This commit is contained in:
eblondel 2013-04-11 19:40:18 +02:00
parent a96ecf4f1c
commit 54df100ea6
8 changed files with 404 additions and 9 deletions

View File

@ -28,6 +28,8 @@ package it.geosolutions.geoserver.rest.decoder;
import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder;
import it.geosolutions.geoserver.rest.encoder.feature.FeatureTypeAttribute;
import it.geosolutions.geoserver.rest.encoder.feature.GSAttributeEncoder;
import it.geosolutions.geoserver.rest.encoder.metadatalink.GSMetadataLinkInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.metadatalink.ResourceMetadataLinkInfo;
import java.util.ArrayList;
import java.util.HashMap;
@ -154,6 +156,62 @@ public class RESTResource {
return attrsList;
}
/**
* Decodes the list of MetadataLinkInfo from the GeoServer Resource
*
* @return the list of Map<ResourceMetadataLinkInfo,String>
*/
public List<Map<ResourceMetadataLinkInfo, String>> getMetadataLinkInfoList() {
List<Map<ResourceMetadataLinkInfo, String>> metaLinksList = null;
final Element metaLinksRoot = rootElem.getChild("metadataLinks");
final List<Element> metaLinks = metaLinksRoot.getChildren();
if (metaLinks != null) {
metaLinksList = new ArrayList<Map<ResourceMetadataLinkInfo, String>>(
metaLinks.size());
for (Element metaLink : metaLinks) {
Map<ResourceMetadataLinkInfo, String> metaLinkMap = new HashMap<ResourceMetadataLinkInfo, String>();
metaLinksList.add(metaLinkMap);
for (ResourceMetadataLinkInfo rmd : ResourceMetadataLinkInfo
.values()) {
String key = rmd.toString();
metaLinkMap.put(rmd, metaLink.getChildText(key));
}
}
}
return metaLinksList;
}
/**
* Decodes the list of MetadataLinkInfo from the GeoServer Resource
*
* @return the list of GSMetadataLinkEncoder
*/
public List<GSMetadataLinkInfoEncoder> getEncodedMetadataLinkInfoList() {
List<GSMetadataLinkInfoEncoder> metaLinksList = null;
final Element metaLinksRoot = rootElem.getChild("metadataLinks");
final List<Element> metaLinks = metaLinksRoot.getChildren();
if (metaLinks != null) {
metaLinksList = new ArrayList<GSMetadataLinkInfoEncoder>(
metaLinks.size());
for (Element metaLink : metaLinks) {
final GSMetadataLinkInfoEncoder metaLinkEnc = new GSMetadataLinkInfoEncoder();
for (ResourceMetadataLinkInfo rmd : ResourceMetadataLinkInfo
.values()) {
String key = rmd.toString();
metaLinkEnc.setMetadataLinkInfoMember(rmd,
metaLink.getChildText(key)); // change
}
metaLinksList.add(metaLinkEnc);
}
}
return metaLinksList;
}
// /**
// * @return the list of available attribute names
// */

View File

@ -29,6 +29,7 @@ import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.metadatalink.GSMetadataLinkInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils;
import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder;
import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
@ -52,9 +53,11 @@ public abstract class GSResourceEncoder
public final static String NAME = "name";
public final static String METADATA="metadata";
public final static String KEYWORDS="keywords";
public final static String METADATALINKS="metadataLinks";
final private GSMetadataEncoder metadata = new GSMetadataEncoder();
final private Element keywordsListEncoder = new Element(KEYWORDS);
final private Element metadataLinksListEncoder = new Element(METADATALINKS);
private class GSMetadataEncoder extends NestedElementEncoder{
public GSMetadataEncoder() {
@ -75,16 +78,13 @@ public abstract class GSResourceEncoder
// Link members to the parent
addContent(metadata.getRoot());
addContent(keywordsListEncoder);
addContent(metadataLinksListEncoder);
}
public void setEnabled(boolean enabled) {
set("enabled", (enabled) ? "true" : "false");
}
// TODO MetadataLink
// public void setMetadata(String key, String url){
// metadata.set(key, url);
// }
/**
* @param key
@ -136,6 +136,46 @@ public abstract class GSResourceEncoder
})).size() == 0 ? false : true;
}
/**
* Adds a MetadataLinkInfo to the GeoServer Resource
*
* @param MetadataLink
*/
public void addMetadataLinkInfo(GSMetadataLinkInfoEncoder metadataLinkInfo) {
metadataLinksListEncoder.addContent(metadataLinkInfo.getRoot());
}
/**
* Adds quickly a MetadataLinkInfo to the GeoServer Resource
*
* @param type
* @param metadataType
* @param content
*/
public void addMetadataLinkInfo(String type, String metadataType,
String content) {
final GSMetadataLinkInfoEncoder mde = new GSMetadataLinkInfoEncoder();
mde.setup(type, metadataType, content);
metadataLinksListEncoder.addContent(mde.getRoot());
}
/**
* Deletes a metadataLinkInfo from the list using the metadataURL
* (MetadataLinkInfo content)
*
* @param metadataURL
* @return true if something is removed, false otherwise
*/
public boolean delMetadataLinkInfo(final String metadataURL) {
return (metadataLinksListEncoder
.removeContent(GSMetadataLinkInfoEncoder
.getFilterByContent(metadataURL))).size() == 0 ? false
: true;
}
/**
* Reprojection policy for a published layer. One of:
* <ul>

View File

@ -0,0 +1,164 @@
/*
* 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.metadatalink;
import java.util.Map;
import java.util.Map.Entry;
import org.jdom.Element;
import org.jdom.filter.Filter;
import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils;
import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
/**
* GSMetadataLinkEncoder - encodes a metadataLink for a given GeoServer Resource
* (feature type /coverage), as follows:
* <pre>
* {@code
* final GSMetadataLinkInfoEncoder mde = new GSMetadataLinkInfoEncoder();
* mde.setup("text/xml", "ISO19115:2003","http://www.organization.org/metadata");
* }
* </pre>
* For this example, the XML output is:
* <pre>
* {@code
* <metadataLink>
* <type>text/xml</type>
* <metadataType>ISO19115:2003</metadataType>
* <content>http://www.organization.org/metadata</content>
* </metadataLink>
* }
* </pre>
*
* @author Emmanuel Blondel - emmanuel.blondel1@gmail.com |
* emmanuel.blondel@fao.org
*
*/
public class GSMetadataLinkInfoEncoder extends PropertyXMLEncoder {
/** A class to filter the MetadataLinkInfo by content
*
*
*/
public static class filterByContent implements Filter {
final private String key;
public filterByContent(String content) {
this.key = content;
}
private static final long serialVersionUID = 1L;
public boolean matches(Object obj) {
Element el = ((Element) obj)
.getChild(ResourceMetadataLinkInfo.content.toString());
if (el != null && el.getTextTrim().equals(key)) {
return true;
}
return false;
}
}
/**
* Get a Filter using the MetadataLinkInfo content (metadataURL)
*
* @param content
* @return the filter
*/
public static Filter getFilterByContent(String content) {
return new filterByContent(content);
}
/**
* Constructs a new GSMetadataLinkInfoEncoder
*
*/
public GSMetadataLinkInfoEncoder() {
super("metadataLink");
}
/**
* Set-up quickly a metadataLinkInfo
*
* @param type
* @param metadataType
* @param content
*/
public void setup(String type, String metadataType, String content) {
set(ResourceMetadataLinkInfo.type.name(), type);
set(ResourceMetadataLinkInfo.metadataType.name(), metadataType);
set(ResourceMetadataLinkInfo.content.name(), content);
}
/**
* Set-up a metadataLinkInfo
*
* @param metadataLinkInfos
*/
public void setup(Map<ResourceMetadataLinkInfo, String> metadataLinkInfos) {
for (Entry<ResourceMetadataLinkInfo, String> mdLinkInfo : metadataLinkInfos
.entrySet()) {
set(mdLinkInfo.getKey().toString(), mdLinkInfo.getValue());
}
}
/**
* Set a MetadataLinkInfo member (type, metadataType or content)
*
* @param type
* @param value
*/
public void setMetadataLinkInfoMember(ResourceMetadataLinkInfo type,
String value) {
set(type.toString(), value);
}
/**
* Deletes a MetadataLinkInfo member
*
* @param type
* @return true if the metadataLinkInfo member is removed
*/
public boolean delMetadataLinkInfoMember(ResourceMetadataLinkInfo type) {
return ElementUtils.remove(this.getRoot(), get(type.toString()));
}
/**
* Get the value of the MetadataLinkInfo member
*
* @param type
* @return the value of the MetadataLinkInfo member
*/
public String getMetadataLinkInfoMember(ResourceMetadataLinkInfo type) {
Element el = get(type.toString());
if (el != null)
return el.getTextTrim();
else
return null;
}
}

View File

@ -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.metadatalink;
/**
* Enumeration of featureType metadataLink member
*
* @author Emmanuel Blondel - emmanuel.blondel1@gmail.com |
* emmanuel.blondel@fao.org
*
*/
public enum ResourceMetadataLinkInfo {
type, metadataType, content
}

View File

@ -23,6 +23,8 @@ package it.geosolutions.geoserver.decoder;
import it.geosolutions.geoserver.rest.decoder.RESTCoverage;
import it.geosolutions.geoserver.rest.decoder.RESTDimensionInfo;
import it.geosolutions.geoserver.rest.encoder.metadatalink.GSMetadataLinkInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.metadatalink.ResourceMetadataLinkInfo;
import java.io.File;
import java.io.IOException;
@ -65,10 +67,25 @@ public class MetadataDecoderTest {
Assert.assertEquals(el.isEnabled(),true);
}
}
}
@Test
public void testMetadataLinkInfo() throws IOException{
File coverageFile = new ClassPathResource("testdata/coverageExample.xml").getFile();
String coverageString = FileUtils.readFileToString(coverageFile);
RESTCoverage coverage = RESTCoverage.build(coverageString);
List<GSMetadataLinkInfoEncoder> list = coverage.getEncodedMetadataLinkInfoList();
GSMetadataLinkInfoEncoder metadataLinkInfo1 = list.get(0);
Assert.assertEquals(metadataLinkInfo1.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.type),"text/xml");
Assert.assertEquals(metadataLinkInfo1.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.metadataType),"ISO19115:2003");
Assert.assertEquals(metadataLinkInfo1.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.content),"http://www.organization.org/metadata1");
GSMetadataLinkInfoEncoder metadataLinkInfo2 = list.get(1);
Assert.assertEquals(metadataLinkInfo2.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.type),"text/html");
Assert.assertEquals(metadataLinkInfo2.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.metadataType),"ISO19115:2003");
Assert.assertEquals(metadataLinkInfo2.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.content),"http://www.organization.org/metadata2");
}
}

View File

@ -28,6 +28,7 @@ import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.Presentation;
import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.PresentationDiscrete;
import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.metadatalink.GSMetadataLinkInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils;
import it.geosolutions.geoserver.rest.publisher.GeoserverRESTPublisherTest;
@ -70,6 +71,11 @@ public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest {
fte.setNativeCRS("EPSG:4326");
fte.setDescription("desc");
fte.setEnabled(true);
//metadataLink
GSMetadataLinkInfoEncoder metadatalink = new GSMetadataLinkInfoEncoder();
metadatalink.setup("text/xml", "ISO19115:2003","http://www.organization.org/metadata1");
fte.addMetadataLinkInfo(metadatalink);
GSLayerEncoder layerEncoder = new GSLayerEncoder();
layerEncoder.setEnabled(true);
@ -187,6 +193,14 @@ public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest {
Assert.assertTrue(encoder.delKeyword("KEYWORD_2"));
Assert.assertFalse(encoder.delKeyword("KEYWORD_M"));
//metadataLinkInfo
encoder.addMetadataLinkInfo("text/xml", "ISO19115:2003","http://www.organization.org/metadata1");
encoder.addMetadataLinkInfo("text/html", "ISO19115:2003","http://www.organization.org/metadata2");
Assert.assertTrue(encoder.delMetadataLinkInfo("http://www.organization.org/metadata2"));
Assert.assertFalse(encoder.delMetadataLinkInfo("http://www.organization.org/metadata3"));
//dimensions
final GSFeatureDimensionInfoEncoder elevationDimension = new GSFeatureDimensionInfoEncoder(
"elevation_field");

View File

@ -0,0 +1,53 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.geosolutions.geoserver.rest.encoder.metadatalink;
import junit.framework.Assert;
import org.junit.Test;
/**
*
* @author eblondel
*
*/
public class GSMetadataLinkInfoEncoderTest {
@Test
public void metadataLinkInfoTest(){
GSMetadataLinkInfoEncoder encoder = new GSMetadataLinkInfoEncoder();
encoder.setup("text/xml", "ISO19115:2003","http://www.organization.org/metadata1");
Assert.assertEquals("text/xml", encoder.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.type));
Assert.assertEquals("ISO19115:2003", encoder.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.metadataType));
Assert.assertEquals("http://www.organization.org/metadata1", encoder.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.content));
Assert.assertTrue(encoder.delMetadataLinkInfoMember(ResourceMetadataLinkInfo.content));
Assert.assertNull(encoder.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.content));
encoder.setMetadataLinkInfoMember(ResourceMetadataLinkInfo.type, "text/html");
encoder.setMetadataLinkInfoMember(ResourceMetadataLinkInfo.metadataType, "FGDC");
encoder.setMetadataLinkInfoMember(ResourceMetadataLinkInfo.content, "http://www.organization.org/metadata2");
Assert.assertEquals("text/html", encoder.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.type));
Assert.assertEquals("FGDC", encoder.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.metadataType));
Assert.assertEquals("http://www.organization.org/metadata2", encoder.getMetadataLinkInfoMember(ResourceMetadataLinkInfo.content));
}
}

View File

@ -7,6 +7,18 @@
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/namespaces/topp.xml" type="application/xml"/>
</namespace>
<title>granuleTestMosaic</title>
<metadataLinks>
<metadataLink>
<type>text/xml</type>
<metadataType>ISO19115:2003</metadataType>
<content>http://www.organization.org/metadata1</content>
</metadataLink>
<metadataLink>
<type>text/html</type>
<metadataType>ISO19115:2003</metadataType>
<content>http://www.organization.org/metadata2</content>
</metadataLink>
</metadataLinks>
<nativeCRS>GEOGCS[&quot;WGS 84&quot;, &#xd;
DATUM[&quot;World Geodetic System 1984&quot;, &#xd;
SPHEROID[&quot;WGS 84&quot;, 6378137.0, 298.257223563, AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]], &#xd;