This is an automated email from the ASF dual-hosted git repository. pauls pushed a commit to branch Metrics in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-xss.git
commit 3b651d39d6e3a4ab8c7ec4b7acad0c4e4bdcad19 Author: Karl Pauls <karlpa...@gmail.com> AuthorDate: Tue Dec 5 01:10:35 2023 +0100 Make commons metrics optional --- bnd.bnd | 1 + .../org/apache/sling/xss/impl/XSSFilterImpl.java | 12 ++++--- .../apache/sling/xss/impl/XSSMetricsService.java | 40 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/bnd.bnd b/bnd.bnd index 6ff8d06..1c193b8 100644 --- a/bnd.bnd +++ b/bnd.bnd @@ -31,6 +31,7 @@ Import-Package: !bsh, \ !javax.servlet.jsp, \ !javax.servlet.jsp.tagext, \ !sun.io, \ + org.apache.sling.commons.metrics.*;resolution:=optional, \ * Private-Package: org.apache.sling.xss.impl, \ org.apache.batik.*, \ diff --git a/src/main/java/org/apache/sling/xss/impl/XSSFilterImpl.java b/src/main/java/org/apache/sling/xss/impl/XSSFilterImpl.java index 86d9880..98f4c3d 100644 --- a/src/main/java/org/apache/sling/xss/impl/XSSFilterImpl.java +++ b/src/main/java/org/apache/sling/xss/impl/XSSFilterImpl.java @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.Dictionary; import java.util.Hashtable; import java.util.List; +import java.util.Optional; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; @@ -53,6 +54,9 @@ import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Modified; 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 org.osgi.service.metatype.annotations.AttributeDefinition; import org.osgi.service.metatype.annotations.Designate; import org.osgi.service.metatype.annotations.ObjectClassDefinition; @@ -179,13 +183,12 @@ public class XSSFilterImpl implements XSSFilter { @Reference private ServiceUserMapped serviceUserMapped; - @Reference - private MetricsService metricsService; + @Reference(policy=ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.OPTIONAL, policyOption = ReferencePolicyOption.GREEDY) + private volatile XSSMetricsService metricsService; @Reference private XSSStatusService statusService; - private Counter invalidHrefs; private static final String COUNTER_INVALID_HREFS = "xss.invalid_hrefs"; @Override @@ -253,7 +256,7 @@ public class XSSFilterImpl implements XSSFilter { } if (!isValid) { statusService.reportInvalidUrl(url); - invalidHrefs.increment(); + Optional.ofNullable(metricsService).ifPresent(service -> service.invalidHref()); } return isValid; } @@ -261,7 +264,6 @@ public class XSSFilterImpl implements XSSFilter { @Activate @Modified protected void activate(ComponentContext componentContext, Configuration configuration) { - invalidHrefs = metricsService.counter(COUNTER_INVALID_HREFS); // load default handler policyPath = configuration.policyPath(); updatePolicy(); diff --git a/src/main/java/org/apache/sling/xss/impl/XSSMetricsService.java b/src/main/java/org/apache/sling/xss/impl/XSSMetricsService.java new file mode 100644 index 0000000..a4ecf8b --- /dev/null +++ b/src/main/java/org/apache/sling/xss/impl/XSSMetricsService.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one or + * more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you 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. + ******************************************************************************/ +package org.apache.sling.xss.impl; + +import org.apache.sling.commons.metrics.Counter; +import org.apache.sling.commons.metrics.MetricsService; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; + +@Component(service = XSSMetricsService.class) +public class XSSMetricsService { + + private static final String COUNTER_INVALID_HREFS = "xss.invalid_hrefs"; + + private final Counter invalidHrefs; + + @Activate + public XSSMetricsService(@Reference MetricsService metricsService) { + invalidHrefs = metricsService.counter(COUNTER_INVALID_HREFS); + } + + public void invalidHref() { + invalidHrefs.increment(); + } +}