Added method for creating PostGIS datastores (with some code from E.Grosso).
Improved encoding of lists inside xml payloads.
This commit is contained in:
parent
629e622b46
commit
434b9ec61c
14
gitignore
Normal file
14
gitignore
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
##
|
||||||
|
## This file has been automatically generated with the command line:
|
||||||
|
## for pom in $(find -name "pom.xml") ; do path=${pom%*pom.xml} ; echo '#' $path ; echo ; echo ${path}target ; echo ; done > .gitignore
|
||||||
|
##
|
||||||
|
## - ETj
|
||||||
|
|
||||||
|
nb*.xml
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
# ./
|
||||||
|
|
||||||
|
./target
|
||||||
|
|
||||||
@ -22,7 +22,6 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package it.geosolutions.geoserver.rest;
|
package it.geosolutions.geoserver.rest;
|
||||||
|
|
||||||
import it.geosolutions.geoserver.rest.decoder.RESTCoverageList;
|
import it.geosolutions.geoserver.rest.decoder.RESTCoverageList;
|
||||||
@ -30,6 +29,7 @@ import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore;
|
|||||||
import it.geosolutions.geoserver.rest.encoder.GSCoverageEncoder;
|
import it.geosolutions.geoserver.rest.encoder.GSCoverageEncoder;
|
||||||
import it.geosolutions.geoserver.rest.encoder.GSFeatureTypeEncoder;
|
import it.geosolutions.geoserver.rest.encoder.GSFeatureTypeEncoder;
|
||||||
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
|
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
|
||||||
|
import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder;
|
||||||
import it.geosolutions.geoserver.rest.encoder.GSWorkspaceEncoder;
|
import it.geosolutions.geoserver.rest.encoder.GSWorkspaceEncoder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -68,7 +68,6 @@ public class GeoServerRESTPublisher {
|
|||||||
this.gspass = password;
|
this.gspass = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//==========================================================================
|
//==========================================================================
|
||||||
//=== WORKSPACES
|
//=== WORKSPACES
|
||||||
//==========================================================================
|
//==========================================================================
|
||||||
@ -139,8 +138,9 @@ public class GeoServerRESTPublisher {
|
|||||||
*/
|
*/
|
||||||
public boolean publishStyle(File sldFile, String name) {
|
public boolean publishStyle(File sldFile, String name) {
|
||||||
String sUrl = restURL + "/rest/styles";
|
String sUrl = restURL + "/rest/styles";
|
||||||
if(name != null)
|
if (name != null) {
|
||||||
sUrl += "?name=" + encode(name);
|
sUrl += "?name=" + encode(name);
|
||||||
|
}
|
||||||
LOGGER.debug("POSTing new style " + name + " to " + sUrl);
|
LOGGER.debug("POSTing new style " + name + " to " + sUrl);
|
||||||
String result = HTTPUtils.post(sUrl, sldFile, "application/vnd.ogc.sld+xml", gsuser, gspass);
|
String result = HTTPUtils.post(sUrl, sldFile, "application/vnd.ogc.sld+xml", gsuser, gspass);
|
||||||
return result != null;
|
return result != null;
|
||||||
@ -162,6 +162,25 @@ public class GeoServerRESTPublisher {
|
|||||||
return HTTPUtils.delete(sUrl, gsuser, gspass);
|
return HTTPUtils.delete(sUrl, gsuser, gspass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//==========================================================================
|
||||||
|
//=== DATASTORE
|
||||||
|
//==========================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a PostGIS datastore.
|
||||||
|
*
|
||||||
|
* @param workspace Name of the workspace to contain the database. This will also be the prefix of any layer names created from tables in the database.
|
||||||
|
* @param datastoreEncoder the set of parameters to be set to the datastore (including connection params)
|
||||||
|
*
|
||||||
|
* @return <TT>true</TT> if the PostGIS datastore has been successfully created, <TT>false</TT> otherwise
|
||||||
|
*/
|
||||||
|
public boolean createPostGISDatastore(String workspace, GSPostGISDatastoreEncoder datastoreEncoder) {
|
||||||
|
String sUrl = restURL + "/rest/workspaces/" + workspace + "/datastores/";
|
||||||
|
String xml = datastoreEncoder.encodeXml();
|
||||||
|
String result = HTTPUtils.postXml(sUrl, xml, gsuser, gspass);
|
||||||
|
return result != null;
|
||||||
|
}
|
||||||
|
|
||||||
//==========================================================================
|
//==========================================================================
|
||||||
//=== SHAPEFILES
|
//=== SHAPEFILES
|
||||||
//==========================================================================
|
//==========================================================================
|
||||||
@ -201,6 +220,7 @@ public class GeoServerRESTPublisher {
|
|||||||
layerEncoder.setDefaultStyle(defaultStyle);
|
layerEncoder.setDefaultStyle(defaultStyle);
|
||||||
configureLayer(layerEncoder, layerName);
|
configureLayer(layerEncoder, layerName);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("Error in publishing shapefile " + e.getMessage(), e);
|
||||||
sent = false;
|
sent = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,7 +246,10 @@ public class GeoServerRESTPublisher {
|
|||||||
*/
|
*/
|
||||||
public boolean publishShp(String workspace, String storename, String layername, File zipFile, String srs) throws FileNotFoundException {
|
public boolean publishShp(String workspace, String storename, String layername, File zipFile, String srs) throws FileNotFoundException {
|
||||||
// build full URL
|
// build full URL
|
||||||
StringBuilder sbUrl = new StringBuilder(restURL).append("/rest/workspaces/").append(workspace).append("/datastores/").append(storename).append("/file.shp?");
|
StringBuilder sbUrl = new StringBuilder(restURL)
|
||||||
|
.append("/rest/workspaces/").append(workspace)
|
||||||
|
.append("/datastores/").append(storename)
|
||||||
|
.append("/file.shp?");
|
||||||
// if (workspace != null) {
|
// if (workspace != null) {
|
||||||
// sbUrl.append("namespace=").append(workspace);
|
// sbUrl.append("namespace=").append(workspace);
|
||||||
// }
|
// }
|
||||||
@ -458,14 +481,16 @@ public class GeoServerRESTPublisher {
|
|||||||
RESTCoverageStore store = configureExternaMosaicDatastore(workspace, storeName, mosaicDir);
|
RESTCoverageStore store = configureExternaMosaicDatastore(workspace, storeName, mosaicDir);
|
||||||
|
|
||||||
if (coverageEncoder == null) {
|
if (coverageEncoder == null) {
|
||||||
if(LOGGER.isDebugEnabled())
|
if (LOGGER.isDebugEnabled()) {
|
||||||
LOGGER.debug("no coverageEncoder provided for " + workspace + ":" + storeName);
|
LOGGER.debug("no coverageEncoder provided for " + workspace + ":" + storeName);
|
||||||
|
}
|
||||||
coverageEncoder = new GSCoverageEncoder();
|
coverageEncoder = new GSCoverageEncoder();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (layerEncoder == null) {
|
if (layerEncoder == null) {
|
||||||
if(LOGGER.isDebugEnabled())
|
if (LOGGER.isDebugEnabled()) {
|
||||||
LOGGER.debug("no layerEncoder provided for " + workspace + ":" + storeName);
|
LOGGER.debug("no layerEncoder provided for " + workspace + ":" + storeName);
|
||||||
|
}
|
||||||
layerEncoder = new GSLayerEncoder();
|
layerEncoder = new GSLayerEncoder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -722,13 +747,13 @@ public class GeoServerRESTPublisher {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected String sanitize(String s) {
|
protected String sanitize(String s) {
|
||||||
if(s.indexOf(".")!=-1)
|
if (s.indexOf(".") != -1) {
|
||||||
return s + ".DUMMY";
|
return s + ".DUMMY";
|
||||||
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String encode(String s) {
|
protected String encode(String s) {
|
||||||
return URLEncoder.encode(s);
|
return URLEncoder.encode(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
<html><head></head><body>
|
<html><head></head><body>
|
||||||
Some util classes.
|
Some util classes for decoders.
|
||||||
</body></html>
|
</body></html>
|
||||||
@ -25,17 +25,20 @@
|
|||||||
|
|
||||||
package it.geosolutions.geoserver.rest.encoder;
|
package it.geosolutions.geoserver.rest.encoder;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import it.geosolutions.geoserver.rest.encoder.utils.EntryKeyListEncoder;
|
||||||
import java.util.Map;
|
import it.geosolutions.geoserver.rest.encoder.utils.TextNodeListEncoder;
|
||||||
import org.jdom.Element;
|
import org.jdom.Element;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Creates an XML
|
||||||
*
|
*
|
||||||
* @author ETj (etj at geo-solutions.it)
|
* @author ETj (etj at geo-solutions.it)
|
||||||
*/
|
*/
|
||||||
public class GSCoverageEncoder extends GSResourceEncoder {
|
public class GSCoverageEncoder extends GSResourceEncoder {
|
||||||
|
|
||||||
private Map<String, String> metadata = new HashMap<String, String>();
|
private EntryKeyListEncoder metadataEncoder = new EntryKeyListEncoder("metadata");
|
||||||
|
private TextNodeListEncoder keywordsListEncoder = new TextNodeListEncoder("keywords");
|
||||||
|
|
||||||
|
|
||||||
public GSCoverageEncoder() {
|
public GSCoverageEncoder() {
|
||||||
super("coverage");
|
super("coverage");
|
||||||
@ -43,21 +46,17 @@ public class GSCoverageEncoder extends GSResourceEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addMetadata(String key, String value) {
|
public void addMetadata(String key, String value) {
|
||||||
metadata.put(key, value);
|
metadataEncoder.add(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addKeyword(String keyword) {
|
||||||
|
keywordsListEncoder.add("string", keyword);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void addNodesBeforeOutput(Element e) {
|
protected void addNodesBeforeOutput(Element e) {
|
||||||
super.addNodesBeforeOutput(e);
|
super.addNodesBeforeOutput(e);
|
||||||
|
keywordsListEncoder.attachList(e);
|
||||||
if( ! metadata.isEmpty() ) {
|
metadataEncoder.attachList(e);
|
||||||
Element md = new Element("metadata");
|
|
||||||
for (Map.Entry<String, String> entry : metadata.entrySet()) {
|
|
||||||
md.addContent("entry")
|
|
||||||
.setAttribute("key", entry.getKey())
|
|
||||||
.setText(entry.getValue());
|
|
||||||
}
|
|
||||||
e.addContent(md);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,8 @@
|
|||||||
|
|
||||||
package it.geosolutions.geoserver.rest.encoder;
|
package it.geosolutions.geoserver.rest.encoder;
|
||||||
|
|
||||||
|
import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author ETj (etj at geo-solutions.it)
|
* @author ETj (etj at geo-solutions.it)
|
||||||
|
|||||||
@ -0,0 +1,170 @@
|
|||||||
|
/*
|
||||||
|
* 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 it.geosolutions.geoserver.rest.encoder.utils.EntryKeyListEncoder;
|
||||||
|
import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
|
||||||
|
import org.jdom.Element;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Geoserver datastore XML encoder.
|
||||||
|
*
|
||||||
|
* @author Eric Grosso
|
||||||
|
* @author ETj
|
||||||
|
*/
|
||||||
|
public class GSPostGISDatastoreEncoder extends PropertyXMLEncoder {
|
||||||
|
|
||||||
|
private EntryKeyListEncoder connectionParameters = new EntryKeyListEncoder("connectionParameters");
|
||||||
|
|
||||||
|
|
||||||
|
public GSPostGISDatastoreEncoder() {
|
||||||
|
super("dataStore");
|
||||||
|
setType("PostGIS"); // may be overwritten with e.g. "PostGIS (JNDI)"
|
||||||
|
setDatabaseType("postgis");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set some initial defaults.
|
||||||
|
* <br/><br/>
|
||||||
|
* The default parameters are as follows: <ul>
|
||||||
|
* <li>maximum connections: 10, </li>
|
||||||
|
* <li>minimum connections: 1,</li>
|
||||||
|
* <li>fetch size: 1000, </li>
|
||||||
|
* <li>connection timeout: 20 seconds, </li>
|
||||||
|
* <li>loose BBox: true, </li>
|
||||||
|
* <li>prepared statements: false,</li>
|
||||||
|
* <li>maximum open prepared statements: 50. </li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
public void defaultInit() {
|
||||||
|
setMinConnections(1);
|
||||||
|
setMaxConnections(10);
|
||||||
|
setFetchSize(1000);
|
||||||
|
setConnectionTimeout(20);
|
||||||
|
setLooseBBox(true);
|
||||||
|
setPreparedStatements(false);
|
||||||
|
setMaxOpenPreparedStatements(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
setOrRemove("name", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
setOrRemove("description", description);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
setOrRemove("type", type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
setOrRemove("enabled", Boolean.toString(enabled));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNamespace(String namespace) {
|
||||||
|
connectionParameters.add("namespace", namespace);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
connectionParameters.add("host", host);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
connectionParameters.add("port", Integer.toString(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDatabase(String database) {
|
||||||
|
connectionParameters.add("database", database);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSchema(String schema) {
|
||||||
|
connectionParameters.add("schema", schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(String user) {
|
||||||
|
connectionParameters.add("user", user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
connectionParameters.add("passwd", password);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDatabaseType(String dbtype) {
|
||||||
|
connectionParameters.add("dbtype", dbtype);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJndiReferenceName(String jndiReferenceName) {
|
||||||
|
connectionParameters.add("jndiReferenceName", jndiReferenceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExposePrimaryKeys(boolean exposePrimaryKeys) {
|
||||||
|
connectionParameters.add("Expose primary keys", Boolean.toString(exposePrimaryKeys));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxConnections(int maxConnections) {
|
||||||
|
connectionParameters.add("max connections", Integer.toString(maxConnections));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMinConnections(int minConnections) {
|
||||||
|
connectionParameters.add("min connections", Integer.toString(minConnections));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFetchSize(int fetchSize) {
|
||||||
|
connectionParameters.add("fetch size", Integer.toString(fetchSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnectionTimeout(int seconds) {
|
||||||
|
connectionParameters.add("Connection timeout", Integer.toString(seconds));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValidateConnections(boolean validateConnections) {
|
||||||
|
connectionParameters.add("validate connections", Boolean.toString(validateConnections));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrimaryKeyMetadataTable(String primaryKeyMetadataTable) {
|
||||||
|
connectionParameters.add("Primary key metadata table", primaryKeyMetadataTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLooseBBox(boolean looseBBox) {
|
||||||
|
connectionParameters.add("Loose bbox", Boolean.toString(looseBBox));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPreparedStatements(boolean preparedStatements) {
|
||||||
|
connectionParameters.add("preparedStatements", Boolean.toString(preparedStatements));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxOpenPreparedStatements(int maxOpenPreparedStatements) {
|
||||||
|
connectionParameters.add("Max open prepared statements", Integer.toString(maxOpenPreparedStatements));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void addNodesBeforeOutput(Element e) {
|
||||||
|
super.addNodesBeforeOutput(e);
|
||||||
|
connectionParameters.attachList(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -25,6 +25,8 @@
|
|||||||
|
|
||||||
package it.geosolutions.geoserver.rest.encoder;
|
package it.geosolutions.geoserver.rest.encoder;
|
||||||
|
|
||||||
|
import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author ETj (etj at geo-solutions.it)
|
* @author ETj (etj at geo-solutions.it)
|
||||||
|
|||||||
@ -25,6 +25,8 @@
|
|||||||
|
|
||||||
package it.geosolutions.geoserver.rest.encoder;
|
package it.geosolutions.geoserver.rest.encoder;
|
||||||
|
|
||||||
|
import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author ETj (etj at geo-solutions.it)
|
* @author ETj (etj at geo-solutions.it)
|
||||||
|
|||||||
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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.utils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.jdom.Element;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes lists of entries with key attribute.
|
||||||
|
* <br/>e.g.:
|
||||||
|
* <PRE>
|
||||||
|
* {@code
|
||||||
|
* <listName>
|
||||||
|
* <entry key="k1">val1</entry>
|
||||||
|
* <entry key="k2">val2</entry>
|
||||||
|
* <entry key="k3">val3</entry>
|
||||||
|
* </listName>}
|
||||||
|
* <PRE>
|
||||||
|
*
|
||||||
|
* @author ETj (etj at geo-solutions.it)
|
||||||
|
*/
|
||||||
|
public class EntryKeyListEncoder {
|
||||||
|
|
||||||
|
private Map<String, String> metadata = new HashMap<String, String>();
|
||||||
|
private final String listName;
|
||||||
|
|
||||||
|
public EntryKeyListEncoder(String listName) {
|
||||||
|
this.listName = listName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(String key, String value) {
|
||||||
|
metadata.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void attachList(Element e) {
|
||||||
|
|
||||||
|
if( ! metadata.isEmpty() ) {
|
||||||
|
Element md = new Element(listName);
|
||||||
|
for (Map.Entry<String, String> entry : metadata.entrySet()) {
|
||||||
|
md.addContent("entry")
|
||||||
|
.setAttribute("key", entry.getKey())
|
||||||
|
.setText(entry.getValue());
|
||||||
|
}
|
||||||
|
e.addContent(md);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -23,7 +23,7 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package it.geosolutions.geoserver.rest.encoder;
|
package it.geosolutions.geoserver.rest.encoder.utils;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -33,12 +33,14 @@ import org.jdom.output.XMLOutputter;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an XML document by mapping properties to XML nodes.<br/>
|
* Creates an XML document by mapping properties to XML nodes.<br/>
|
||||||
* You can set the root element in the constructor.
|
* You can set the root element name in the constructor.
|
||||||
* Any key/value pair will be encoded as <key>value</key> node.
|
* Any key/value pair will be encoded as {@code <key>value</key>} node. <br/><br/>
|
||||||
|
*
|
||||||
|
* <h4>Nested nodes</h4>
|
||||||
* Any key containing one or more slash ("/") will be encoded as nested nodes;
|
* Any key containing one or more slash ("/") will be encoded as nested nodes;
|
||||||
* <P>e.g.:</P>
|
* <br/>e.g.:
|
||||||
* <PRE> {@code key = "k1/k2/k3", value = "value" }</pre>
|
* <PRE> {@code
|
||||||
* will be encoded as
|
* key = "k1/k2/k3", value = "value" }</pre> will be encoded as
|
||||||
* <PRE> {@code <k1><k2><k3>value</k3></k2></k1> }</pre>
|
* <PRE> {@code <k1><k2><k3>value</k3></k2></k1> }</pre>
|
||||||
*
|
*
|
||||||
* @author ETj (etj at geo-solutions.it)
|
* @author ETj (etj at geo-solutions.it)
|
||||||
@ -69,20 +71,29 @@ public class PropertyXMLEncoder {
|
|||||||
return configElements.isEmpty();
|
return configElements.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return an xml document representing the stored properties.
|
||||||
|
*/
|
||||||
public String encodeXml() {
|
public String encodeXml() {
|
||||||
|
|
||||||
Element layer = new Element(rootName);
|
Element root = new Element(rootName);
|
||||||
for (String key : configElements.keySet()) {
|
for (String key : configElements.keySet()) {
|
||||||
final String value = configElements.get(key);
|
final String value = configElements.get(key);
|
||||||
add(layer, key, value);
|
add(root, key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
addNodesBeforeOutput(layer);
|
addNodesBeforeOutput(root);
|
||||||
return OUTPUTTER.outputString(layer);
|
return OUTPUTTER.outputString(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addNodesBeforeOutput(Element e) {
|
/**
|
||||||
|
* Subclasses may need to override this method if some more info in the XML
|
||||||
|
* string are needed when calling {@link #encodeXml() encodeXml()}.
|
||||||
|
*
|
||||||
|
* @param root the root element that will be converted into String by encodeXml
|
||||||
|
*/
|
||||||
|
protected void addNodesBeforeOutput(Element root) {
|
||||||
|
// nothing to do, just override when needed.
|
||||||
}
|
}
|
||||||
|
|
||||||
private void add(Element e, String key, String value) {
|
private void add(Element e, String key, String value) {
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* 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.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import org.jdom.Element;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes lists of simple text nodes.
|
||||||
|
* <br/>e.g.:
|
||||||
|
* <PRE>
|
||||||
|
* {@code
|
||||||
|
* <parentnodename>
|
||||||
|
* <nodename1>nodetext1</nodename1>
|
||||||
|
* <nodename2>nodetext2</nodename2>
|
||||||
|
* <nodename3>nodetext3</nodename3>
|
||||||
|
* </parentnodename>}
|
||||||
|
* <PRE>
|
||||||
|
*
|
||||||
|
* @author ETj (etj at geo-solutions.it)
|
||||||
|
*/
|
||||||
|
public class TextNodeListEncoder {
|
||||||
|
|
||||||
|
private List<Pair> list = new ArrayList<Pair>();
|
||||||
|
private final String listName;
|
||||||
|
|
||||||
|
public TextNodeListEncoder(String listName) {
|
||||||
|
this.listName = listName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(String nodename, String nodetext) {
|
||||||
|
list.add(new Pair(nodename, nodetext));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void attachList(Element e) {
|
||||||
|
|
||||||
|
if( ! list.isEmpty() ) {
|
||||||
|
Element elist = new Element(listName);
|
||||||
|
for (Pair pair : list) {
|
||||||
|
elist.addContent(pair.v1).setText(pair.v2);
|
||||||
|
}
|
||||||
|
e.addContent(elist);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Pair {
|
||||||
|
String v1;
|
||||||
|
String v2;
|
||||||
|
|
||||||
|
public Pair(String v1, String v2) {
|
||||||
|
this.v1 = v1;
|
||||||
|
this.v2 = v2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
<html><head></head><body>
|
||||||
|
Some util classes for encoders.
|
||||||
|
</body></html>
|
||||||
@ -0,0 +1,130 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
|
||||||
|
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
|
||||||
|
import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testcase for creating postgis-based resources on geoserver.
|
||||||
|
* <P>
|
||||||
|
* Since these tests require a running postgis instance, this is more like integration tests.<br/>
|
||||||
|
* You may skip them by defining<tt> <pre>
|
||||||
|
* -DpgIgnore=true </pre></tt>
|
||||||
|
* When <tt>pgIgnore</tt> is defined that way, failing tests will not break
|
||||||
|
* the build: they will be logged as errors instead.
|
||||||
|
*
|
||||||
|
* <P>
|
||||||
|
* The target postgis instance can be customized by defining the following env vars: <ul>
|
||||||
|
* <LI><TT>pgHost</TT> (default <TT>localhost</TT>)</LI>
|
||||||
|
* <LI><TT>pgPort</TT> (default: <TT>5432</TT>)</LI>
|
||||||
|
* <LI><TT>pgDatabase</TT> (default: <TT>test</TT>)</LI>
|
||||||
|
* <LI><TT>pgSchema</TT> (default: <TT>public</TT>)</LI>
|
||||||
|
* <LI><TT>pgUser</TT> (default: <TT>utest</TT>)</LI>
|
||||||
|
* <LI><TT>pgPassword</TT> (default: <TT>ptest</TT>)</LI>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @author etj
|
||||||
|
* @author Eric Grosso
|
||||||
|
*
|
||||||
|
* @see GeoserverRESTTest
|
||||||
|
*/
|
||||||
|
public class GeoserverRESTPostgisDatastoreTest extends GeoserverRESTTest {
|
||||||
|
|
||||||
|
private final static Logger LOGGER = Logger.getLogger(GeoserverRESTPostgisDatastoreTest.class);
|
||||||
|
private static final String DEFAULT_WS = "it.geosolutions";
|
||||||
|
|
||||||
|
private final boolean pgIgnore;
|
||||||
|
private final String pgHost;
|
||||||
|
private final int pgPort;
|
||||||
|
private final String pgDatabase;
|
||||||
|
private final String pgSchema;
|
||||||
|
private final String pgUser;
|
||||||
|
private final String pgPassword;
|
||||||
|
|
||||||
|
public GeoserverRESTPostgisDatastoreTest(String testName) {
|
||||||
|
super(testName);
|
||||||
|
|
||||||
|
pgIgnore = System.getProperty("pgIgnore", "false").equalsIgnoreCase("true");
|
||||||
|
pgHost = System.getProperty("pgHost", "localhost");
|
||||||
|
pgPort = Integer.parseInt(System.getProperty("pgPort", "5432"));
|
||||||
|
pgDatabase = System.getProperty("pgDatabase", "test");
|
||||||
|
pgSchema = System.getProperty("pgSchema", "public");
|
||||||
|
pgUser = System.getProperty("pgUser", "utest");
|
||||||
|
pgPassword = System.getProperty("pgPassword", "ptest");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCreateDeletePostGISDatastore() {
|
||||||
|
if (!enabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String wsName = "it.geosolutions";
|
||||||
|
String datastoreName = "resttestpostgis";
|
||||||
|
String description = "description";
|
||||||
|
String dsNamespace = "http://www.geo-solutions.it";
|
||||||
|
boolean exposePrimaryKeys = true;
|
||||||
|
boolean validateConnections = false;
|
||||||
|
String primaryKeyMetadataTable = "test";
|
||||||
|
|
||||||
|
GSPostGISDatastoreEncoder datastoreEncoder = new GSPostGISDatastoreEncoder();
|
||||||
|
datastoreEncoder.defaultInit();
|
||||||
|
datastoreEncoder.setName(datastoreName);
|
||||||
|
datastoreEncoder.setDescription(description);
|
||||||
|
datastoreEncoder.setNamespace(dsNamespace);
|
||||||
|
datastoreEncoder.setHost(pgHost);
|
||||||
|
datastoreEncoder.setPort(pgPort);
|
||||||
|
datastoreEncoder.setDatabase(pgDatabase);
|
||||||
|
datastoreEncoder.setSchema(pgSchema);
|
||||||
|
datastoreEncoder.setUser(pgUser);
|
||||||
|
datastoreEncoder.setPassword(pgPassword);
|
||||||
|
datastoreEncoder.setExposePrimaryKeys(exposePrimaryKeys);
|
||||||
|
datastoreEncoder.setValidateConnections(validateConnections);
|
||||||
|
datastoreEncoder.setPrimaryKeyMetadataTable(primaryKeyMetadataTable);
|
||||||
|
|
||||||
|
// creation test
|
||||||
|
boolean created = publisher.createPostGISDatastore(wsName, datastoreEncoder);
|
||||||
|
|
||||||
|
if( ! pgIgnore )
|
||||||
|
assertTrue("PostGIS datastore not created", created);
|
||||||
|
else if( ! created)
|
||||||
|
LOGGER.error("*** Datastore " + datastoreName + " has not been created.");
|
||||||
|
|
||||||
|
|
||||||
|
RESTDataStore datastore = reader.getDatastore(wsName, datastoreName);
|
||||||
|
LOGGER.info("The type of the created datastore is: " + datastore.getType());
|
||||||
|
|
||||||
|
// removing test
|
||||||
|
boolean removed = publisher.removeDatastore(wsName, datastoreName);
|
||||||
|
if( ! pgIgnore )
|
||||||
|
assertTrue("PostGIS datastore not removed", removed);
|
||||||
|
else if( ! removed )
|
||||||
|
LOGGER.error("*** Datastore " + datastoreName + " has not been removed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -42,9 +42,14 @@ import org.apache.log4j.Logger;
|
|||||||
/**
|
/**
|
||||||
* Initializes REST params.
|
* Initializes REST params.
|
||||||
* <P>
|
* <P>
|
||||||
* Default target geoserver instance is at <TT>http://localhost:8080/geoserver</TT>.
|
* <B>These tests are destructive, so you have to explicitly enable them</B>
|
||||||
* <BR>Connection parameters can be customized by defining env vars
|
* by setting the env var <TT>resttest</TT> to <TT>true</TT>.
|
||||||
* <I>resturl</I>, <I>restuser</I>, <I>restpw</I>,
|
* <P>
|
||||||
|
* The target geoserver instance can be customized by defining the following env vars: <ul>
|
||||||
|
* <LI><TT>resturl</TT> (default <TT>http://localhost:8080/geoserver</TT>)</LI>
|
||||||
|
* <LI><TT>restuser</TT> (default: <TT>admin</TT>)</LI>
|
||||||
|
* <LI><TT>restpw</TT> (default: <TT>geoserver</TT>)</LI>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author etj
|
* @author etj
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user