From 9782f0aaa523eb03ffdd524289b135b98e025f22 Mon Sep 17 00:00:00 2001 From: Gabriel Roldan Date: Sat, 18 Nov 2023 00:29:22 -0300 Subject: [PATCH] Improved resolving of catalog property for CatalogInfo objects --- .../repository/CatalogInfoRowMapper.java | 6 +- .../cloud/event/bus/InfoEventResolver.java | 6 +- .../resolving/CatalogPropertyResolver.java | 65 ++++++++++--------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/catalog/backends/pgsql/src/main/java/org/geoserver/cloud/backend/pgsql/catalog/repository/CatalogInfoRowMapper.java b/src/catalog/backends/pgsql/src/main/java/org/geoserver/cloud/backend/pgsql/catalog/repository/CatalogInfoRowMapper.java index a6e20857e..53ae266f7 100644 --- a/src/catalog/backends/pgsql/src/main/java/org/geoserver/cloud/backend/pgsql/catalog/repository/CatalogInfoRowMapper.java +++ b/src/catalog/backends/pgsql/src/main/java/org/geoserver/cloud/backend/pgsql/catalog/repository/CatalogInfoRowMapper.java @@ -320,7 +320,11 @@ private void setDefaultStyle(LayerInfo layer, ResultSet rs) { if (null != defaultStyle) { String styleId = defaultStyle.getId(); defaultStyle = mapStyle(styleId, "defaultStyle", rs); - layer.setDefaultStyle(ModificationProxy.create(defaultStyle, StyleInfo.class)); + if (null == defaultStyle) { + layer.setDefaultStyle(null); + } else { + layer.setDefaultStyle(ModificationProxy.create(defaultStyle, StyleInfo.class)); + } } } diff --git a/src/catalog/event-bus/src/main/java/org/geoserver/cloud/event/bus/InfoEventResolver.java b/src/catalog/event-bus/src/main/java/org/geoserver/cloud/event/bus/InfoEventResolver.java index 0d6f2498c..51390facb 100644 --- a/src/catalog/event-bus/src/main/java/org/geoserver/cloud/event/bus/InfoEventResolver.java +++ b/src/catalog/event-bus/src/main/java/org/geoserver/cloud/event/bus/InfoEventResolver.java @@ -33,17 +33,13 @@ */ public class InfoEventResolver { - private final Catalog rawCatalog; - private final GeoServer geoserverConfig; - private Function configInfoResolver; private Function catalogInfoResolver; // REVISIT: merge ProxyUtils with ResolvingProxyResolver private ProxyUtils proxyUtils; public InfoEventResolver(@NonNull Catalog rawCatalog, @NonNull GeoServer geoserverConfig) { - this.rawCatalog = rawCatalog; - this.geoserverConfig = geoserverConfig; + proxyUtils = new ProxyUtils(rawCatalog, Optional.of(geoserverConfig)); configInfoResolver = diff --git a/src/catalog/plugin/src/main/java/org/geoserver/catalog/plugin/resolving/CatalogPropertyResolver.java b/src/catalog/plugin/src/main/java/org/geoserver/catalog/plugin/resolving/CatalogPropertyResolver.java index 4072319c3..571cb72bb 100644 --- a/src/catalog/plugin/src/main/java/org/geoserver/catalog/plugin/resolving/CatalogPropertyResolver.java +++ b/src/catalog/plugin/src/main/java/org/geoserver/catalog/plugin/resolving/CatalogPropertyResolver.java @@ -4,6 +4,8 @@ */ package org.geoserver.catalog.plugin.resolving; +import lombok.NonNull; + import org.geoserver.catalog.Catalog; import org.geoserver.catalog.CatalogInfo; import org.geoserver.catalog.Info; @@ -13,12 +15,14 @@ import org.geoserver.catalog.ResourceInfo; import org.geoserver.catalog.StoreInfo; import org.geoserver.catalog.StyleInfo; +import org.geoserver.catalog.WMSLayerInfo; import org.geoserver.catalog.impl.LayerGroupStyle; import org.geoserver.catalog.impl.ModificationProxy; import org.geoserver.catalog.impl.StoreInfoImpl; import org.geoserver.catalog.impl.StyleInfoImpl; import org.geoserver.catalog.plugin.forwarding.ResolvingCatalogFacadeDecorator; +import java.util.Collection; import java.util.Objects; import java.util.function.UnaryOperator; @@ -45,65 +49,62 @@ public static CatalogPropertyResolver of(Catalog catalog) { } public @Override T apply(T i) { + return resolve(i); + } + + private I resolve(I i) { + i = null == i ? null : ModificationProxy.unwrap(i); if (i instanceof StoreInfo) setCatalog((StoreInfo) i); else if (i instanceof ResourceInfo) setCatalog((ResourceInfo) i); else if (i instanceof StyleInfo) setCatalog((StyleInfo) i); else if (i instanceof PublishedInfo) setCatalog((PublishedInfo) i); + else if (i instanceof LayerGroupStyle) setCatalog((LayerGroupStyle) i); return i; } - private void setCatalog(PublishedInfo i) { + private void resolve(Collection list) { + if (null != list) list.forEach(this::resolve); + } + + private void setCatalog(@NonNull PublishedInfo i) { if (i instanceof LayerInfo) setCatalog((LayerInfo) i); else if (i instanceof LayerGroupInfo) setCatalog((LayerGroupInfo) i); } - private void setCatalog(LayerInfo i) { - if (null == i) return; - ResourceInfo resource = i.getResource(); - if (null != resource) { - if (null == resource.getCatalog()) { - setCatalog(resource); - } - StoreInfo store = resource.getStore(); - if (null != store && null == store.getCatalog()) setCatalog(store); - } - setCatalog(i.getDefaultStyle()); - if (i.getStyles() != null) i.getStyles().forEach(this::setCatalog); + private void setCatalog(@NonNull LayerInfo i) { + resolve(i.getResource()); + resolve(i.getDefaultStyle()); + resolve(i.getStyles()); } - private void setCatalog(LayerGroupInfo i) { - if (null == i) return; - if (i.getLayerGroupStyles() != null) i.getLayerGroupStyles().forEach(this::setCatalog); - - if (i.getLayers() != null) i.getLayers().forEach(this::setCatalog); - - setCatalog(i.getRootLayer()); - setCatalog(i.getRootLayerStyle()); + private void setCatalog(@NonNull LayerGroupInfo i) { + resolve(i.getRootLayer()); + resolve(i.getRootLayerStyle()); + resolve(i.getLayers()); + resolve(i.getStyles()); + resolve(i.getLayerGroupStyles()); } - private void setCatalog(LayerGroupStyle i) { - if (null == i) return; + private void setCatalog(@NonNull LayerGroupStyle i) { // TODO: check if this would result in a stack overflow // if(null!=i.getLayers())i.getLayers().forEach(this::setCatalog); // if(null != i.getStyles())i.getStyles().forEach(this::setCatalog); } - private void setCatalog(StoreInfo i) { - if (null == i) return; - i = ModificationProxy.unwrap(i); + private void setCatalog(@NonNull StoreInfo i) { if (i instanceof StoreInfoImpl) ((StoreInfoImpl) i).setCatalog(catalog); } - private void setCatalog(ResourceInfo i) { - if (null == i) return; - i = ModificationProxy.unwrap(i); + private void setCatalog(@NonNull ResourceInfo i) { i.setCatalog(catalog); + resolve(i.getStore()); + if (i instanceof WMSLayerInfo) { + resolve(((WMSLayerInfo) i).getAllAvailableRemoteStyles()); + } } - private void setCatalog(StyleInfo i) { - if (null == i) return; - i = ModificationProxy.unwrap(i); + private void setCatalog(@NonNull StyleInfo i) { if (i instanceof StyleInfoImpl) ((StyleInfoImpl) i).setCatalog(catalog); } }