GeoServer manager REST examples

The GeoServer manager is a REST client library to interact with GeoServer (http://www.geoserver.org)

The purpose of this project is to hold a REST client library to interact with GeoServer; a requirement for this library is to depend as less as possible on external libraries. This library aims at being lean and mean.

For any questions about this project feel free to use the mailing lists.

Mailing Lists:

  1. USERS
  2. DEVELOPERS

For more information visit this page

Using the library

You will only need to instantiate GeoServerRESTReader for reading data from a running GeoServer instance:

public GeoServerRESTReader(String restUrl, String username, String password);

or GeoServerRESTPublisher to modify the catalog (publish or remove styles, featuretypes or coverages):

public GeoServerRESTPublisher(String restURL, String username, String pw);

These classes just handles the REST URLs, and the upper level logic (e.g.: publishing a layer means sending the data, optionally a style, and configuring the layer). The basic HTTP calls are handled by HTTPUtils.

There are no beans mirroring GeoServer’s ones. The info for publishing layers are simple Strings in method calls.

The info read from GeoServer are stored as XML elements and parsed using JDOM only when requested. The parsing is performed by the classes in the decoder package.

Examples

Init reader and publisher

String RESTURL  = "http://localhost:8083/geoserver";
String RESTUSER = "admin";
String RESTPW   = "Geos";

GeoServerRESTReader reader = new GeoServerRESTReader(RESTURL, RESTUSER, RESTPW);
GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW);

Create a Workspace:

boolean created = publisher.createWorkspace("myWorkspace");

Create a Style

File sldFile = ...;
boolean published = publisher.publishStyle(sldFile); // Will take the name from SLD contents

Or

File sldFile = ...;
boolean published = publisher.publishStyle(sldFile, "myStyle");

Create a layer from a Shapefile

File zipFile = ...;
boolean published = publisher.publishShp("myWorkspace", "myStore", "cities", zipFile, "EPSG:4326", "default_point");

Note: that the layername (“cities”) should be the same as the shapefile in the .zip file. In our case, we have “cities.shp” in the zipfile.

If you want to remove this layer entirely from the configuration, you will have to remove the layer and the datastore:

boolean ftRemoved = publisher.unpublishFeatureType("myWorkspace", "myStore", "cities");
// remove  datastore
boolean dsRemoved = publisher.removeDatastore("myWorkspace", "myStore", true);

Create a layer from a DB table

GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder();
fte.setProjectionPolicy(GSResourceEncoder.ProjectionPolicy.REPROJECT_TO_DECLARED);
fte.addKeyword("KEYWORD");
fte.setTitle("easia_gaul_0_aggr");
fte.setName("easia_gaul_0_aggr");
fte.setSRS("EPSG:4326");

final GSLayerEncoder layerEncoder = new GSLayerEncoder();
layerEncoder.setDefaultStyle("default_polygon");

boolean ok = publisher.publishDBLayer("myWorkspace", "pg_kids", fte, layerEncoder);

_pg_kids_ is a PostGIS datastore already configured in GeoServer

_easia_gaul_0_aggr_ is a table that has not yet been published in GeoServer

Working with Maven

In order to include the lib and its dependencies in a Maven project, the repository to point at is this one:

http://maven.geo-solutions.it/

and the dependency tag for your pom is as follows:

<dependency>
  <groupId>it.geosolutions</groupId>
  <artifactId>geoserver-manager</artifactId>
  <version>1.6.0</version>
</dependency>