@RequestMapping(value="/{wsName}/{name}/style", method = RequestMethod.GET, produces = YsldHandler.MIMETYPE)
public @ResponseBody Object style(@PathVariable String wsName, @PathVariable String name)
throws IOException {
Catalog cat = geoServer.getCatalog();
LayerInfo l = findLayer(wsName, name, cat);
StyleInfo s = l.getDefaultStyle();
if (s == null) {
throw new NotFoundException(String.format("Layer %s:%s has no default style", wsName, name));
}
// if the style is already stored in ySLD format just pull it directly, otherwise encode the style
if (YsldHandler.FORMAT.equalsIgnoreCase(s.getFormat())) {
return dataDir().style(s);
}
else {
GeoServerResourceLoader rl = cat.getResourceLoader();
String path;
if( s.getWorkspace() == null ){
path = Paths.path("styles",s.getFilename());
}
else {
path = Paths.path("workspaces",s.getWorkspace().getName(),"styles",s.getFilename());
}
final Resource r = rl.get(path);
// Similar to s.getStyle() and GeoServerDataDirectory.parsedStyle(s)
// But avoid resolving external graphics to absolute file references
if ( r == null || r.getType() == Type.UNDEFINED ){
throw new IOException( "No such resource: " + s.getFilename());
}
// Force use of unmodified URI, avoiding absolute file references
ResourceLocator locator = new ResourceLocator(){
public URL locateResource(String spec) {
return null;
}
};
StyleHandler handler = Styles.handler(s.getFormat());
StyledLayerDescriptor sld = handler.parse(r, s.getFormatVersion(), locator, null);
final Style style = Styles.style(sld); // extract 1st style
return Styles.sld(style); // encode in generated SLD
}
}