Skip to content
Emanuele Tajariol edited this page Aug 22, 2013 · 2 revisions

#Styles

You can work with both global and workspace-scoped styles.

Workspace-scoped styles have been introduced in GeoServer 2.2: http://blog.geoserver.org/2012/09/21/geoserver-2-2-released/
Related GSIP is here: http://geoserver.org/display/GEOS/GSIP+73+-+Workspace+Local+Styles+and+Layer+Groups

Global and workspaced styles have different sets of methods:

Read-only methods

  • public boolean existsStyle(String styleName)
    check if a global style exists
  • public RESTStyle getStyle(String name)
    get global style information (quite useless in this form, more useful with Util.searchStyles()
  • public RESTStyleList getStyles()
    returns an iterable list of styles
  • public String getSLD(String styleName)
    get the SLD

These methods have the workspace counterparts:

  • public boolean existsStyle(String workspace, String styleName)
  • public RESTStyle getStyle(String workspace, String name)
  • public RESTStyleList getStyles(String workspace)
  • public String getSLD(String workspace, String styleName)

Insert, update, delete

You can publish a style with different methods.
You may provide the SLD as a String or using a File; furthermore you may specify a name for the style: if not provided, it will be extracted from within the SLD. Here the method list:

  • public boolean publishStyle(String sldBody)
  • public boolean publishStyle(final String sldBody, final String name)
  • public boolean publishStyle(File sldFile)
  • public boolean publishStyle(File sldFile, String name)

For updating, you have the same options using either a String or a File:

  • public boolean updateStyle(final String sldBody, final String name)
  • public boolean updateStyle(final File sldFile, final String name)

When removing a style, you can use either

  • public boolean removeStyle(String styleName)
    Remove a style and its related SLD on filesystem
  • public boolean removeStyle(String styleName, purge)
    Remove a style and optionally its related SLD on filesystem

All of these methods are for global styles.
For each of them, you can find the corresponding method for operating with the styles in a workspace:

  • public boolean publishStyleInWorkspace(String workspace, String sldBody)
  • public boolean publishStyleInWorkspace(String workspace, String sldBody, String name)
  • public boolean publishStyleInWorkspace(String workspace, File sldFile)
  • public boolean publishStyleInWorkspace(String workspace, File sldFile, String name)
  • public boolean updateStyleInWorkspace(String workspace, String sldBody, String name)
  • public boolean updateStyleInWorkspace(String workspace, File sldFile, String name)
  • public boolean removeStyleInWorkspace(String workspace, String styleName, boolean purge)
  • public boolean removeStyleInWorkspace(String workspace, String styleName)

Some examples

Create a Style

Send to GeoServer a SLD loaded from the local filesystem. The style name will be read from the provided SLD.

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

If you want to override the name defined in the SLD, you can specify a name explicitly.

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

Find a style

If you only have the name of the style but you don't know if it's inside a workspace, you'll need to search inside each workspace, because REST operations only allow you to pick up a style only if you specify its workspace.

You may find useful the class Util:

import it.geosolutions.geoserver.rest.Util;
...
GeoServerRESTReader reader = ...
List<RESTStyle> styles = Util.searchStyles(reader, "myStyle");
for(RESTStyle style : styles) {
   String stylename = style.getName(); // it will be "myStyle" 
   String workspace = style.workspace();
   if( workspace == null) {
      // style is global
   } else {
      // style belongs to workspace
   }
}