From 33a6b01aed3e2c9a4fb92c191aa426ef7732dfdb Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Thu, 22 Feb 2024 16:16:58 -0700 Subject: [PATCH 1/2] Add wms styles directory to config --- .../src/site/pages/thredds/ThreddsConfigRef.md | 8 ++++++++ .../thredds/server/config/TdsConfigMapper.java | 15 +++++++++++++++ .../java/thredds/server/config/WmsConfigBean.java | 9 +++++++++ 3 files changed, 32 insertions(+) diff --git a/docs/userguide/src/site/pages/thredds/ThreddsConfigRef.md b/docs/userguide/src/site/pages/thredds/ThreddsConfigRef.md index 9b5c45f34b..5544c8f1db 100644 --- a/docs/userguide/src/site/pages/thredds/ThreddsConfigRef.md +++ b/docs/userguide/src/site/pages/thredds/ThreddsConfigRef.md @@ -236,6 +236,7 @@ The following shows all the configuration options available in the WMS section o true false wmsPalettes + wmsStyles 2048 2048 @@ -259,6 +260,13 @@ where they are contained. * More information on the format of palette files can also be found in the [ncWMS documentation](https://reading-escience-centre.gitbooks.io/ncwms-user-guide/content/06-development.html#:~:text=To%20add%20new,in%20hexadecimal%20notation.). * If you created palette files for TDS 4.x and would like to use them in TDS 5.x, an open source tool named [Magic Palette Converter](https://github.com/billyz313/magic-palette-converter){:target="_blank"} for THREDDS is available to assist in the conversion (special thanks to [Billy Ashmall](https://github.com/Unidata/tds/discussions/346){:target="_blank"}!) +* `stylesLocationDir`: optionally specify the location of the directory containing your own style files, by specifying the directory + where they are contained. + * If the directory location starts with a `/`, the path is absolute, otherwise it is relative to `${tds.content.root.path}/thredds/`. + * The default directory for custom styles files is `${tds.content.root.path}/thredds/wmsStyles`. + * If you don't specify a custom styles directory, or specify it incorrectly, the default directory will be used. + * More information on the format of style files can also be found in the + [ncWMS documentation](https://reading-escience-centre.gitbooks.io/ncwms-user-guide/content/06-development.html#styles). * `maxImageWidth`: the maximum image width in pixels that this WMS service will return. * `maxImageHeight`: the maximum image height in pixels that this WMS service will return. diff --git a/tds/src/main/java/thredds/server/config/TdsConfigMapper.java b/tds/src/main/java/thredds/server/config/TdsConfigMapper.java index dd56f5160b..acc80df29d 100644 --- a/tds/src/main/java/thredds/server/config/TdsConfigMapper.java +++ b/tds/src/main/java/thredds/server/config/TdsConfigMapper.java @@ -16,6 +16,7 @@ import thredds.server.wms.ThreddsWmsCatalogue; import thredds.server.wms.config.WmsDetailedConfig; import uk.ac.rdg.resc.edal.graphics.utils.ColourPalette; +import uk.ac.rdg.resc.edal.graphics.utils.SldTemplateStyleCatalogue; /** * Centralize the mapping of threddsConfig.xml configuration settings to the data objects used by @@ -173,6 +174,7 @@ enum WmsConfigMappings { WMS_ALLOW("WMS.allow", null, "true"), WMS_ALLOW_REMOTE("WMS.allowRemote", null, "false"), WMS_PALETTE_LOCATION_DIR("WMS.paletteLocationDir", null, null), + WMS_STYLES_LOCATION_DIR("WMS.stylesLocationDir", null, null), WMS_MAXIMUM_IMAGE_WIDTH("WMS.maxImageWidth", null, "2048"), WMS_MAXIMUM_IMAGE_HEIGHT("WMS.maxImageHeight", null, "2048"), WMS_CONFIG_FILE("WMS.configFile", null, null); @@ -200,6 +202,7 @@ String getValueFromThreddsConfig() { static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) { final String defaultPaletteLocation = tdsContext.getThreddsDirectory() + "/wmsPalettes"; + final String defaultStylesLocation = tdsContext.getThreddsDirectory() + "/wmsStyles"; final String defaultWmsConfigFile = tdsContext.getThreddsDirectory() + "/wmsConfig.xml"; wmsConfig.setAllow(Boolean.parseBoolean(WMS_ALLOW.getValueFromThreddsConfig())); @@ -221,6 +224,18 @@ static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) { } } + String stylesLocation = WMS_STYLES_LOCATION_DIR.getValueFromThreddsConfig(); + if (stylesLocation == null) + stylesLocation = defaultStylesLocation; + wmsConfig.setStylesLocationDir(stylesLocation); + try { + SldTemplateStyleCatalogue.getStyleCatalogue().addStylesInDirectory(new File(stylesLocation)); + } catch (FileNotFoundException e) { + if (!stylesLocation.equals(defaultStylesLocation)) { + startupLog.warn("Could not find custom styles directory {}", stylesLocation, e); + } + } + String wmsConfigFile = WMS_CONFIG_FILE.getValueFromThreddsConfig(); if (wmsConfigFile == null) wmsConfigFile = defaultWmsConfigFile; diff --git a/tds/src/main/java/thredds/server/config/WmsConfigBean.java b/tds/src/main/java/thredds/server/config/WmsConfigBean.java index d2bdb4f7e0..4051006e95 100644 --- a/tds/src/main/java/thredds/server/config/WmsConfigBean.java +++ b/tds/src/main/java/thredds/server/config/WmsConfigBean.java @@ -19,6 +19,7 @@ public class WmsConfigBean { private boolean allow; private boolean allowRemote; private String paletteLocationDir; + private String stylesLocationDir; private int maxImageWidth; private int maxImageHeight; @@ -49,6 +50,14 @@ public void setPaletteLocationDir(String paletteLocationDir) { this.paletteLocationDir = paletteLocationDir; } + public String getStylesLocationDir() { + return stylesLocationDir; + } + + public void setStylesLocationDir(String stylesLocationDir) { + this.stylesLocationDir = stylesLocationDir; + } + public int getMaxImageWidth() { return maxImageWidth; } From 857332fe2d260ad3256db7f32de33da2a37962a4 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Thu, 22 Feb 2024 16:26:16 -0700 Subject: [PATCH 2/2] Refactor to helper function to reduce duplication --- .../server/config/TdsConfigMapper.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tds/src/main/java/thredds/server/config/TdsConfigMapper.java b/tds/src/main/java/thredds/server/config/TdsConfigMapper.java index acc80df29d..cb1e3a640b 100644 --- a/tds/src/main/java/thredds/server/config/TdsConfigMapper.java +++ b/tds/src/main/java/thredds/server/config/TdsConfigMapper.java @@ -208,9 +208,8 @@ static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) { wmsConfig.setAllow(Boolean.parseBoolean(WMS_ALLOW.getValueFromThreddsConfig())); wmsConfig.setAllowRemote(Boolean.parseBoolean(WMS_ALLOW_REMOTE.getValueFromThreddsConfig())); - String paletteLocation = WMS_PALETTE_LOCATION_DIR.getValueFromThreddsConfig(); - if (paletteLocation == null) - paletteLocation = defaultPaletteLocation; + final String paletteLocation = + getValueFromThreddsConfigOrDefault(WMS_PALETTE_LOCATION_DIR, defaultPaletteLocation); wmsConfig.setPaletteLocationDir(paletteLocation); try { ColourPalette.addPaletteDirectory(new File(paletteLocation)); @@ -224,9 +223,7 @@ static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) { } } - String stylesLocation = WMS_STYLES_LOCATION_DIR.getValueFromThreddsConfig(); - if (stylesLocation == null) - stylesLocation = defaultStylesLocation; + final String stylesLocation = getValueFromThreddsConfigOrDefault(WMS_STYLES_LOCATION_DIR, defaultStylesLocation); wmsConfig.setStylesLocationDir(stylesLocation); try { SldTemplateStyleCatalogue.getStyleCatalogue().addStylesInDirectory(new File(stylesLocation)); @@ -236,9 +233,7 @@ static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) { } } - String wmsConfigFile = WMS_CONFIG_FILE.getValueFromThreddsConfig(); - if (wmsConfigFile == null) - wmsConfigFile = defaultWmsConfigFile; + final String wmsConfigFile = getValueFromThreddsConfigOrDefault(WMS_CONFIG_FILE, defaultWmsConfigFile); WmsDetailedConfig wdc = WmsDetailedConfig.fromLocation(wmsConfigFile); if (wdc == null) { @@ -276,6 +271,14 @@ static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) { } } + private static String getValueFromThreddsConfigOrDefault(WmsConfigMappings property, String defaultValue) { + final String value = property.getValueFromThreddsConfig(); + if (value == null) { + return defaultValue; + } + return value; + } + enum TdsUpdateConfigMappings { TDSUPDAATE_LOGVERSIONINFO("TdsUpdateConfig.logVersionInfo", null, "true");