You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2022/08/09 12:20:35 UTC
[sling-org-apache-sling-engine] branch master updated: SLING-11521 : Clean up Engine code
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-engine.git
The following commit(s) were added to refs/heads/master by this push:
new 31eafb6 SLING-11521 : Clean up Engine code
31eafb6 is described below
commit 31eafb67fc0514cf39aea8208c16b87916bc8882
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Aug 9 14:20:27 2022 +0200
SLING-11521 : Clean up Engine code
---
pom.xml | 12 +--
.../apache/sling/engine/impl/SlingMainServlet.java | 1 -
.../engine/impl/SlingRequestProcessorImpl.java | 95 +++++++---------------
.../impl/console/WebConsoleConfigPrinter.java | 18 ++--
.../debug/RequestProgressTrackerLogFilter.java | 2 +-
.../impl/filter/AbstractSlingFilterChain.java | 6 +-
.../engine/impl/filter/ErrorFilterChainStatus.java | 49 +++++++++++
.../impl/filter/ErrorFilterChainThrowable.java | 46 +++++++++++
.../sling/engine/impl/filter/FilterHandle.java | 8 +-
.../sling/engine/impl/filter/FilterPredicate.java | 51 ++++++------
.../engine/impl/filter/ServletFilterManager.java | 65 +++++++--------
.../engine/impl/filter/SlingFilterChainHelper.java | 50 ++++++------
12 files changed, 228 insertions(+), 175 deletions(-)
diff --git a/pom.xml b/pom.xml
index b976ddf..142c958 100644
--- a/pom.xml
+++ b/pom.xml
@@ -137,12 +137,6 @@
<version>2.1.2</version>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.osgi</artifactId>
- <version>2.1.0</version>
- <scope>provided</scope>
- </dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
@@ -153,6 +147,12 @@
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.util.converter</artifactId>
+ <version>1.0.9</version>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
diff --git a/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java b/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
index eabc7eb..3bcbda1 100644
--- a/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
+++ b/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
@@ -41,7 +41,6 @@ import org.apache.sling.api.servlets.ServletResolver;
import org.apache.sling.auth.core.AuthenticationSupport;
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.engine.SlingRequestProcessor;
-import org.apache.sling.engine.impl.console.RequestHistoryConsolePlugin;
import org.apache.sling.engine.impl.debug.RequestInfoProviderImpl;
import org.apache.sling.engine.impl.filter.ServletFilterManager;
import org.apache.sling.engine.impl.helper.ClientAbortException;
diff --git a/src/main/java/org/apache/sling/engine/impl/SlingRequestProcessorImpl.java b/src/main/java/org/apache/sling/engine/impl/SlingRequestProcessorImpl.java
index 8337d5a..622cc83 100644
--- a/src/main/java/org/apache/sling/engine/impl/SlingRequestProcessorImpl.java
+++ b/src/main/java/org/apache/sling/engine/impl/SlingRequestProcessorImpl.java
@@ -47,9 +47,9 @@ import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.ServletResolver;
import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
import org.apache.sling.engine.SlingRequestProcessor;
-import org.apache.sling.engine.impl.console.RequestHistoryConsolePlugin;
import org.apache.sling.engine.impl.debug.RequestInfoProviderImpl;
-import org.apache.sling.engine.impl.filter.AbstractSlingFilterChain;
+import org.apache.sling.engine.impl.filter.ErrorFilterChainStatus;
+import org.apache.sling.engine.impl.filter.ErrorFilterChainThrowable;
import org.apache.sling.engine.impl.filter.FilterHandle;
import org.apache.sling.engine.impl.filter.RequestSlingFilterChain;
import org.apache.sling.engine.impl.filter.ServletFilterManager;
@@ -138,22 +138,13 @@ public class SlingRequestProcessorImpl implements SlingRequestProcessor {
Resource resource = requestData.initResource(resourceResolver);
requestData.initServlet(resource, sr);
- FilterHandle[] filters = filterManager.getFilters(FilterChainType.REQUEST);
- if (filters != null) {
- FilterChain processor = new RequestSlingFilterChain(this,
- filters);
+ final FilterHandle[] filters = filterManager.getFilters(FilterChainType.REQUEST);
+ FilterChain processor = new RequestSlingFilterChain(this, filters);
- request.getRequestProgressTracker().log(
- "Applying " + FilterChainType.REQUEST + "filters");
-
- processor.doFilter(request, response);
-
- } else {
-
- // no filters, directly call resource level filters and servlet
- processComponent(request, response, FilterChainType.COMPONENT);
+ request.getRequestProgressTracker().log(
+ "Applying " + FilterChainType.REQUEST + "filters");
- }
+ processor.doFilter(request, response);
} catch ( final SlingHttpServletResponseImpl.WriterAlreadyClosedException wace ) {
log.error("Writer has already been closed.", wace);
@@ -275,20 +266,12 @@ public class SlingRequestProcessorImpl implements SlingRequestProcessor {
final FilterChainType filterChainType) throws IOException,
ServletException {
- FilterHandle filters[] = filterManager.getFilters(filterChainType);
- if (filters != null) {
+ final FilterHandle filters[] = filterManager.getFilters(filterChainType);
- FilterChain processor = new SlingComponentFilterChain(filters);
- request.getRequestProgressTracker().log(
- "Applying " + filterChainType + "filters");
- processor.doFilter(request, response);
-
- } else {
-
- log.debug("service: No Resource level filters, calling servlet");
- RequestData.service(request, response);
-
- }
+ FilterChain processor = new SlingComponentFilterChain(filters);
+ request.getRequestProgressTracker().log(
+ "Applying " + filterChainType + "filters");
+ processor.doFilter(request, response);
}
// ---------- Generic Content Request processor ----------------------------
@@ -344,26 +327,15 @@ public class SlingRequestProcessorImpl implements SlingRequestProcessor {
// the response output stream if reset does not reset this
response = new ErrorResponseWrapper(response);
- FilterHandle[] filters = filterManager.getFilters(FilterChainType.ERROR);
- if (filters != null && filters.length > 0) {
- FilterChain processor = new AbstractSlingFilterChain(filters) {
+ final FilterHandle[] filters = filterManager.getFilters(FilterChainType.ERROR);
+ FilterChain processor = new ErrorFilterChainStatus(filters, errorHandler, status, message);
+ request.getRequestProgressTracker().log(
+ "Applying " + FilterChainType.ERROR + " filters");
- @Override
- protected void render(SlingHttpServletRequest request,
- SlingHttpServletResponse response) throws IOException {
- errorHandler.handleError(status, message, request, response);
- }
- };
- request.getRequestProgressTracker().log(
- "Applying " + FilterChainType.ERROR + " filters");
-
- try {
- processor.doFilter(request, response);
- } catch (ServletException se) {
- throw new SlingServletException(se);
- }
- } else {
- errorHandler.handleError(status, message, request, response);
+ try {
+ processor.doFilter(request, response);
+ } catch (ServletException se) {
+ throw new SlingServletException(se);
}
}
@@ -376,26 +348,15 @@ public class SlingRequestProcessorImpl implements SlingRequestProcessor {
// the response output stream if reset does not reset this
response = new ErrorResponseWrapper(response);
- FilterHandle[] filters = filterManager.getFilters(FilterChainType.ERROR);
- if (filters != null && filters.length > 0) {
- FilterChain processor = new AbstractSlingFilterChain(filters) {
+ final FilterHandle[] filters = filterManager.getFilters(FilterChainType.ERROR);
+ FilterChain processor = new ErrorFilterChainThrowable(filters, errorHandler, throwable);
+ request.getRequestProgressTracker().log(
+ "Applying " + FilterChainType.ERROR + " filters");
- @Override
- protected void render(SlingHttpServletRequest request,
- SlingHttpServletResponse response) throws IOException {
- errorHandler.handleError(throwable, request, response);
- }
- };
- request.getRequestProgressTracker().log(
- "Applying " + FilterChainType.ERROR + " filters");
-
- try {
- processor.doFilter(request, response);
- } catch (ServletException se) {
- throw new SlingServletException(se);
- }
- } else {
- errorHandler.handleError(throwable, request, response);
+ try {
+ processor.doFilter(request, response);
+ } catch (ServletException se) {
+ throw new SlingServletException(se);
}
}
diff --git a/src/main/java/org/apache/sling/engine/impl/console/WebConsoleConfigPrinter.java b/src/main/java/org/apache/sling/engine/impl/console/WebConsoleConfigPrinter.java
index 1e4b4ad..a81306e 100644
--- a/src/main/java/org/apache/sling/engine/impl/console/WebConsoleConfigPrinter.java
+++ b/src/main/java/org/apache/sling/engine/impl/console/WebConsoleConfigPrinter.java
@@ -42,7 +42,7 @@ public class WebConsoleConfigPrinter {
this.filterManager = filterManager;
}
- public static ServiceRegistration register(final BundleContext bundleContext,
+ public static ServiceRegistration<WebConsoleConfigPrinter> register(final BundleContext bundleContext,
final ServletFilterManager filterManager) {
// first we register the plugin for the filters
final WebConsoleConfigPrinter filterPrinter = new WebConsoleConfigPrinter(filterManager);
@@ -54,12 +54,12 @@ public class WebConsoleConfigPrinter {
serviceProps.put("felix.webconsole.title", "Sling Servlet Filter");
serviceProps.put("felix.webconsole.configprinter.modes", "always");
- return bundleContext.registerService(WebConsoleConfigPrinter.class.getName(),
+ return bundleContext.registerService(WebConsoleConfigPrinter.class,
filterPrinter,
serviceProps);
}
- public static void unregister(final ServiceRegistration reg) {
+ public static void unregister(final ServiceRegistration<WebConsoleConfigPrinter> reg) {
if (reg != null) {
reg.unregister();
}
@@ -69,14 +69,10 @@ public class WebConsoleConfigPrinter {
* Helper method for printing out a filter chain.
*/
private void printFilterChain(final PrintWriter pw, final FilterHandle[] entries) {
- if ( entries == null ) {
- pw.println("---");
- } else {
- for(final FilterHandle entry : entries) {
- pw.printf("%d : %s (id: %d, property: %s); called: %d; time: %dms; time/call: %dµs%n",
- entry.getOrder(), entry.getFilter().getClass(), entry.getFilterId(), entry.getOrderSource(),
- entry.getCalls(), entry.getTime(), entry.getTimePerCall());
- }
+ for(final FilterHandle entry : entries) {
+ pw.printf("%d : %s (id: %d, property: %s); called: %d; time: %dms; time/call: %dµs%n",
+ entry.getOrder(), entry.getFilter().getClass(), entry.getFilterId(), entry.getOrderSource(),
+ entry.getCalls(), entry.getTime(), entry.getTimePerCall());
}
}
diff --git a/src/main/java/org/apache/sling/engine/impl/debug/RequestProgressTrackerLogFilter.java b/src/main/java/org/apache/sling/engine/impl/debug/RequestProgressTrackerLogFilter.java
index 1b19ca3..4019428 100644
--- a/src/main/java/org/apache/sling/engine/impl/debug/RequestProgressTrackerLogFilter.java
+++ b/src/main/java/org/apache/sling/engine/impl/debug/RequestProgressTrackerLogFilter.java
@@ -199,6 +199,6 @@ public class RequestProgressTrackerLogFilter implements Filter {
// extensions needs to be sorted for Arrays.binarySearch() to work
this.extensions = sortAndClean(this.configuration.extensions());
log.debug("activated: extensions = {}, min = {}, max = {}, compact = {}",
- new Object[]{extensions, configuration.minDurationMs(), configuration.maxDurationMs(), configuration.compactLogFormat()});
+ extensions, configuration.minDurationMs(), configuration.maxDurationMs(), configuration.compactLogFormat());
}
}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java b/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java
index 12af6c8..90a0a0b 100644
--- a/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java
+++ b/src/main/java/org/apache/sling/engine/impl/filter/AbstractSlingFilterChain.java
@@ -41,13 +41,13 @@ public abstract class AbstractSlingFilterChain implements FilterChain {
private long[] times;
- protected AbstractSlingFilterChain(FilterHandle[] filters) {
+ protected AbstractSlingFilterChain(final FilterHandle[] filters) {
this.filters = filters;
this.current = -1;
- this.times = (filters != null) ? new long[filters.length + 1] : null;
+ this.times = new long[filters.length + 1];
}
- public void doFilter(ServletRequest request, ServletResponse response)
+ public void doFilter(final ServletRequest request, final ServletResponse response)
throws ServletException, IOException {
final int filterIdx = ++this.current;
diff --git a/src/main/java/org/apache/sling/engine/impl/filter/ErrorFilterChainStatus.java b/src/main/java/org/apache/sling/engine/impl/filter/ErrorFilterChainStatus.java
new file mode 100644
index 0000000..4f0998f
--- /dev/null
+++ b/src/main/java/org/apache/sling/engine/impl/filter/ErrorFilterChainStatus.java
@@ -0,0 +1,49 @@
+/*
+ * 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.engine.impl.filter;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.engine.servlets.ErrorHandler;
+
+
+public class ErrorFilterChainStatus extends AbstractSlingFilterChain {
+
+ private final int status;
+
+ private final String message;
+
+ private final ErrorHandler errorHandler;
+
+ public ErrorFilterChainStatus(final FilterHandle[] filters, final ErrorHandler errorHandler, final int status, final String message) {
+ super(filters);
+ this.status = status;
+ this.message = message;
+ this.errorHandler = errorHandler;
+ }
+
+ protected void render(final SlingHttpServletRequest request,
+ final SlingHttpServletResponse response) throws IOException, ServletException {
+ this.errorHandler.handleError(this.status, this.message, request, response);
+ }
+}
diff --git a/src/main/java/org/apache/sling/engine/impl/filter/ErrorFilterChainThrowable.java b/src/main/java/org/apache/sling/engine/impl/filter/ErrorFilterChainThrowable.java
new file mode 100644
index 0000000..5d5f750
--- /dev/null
+++ b/src/main/java/org/apache/sling/engine/impl/filter/ErrorFilterChainThrowable.java
@@ -0,0 +1,46 @@
+/*
+ * 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.engine.impl.filter;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.engine.servlets.ErrorHandler;
+
+
+public class ErrorFilterChainThrowable extends AbstractSlingFilterChain {
+
+ private final Throwable throwable;
+
+ private final ErrorHandler errorHandler;
+
+ public ErrorFilterChainThrowable(final FilterHandle[] filters, final ErrorHandler errorHandler, final Throwable t) {
+ super(filters);
+ this.throwable = t;
+ this.errorHandler = errorHandler;
+ }
+
+ protected void render(final SlingHttpServletRequest request,
+ final SlingHttpServletResponse response) throws IOException, ServletException {
+ this.errorHandler.handleError(this.throwable, request, response);
+ }
+}
diff --git a/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java b/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java
index 4e281b1..3e713a0 100644
--- a/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java
+++ b/src/main/java/org/apache/sling/engine/impl/filter/FilterHandle.java
@@ -34,13 +34,13 @@ public class FilterHandle implements Comparable<FilterHandle> {
private final String orderSource;
- private AtomicLong calls;
+ private final AtomicLong calls;
- private AtomicLong time;
+ private final AtomicLong time;
- private FilterPredicate predicate;
+ private final FilterPredicate predicate;
- FilterProcessorMBeanImpl mbean;
+ private final FilterProcessorMBeanImpl mbean;
FilterHandle(Filter filter, FilterPredicate predicate, long filterId, int order, final String orderSource,
FilterProcessorMBeanImpl mbean) {
diff --git a/src/main/java/org/apache/sling/engine/impl/filter/FilterPredicate.java b/src/main/java/org/apache/sling/engine/impl/filter/FilterPredicate.java
index 440044e..7b2cdd4 100644
--- a/src/main/java/org/apache/sling/engine/impl/filter/FilterPredicate.java
+++ b/src/main/java/org/apache/sling/engine/impl/filter/FilterPredicate.java
@@ -37,8 +37,8 @@ import javax.servlet.Filter;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
-import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.framework.ServiceReference;
+import org.osgi.util.converter.Converters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -51,27 +51,28 @@ public class FilterPredicate {
private static final Logger LOG = LoggerFactory.getLogger(FilterPredicate.class);
- Collection<String> methods;
- Collection<String> selectors;
- Collection<String> extensions;
- Collection<String> resourceTypes;
- Pattern pathRegex;
- Pattern resourcePathRegex;
- Pattern requestPathRegex;
- Pattern suffixRegex;
+ private final Collection<String> methods;
+ private final Collection<String> selectors;
+ private final Collection<String> extensions;
+ private final Collection<String> resourceTypes;
+ private final Pattern pathRegex;
+ private final Pattern resourcePathRegex;
+ private final Pattern requestPathRegex;
+ private final Pattern suffixRegex;
- /*
+ /**
+ * Create a new predicate
* @param reference osgi service configuration
*/
- public FilterPredicate(ServiceReference<Filter> reference) {
- selectors = asCollection(reference, SLING_FILTER_SELECTORS);
- extensions = asCollection(reference, SLING_FILTER_EXTENSIONS);
- resourceTypes = asCollection(reference, SLING_FILTER_RESOURCETYPES);
- methods = asCollection(reference, SLING_FILTER_METHODS);
- pathRegex = asPattern(reference, SLING_FILTER_PATTERN);
- resourcePathRegex = asPattern(reference, SLING_FILTER_RESOURCE_PATTERN);
- requestPathRegex = asPattern(reference, SLING_FILTER_REQUEST_PATTERN);
- suffixRegex = asPattern(reference, SLING_FILTER_SUFFIX_PATTERN);
+ public FilterPredicate(final ServiceReference<Filter> reference) {
+ this.selectors = asCollection(reference, SLING_FILTER_SELECTORS);
+ this.extensions = asCollection(reference, SLING_FILTER_EXTENSIONS);
+ this.resourceTypes = asCollection(reference, SLING_FILTER_RESOURCETYPES);
+ this.methods = asCollection(reference, SLING_FILTER_METHODS);
+ this.pathRegex = asPattern(reference, SLING_FILTER_PATTERN);
+ this.resourcePathRegex = asPattern(reference, SLING_FILTER_RESOURCE_PATTERN);
+ this.requestPathRegex = asPattern(reference, SLING_FILTER_REQUEST_PATTERN);
+ this.suffixRegex = asPattern(reference, SLING_FILTER_SUFFIX_PATTERN);
}
/**
@@ -80,7 +81,7 @@ public class FilterPredicate {
* @return value of the given property, as a collection, or null if it does not exist
*/
private Collection<String> asCollection(final ServiceReference<Filter> reference, final String propertyName) {
- String[] value = PropertiesUtil.toStringArray(reference.getProperty(propertyName));
+ final String[] value = Converters.standardConverter().convert(reference.getProperty(propertyName)).to(String[].class);
return value != null && value.length > 0 ? asList(value) : null;
}
@@ -90,7 +91,7 @@ public class FilterPredicate {
* @return value of the given property, as a compiled pattern, or null if it does not exist
*/
private Pattern asPattern(final ServiceReference<Filter> reference, String propertyName) {
- String pattern = PropertiesUtil.toString(reference.getProperty(propertyName), null);
+ String pattern = Converters.standardConverter().convert(reference.getProperty(propertyName)).to(String.class);
return pattern != null && pattern.length() > 0 ? Pattern.compile(pattern) : null;
}
@@ -134,11 +135,11 @@ public class FilterPredicate {
* @param req request that is tested upon this predicate
* @return true if this predicate's configuration match the request
*/
- boolean test(SlingHttpServletRequest req) {
+ boolean test(final SlingHttpServletRequest req) {
LOG.debug("starting filter test against {} request", req);
- RequestPathInfo requestPathInfo = req.getRequestPathInfo();
- String path = requestPathInfo.getResourcePath();
- String uri = req.getPathInfo();
+ final RequestPathInfo requestPathInfo = req.getRequestPathInfo();
+ final String path = requestPathInfo.getResourcePath();
+ final String uri = req.getPathInfo();
boolean select = anyElementMatches(methods, req.getMethod())
&& anyElementMatches(selectors, requestPathInfo.getSelectors())
&& anyElementMatches(extensions, requestPathInfo.getExtension())
diff --git a/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java b/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java
index d22676a..34e713e 100644
--- a/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java
+++ b/src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java
@@ -27,7 +27,6 @@ import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
-import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.engine.EngineConstants;
import org.apache.sling.engine.impl.console.WebConsoleConfigPrinter;
import org.apache.sling.engine.impl.helper.SlingFilterConfig;
@@ -39,6 +38,7 @@ import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.converter.Converters;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -104,7 +104,7 @@ public class ServletFilterManager extends ServiceTracker<Filter, Filter> {
private final Map<Long, MBeanReg> mbeanMap = new ConcurrentHashMap<>();
- private volatile ServiceRegistration printerRegistration;
+ private volatile ServiceRegistration<WebConsoleConfigPrinter> printerRegistration;
private static final org.osgi.framework.Filter SERVICE_FILTER;
static {
@@ -269,51 +269,52 @@ public class ServletFilterManager extends ServiceTracker<Filter, Filter> {
orderObj = reference.getProperty(EngineConstants.FILTER_ORDER);
if (orderObj != null) {
log.warn("Filter service {} is using deprecated property {}. Use {} instead.",
- new Object[] { reference, EngineConstants.FILTER_ORDER, Constants.SERVICE_RANKING });
+ reference, EngineConstants.FILTER_ORDER, Constants.SERVICE_RANKING );
// we can use 0 as the default as this will be applied
// in the next step anyway if this props contains an
// invalid value
- orderSource = EngineConstants.FILTER_ORDER + "=" + orderObj;
- orderObj = Integer.valueOf(-1 * OsgiUtil.toInteger(orderObj, 0));
+ orderSource = EngineConstants.FILTER_ORDER.concat("=").concat(orderObj.toString());
+ orderObj = Integer.valueOf(-1 * Converters.standardConverter().convert(orderObj).defaultValue(0).to(Integer.class));
} else {
orderSource = "none";
}
} else {
- orderSource = Constants.SERVICE_RANKING + "=" + orderObj;
+ orderSource = Constants.SERVICE_RANKING.concat("=").concat(orderObj.toString());
}
final int order = (orderObj instanceof Integer) ? ((Integer) orderObj).intValue() : 0;
// register by scope
- String[] scopes = OsgiUtil.toStringArray(reference.getProperty(EngineConstants.SLING_FILTER_SCOPE), null);
-
- FilterPredicate predicate = new FilterPredicate(reference);
-
- if (scopes == null) {
- scopes = OsgiUtil.toStringArray(reference.getProperty(EngineConstants.FILTER_SCOPE), null);
+ Object scopeValue = reference.getProperty(EngineConstants.SLING_FILTER_SCOPE);
+ if ( scopeValue == null ) {
+ scopeValue = reference.getProperty(EngineConstants.FILTER_SCOPE);
log.warn("Filter service {} is using deprecated property {}. Use {} instead.",
- new Object[] { reference, EngineConstants.FILTER_SCOPE, EngineConstants.SLING_FILTER_SCOPE });
+ reference, EngineConstants.FILTER_SCOPE, EngineConstants.SLING_FILTER_SCOPE );
}
- if (scopes != null && scopes.length > 0) {
- for (String scope : scopes) {
- scope = scope.toUpperCase();
- try {
- FilterChainType type = FilterChainType.valueOf(scope.toString());
- getFilterChain(type).addFilter(filter, predicate, serviceId, order, orderSource, mbean);
-
- if (type == FilterChainType.COMPONENT) {
- getFilterChain(FilterChainType.INCLUDE).addFilter(filter, predicate, serviceId, order,
- orderSource, mbean);
- getFilterChain(FilterChainType.FORWARD).addFilter(filter, predicate, serviceId, order,
- orderSource, mbean);
- }
-
- } catch (IllegalArgumentException iae) {
- // TODO: log ...
+ final String[] scopes = Converters.standardConverter().convert(scopeValue).to(String[].class);
+ final FilterPredicate predicate = new FilterPredicate(reference);
+
+ boolean used = false;
+ for (String scope : scopes) {
+ scope = scope.toUpperCase();
+ try {
+ FilterChainType type = FilterChainType.valueOf(scope.toString());
+ getFilterChain(type).addFilter(filter, predicate, serviceId, order, orderSource, mbean);
+
+ if (type == FilterChainType.COMPONENT) {
+ getFilterChain(FilterChainType.INCLUDE).addFilter(filter, predicate, serviceId, order,
+ orderSource, mbean);
+ getFilterChain(FilterChainType.FORWARD).addFilter(filter, predicate, serviceId, order,
+ orderSource, mbean);
}
+
+ used = true;
+ } catch (final IllegalArgumentException iae) {
+ log.warn("Filter service {} has invalid value {} for scope. Value is ignored", reference, scope);
}
- } else {
- log.warn(String.format("A Filter (Service ID %s) has been registered without a %s property.", serviceId,
- EngineConstants.SLING_FILTER_SCOPE));
+ }
+ if ( !used ){
+ log.warn("Filter service {} has been registered without a valid {} property. Using default value.", serviceId,
+ EngineConstants.SLING_FILTER_SCOPE);
getFilterChain(FilterChainType.REQUEST).addFilter(filter, predicate, serviceId, order, orderSource, mbean);
}
diff --git a/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java b/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java
index 24fcb90..481ea04 100644
--- a/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java
+++ b/src/main/java/org/apache/sling/engine/impl/filter/SlingFilterChainHelper.java
@@ -35,32 +35,33 @@ public class SlingFilterChainHelper {
private static final FilterHandle[] EMPTY_FILTER_ARRAY = new FilterHandle[0];
- private SortedSet<FilterHandle> filterList;
+ private final SortedSet<FilterHandle> filterList = new TreeSet<FilterHandle>();
- private FilterHandle[] filters = EMPTY_FILTER_ARRAY;
+ private volatile FilterHandle[] filters = EMPTY_FILTER_ARRAY;
- SlingFilterChainHelper() {
- }
-
- public synchronized Filter addFilter(final Filter filter, FilterPredicate pattern,
+ /**
+ * Add a filter
+ * @param filter The filter
+ * @param pattern Optional pattern
+ * @param filterId Id of the filter
+ * @param order The order index
+ * @param orderSource The source for the order
+ * @param mbean MBean
+ * @return
+ */
+ public synchronized void addFilter(final Filter filter, FilterPredicate pattern,
final long filterId, final int order, final String orderSource, FilterProcessorMBeanImpl mbean) {
- if (filterList == null) {
- filterList = new TreeSet<FilterHandle>();
- }
- filterList.add(new FilterHandle(filter, pattern, filterId, order, orderSource, mbean));
- filters = getFiltersInternal();
- return filter;
+ this.filterList.add(new FilterHandle(filter, pattern, filterId, order, orderSource, mbean));
+ this.filters = getFiltersInternal();
}
public synchronized boolean removeFilterById(final long filterId) {
- if (filterList != null) {
- for (Iterator<FilterHandle> fi = filterList.iterator(); fi.hasNext();) {
- FilterHandle test = fi.next();
- if (test.getFilterId() == filterId) {
- fi.remove();
- filters = getFiltersInternal();
- return true;
- }
+ for (Iterator<FilterHandle> fi = filterList.iterator(); fi.hasNext();) {
+ final FilterHandle test = fi.next();
+ if (test.getFilterId() == filterId) {
+ fi.remove();
+ this.filters = getFiltersInternal();
+ return true;
}
}
@@ -69,10 +70,9 @@ public class SlingFilterChainHelper {
}
/**
- * Returns the list of <code>Filter</code>s added to this instance
- * or <code>null</code> if no filters have been added.
- * This method doesn't need to be synced as it is called from synced methods.
- *
+ * Returns the list of {@code Filter}s added to this instance.
+ * The array might be empty
+ * This method doesn't need to be synced as the update is atomioc.
* @return the filters
*/
public FilterHandle[] getFilters() {
@@ -80,7 +80,7 @@ public class SlingFilterChainHelper {
}
private FilterHandle[] getFiltersInternal() {
- if (filterList == null || filterList.isEmpty()) {
+ if (filterList.isEmpty()) {
return EMPTY_FILTER_ARRAY;
}
return filterList.toArray(new FilterHandle[filterList.size()]);