/** * Retrieve the style definition (SLD) as a Resource * * @param c The style * @return A {@link Resource} */ public @Nonnull Resource style(StyleInfo s) { // Must be a simple filename final String filename = s.getFilename(); Resource r = get(s, filename); assert r != null; return r; }
/** * Reads a raw style from persistence. * * @param style The configuration for the style. * @return A reader for the style. */ public BufferedReader readStyle(StyleInfo style) throws IOException { Resource styleResource = dataDir().style(style); if (styleResource == null) { throw new IOException("No such resource: " + style.getFilename()); } return new BufferedReader(new InputStreamReader(styleResource.in())); }
/** * Retrieve the style configuration XML as a Resource * * @param c The style * @return A {@link Resource} */ public @Nonnull Resource config(StyleInfo s) { // special case for styles, if the file name (minus the suffix) matches the id of the style // and the suffix is xml (rather than sld) we need to avoid overwritting the actual // style file final String filename; if (s.getFilename() != null && s.getFilename().endsWith(".xml") && s.getFilename().startsWith(s.getName() + ".")) { // append a second .xml suffix filename = s.getName() + ".xml.xml"; } else { filename = s.getName() + ".xml"; } Resource r = get(s, filename); assert r != null; return r; }
public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof StyleInfo)) return false; final StyleInfo other = (StyleInfo) obj; if (filename == null) { if (other.getFilename() != null) return false; } else if (!filename.equals(other.getFilename())) return false; if (id == null) { if (other.getId() != null) return false; } else if (!id.equals(other.getId())) return false; if (name == null) { if (other.getName() != null) return false; } else if (!name.equals(other.getName())) return false; if (workspace == null) { if (other.getWorkspace() != null) return false; } else if (!workspace.equals(other.getWorkspace())) return false; if (format == null) { if (other.getFormat() != null) return false; } else { if (!format.equals(other.getFormat())) return false; } if (languageVersion == null) { if (other.getFormatVersion() != null) return false; } else if (!languageVersion.equals(other.getFormatVersion())) return false; return true; }
/** * Retrieve the StyleInfo as a GeoTools Style object. Note this is just the data structure as * written, the matching external graphics are unmodified and may not be (yet) available on the * local system. * * @param s The style * @return A {@link Resource} */ protected @Nonnull Style parsedStyleResources(StyleInfo s) throws IOException { final Resource styleResource = style(s); if (styleResource.getType() == Type.UNDEFINED) { throw new FileNotFoundException("No such resource: " + s.getFilename()); } final DefaultResourceLocator locator = new ResourceAwareResourceLocator(); locator.setSourceUrl(Resources.toURL(styleResource)); StyledLayerDescriptor sld = Styles.handler(s.getFormat()) .parse(styleResource, s.getFormatVersion(), locator, null); final Style style = Styles.style(sld); assert style != null; return style; }
throw new NullPointerException("Style name must not be null"); if (isNull(style.getFilename())) { throw new NullPointerException("Style fileName must not be null");
StyleInfo backupStyle(StyleInfo s) throws IOException { StyleInfo backup = catalog.getFactory().createStyle(); new CatalogBuilder(catalog).updateStyle(backup, s); backup.setWorkspace(s.getWorkspace()); // find a unique name for the style String name = findUniqueStyleName( s.getWorkspace() == null ? null : s.getWorkspace().getName(), s.getName() + "_BACKUP"); backup.setName(name); // update it's file name backup.setFilename(name + "." + FilenameUtils.getExtension(s.getFilename())); // copy over the style contents try (BufferedReader reader = catalog.getResourcePool().readStyle(s)) { catalog.getResourcePool() .writeStyle(backup, new ByteArrayInputStream(IOUtils.toByteArray(reader))); } return backup; }
/** * Retrieve the styled layer descriptor prepared for direct GeoTools use. All file references * have been made absolute. * * @param s The style * @return A {@link StyledLayerDescriptor} */ public @Nonnull StyledLayerDescriptor parsedSld(final StyleInfo s) throws IOException { final Resource styleResource = style(s); if (styleResource.getType() == Type.UNDEFINED) { throw new IOException( "No such resource: " + s.getFilename() + (s.getWorkspace() != null ? " in workspace " + s.getWorkspace() : "")); } File input = styleResource.file(); DefaultResourceLocator locator = new GeoServerResourceLocator(); locator.setSourceUrl(Resources.toURL(styleResource)); EntityResolver entityResolver = getEntityResolver(); final StyledLayerDescriptor sld = Styles.handler(s.getFormat()) .parse(input, s.getFormatVersion(), locator, getEntityResolver()); return sld; }
expect(s.getFilename()).andReturn(filename).anyTimes(); expect(s.getSLDVersion()).andReturn(version).anyTimes(); try {
@Test public void testGeneratedStyles() throws Exception { XStreamPersisterFactory xpf = new XStreamPersisterFactory(); XStreamPersister xp = xpf.createXMLPersister(); xp.setCatalog(catalog); loader.initializeStyles(catalog, xp); StyleInfo polygon = catalog.getStyleByName(StyleInfo.DEFAULT_POLYGON); assertEquals("default_polygon.sld", polygon.getFilename()); }
s2.setFilename(s.getFilename()); s3.setFilename(s2.getFilename());
s = catalog.getStyles().get(0); assertEquals("style", s.getName()); assertEquals("style.sld", s.getFilename());
assertEquals(Resource.Type.RESOURCE, oldStyle.getType()); assertEquals("singleStyleGroup.sld", s.getFilename());
/** * Gets an SLD resource for the given style. * * @param style */ private Resource getStyleResource(StyleInfo style) { String[] prefix = new String[0]; if (style.getWorkspace() != null) { prefix = new String[] {"workspaces", style.getWorkspace().getName()}; } String fileName = style.getFilename(); String[] pathParts = (String[]) ArrayUtils.addAll(prefix, new String[] {"styles", fileName}); String path = Paths.path(pathParts); return loader.get(path); }
/** Retrieves a style file content has a string. */ private String getStyleFile(StyleInfo styleInfo, GeoServerDataDirectory dataDir) { Resource resource = dataDir.get(styleInfo, styleInfo.getFilename()); try (InputStream input = resource.in(); ByteArrayOutputStream output = new ByteArrayOutputStream()) { // reads the style file content to the byte array IOUtils.copy(input, output); return new String(output.toByteArray()); } catch (IOException exception) { throw new RuntimeException( String.format("Error reading style '%s'.", styleInfo.getName())); } }
protected StyleInfo buildStyle(String name, WorkspaceInfo ws) { StyleInfo style = createNiceMock(StyleInfo.class); expect(style.getName()).andReturn(name).anyTimes(); expect(style.getFilename()).andReturn(name + ".sld").anyTimes(); expect(style.getWorkspace()).andReturn(ws).anyTimes(); replay(style); return style; }
@Test public void testPut() throws Exception { StyleInfo style = catalog.getStyleByName("Ponds"); assertEquals("Ponds.sld", style.getFilename()); String xml = "<style>" + "<name>Ponds</name>" + "<filename>Forests.sld</filename>" + "</style>"; MockHttpServletResponse response = putAsServletResponse( RestBaseController.ROOT_PATH + "/styles/Ponds", xml.getBytes(), "text/xml"); assertEquals(200, response.getStatus()); style = catalog.getStyleByName("Ponds"); assertEquals("Forests.sld", style.getFilename()); }
@Test public void testPutRenameDefault() throws Exception { StyleInfo style = catalog.getStyleByName("Ponds"); assertEquals("Ponds.sld", style.getFilename()); String xml = "<style>" + "<name>Ponds</name>" + "<filename>Forests.sld</filename>" + "</style>"; MockHttpServletResponse response = putAsServletResponse( RestBaseController.ROOT_PATH + "/styles/line", xml.getBytes(), "text/xml"); assertEquals(500, response.getStatus()); }
@Test public void testPutToWorkspace() throws Exception { testPostToWorkspace(); Catalog cat = getCatalog(); assertEquals("foo.sld", cat.getStyleByName("gs", "foo").getFilename()); String xml = "<style>" + "<filename>bar.sld</filename>" + "</style>"; MockHttpServletResponse response = putAsServletResponse( RestBaseController.ROOT_PATH + "/workspaces/gs/styles/foo", xml, "application/xml"); assertEquals(200, response.getStatus()); assertEquals("bar.sld", cat.getStyleByName("gs", "foo").getFilename()); }
@Test public void testStyleInfoAdditionalResourceWriter() throws IOException { GeoServerDataDirectory dd = backupFacade.getGeoServerDataDirectory(); GeoServerDataDirectory td = new GeoServerDataDirectory(root); StyleInfo style = catalog.getStyleByName(StyleInfo.DEFAULT_POINT); StyleInfoAdditionalResourceWriter siarw = new StyleInfoAdditionalResourceWriter(); siarw.writeAdditionalResources(backupFacade, td.get(Paths.BASE), style); Resource srcStylesDir = BackupUtils.dir(dd.get(Paths.BASE), "styles"); Resource trgStylesDir = BackupUtils.dir(td.get(Paths.BASE), "styles"); assertTrue(Resources.exists(srcStylesDir)); assertTrue(Resources.exists(trgStylesDir)); assertTrue( Resources.exists( Files.asResource(new File(trgStylesDir.dir(), style.getFilename())))); }