Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Dec 5, 2022
2 parents 5f8ea7c + e925b3a commit 5c3223d
Show file tree
Hide file tree
Showing 24 changed files with 643 additions and 35 deletions.
8 changes: 4 additions & 4 deletions bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
<parent>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.caconfig.editor.parent</artifactId>
<version>1.14.0</version>
<version>1.15.0</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<groupId>io.wcm</groupId>
<artifactId>io.wcm.caconfig.editor</artifactId>
<version>1.14.0</version>
<version>1.15.0</version>
<packaging>jar</packaging>

<name>Context-Aware Configuration Editor</name>
Expand All @@ -42,7 +42,7 @@
<site.url.module.prefix>caconfig/editor/bundle</site.url.module.prefix>

<!-- Enable reproducible builds -->
<project.build.outputTimestamp>2022-11-16T10:29:54Z</project.build.outputTimestamp>
<project.build.outputTimestamp>2022-12-05T12:47:52Z</project.build.outputTimestamp>
</properties>

<dependencies>
Expand All @@ -62,7 +62,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.sling.commons</artifactId>
<version>1.4.0</version>
<version>1.6.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2022 wcm.io
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.caconfig.editor;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ProviderType;

/**
* Describes a configuration category.
*/
@ProviderType
public final class ConfigurationCategory implements Comparable<ConfigurationCategory> {

private final String category;
private String label;

/**
* @param category Category name
*/
public ConfigurationCategory(@NotNull String category) {
this.category = category;
}

/**
* @return Category name
*/
public @NotNull String getCategory() {
return this.category;
}

/**
* @return Category label. Falls back to category name if not defined.
*/
public @NotNull String getLabel() {
return StringUtils.defaultString(this.label, this.category);
}

/**
* @param value Category label
* @return this
*/
public ConfigurationCategory label(@Nullable String value) {
this.label = value;
return this;
}

@Override
public int hashCode() {
return this.category.hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof ConfigurationCategory) {
return this.category.equals(((ConfigurationCategory)obj).category);
}
return false;
}

@Override
public int compareTo(ConfigurationCategory other) {
return this.category.compareTo(other.category);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2022 wcm.io
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.caconfig.editor;

import org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ConsumerType;

import io.wcm.sling.commons.caservice.ContextAwareService;

/**
* Provides metadata for configuration categories used by an application.
* <p>
* Applications can set service properties or bundle headers as defined in {@link ContextAwareService} to apply this
* configuration only for resources that match the relevant resource paths.
* </p>
*/
@ConsumerType
public interface ConfigurationCategoryProvider extends ContextAwareService {

/**
* Get metadata for a given configuration name.
* @param category Category name
* @return Metadata or null if category is not known
*/
@Nullable
ConfigurationCategory getCategoryMetadata(@NotNull String category);

/**
* This method is called for configurations that are in use and do not have a category name assigned.
* With this method it's possible to assign categories by inspecting the configuration metadata, or
* to just assign a default category.
* @param configurationMetadata Metadata of configuration that does not have a category defined
* @return Category name or null if no category should be assigned
*/
@Nullable
String getCategory(@NotNull ConfigurationMetadata configurationMetadata);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@
*/
package io.wcm.caconfig.editor;

import org.osgi.annotation.versioning.ProviderType;

/**
* Properties that can be used for configuration property definitions to customize the edit widget within the editor.
*/
@ProviderType
public final class EditorProperties {

private EditorProperties() {
// constants only
}

/**
* Property name for defining a category for configurations. The editor allows to filter by category.
* This property has to be applied to a configuration definition, not to a property definition.
*/
public static final String PROPERTY_CATEGORY = "category";

/**
* Property name for defining the widget type. Values should be one of the WIDGET_TYPE_* properties.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
Expand All @@ -42,12 +44,15 @@
import org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;

import io.wcm.caconfig.editor.ConfigurationCategory;
import io.wcm.caconfig.editor.impl.data.confignames.ConfigCategoryItem;
import io.wcm.caconfig.editor.impl.data.confignames.ConfigNameItem;
import io.wcm.caconfig.editor.impl.data.confignames.ConfigNamesResponse;

Expand Down Expand Up @@ -77,6 +82,9 @@ public class ConfigNamesServlet extends SlingSafeMethodsServlet {
@Reference(policy = ReferencePolicy.STATIC, cardinality = ReferenceCardinality.OPTIONAL,
policyOption = ReferencePolicyOption.GREEDY)
private ConfigurationEditorFilterService configurationEditorFilterService;
@Reference(policy = ReferencePolicy.STATIC, cardinality = ReferenceCardinality.OPTIONAL,
policyOption = ReferencePolicyOption.GREEDY)
private ConfigurationCategoryProviderService configurationCategoryProviderService;

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
Expand All @@ -89,6 +97,7 @@ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHtt
ConfigNamesResponse result = new ConfigNamesResponse();
result.setContextPath(getContextPath(contextResource));
result.setConfigNames(getConfigNames(contextResource));
result.setConfigCategories(getCategories(result.getConfigNames()));

response.setContentType("application/json;charset=" + StandardCharsets.UTF_8.name());
response.getWriter().write(OBJECT_MAPPER.writeValueAsString(result));
Expand Down Expand Up @@ -117,6 +126,7 @@ private Collection<ConfigNameItem> getConfigNames(Resource contextResource) {
item.setConfigName(configName);
item.setLabel(metadata.getLabel());
item.setDescription(metadata.getDescription());
item.setConfigurationCategory(getConfigurationCategory(contextResource, metadata));
item.setCollection(metadata.isCollection());

ConfigurationState state = getConfigurationState(contextResource, configName, metadata.isCollection());
Expand All @@ -131,6 +141,24 @@ private Collection<ConfigNameItem> getConfigNames(Resource contextResource) {
return sortedResult;
}

private @Nullable ConfigurationCategory getConfigurationCategory(Resource contextResource, ConfigurationMetadata metadata) {
if (this.configurationCategoryProviderService == null) {
return ConfigurationCategoryProviderService.getAssignedCategory(metadata);
}
return this.configurationCategoryProviderService.getCategory(contextResource, metadata);
}

@SuppressWarnings("null")
private Collection<ConfigCategoryItem> getCategories(Collection<ConfigNameItem> configNames) {
return configNames.stream()
.map(ConfigNameItem::getConfigurationCategory)
.filter(Objects::nonNull)
.distinct()
.sorted()
.map(category -> new ConfigCategoryItem(category.getCategory(), category.getLabel()))
.collect(Collectors.toList());
}

private boolean allowAdd(Resource contextResource, String configName) {
if (configurationEditorFilterService == null) {
return true;
Expand Down
Loading

0 comments on commit 5c3223d

Please sign in to comment.