Skip to content

Commit

Permalink
Merge pull request #325 from ist-dresden/bugfix/caconfig
Browse files Browse the repository at this point in the history
Bugfixes for context aware configuration display
  • Loading branch information
stoerr authored Mar 21, 2024
2 parents 8336b38 + d7a43d1 commit 74b3bbb
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 196 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/createerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ jobs:

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 3

- name: print configuration
run: |
Expand Down Expand Up @@ -182,6 +180,7 @@ jobs:
run: |
# we are now at the "prepare for next development iteration" commit, the commit with the release is the previous one.
# We need to set the master branch to that commit, and then push it.
git fetch --all
git branch temp develop~1
git push origin temp:master
git branch -d temp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.util.SortedSet;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
Expand All @@ -27,6 +30,8 @@
import org.apache.sling.caconfig.management.ConfigurationData;
import org.apache.sling.caconfig.management.ConfigurationManager;
import org.apache.sling.caconfig.management.ValueInfo;
import org.apache.sling.caconfig.management.multiplexer.ContextPathStrategyMultiplexer;
import org.apache.sling.caconfig.resource.spi.ContextResource;
import org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata;
import org.apache.sling.caconfig.spi.metadata.PropertyMetadata;
import org.jetbrains.annotations.NotNull;
Expand All @@ -46,8 +51,12 @@ public class CAConfigModel extends ConsoleServletBean {

private static final Logger LOG = LoggerFactory.getLogger(CAConfigModel.class);

@Inject
protected ConfigurationManager configurationManager;

@Inject
protected ContextPathStrategyMultiplexer contextPathStrategyMultiplexer;

public CAConfigModel(BeanContext context, Resource resource) {
super(context, resource);
}
Expand All @@ -60,17 +69,16 @@ public CAConfigModel() {
super();
}

@Override
@NotNull
public ResourceHandle getResource() {
Resource resource = null;

public void initialize(BeanContext context, Resource resource) {
Resource suffixResource = null;
final SlingHttpServletRequest request = context.getRequest();
final RequestPathInfo pathInfo = request.getRequestPathInfo();
final String suffix = pathInfo.getSuffix();
if (StringUtils.isNotBlank(suffix)) {
resource = request.getResourceResolver().getResource(suffix);
suffixResource = request.getResourceResolver().getResource(suffix);
}
return resource != null ? ResourceHandle.use(resource) : super.getResource();
super.initialize(context, suffixResource != null ? suffixResource : resource);
}

public String getViewType() {
Expand All @@ -82,15 +90,17 @@ public String getViewType() {
return "listConfigurationsView";
} else if (getResource().getParent().getName().equals("sling:configs")) {
String configName = getResource().getName();
ConfigurationMetadata metadata = getConfigurationManager().getConfigurationMetadata(configName);
if (metadata.isCollection()) {
ConfigurationMetadata metadata = configurationManager.getConfigurationMetadata(configName);
if (metadata == null) {
return null;
} else if (metadata.isCollection()) {
return "collectionView";
} else {
return "configurationView";
}
} else if (getResource().getParent().getParent().getName().equals("sling:configs")) {
String configName = getResource().getParent().getName();
ConfigurationMetadata metadata = getConfigurationManager().getConfigurationMetadata(configName);
ConfigurationMetadata metadata = configurationManager.getConfigurationMetadata(configName);
if (metadata.isCollection()) {
return "configurationView";
}
Expand All @@ -102,40 +112,50 @@ public String getViewType() {
return null;
}

protected ConfigurationManager getConfigurationManager() {
if (configurationManager == null) {
configurationManager = context.getService(ConfigurationManager.class);
public List<String> getGlobalConfigPaths() {
List<String> paths = new ArrayList<>();
for (String possibleDefault : new String[]{"/conf/global/sling:configs", "/apps/conf/sling:configs", "/libs/conf/sling:configs"}) {
// add the resource to the list if it exists
Resource defaultResource = getResolver().getResource(possibleDefault);
if (defaultResource != null) {
paths.add(possibleDefault);
}
}
return configurationManager;
return paths;
}

public List<ContextResource> getContextPaths() {
List<ContextResource> contextResources = IteratorUtils.toList(contextPathStrategyMultiplexer.findContextResources(getResource()));
return contextResources;
}

/**
* Returns all configuration metadata.
*/
public List<ConfigurationMetadata> getAllMetaData() {
SortedSet<String> names = getConfigurationManager().getConfigurationNames();
SortedSet<String> names = configurationManager.getConfigurationNames();
return names.stream()
.map(name -> getConfigurationManager().getConfigurationMetadata(name))
.map(name -> configurationManager.getConfigurationMetadata(name))
.collect(Collectors.toList());
}

public List<SingletonConfigInfo> getSingletonConfigurations() {
SortedSet<String> names = getConfigurationManager().getConfigurationNames();
SortedSet<String> names = configurationManager.getConfigurationNames();
List<SingletonConfigInfo> result = names.stream()
.filter(name ->
requireNonNull(getConfigurationManager().getConfigurationMetadata(name)).isSingleton())
requireNonNull(configurationManager.getConfigurationMetadata(name)).isSingleton())
.map(name ->
new SingletonConfigInfo(name, getConfigurationManager().getConfiguration(resource, name)))
new SingletonConfigInfo(name, configurationManager.getConfiguration(resource, name)))
.collect(Collectors.toList());
return result;
}

public List<CollectionConfigInfo> getCollectionConfigurations() {
SortedSet<String> names = getConfigurationManager().getConfigurationNames();
SortedSet<String> names = configurationManager.getConfigurationNames();
List<CollectionConfigInfo> result = names.stream()
.filter(name ->
requireNonNull(getConfigurationManager().getConfigurationMetadata(name)).isCollection())
.map(name -> new CollectionConfigInfo(getConfigurationManager().getConfigurationCollection(resource, name)))
requireNonNull(configurationManager.getConfigurationMetadata(name)).isCollection())
.map(name -> new CollectionConfigInfo(configurationManager.getConfigurationCollection(resource, name)))
.collect(Collectors.toList());
return result;
}
Expand All @@ -161,7 +181,8 @@ public SingletonConfigInfo getThisSingletonConfiguration() {
configResource = configResource.getParent();
}
String configName = configResource != null ? configResource.getName() : null;
ConfigurationData configurationData = configName != null ? getConfigurationManager().getConfiguration(getResource(), configName) : null;
ConfigurationData configurationData;
configurationData = configName != null ? configurationManager.getConfiguration(getResource(), configName) : null;
if (configurationData != null) {
return new SingletonConfigInfo(configName, configurationData);
}
Expand All @@ -181,38 +202,14 @@ public ValueInfo<?> getThisProperty() {
return result;
}

/**
* List of all paths that are referenced from a sling:configRef of the resource or one of it's parents.
*/
public List<String> getReferencedConfigPaths() {
List<String> paths = new ArrayList<>();
Resource resource = getResource();
while (resource != null) {
ValueMap properties = resource.getValueMap();
String configRef = properties.get("sling:configRef", String.class);
if (configRef != null) {
paths.add(configRef + "/sling:configs");
}
resource = resource.getParent();
}
for (String possibleDefault : new String[]{"/conf/global/sling:configs", "/apps/conf/sling:configs", "/libs/conf/sling:configs"}) {
// add the resource to the list if it exists
Resource defaultResource = getResolver().getResource(possibleDefault);
if (defaultResource != null) {
paths.add(possibleDefault);
}
}
return paths;
}

public class CollectionConfigInfo {

protected final ConfigurationCollectionData collectionConfigData;
protected final ConfigurationMetadata metadata;

public CollectionConfigInfo(ConfigurationCollectionData configurationCollection) {
this.collectionConfigData = configurationCollection;
this.metadata = getConfigurationManager().getConfigurationMetadata(collectionConfigData.getConfigName());
this.metadata = configurationManager.getConfigurationMetadata(collectionConfigData.getConfigName());
}

/**
Expand Down Expand Up @@ -250,7 +247,7 @@ public class SingletonConfigInfo {

public SingletonConfigInfo(String name, ConfigurationData configurationData) {
this.name = name;
this.metadata = getConfigurationManager().getConfigurationMetadata(name);
this.metadata = configurationManager.getConfigurationMetadata(name);
this.configurationData = configurationData;
}

Expand Down Expand Up @@ -311,7 +308,7 @@ public class PropertyInfo {

public PropertyInfo(String name, ValueInfo<?> valueInfo) {
this.name = name;
this.metadata = getConfigurationManager().getConfigurationMetadata(name);
this.metadata = configurationManager.getConfigurationMetadata(name);
this.valueInfo = valueInfo;
this.type = valueInfo != null && valueInfo.getPropertyMetadata() != null ? valueInfo.getPropertyMetadata().getType() : null;
}
Expand Down Expand Up @@ -356,7 +353,9 @@ public boolean isRequired() {
&& "true".equals(propMetadata.getProperties().get("required"));
}

/** All properties except "required", which is handled in {@link #isRequired()}. */
/**
* All properties except "required", which is handled in {@link #isRequired()}.
*/
public Properties getProperties() {
Properties props = new Properties();
if (valueInfo.getPropertyMetadata() != null && valueInfo.getPropertyMetadata().getProperties() != null) {
Expand All @@ -366,7 +365,9 @@ public Properties getProperties() {
return props;
}

/** URL encoded value of {@link #getProperties()} to easier put that into an attribute. */
/**
* URL encoded value of {@link #getProperties()} to easier put that into an attribute.
*/
public String getPropertiesJsonEncoded() throws UnsupportedEncodingException {
Properties props = getProperties();
String propertiesJson = props != null ? toJson(getProperties()) : null;
Expand Down
Loading

0 comments on commit 74b3bbb

Please sign in to comment.