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; }
protected void handleNoStyles() { // handle no styles -- use default try { if (styleCount == 0 && info != null && info instanceof LayerInfo) { StyleInfo styleInfo = ((LayerInfo) info).getDefaultStyle(); if (styleInfo != null) { visit(styleInfo.getStyle()); } } } catch (IOException e) { throw new UncheckedIOException(e); } finally { // clean up state styleCount = 0; } }
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; }
private boolean isDefaultStyle(StyleInfo s) { return s.getWorkspace() == null && (StyleInfo.DEFAULT_POINT.equals(s.getName()) || StyleInfo.DEFAULT_LINE.equals(s.getName()) || StyleInfo.DEFAULT_POLYGON.equals(s.getName()) || StyleInfo.DEFAULT_RASTER.equals(s.getName()) || StyleInfo.DEFAULT_GENERIC.equals(s.getName())); }
/** * 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; }
/** * Given a {@link StyleInfo} and a user, returns it back if the user can access it. * * @return <code>null</code> if the user can't acess the style, otherwise the original style. */ protected StyleInfo checkAccess( Authentication user, StyleInfo style, MixedModeBehavior mixedModeBehavior) { if (style == null) return null; WrapperPolicy policy = buildWrapperPolicy(user, style, style.getName(), mixedModeBehavior); // if we don't need to hide it, then we can return it as is since it // can only provide metadata. if (policy.level == AccessLevel.HIDDEN) return null; else return style; }
public ValidationResult validate(StyleInfo style, boolean isNew) { if (isNull(style.getName())) { throw new NullPointerException("Style name must not be null"); if (isNull(style.getFilename())) { throw new NullPointerException("Style fileName must not be null"); WorkspaceInfo ws = style.getWorkspace(); StyleInfo existing = getStyleByName(ws, style.getName()); if (existing != null && (isNew || !existing.getId().equals(style.getId()))) { WorkspaceInfo ews = existing.getWorkspace(); String msg = "Style named '" + style.getName() + "' already exists"; if (ews != null) { msg += " in workspace " + ews.getName(); StyleInfo current = getStyle(style.getId()); if (!current.getName().equals(style.getName())) { throw new IllegalArgumentException("Cannot rename default styles"); if (null != style.getWorkspace()) { throw new IllegalArgumentException( "Cannot change the workspace of default styles");
@Test public void testAddStyleWithWorkspace() throws Exception { File f = new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml"); assertFalse(f.exists()); StyleInfo s = catalog.getFactory().createStyle(); s.setName("foostyle"); s.setFilename("foostyle.sld"); s.setWorkspace(catalog.getDefaultWorkspace()); catalog.add(s); assertTrue(f.exists()); Document dom = dom(f); assertXpathEvaluatesTo(catalog.getDefaultWorkspace().getId(), "/style/workspace/id", dom); }
styles.add(s); expect(s.getId()).andReturn(sId); expect(s.getName()).andReturn(name).anyTimes(); expect(s.getFilename()).andReturn(filename).anyTimes(); expect(s.getSLDVersion()).andReturn(version).anyTimes(); try { expect(s.getStyle()) .andReturn( Styles.style( expect(catalog.getStyleByName(name)).andReturn(s).anyTimes(); s.accept((CatalogVisitor) anyObject()); expectLastCall() .andAnswer(
/** * Imports all styles and loads them into the catalog * * @param factory * @param styles */ void importStyles(CatalogFactory factory, Map styles) { for (Iterator s = styles.entrySet().iterator(); s.hasNext(); ) { Map.Entry entry = (Map.Entry) s.next(); StyleInfo style = factory.createStyle(); style.setName((String) entry.getKey()); style.setFilename((String) entry.getValue()); catalog.add(style); LOGGER.info("Loaded style '" + style.getName() + "'"); } }
protected void resolve(StyleInfo style) { setId(style); // resolve the workspace WorkspaceInfo ws = style.getWorkspace(); if (ws != null) { WorkspaceInfo resolved = ResolvingProxy.resolve(getCatalog(), ws); if (resolved != null) { resolved = unwrap(resolved); style.setWorkspace(resolved); } else { LOGGER.log( Level.INFO, "Failed to resolve workspace for style \"" + style.getName() + "\". This means the workspace has not yet been added to the catalog, keep the proxy around"); } } }
/** Keys for overriding default layer properties */ public static class StyleProperty<T> { T get(Map<StyleProperty, Object> map, T def) { return map != null && map.containsKey(this) ? (T) map.get(this) : def; } public static StyleProperty<String> FORMAT = new StyleProperty<String>(); public static StyleProperty<Version> FORMAT_VERSION = new StyleProperty<Version>(); public static StyleProperty<LegendInfo> LEGEND_INFO = new StyleProperty<LegendInfo>(); }
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; }
private void renameStyle(StyleInfo s, String newName) throws IOException { // rename style definition file Resource style = dd.style(s); StyleHandler format = Styles.handler(s.getFormat()); Resource target = uniqueResource(style, newName, format.getFileExtension()); renameRes(style, target.name()); s.setFilename(target.name()); // rename generated sld if appropriate if (!SLDHandler.FORMAT.equals(format.getFormat())) { Resource sld = style.parent().get(FilenameUtils.getBaseName(style.name()) + ".sld"); if (sld.getType() == Type.RESOURCE) { LOGGER.fine("Renaming style resource " + s.getName() + " to " + newName); Resource generated = uniqueResource(sld, newName, "sld"); renameRes(sld, generated.name()); } } }
StyleInfo doCreateStyle(StyleHandler handler, StyleType styleType, ResourceInfo resource) throws IOException { // find a new style name String styleName = generateUniqueStyleName(resource); // variable replacement String styleData = generateStyle(handler, styleType, styleName); // let's store it StyleInfo style = catalog.getFactory().createStyle(); style.setName(styleName); style.setFilename(styleName + "." + handler.getFileExtension()); style.setFormat(handler.getFormat()); if (workspace != null) { style.setWorkspace(workspace); } catalog.getResourcePool().writeStyle(style, new ByteArrayInputStream(styleData.getBytes())); return style; }
protected StyleInfo newStyle(String name, String fileName) { StyleInfo s2 = catalog.getFactory().createStyle(); s2.setName(name); s2.setFilename(fileName); return s2; }
/** * 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; }
@Test public void testStyle() throws Exception { Catalog catalog = new CatalogImpl(); CatalogFactory cFactory = catalog.getFactory(); StyleInfo s1 = cFactory.createStyle(); s1.setName("foo"); s1.setFilename("foo.sld"); ByteArrayOutputStream out = out(); persister.save(s1, out); ByteArrayInputStream in = in(out); StyleInfo s2 = persister.load(in, StyleInfo.class); assertEquals(s1, s2); Document dom = dom(in(out)); assertEquals("style", dom.getDocumentElement().getNodeName()); catalog.add(s2); assertNull(s2.getWorkspace()); }
private void checkLayerGroupResourceIsInWorkspace(StyleInfo style, WorkspaceInfo ws) { if (style == null) return; if (style.getWorkspace() != null && !ws.equals(style.getWorkspace())) { throw new IllegalArgumentException( "Layer group within a workspace (" + ws.getName() + ") can not contain styles from other workspace: " + style.getWorkspace()); } }
style.setWorkspace(ws); catalog.save(style); ws.accept(remover); assertNull(catalog.getWorkspaceByName(ws.getName())); assertNull(catalog.getStyleByName(style.getName())); assertNull(catalog.getLayerGroupByName(lg.getName()));