Working With The Catalog
In this tutorial you will learn how to interact with the GeoServer catalog. The target is to Create a simple service which queries the catalog for data stores and feature types.
So first we create our service, lets call it CatalogLister which will perform the following operations:
- Listing all data stores in the catalog
- Listing all feature types in the catalog
public class CatalogLister { public void listDataStores( OutputStream output ) { } public void listFeatureTypes( OutputStream output ) { } }
The first thing that is needed is a reference to the catalog itself. So lets add a constructor which takes the catalog as its only paramter.
GeoServerCatalog catalog; public CatalogLister( GeoServerCatalog catalog ) { this.catalog = catalog; }
First up is listDataStores. A data store corresponds to a service in the catalog. In catalog terminology, one must "resolve a service handle into a data store". Implementing this operation, we step through each service and test for a resolve to a data store. When we find a service that is indeed backed by a data store it is printed out.
public void listDataStores( OutputStream output ) { //output PrintStream printer = new PrintStream( output ); //get all services from the catalog List services = catalog.members( null ); for ( Iterator s = services.iterator(); s.hasNext(); ) { Service service = (Service) s.next(); //first we must test to see wether this handle is backed by a datastore if ( service.canResolve( DataStore.class ) ) { //it is a data store, write it to output printer.println( service.getIdentifier().toString() ); } } }
Next up is listFeatureTypes. A feature type corresponds to a geo resource in the catalog. In the catalog hierarchy geo resources are children or services. So for each service in teh catalog, we check each geo resource for a resolve to a feature type. This time we will go a step further, and actually perform the resolve to underlying feature type object.
public void listDataStores( OutputStream output ) { //output PrintStream printer = new PrintStream( output ); //get all services from the catalog List services = catalog.members( null ); for ( Iterator s = services.iterator(); s.hasNext(); ) { Service service = (Service) s.next(); //get all children List geoResources = service.members( null ); for ( Iterator g = geoResources.iterator(); g.hasNext(); ) { GeoResource geoResource = (GeoResourfce) g.next(); //do the test if ( geoResource.canResolve( FeatureType.class ) ) { //do the resolve FeatureType featureType = (FeatureType) geoResource.resolve( FeatureType.class, null ); printer.println( featureType.getTypeName() ); } } } }