Skip to content

Commit

Permalink
Fixing module-default-config
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Oct 20, 2023
1 parent 61fe1bc commit d297806
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ public class PortalConfigurationKeys implements Serializable {
*/
public static final String PORTAL_CONFIG_DIR = CONFIGURATION_BASE + "dir";

/**
* Defines the path, where the module default configs can be found:
* 'META-INF/microprofile-config.properties'
*/
public static final String MODULE_DEFAULT_CONFIG_PROPERTY_PATH = "META-INF/microprofile-config.properties";

/**
* Default portal configuration directory. Must be kept in sync with
* {@code portal-configuration-impl/src/main/resources/META-INF/microprofile-config.yaml}
Expand Down Expand Up @@ -382,8 +388,8 @@ public class PortalConfigurationKeys implements Serializable {
* views / partial trees that do not need any authentication in order to be
* displayed, as comma separated list. Caution: The views are relative to the
* root, usually starting with /faces. The distinct values are to be checked
* using String#startsWith(). The default for the portal is '/guest/'. In
* order to match all views you can use '/'.
* using String#startsWith(). The default for the portal is '/guest/'. In order
* to match all views you can use '/'.
* </p>
*/
public static final String NON_SECURED_VIEWS = VIEW_BASE + "non_secured";
Expand All @@ -398,13 +404,13 @@ public class PortalConfigurationKeys implements Serializable {
* templates that can be overridden by our template mechanisms. But what about
* overriding pages that are delivered from the portal, e.g.
* faces/guest/login.jsf? If a concrete application has its own pages like in
* pep using /pep-ui/guest/login.jsf for login. Due to the deployment
* mechanics of the portal faces/guest/login.jsf will still be accessible. In
* order to deal with this issue the portal view suppression mechanics may be
* used. Caution: The views are relative to the root, usually starting with
* /faces. The distinct values are to be checked using String#startsWith(). The
* default for the portal is '', saying no view is to be suppressed. Multiple
* view supressions can be configured by using the
* pep using /pep-ui/guest/login.jsf for login. Due to the deployment mechanics
* of the portal faces/guest/login.jsf will still be accessible. In order to
* deal with this issue the portal view suppression mechanics may be used.
* Caution: The views are relative to the root, usually starting with /faces.
* The distinct values are to be checked using String#startsWith(). The default
* for the portal is '', saying no view is to be suppressed. Multiple view
* supressions can be configured by using the
* {@linkplain #CONTEXT_PARAM_SEPARATOR} character.
* </p>
*/
Expand All @@ -422,17 +428,15 @@ public class PortalConfigurationKeys implements Serializable {
/**
* Prefix for identifying context-parameter defining the restrictions for
* certain roles: "portal.view.restrict.role.". The portal defines a view-level
* access rights-managements. All Views can be found under '/'. The
* matching of certain views is checked using String#startsWith(). Therefore you
* can omit any suffixes and define partial trees quite easily. The individual
* views and/or subtrees can be separated by colons. If subtree is defined it
* always has to finished by '/', in case of single page without '/'. A concrete
* view restriction is always prepended by the string
* 'portal.view.restrict.role.' Sample:
* portal.view.restrict.role.admin=/admin/,/content/verify will
* result in all views under '/admin/' and the single view
* '/content/verify.jsf' be only accessible if the user has the role
* 'admin'
* access rights-managements. All Views can be found under '/'. The matching of
* certain views is checked using String#startsWith(). Therefore you can omit
* any suffixes and define partial trees quite easily. The individual views
* and/or subtrees can be separated by colons. If subtree is defined it always
* has to finished by '/', in case of single page without '/'. A concrete view
* restriction is always prepended by the string 'portal.view.restrict.role.'
* Sample: portal.view.restrict.role.admin=/admin/,/content/verify will result
* in all views under '/admin/' and the single view '/content/verify.jsf' be
* only accessible if the user has the role 'admin'
*/
public static final String VIEW_ROLE_RESTRICTION_PREFIX = VIEW_BASE + "restrict.role.";

Expand All @@ -443,8 +447,7 @@ public class PortalConfigurationKeys implements Serializable {
* Defines the view that are transient, therefore not part of the state saving.
* Caution: The views are relative to the root, usually starting with /faces.
* The distinct values are to be checked using String#startsWith(). The default
* for the portal is '/guest/'. In order to match all views you can use
* '/'.
* for the portal is '/guest/'. In order to match all views you can use '/'.
* </p>
*/
public static final String TRANSIENT_VIEWS = VIEW_BASE + "transient";
Expand All @@ -467,8 +470,8 @@ public class PortalConfigurationKeys implements Serializable {
* <p>
* Defines the views that are to be excluded from the server-side history, as
* comma separated list. The distinct values are to be checked using
* String#startsWith(). The default for the portal is '/guest/'. In order
* to match all views you can use '/'.
* String#startsWith(). The default for the portal is '/guest/'. In order to
* match all views you can use '/'.
* </p>
*/
public static final String HISTORY_VIEW_EXCLUDE_PARAMETER = HISTORY_BASE + "view_excludes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import de.cuioss.portal.configuration.FileConfigurationSource;
import de.cuioss.portal.configuration.impl.PropertiesConfigurationProvider;
import de.cuioss.tools.collect.CollectionLiterals;
import de.cuioss.tools.io.FileLoader;
import de.cuioss.tools.io.FileLoaderUtility;
import de.cuioss.tools.logging.CuiLogger;
import lombok.Getter;
Expand Down Expand Up @@ -70,15 +71,30 @@ public PropertiesConfigSource(final String path) {
* extension.
*/
public PropertiesConfigSource(final String path, final boolean optional) {
this.path = path;
this(FileLoaderUtility.getLoaderForPath(path), optional);
}

/**
* Instantly loads the given file. The path might be prefixed with one of the
* {@link de.cuioss.tools.io.FileTypePrefix}s.
*
* @param path to the properties file
* @param optional if true, doesn't throw an exception if the file isn't
* available.
*
* @throws IllegalArgumentException if {@code path} is empty or {@code null} or
* does not have a {@code properties} file
* extension.
*/
public PropertiesConfigSource(final FileLoader fileLoader, final boolean optional) {
path = fileLoader.getURL().toString();
validateFileType();
final var fileLoader = FileLoaderUtility.getLoaderForPath(getPath());
if (!optional || fileLoader.isReadable()) {
this.properties = new PropertiesConfigurationProvider(fileLoader).getConfigurationMap();
this.readable = true;
properties = new PropertiesConfigurationProvider(fileLoader).getConfigurationMap();
readable = true;
} else {
this.properties = CollectionLiterals.immutableMap();
this.readable = false;
properties = CollectionLiterals.immutableMap();
readable = false;
}
log.trace("Loaded data for {}: {}", getPath(), getProperties());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
@UtilityClass
public class LoaderUtils {

private static final CuiLogger log = new CuiLogger(LoaderUtils.class);
private static final CuiLogger LOG = new CuiLogger(LoaderUtils.class);

private static final String FILETYPE_NOT_PROVIDED_MSG = "Portal-151: Unsupported configuration file type: {}. Supported is: {}. If you want to support yaml files do not forget to add 'portal-configuration-yaml' to the deployment";

Expand All @@ -54,8 +54,8 @@ public class LoaderUtils {
* @return the {@link Map} of the contained configuration
*/
public static Map<String, String> loadConfigurationFromSource(final FileConfigurationSource source) {
if ((null == source) || isEmpty(source.getPath())) {
log.debug("Nothing to load found");
if (null == source || isEmpty(source.getPath())) {
LOG.debug("Nothing to load found");
return Collections.emptyMap();
}
for (var resolver : loadResolver()) {
Expand All @@ -65,7 +65,7 @@ public static Map<String, String> loadConfigurationFromSource(final FileConfigur
}
}

log.warn(FILETYPE_NOT_PROVIDED_MSG, source, retrieveSupportedSuffixes());
LOG.warn(FILETYPE_NOT_PROVIDED_MSG, source, retrieveSupportedSuffixes());
return Collections.emptyMap();
}

Expand All @@ -77,8 +77,8 @@ public static Map<String, String> loadConfigurationFromSource(final FileConfigur
* @return the {@link Map} of the contained configuration
*/
public static Map<String, String> loadConfigurationFromSource(final FileLoader source) {
if ((null == source) || isEmpty(source.getFileName().getOriginalName())) {
log.debug("Nothing to load found");
if (null == source || isEmpty(source.getFileName().getOriginalName())) {
LOG.debug("Nothing to load found");
return Collections.emptyMap();
}
for (var resolver : loadResolver()) {
Expand All @@ -88,7 +88,7 @@ public static Map<String, String> loadConfigurationFromSource(final FileLoader s
}
}

log.warn(FILETYPE_NOT_PROVIDED_MSG, source, retrieveSupportedSuffixes());
LOG.warn(FILETYPE_NOT_PROVIDED_MSG, source, retrieveSupportedSuffixes());
return Collections.emptyMap();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.cuioss.portal.configuration.impl.source;

import static de.cuioss.portal.configuration.PortalConfigurationKeys.MODULE_DEFAULT_CONFIG_PROPERTY_PATH;

import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

import de.cuioss.portal.configuration.source.PropertiesConfigSource;
import de.cuioss.tools.io.UrlLoader;
import de.cuioss.tools.logging.CuiLogger;

/**
* {@link ConfigSourceProvider} resolving all
* {@link PropertiesDefaultConfigSource#META_INF_LOCATION}
*/
public class PropertiesDefaultConfigSourceProvider implements ConfigSourceProvider {

private static final CuiLogger LOGGER = new CuiLogger(PropertiesDefaultConfigSourceProvider.class);

@Override
public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) {
var list = new ArrayList<ConfigSource>();
try {
var sources = forClassLoader.getResources(MODULE_DEFAULT_CONFIG_PROPERTY_PATH);
while (sources.hasMoreElements()) {
var url = sources.nextElement();
LOGGER.debug("Adding module default configuration '%s'", url);
list.add(new PropertiesConfigSource(new UrlLoader(url), false));
}

} catch (IOException e) {
throw new IllegalStateException(e);
}
LOGGER.debug("Resolving '%s' config-sources");
return list;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
de.cuioss.portal.configuration.impl.source.PropertiesDefaultConfigSourceProvider
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@

import io.smallrye.config.inject.ConfigProducer;

/**
* @author Sven Haag
*/
@EnableAutoWeld
@AddBeanClasses(ConfigProducer.class)
class ConfigDirTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.cuioss.portal.configuration.impl.source;

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.weld.junit5.auto.AddBeanClasses;
import org.jboss.weld.junit5.auto.EnableAutoWeld;
import org.junit.jupiter.api.Test;

import de.cuioss.portal.configuration.PortalConfigurationKeys;
import io.smallrye.config.inject.ConfigProducer;

@EnableAutoWeld
@AddBeanClasses(ConfigProducer.class)
class PropertiesDefaultConfigSourceProviderTest {

@Inject
@ConfigProperty(name = PortalConfigurationKeys.LOCALE_DEFAULT)
String config;

@Test
void shouldLoadConfiguregProperties() {
assertEquals("de", config);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package de.cuioss.portal.core.test.tests.configuration;

import static de.cuioss.portal.configuration.PortalConfigurationKeys.MODULE_DEFAULT_CONFIG_PROPERTY_PATH;

import org.eclipse.microprofile.config.spi.ConfigSource;

import de.cuioss.portal.common.priority.PortalPriorities;
Expand Down Expand Up @@ -44,9 +46,9 @@
*/
public final class PropertiesDefaultConfigSource extends PropertiesConfigSource {

public static final String META_INF_LOCATION = "META-INF/microprofile-config.properties";
@SuppressWarnings("java:S1075") // owolff: the delimiter is correct for classpath-resources
public static final String CLASSPATH_LOCATION = FileTypePrefix.CLASSPATH.getPrefix() + "/" + META_INF_LOCATION;
public static final String CLASSPATH_LOCATION = FileTypePrefix.CLASSPATH.getPrefix() + "/"
+ MODULE_DEFAULT_CONFIG_PROPERTY_PATH;

/**
* Loading properties from {@link #CLASSPATH_LOCATION}. This basically is a
Expand Down

0 comments on commit d297806

Please sign in to comment.