This is an automated email from the ASF dual-hosted git repository. cziegeler pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
The following commit(s) were added to refs/heads/master by this push: new 6253610 SLING-12060 : Remove dependency to Sling osgi.commons 6253610 is described below commit 6253610756c206fa340f27c2b2903bf09f82f116 Author: Carsten Ziegeler <cziege...@apache.org> AuthorDate: Sun Oct 1 12:09:23 2023 +0200 SLING-12060 : Remove dependency to Sling osgi.commons --- pom.xml | 6 +-- .../bundle/BundledRenderUnitCapabilityImpl.java | 9 ++-- .../internal/bundle/BundledScriptTracker.java | 60 +++++++++++----------- .../resource/ServletResourceProviderFactory.java | 56 ++++++++++---------- 4 files changed, 65 insertions(+), 66 deletions(-) diff --git a/pom.xml b/pom.xml index 9536d32..8c59441 100644 --- a/pom.xml +++ b/pom.xml @@ -243,9 +243,9 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.commons.osgi</artifactId> - <version>2.2.0</version> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.util.converter</artifactId> + <version>1.0.9</version> <scope>provided</scope> </dependency> <dependency> diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImpl.java b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImpl.java index 04e0a53..96a422a 100644 --- a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImpl.java +++ b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImpl.java @@ -18,7 +18,6 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ package org.apache.sling.servlets.resolver.internal.bundle; -import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; @@ -29,12 +28,13 @@ import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.apache.sling.api.resource.type.ResourceType; import org.apache.sling.api.servlets.ServletResolverConstants; -import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.scripting.spi.bundle.BundledRenderUnitCapability; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.osgi.framework.Version; import org.osgi.framework.wiring.BundleCapability; +import org.osgi.util.converter.Converters; +import org.osgi.util.converter.TypeReference; class BundledRenderUnitCapabilityImpl implements BundledRenderUnitCapability { @@ -167,7 +167,7 @@ class BundledRenderUnitCapabilityImpl implements BundledRenderUnitCapability { Map<String, Object> attributes = capability.getAttributes(); Set<ResourceType> resourceTypes = new LinkedHashSet<>(); String[] capabilityResourceTypes = - PropertiesUtil.toStringArray(attributes.get(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES), new String[0]); + Converters.standardConverter().convert(attributes.get(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)).to(String[].class); Version version = (Version) attributes.get(BundledScriptTracker.AT_VERSION); for (String rt : capabilityResourceTypes) { if (version == null) { @@ -179,8 +179,7 @@ class BundledRenderUnitCapabilityImpl implements BundledRenderUnitCapability { return new BundledRenderUnitCapabilityImpl( resourceTypes, (String) attributes.get(ServletResolverConstants.SLING_SERVLET_PATHS), - Arrays.asList( - PropertiesUtil.toStringArray(attributes.get(ServletResolverConstants.SLING_SERVLET_SELECTORS), new String[0])), + Converters.standardConverter().convert(attributes.get(ServletResolverConstants.SLING_SERVLET_SELECTORS)).to(new TypeReference<List<String>>() {}), (String) attributes.get(ServletResolverConstants.SLING_SERVLET_EXTENSIONS), (String) attributes.get(ServletResolverConstants.SLING_SERVLET_METHODS), (String) attributes.get(BundledScriptTracker.AT_EXTENDS), diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptTracker.java b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptTracker.java index c907d53..6b9144d 100644 --- a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptTracker.java +++ b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptTracker.java @@ -57,7 +57,6 @@ import org.apache.sling.api.request.RequestDispatcherOptions; import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.api.resource.type.ResourceType; import org.apache.sling.api.servlets.ServletResolverConstants; -import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.scripting.spi.bundle.BundledRenderUnit; import org.apache.sling.scripting.spi.bundle.BundledRenderUnitCapability; import org.apache.sling.scripting.spi.bundle.BundledRenderUnitFinder; @@ -84,6 +83,8 @@ import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ReferencePolicy; +import org.osgi.util.converter.Converter; +import org.osgi.util.converter.Converters; import org.osgi.util.tracker.BundleTracker; import org.osgi.util.tracker.BundleTrackerCustomizer; import org.slf4j.Logger; @@ -285,17 +286,15 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic properties.put(ServletResolverConstants.SLING_SERVLET_PATHS, paths.toArray(new String[0])); } if (!properties.containsKey(ServletResolverConstants.SLING_SERVLET_PATHS)) { - String[] rts = PropertiesUtil.toStringArray(properties.get(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)); - if (rts != null) { - for (String resourceType : rts) { - String path; - if (resourceType.startsWith("/")) { - path = resourceType + "/" + resourceType.substring(resourceType.lastIndexOf('/') + 1) + "." + FilenameUtils.getExtension(scriptName); - } else { - path = this.searchPaths.get(0) + resourceType + "/" + resourceType.substring(resourceType.lastIndexOf('/') + 1) + "." + FilenameUtils.getExtension(scriptName); - } - properties.put(ServletResolverConstants.SLING_SERVLET_PATHS, path); + String[] rts = Converters.standardConverter().convert(properties.get(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)).to(String[].class); + for (String resourceType : rts) { + String path; + if (resourceType.startsWith("/")) { + path = resourceType + "/" + resourceType.substring(resourceType.lastIndexOf('/') + 1) + "." + FilenameUtils.getExtension(scriptName); + } else { + path = this.searchPaths.get(0) + resourceType + "/" + resourceType.substring(resourceType.lastIndexOf('/') + 1) + "." + FilenameUtils.getExtension(scriptName); } + properties.put(ServletResolverConstants.SLING_SERVLET_PATHS, path); } } } @@ -467,6 +466,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic } Map<Set<String>, ServiceRegistration<Servlet>> oldDispatchers = dispatchers.get(); Map<Set<String>, ServiceRegistration<Servlet>> newDispatchers = new HashMap<>(); + final Converter c = Converters.standardConverter(); Stream.concat(tracked.values().stream(), Stream.of(regs)).flatMap(List::stream) .filter(ref -> getResourceTypeVersion(ref.getReference()) != null) .map(this::toProperties) @@ -477,9 +477,9 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic properties.put(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, rt.toArray()); Set<String> methods = propList.stream() .map(props -> props.getOrDefault(ServletResolverConstants.SLING_SERVLET_METHODS, new String[]{"GET", "HEAD"})) - .map(PropertiesUtil::toStringArray).map(Arrays::asList).flatMap(List::stream).collect(Collectors.toSet()); + .map(v -> c.convert(v).to(String[].class)).map(Arrays::asList).flatMap(List::stream).collect(Collectors.toSet()); Set<String> extensions = propList.stream().map(props -> props.getOrDefault(ServletResolverConstants - .SLING_SERVLET_EXTENSIONS, new String[]{"html"})).map(PropertiesUtil::toStringArray).map(Arrays::asList).flatMap + .SLING_SERVLET_EXTENSIONS, new String[]{"html"})).map(v -> c.convert(v).to(String[].class)).map(Arrays::asList).flatMap (List::stream).collect(Collectors.toSet()); properties.put(ServletResolverConstants.SLING_SERVLET_EXTENSIONS, extensions.toArray(new String[0])); if (!methods.equals(new HashSet<>(Arrays.asList("GET", "HEAD")))) { @@ -504,8 +504,8 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic reg = register(registeringBundle.orElse(bc), new DispatcherServlet(rt), properties); } else { - if (!new HashSet<>(Arrays.asList(PropertiesUtil - .toStringArray(reg.getReference().getProperty(ServletResolverConstants.SLING_SERVLET_METHODS), new String[0]))) + if (!new HashSet<>(Arrays.asList(Converters.standardConverter() + .convert(reg.getReference().getProperty(ServletResolverConstants.SLING_SERVLET_METHODS)).to(String[].class))) .equals(methods)) { reg.setProperties(properties); } @@ -571,6 +571,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic BundleContext bc = bundleContext.get(); final Bundle bcBundle = bc == null ? null : bc.getBundle(); + final Converter c = Converters.standardConverter(); Optional<ServiceRegistration<Servlet>> target = tracked.values().stream().flatMap(List::stream) .filter( reg -> !reg.getReference().getBundle().equals(bcBundle) @@ -580,21 +581,22 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic { Map<String, Object> props = toProperties(reg); return getResourceTypes(props).equals(resourceType) && - Arrays.asList(PropertiesUtil - .toStringArray(props.get(ServletResolverConstants.SLING_SERVLET_METHODS), - new String[]{"GET", "HEAD"})) + Arrays.asList(c + .convert(props.get(ServletResolverConstants.SLING_SERVLET_METHODS)) + .defaultValue(new String[]{"GET", "HEAD"}).to(String[].class)) .contains(slingRequest.getMethod()) && - Arrays.asList(PropertiesUtil - .toStringArray(props.get(ServletResolverConstants.SLING_SERVLET_EXTENSIONS), new String[]{"html"})) + Arrays.asList(c + .convert(props.get(ServletResolverConstants.SLING_SERVLET_EXTENSIONS)) + .defaultValue(new String[]{"html"}).to(String[].class)) .contains(slingRequest.getRequestPathInfo().getExtension() == null ? "html" : slingRequest.getRequestPathInfo().getExtension()); }).min((left, right) -> { - boolean la = Arrays.asList(PropertiesUtil - .toStringArray(toProperties(left).get(ServletResolverConstants.SLING_SERVLET_SELECTORS), new String[0])) + boolean la = Arrays.asList(c + .convert(toProperties(left).get(ServletResolverConstants.SLING_SERVLET_SELECTORS)).to(String[].class)) .containsAll(Arrays.asList(slingRequest.getRequestPathInfo().getSelectors())); - boolean ra = Arrays.asList(PropertiesUtil - .toStringArray(toProperties(right).get(ServletResolverConstants.SLING_SERVLET_SELECTORS), new String[0])) + boolean ra = Arrays.asList(c + .convert(toProperties(right).get(ServletResolverConstants.SLING_SERVLET_SELECTORS)).to(String[].class)) .containsAll(Arrays.asList(slingRequest.getRequestPathInfo().getSelectors())); if ((la && ra) || (!la && !ra)) { Version rightVersion = getResourceTypeVersion(right.getReference()); @@ -616,8 +618,8 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic if (target.isPresent()) { String[] targetRT = - PropertiesUtil.toStringArray(target.get().getReference().getProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)); - if (targetRT == null || targetRT.length == 0) { + c.convert(target.get().getReference().getProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)).to(String[].class); + if (targetRT.length == 0) { ((SlingHttpServletResponse) res).sendError(HttpServletResponse.SC_NOT_FOUND); } else { String rt = targetRT[0]; @@ -647,8 +649,8 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic } private static @Nullable Version getResourceTypeVersion(ServiceReference<?> ref) { - String[] values = PropertiesUtil.toStringArray(ref.getProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)); - if (values != null) { + String[] values = Converters.standardConverter().convert(ref.getProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)).to(String[].class); + if (values.length > 0) { String resourceTypeValue = values[0]; ResourceType resourceType = ResourceType.parseResourceType(resourceTypeValue); return resourceType.getVersion(); @@ -658,7 +660,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic private static Set<String> getResourceTypes(Map<String, Object> props) { Set<String> resourceTypes = new HashSet<>(); - String[] values = PropertiesUtil.toStringArray(props.get(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)); + String[] values = Converters.standardConverter().convert(props.get(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)).to(String[].class); for (String resourceTypeValue : values) { resourceTypes.add(ResourceType.parseResourceType(resourceTypeValue).getType()); } diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java index 169066f..afe703c 100644 --- a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java +++ b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java @@ -38,10 +38,10 @@ import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.api.servlets.HttpConstants; -import org.apache.sling.commons.osgi.PropertiesUtil; import org.osgi.framework.Bundle; import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; +import org.osgi.util.converter.Converters; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -147,10 +147,10 @@ public class ServletResourceProviderFactory { log.debug("create({}): Registering servlet for paths {}", getServiceReferenceInfo(ref), pathSet); } - String resourceSuperType = PropertiesUtil.toString(ref.getProperty(SLING_SERVLET_RESOURCE_SUPER_TYPE), null); + String resourceSuperType = Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_RESOURCE_SUPER_TYPE)).to(String.class); Set<String> resourceSuperTypeMarkers = new HashSet<>(); if (StringUtils.isNotEmpty(resourceSuperType) && !ServletResource.DEFAULT_RESOURCE_SUPER_TYPE.equals(resourceSuperType)) { - for (String rt : PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES))) { + for (String rt : Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES)).to(String[].class)) { if (!rt.startsWith("/")) { rt = getPrefix(ref).concat(ResourceUtil.resourceTypeToPath(rt)); } @@ -211,23 +211,21 @@ public class ServletResourceProviderFactory { * @param ref */ private void addByPath(Set<String> pathSet, ServiceReference<Servlet> ref) { - String[] paths = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_PATHS)); - if (paths != null && paths.length > 0) { - for (String path : paths) { - if (!path.startsWith("/")) { - path = getPrefix(ref).concat(path); - } + String[] paths = Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_PATHS)).to(String[].class); + for (String path : paths) { + if (!path.startsWith("/")) { + path = getPrefix(ref).concat(path); + } - // add the unmodified path - pathSet.add(path); + // add the unmodified path + pathSet.add(path); - String[] types = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES)); + String[] types = Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES)).to(String[].class); - if ((types == null || types.length == 0) || StringUtils.isEmpty(FilenameUtils.getExtension(path))) { - // ensure we have another entry which has the .servlet ext. if there wasn't one to begin with - // Radu says: this will make sure that scripts are equal to servlets in the resolution process - pathSet.add(ensureServletNameExtension(path)); - } + if ((types.length == 0) || StringUtils.isEmpty(FilenameUtils.getExtension(path))) { + // ensure we have another entry which has the .servlet ext. if there wasn't one to begin with + // Radu says: this will make sure that scripts are equal to servlets in the resolution process + pathSet.add(ensureServletNameExtension(path)); } } } @@ -238,13 +236,13 @@ public class ServletResourceProviderFactory { * @param ref */ private void addByType(Set<String> pathSet, ServiceReference<Servlet> ref) { - String[] types = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES)); - String[] paths = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_PATHS)); + String[] types = Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES)).to(String[].class); + String[] paths = Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_PATHS)).to(String[].class); boolean hasPathRegistration = true; - if (paths == null || paths.length == 0) { + if (paths.length == 0) { hasPathRegistration = false; } - if (types == null || types.length == 0) { + if (types.length == 0) { if (log.isDebugEnabled()) { log.debug("addByType({}): no resource types declared", getServiceReferenceInfo(ref)); @@ -253,20 +251,20 @@ public class ServletResourceProviderFactory { } // check for selectors - String[] selectors = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_SELECTORS)); - if (selectors == null) { + String[] selectors = Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_SELECTORS)).to(String[].class); + if (selectors.length == 0) { selectors = new String[] { null }; } // we have types and expect extensions and/or methods - String[] extensions = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_EXTENSIONS)); + String[] extensions = Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_EXTENSIONS)).to(String[].class); // handle the methods property specially (SLING-430) - String[] methods = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_METHODS)); - if (methods == null || methods.length == 0) { + String[] methods = Converters.standardConverter().convert(ref.getProperty(SLING_SERVLET_METHODS)).to(String[].class); + if (methods.length == 0) { // SLING-512 only, set default methods if no extensions are declared - if ((extensions == null || extensions.length == 0) && !hasPathRegistration) { + if (extensions.length == 0 && !hasPathRegistration) { if (log.isDebugEnabled()) { log.debug( "addByType({}): No methods declared, assuming GET/HEAD", @@ -307,8 +305,8 @@ public class ServletResourceProviderFactory { } boolean pathAdded = false; - if (extensions != null) { - if (methods != null) { + if (extensions.length > 0) { + if (methods != null && methods.length > 0) { // both methods and extensions declared for (String ext : extensions) { for (String method : methods) {