From 7ea603c24cf0aa2635e60d8849d00a8127542942 Mon Sep 17 00:00:00 2001 From: joerg1985 <16140691+joerg1985@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:25:14 +0100 Subject: [PATCH] Use precompiled regular expressions to validate the segments of a UID (#4064) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörg Sautter --- .../rest/core/internal/item/MetadataSelectorMatcher.java | 3 +-- .../main/java/org/openhab/core/common/AbstractUID.java | 9 +++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcher.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcher.java index f1f13df4540..104a53b5415 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcher.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/MetadataSelectorMatcher.java @@ -78,8 +78,7 @@ public Set filterNamespaces(@Nullable String namespaceSelector, @Nullabl result.addAll(metadataNamespaces); // filter all name spaces which do not match the UID segment pattern (this will be the regex tokens): - return result.stream().filter(namespace -> namespace.matches(AbstractUID.SEGMENT_PATTERN)) - .collect(Collectors.toSet()); + return result.stream().filter(AbstractUID::isValid).collect(Collectors.toSet()); } } } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/AbstractUID.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/AbstractUID.java index 1181151185a..387721c2ca7 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/AbstractUID.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/AbstractUID.java @@ -14,6 +14,7 @@ import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -26,7 +27,7 @@ @NonNullByDefault public abstract class AbstractUID { - public static final String SEGMENT_PATTERN = "[\\w-]*"; + private static final Pattern SEGMENT_PATTERN = Pattern.compile("[\\w-]*"); public static final String SEPARATOR = ":"; private final List segments; private String uid = ""; @@ -95,8 +96,12 @@ protected String getSegment(int segment) { return segments.get(segment); } + public static boolean isValid(@Nullable String segment) { + return segment != null && SEGMENT_PATTERN.matcher(segment).matches(); + } + protected void validateSegment(String segment, int index, int length) { - if (!segment.matches(SEGMENT_PATTERN)) { + if (!isValid(segment)) { throw new IllegalArgumentException(String.format( "ID segment '%s' contains invalid characters. Each segment of the ID must match the pattern %s.", segment, SEGMENT_PATTERN));