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/felix-dev.git
The following commit(s) were added to refs/heads/master by this push: new 2f4a002 Remove unused code/classes and adjust tests 2f4a002 is described below commit 2f4a0025faffd4ddb30eeccecfab75d1373843db Author: Carsten Ziegeler <cziege...@apache.org> AuthorDate: Thu Dec 30 14:10:45 2021 +0100 Remove unused code/classes and adjust tests --- .../http/base/internal/handler/FilterHandler.java | 61 ++++++----- .../internal/handler/HttpServiceFilterHandler.java | 37 ------- .../base/internal/handler/ListenerHandler.java | 74 +++++++------ .../internal/handler/WhiteboardFilterHandler.java | 80 -------------- .../handler/WhiteboardListenerHandler.java | 80 -------------- .../http/base/internal/runtime/FilterInfo.java | 19 ---- .../internal/service/SharedHttpServiceImpl.java | 12 +- .../internal/whiteboard/WhiteboardManager.java | 6 +- .../base/internal/handler/FilterHandlerTest.java | 39 ++++--- .../registry/EventListenerRegistryTest.java | 114 ++++++++----------- .../base/internal/registry/FilterRegistryTest.java | 121 +++++++++++++-------- http/jetty/pom.xml | 2 +- 12 files changed, 227 insertions(+), 418 deletions(-) diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/FilterHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/FilterHandler.java index e7c754c..09de56f 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/FilterHandler.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/FilterHandler.java @@ -27,14 +27,17 @@ import javax.servlet.ServletResponse; import org.apache.felix.http.base.internal.context.ExtServletContext; import org.apache.felix.http.base.internal.logger.SystemLogger; import org.apache.felix.http.base.internal.runtime.FilterInfo; +import org.apache.felix.http.base.internal.util.ServiceUtils; import org.jetbrains.annotations.NotNull; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; import org.osgi.service.http.runtime.dto.DTOConstants; /** * The filter handler handles the initialization and destruction of filter * objects. */ -public abstract class FilterHandler implements Comparable<FilterHandler> +public class FilterHandler implements Comparable<FilterHandler> { private final long contextServiceId; @@ -42,17 +45,21 @@ public abstract class FilterHandler implements Comparable<FilterHandler> private final ExtServletContext context; + private final BundleContext bundleContext; + private volatile Filter filter; protected volatile int useCount; public FilterHandler(final long contextServiceId, final ExtServletContext context, - final FilterInfo filterInfo) + final FilterInfo filterInfo, + final BundleContext bundleContext) { this.contextServiceId = contextServiceId; this.context = context; this.filterInfo = filterInfo; + this.bundleContext = bundleContext; } @Override @@ -76,11 +83,6 @@ public abstract class FilterHandler implements Comparable<FilterHandler> return filter; } - protected void setFilter(final Filter f) - { - this.filter = f; - } - public FilterInfo getFilterInfo() { return this.filterInfo; @@ -112,6 +114,9 @@ public abstract class FilterHandler implements Comparable<FilterHandler> return -1; } + final ServiceReference<Filter> serviceReference = getFilterInfo().getServiceReference(); + this.filter = ServiceUtils.safeGetServiceObjects(this.bundleContext, serviceReference); + if (this.filter == null) { return DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE; @@ -126,8 +131,10 @@ public abstract class FilterHandler implements Comparable<FilterHandler> SystemLogger.error(this.getFilterInfo().getServiceReference(), "Error during calling init() on filter " + this.filter, e); + ServiceUtils.safeUngetServiceObjects(this.bundleContext, serviceReference, this.getFilter()); return DTOConstants.FAILURE_REASON_EXCEPTION_ON_INIT; } + this.useCount++; return -1; } @@ -149,28 +156,30 @@ public abstract class FilterHandler implements Comparable<FilterHandler> public boolean destroy() { - if (this.filter == null) + final Filter f = this.getFilter(); + if ( f != null ) { - return false; - } - - this.useCount--; - if ( this.useCount == 0 ) - { - try + this.useCount--; + if ( this.useCount == 0 ) { - filter.destroy(); + filter = null; + try + { + f.destroy(); + } + catch ( final Exception ignore ) + { + // we ignore this + SystemLogger.error(this.getFilterInfo().getServiceReference(), + "Error during calling destroy() on filter " + f, + ignore); + } + + ServiceUtils.safeUngetServiceObjects(this.bundleContext, + getFilterInfo().getServiceReference(), f); + + return true; } - catch ( final Exception ignore ) - { - // we ignore this - SystemLogger.error(this.getFilterInfo().getServiceReference(), - "Error during calling destroy() on filter " + this.filter, - ignore); - } - - filter = null; - return true; } return false; } diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/HttpServiceFilterHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/HttpServiceFilterHandler.java deleted file mode 100644 index 013af7f..0000000 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/HttpServiceFilterHandler.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.felix.http.base.internal.handler; - -import javax.servlet.Filter; - -import org.apache.felix.http.base.internal.context.ExtServletContext; -import org.apache.felix.http.base.internal.runtime.FilterInfo; -import org.apache.felix.http.base.internal.service.HttpServiceFactory; - -/** - * Servlet handler for filters registered through the ext http service. - */ -public final class HttpServiceFilterHandler extends FilterHandler -{ - public HttpServiceFilterHandler(final ExtServletContext context, - final FilterInfo filterInfo, - final Filter filter) - { - super(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID, context, filterInfo); - this.setFilter(filter); - } -} diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ListenerHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ListenerHandler.java index 415ff63..57e3063 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ListenerHandler.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ListenerHandler.java @@ -20,13 +20,16 @@ import java.util.EventListener; import org.apache.felix.http.base.internal.context.ExtServletContext; import org.apache.felix.http.base.internal.runtime.ListenerInfo; +import org.apache.felix.http.base.internal.util.ServiceUtils; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; import org.osgi.service.http.runtime.dto.DTOConstants; /** * The listener handler handles the initialization and destruction of listener * objects. */ -public abstract class ListenerHandler implements Comparable<ListenerHandler> +public class ListenerHandler implements Comparable<ListenerHandler> { private final long contextServiceId; @@ -34,17 +37,21 @@ public abstract class ListenerHandler implements Comparable<ListenerHandler> private final ExtServletContext context; - private EventListener listener; + private final BundleContext bundleContext; + + private volatile EventListener listener; protected volatile int useCount; public ListenerHandler(final long contextServiceId, final ExtServletContext context, - final ListenerInfo listenerInfo) + final ListenerInfo listenerInfo, + final BundleContext bundleContext) { this.contextServiceId = contextServiceId; this.context = context; this.listenerInfo = listenerInfo; + this.bundleContext = bundleContext; } @Override @@ -68,14 +75,35 @@ public abstract class ListenerHandler implements Comparable<ListenerHandler> return listener; } - protected void setListener(final EventListener f) + public ListenerInfo getListenerInfo() { - this.listener = f; + return this.listenerInfo; } - public ListenerInfo getListenerInfo() + public boolean destroy() { - return this.listenerInfo; + final EventListener l = this.listener; + if ( l != null ) + { + this.useCount--; + if ( this.useCount == 0 ) + { + this.listener = null; + + ServiceUtils.safeUngetServiceObjects(this.bundleContext, + getListenerInfo().getServiceReference(), l); + + return true; + } + } + return false; + } + + public boolean dispose() + { + // fully destroy the listener + this.useCount = 1; + return this.destroy(); } /** @@ -90,37 +118,21 @@ public abstract class ListenerHandler implements Comparable<ListenerHandler> return -1; } - if (this.listener == null) - { - return DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE; - } + final ServiceReference<EventListener> serviceReference = getListenerInfo().getServiceReference(); + this.listener = ServiceUtils.safeGetServiceObjects(this.bundleContext, serviceReference); - this.useCount++; - return -1; - } - - public boolean destroy() - { + final int reason; if (this.listener == null) { - return false; + reason = DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE; } - - this.useCount--; - if ( this.useCount == 0 ) + else { - - listener = null; - return true; + this.useCount++; + reason = -1; } - return false; - } - public boolean dispose() - { - // fully destroy the listener - this.useCount = 1; - return this.destroy(); + return reason; } @Override diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardFilterHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardFilterHandler.java deleted file mode 100644 index 8cb7751..0000000 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardFilterHandler.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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.felix.http.base.internal.handler; - -import javax.servlet.Filter; - -import org.apache.felix.http.base.internal.context.ExtServletContext; -import org.apache.felix.http.base.internal.runtime.FilterInfo; -import org.apache.felix.http.base.internal.util.ServiceUtils; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; - -/** - * Filter holder for filters registered through the http whiteboard. - */ -public final class WhiteboardFilterHandler extends FilterHandler -{ - private final BundleContext bundleContext; - - public WhiteboardFilterHandler(final long contextServiceId, - final ExtServletContext context, - final FilterInfo filterInfo, - final BundleContext bundleContext) - { - super(contextServiceId, context, filterInfo); - this.bundleContext = bundleContext; - } - - @Override - public int init() - { - if ( this.useCount > 0 ) - { - this.useCount++; - return -1; - } - - final ServiceReference<Filter> serviceReference = getFilterInfo().getServiceReference(); - this.setFilter(ServiceUtils.safeGetServiceObjects(this.bundleContext, serviceReference)); - - final int reason = super.init(); - if ( reason != -1 ) - { - ServiceUtils.safeUngetServiceObjects(this.bundleContext, serviceReference, this.getFilter()); - this.setFilter(null); - } - return reason; - } - - @Override - public boolean destroy() - { - final Filter f = this.getFilter(); - if ( f != null ) - { - if ( super.destroy() ) - { - ServiceUtils.safeUngetServiceObjects(this.bundleContext, - getFilterInfo().getServiceReference(), f); - - return true; - } - } - return false; - } -} diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardListenerHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardListenerHandler.java deleted file mode 100644 index 3a4ce9f..0000000 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardListenerHandler.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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.felix.http.base.internal.handler; - -import java.util.EventListener; - -import org.apache.felix.http.base.internal.context.ExtServletContext; -import org.apache.felix.http.base.internal.runtime.ListenerInfo; -import org.apache.felix.http.base.internal.util.ServiceUtils; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; - -/** - * Listener handler for listeners registered through the http whiteboard. - */ -public final class WhiteboardListenerHandler extends ListenerHandler -{ - private final BundleContext bundleContext; - - public WhiteboardListenerHandler(final long contextServiceId, - final ExtServletContext context, - final ListenerInfo listenerInfo, - final BundleContext bundleContext) - { - super(contextServiceId, context, listenerInfo); - this.bundleContext = bundleContext; - } - - @Override - public int init() - { - if ( this.useCount > 0 ) - { - this.useCount++; - return -1; - } - - final ServiceReference<EventListener> serviceReference = getListenerInfo().getServiceReference(); - this.setListener(ServiceUtils.safeGetServiceObjects(this.bundleContext, serviceReference)); - - final int reason = super.init(); - if ( reason != -1 ) - { - ServiceUtils.safeUngetServiceObjects(this.bundleContext, serviceReference, this.getListener()); - this.setListener(null); - } - return reason; - } - - @Override - public boolean destroy() - { - final EventListener l = this.getListener(); - if ( l != null ) - { - if ( super.destroy() ) - { - ServiceUtils.safeUngetServiceObjects(this.bundleContext, - getListenerInfo().getServiceReference(), l); - - return true; - } - } - return false; - } -} diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/FilterInfo.java b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/FilterInfo.java index c477bde..d08dbc0 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/FilterInfo.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/FilterInfo.java @@ -18,7 +18,6 @@ */ package org.apache.felix.http.base.internal.runtime; -import java.util.Collections; import java.util.Map; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -123,24 +122,6 @@ public final class FilterInfo extends WhiteboardServiceInfo<Filter> } } - /** - * Constructor for Http Service - */ - public FilterInfo(final String name, - final String regex, - final int serviceRanking, - final Map<String, String> initParams) - { - super(serviceRanking); - this.name = name; - this.patterns = null; - this.servletNames = null; - this.regexs = new String[] {regex}; - this.initParams = Collections.unmodifiableMap(initParams); - this.asyncSupported = false; - this.dispatcher = new DispatcherType[] {DispatcherType.REQUEST}; - } - @Override public boolean isValid() { diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/service/SharedHttpServiceImpl.java b/http/base/src/main/java/org/apache/felix/http/base/internal/service/SharedHttpServiceImpl.java index 5ac6dec..7ce664b 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/service/SharedHttpServiceImpl.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/service/SharedHttpServiceImpl.java @@ -20,16 +20,15 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import org.jetbrains.annotations.NotNull; import javax.servlet.Servlet; import javax.servlet.ServletException; import org.apache.felix.http.base.internal.context.ExtServletContext; -import org.apache.felix.http.base.internal.handler.FilterHandler; import org.apache.felix.http.base.internal.handler.HttpServiceServletHandler; import org.apache.felix.http.base.internal.handler.ServletHandler; import org.apache.felix.http.base.internal.registry.HandlerRegistry; import org.apache.felix.http.base.internal.runtime.ServletInfo; +import org.jetbrains.annotations.NotNull; import org.osgi.service.http.NamespaceException; public final class SharedHttpServiceImpl @@ -49,15 +48,6 @@ public final class SharedHttpServiceImpl } /** - * Register a filter - */ - public boolean registerFilter(@NotNull final FilterHandler handler) - { - this.handlerRegistry.getRegistry(handler.getContextServiceId()).registerFilter(handler); - return true; - } - - /** * Register a servlet */ public void registerServlet(@NotNull final String alias, diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java index bfcb293..6832efb 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java @@ -53,8 +53,6 @@ import org.apache.felix.http.base.internal.handler.HttpSessionWrapper; import org.apache.felix.http.base.internal.handler.ListenerHandler; import org.apache.felix.http.base.internal.handler.PreprocessorHandler; import org.apache.felix.http.base.internal.handler.ServletHandler; -import org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler; -import org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler; import org.apache.felix.http.base.internal.handler.WhiteboardServletHandler; import org.apache.felix.http.base.internal.logger.SystemLogger; import org.apache.felix.http.base.internal.registry.EventListenerRegistry; @@ -788,7 +786,7 @@ public final class WhiteboardManager } else { - final FilterHandler filterHandler = new WhiteboardFilterHandler( + final FilterHandler filterHandler = new FilterHandler( handler.getContextInfo().getServiceId(), servletContext, (FilterInfo)info, @@ -824,7 +822,7 @@ public final class WhiteboardManager } else { - final ListenerHandler listenerHandler = new WhiteboardListenerHandler( + final ListenerHandler listenerHandler = new ListenerHandler( handler.getContextInfo().getServiceId(), servletContext, (ListenerInfo)info, diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/FilterHandlerTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/FilterHandlerTest.java index 3eefc72..958dbd0 100644 --- a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/FilterHandlerTest.java +++ b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/FilterHandlerTest.java @@ -20,15 +20,12 @@ import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN; import static javax.servlet.http.HttpServletResponse.SC_OK; import static javax.servlet.http.HttpServletResponse.SC_PAYMENT_REQUIRED; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.Collections; -import java.util.Map; - import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -42,6 +39,11 @@ import org.apache.felix.http.base.internal.runtime.FilterInfo; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceObjects; +import org.osgi.framework.ServiceReference; +import org.osgi.service.http.whiteboard.HttpWhiteboardConstants; public class FilterHandlerTest { @@ -86,6 +88,7 @@ public class FilterHandlerTest public void testHandleFound() throws Exception { FilterHandler h1 = createHandler(0, "/a"); + h1.init(); HttpServletRequest req = createServletRequest(); HttpServletResponse res = createServletResponse(); FilterChain chain = mock(FilterChain.class); @@ -102,6 +105,7 @@ public class FilterHandlerTest public void testHandleFoundContextRoot() throws Exception { FilterHandler h1 = createHandler(0, "/"); + h1.init(); HttpServletRequest req = createServletRequest(); HttpServletResponse res = createServletResponse(); FilterChain chain = mock(FilterChain.class); @@ -234,19 +238,24 @@ public class FilterHandlerTest verify(this.filter).init(any(FilterConfig.class)); } - private FilterHandler createHandler(int ranking, String pattern) - { - return createHandler(pattern, ranking, null); - } + private static long id = 1; - private FilterHandler createHandler(String pattern, int ranking, Map<String, String> initParams) + private FilterHandler createHandler(int ranking, String pattern) { - if ( initParams == null ) - { - initParams = Collections.emptyMap(); - } - final FilterInfo info = new FilterInfo(null, pattern, ranking, initParams); - return new HttpServiceFilterHandler(this.context, info, this.filter); + @SuppressWarnings("unchecked") + final ServiceReference<Filter> ref = mock(ServiceReference.class); + when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(id++); + when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(ranking); + when(ref.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN)).thenReturn(pattern); + when(ref.getPropertyKeys()).thenReturn(new String[0]); + final FilterInfo info = new FilterInfo(ref); + + @SuppressWarnings("unchecked") + final ServiceObjects<Filter> so = mock(ServiceObjects.class); + final BundleContext ctx = mock(BundleContext.class); + when(ctx.getServiceObjects(ref)).thenReturn(so); + when(so.getService()).thenReturn(this.filter); + return new FilterHandler(-1, this.context, info, ctx); } private HttpServletRequest createServletRequest() diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/EventListenerRegistryTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/EventListenerRegistryTest.java index e01d072..87235c2 100644 --- a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/EventListenerRegistryTest.java +++ b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/EventListenerRegistryTest.java @@ -21,18 +21,16 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import javax.servlet.DispatcherType; -import javax.servlet.Filter; -import javax.servlet.FilterConfig; +import java.util.EventListener; + +import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import org.apache.felix.http.base.internal.context.ExtServletContext; -import org.apache.felix.http.base.internal.handler.FilterHandler; -import org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler; -import org.apache.felix.http.base.internal.runtime.FilterInfo; +import org.apache.felix.http.base.internal.handler.ListenerHandler; +import org.apache.felix.http.base.internal.runtime.ListenerInfo; import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder; import org.junit.Test; import org.mockito.ArgumentMatchers; @@ -40,114 +38,92 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceObjects; import org.osgi.framework.ServiceReference; import org.osgi.service.http.runtime.dto.ServletContextDTO; -import org.osgi.service.http.whiteboard.HttpWhiteboardConstants; public class EventListenerRegistryTest { - private final FilterRegistry reg = new FilterRegistry(); - private void assertEmpty(final ServletContextDTO dto, final FailedDTOHolder holder) { - assertNull(dto.filterDTOs); - assertTrue(holder.failedFilterDTOs.isEmpty()); + assertNull(dto.listenerDTOs); + assertTrue(holder.failedListenerDTOs.isEmpty()); } private void clear(final ServletContextDTO dto, final FailedDTOHolder holder) { - dto.filterDTOs = null; - holder.failedFilterDTOs.clear(); + dto.listenerDTOs = null; + holder.failedListenerDTOs.clear(); } - @Test public void testSingleFilter() throws InvalidSyntaxException, ServletException + @Test public void testSingleListener() throws InvalidSyntaxException, ServletException { + final EventListenerRegistry reg = new EventListenerRegistry(); final FailedDTOHolder holder = new FailedDTOHolder(); final ServletContextDTO dto = new ServletContextDTO(); // check DTO - reg.getRuntimeInfo(dto, holder.failedFilterDTOs); + reg.getRuntimeInfo(dto, holder.failedListenerDTOs); assertEmpty(dto, holder); - // register filter - final FilterHandler h1 = createFilterHandler(1L, 0, "/foo"); - reg.addFilter(h1); - - verify(h1.getFilter()).init(ArgumentMatchers.any(FilterConfig.class)); + // register listener + final ListenerHandler h1 = createListenerHandler(1L, 0, ServletContextListener.class); + reg.addListeners(h1); // one entry in DTO clear(dto, holder); - reg.getRuntimeInfo(dto, holder.failedFilterDTOs); - assertTrue(holder.failedFilterDTOs.isEmpty()); - assertNotNull(dto.filterDTOs); - assertEquals(1, dto.filterDTOs.length); - assertEquals(1, dto.filterDTOs[0].patterns.length); - assertEquals("/foo", dto.filterDTOs[0].patterns[0]); - - // remove filter - final Filter f = h1.getFilter(); - reg.removeFilter(h1.getFilterInfo(), true); - verify(f).destroy(); + reg.getRuntimeInfo(dto, holder.failedListenerDTOs); + assertTrue(holder.failedListenerDTOs.isEmpty()); + assertNotNull(dto.listenerDTOs); + assertEquals(1, dto.listenerDTOs.length); + assertEquals(1, dto.listenerDTOs[0].types.length); + assertEquals(ServletContextListener.class.getName(), dto.listenerDTOs[0].types[0]); + + // remove listener + reg.removeListeners(h1.getListenerInfo()); // empty again clear(dto, holder); - reg.getRuntimeInfo(dto, holder.failedFilterDTOs); + reg.getRuntimeInfo(dto, holder.failedListenerDTOs); assertEmpty(dto, holder); } - @Test public void testFilterOrdering() throws InvalidSyntaxException + private static ListenerInfo createListenerInfo(final long id, final int ranking, final Class<? extends EventListener> type) throws InvalidSyntaxException { - final FilterHandler h1 = createFilterHandler(1L, 20, "/foo"); - reg.addFilter(h1); - final FilterHandler h2 = createFilterHandler(2L, 10, "/foo"); - reg.addFilter(h2); - final FilterHandler h3 = createFilterHandler(3L, 30, "/foo"); - reg.addFilter(h3); - final FilterHandler h4 = createFilterHandler(4L, 0, "/other"); - reg.addFilter(h4); - final FilterHandler h5 = createFilterHandler(5L, 90, "/foo"); - reg.addFilter(h5); - - final FilterHandler[] handlers = reg.getFilterHandlers(null, DispatcherType.REQUEST, "/foo"); - assertEquals(4, handlers.length); - assertEquals(h5.getFilterInfo(), handlers[0].getFilterInfo()); - assertEquals(h3.getFilterInfo(), handlers[1].getFilterInfo()); - assertEquals(h1.getFilterInfo(), handlers[2].getFilterInfo()); - assertEquals(h2.getFilterInfo(), handlers[3].getFilterInfo()); - - // cleanup - reg.removeFilter(h1.getFilterInfo(), true); - reg.removeFilter(h2.getFilterInfo(), true); - reg.removeFilter(h3.getFilterInfo(), true); - reg.removeFilter(h4.getFilterInfo(), true); - reg.removeFilter(h5.getFilterInfo(), true); - } + final String[] typeNames = new String[1]; + int index = 0; + typeNames[index++] = type.getName(); - private static FilterInfo createFilterInfo(final long id, final int ranking, final String... paths) throws InvalidSyntaxException - { final BundleContext bCtx = mock(BundleContext.class); when(bCtx.createFilter(ArgumentMatchers.anyString())).thenReturn(null); final Bundle bundle = mock(Bundle.class); when(bundle.getBundleContext()).thenReturn(bCtx); @SuppressWarnings("unchecked") - final ServiceReference<Filter> ref = mock(ServiceReference.class); + final ServiceReference<EventListener> ref = mock(ServiceReference.class); when(ref.getBundle()).thenReturn(bundle); when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(id); when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(ranking); - when(ref.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN)).thenReturn(paths); + when(ref.getProperty(Constants.OBJECTCLASS)).thenReturn(typeNames); when(ref.getPropertyKeys()).thenReturn(new String[0]); - final FilterInfo si = new FilterInfo(ref); - return si; + final EventListener listener = mock(type); + @SuppressWarnings("unchecked") + final ServiceObjects<EventListener> so = mock(ServiceObjects.class); + when(bCtx.getServiceObjects(ref)).thenReturn(so); + when(so.getService()).thenReturn(listener); + + final ListenerInfo info = new ListenerInfo(ref); + + return info; } - private static FilterHandler createFilterHandler(final long id, final int ranking, final String... paths) throws InvalidSyntaxException + private static ListenerHandler createListenerHandler(final long id, final int ranking, final Class<? extends EventListener> type) throws InvalidSyntaxException { - final FilterInfo si = createFilterInfo(id, ranking, paths); + final ListenerInfo info = createListenerInfo(id, ranking, type); final ExtServletContext ctx = mock(ExtServletContext.class); - final Filter filter = mock(Filter.class); - return new HttpServiceFilterHandler(ctx, si, filter); + return new ListenerHandler(1L, ctx, info, info.getServiceReference().getBundle().getBundleContext()); } + } diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/FilterRegistryTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/FilterRegistryTest.java index da93f92..db247fc 100644 --- a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/FilterRegistryTest.java +++ b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/FilterRegistryTest.java @@ -21,17 +21,17 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.EventListener; - -import javax.servlet.ServletContextListener; +import javax.servlet.DispatcherType; +import javax.servlet.Filter; +import javax.servlet.FilterConfig; import javax.servlet.ServletException; import org.apache.felix.http.base.internal.context.ExtServletContext; -import org.apache.felix.http.base.internal.handler.ListenerHandler; -import org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler; -import org.apache.felix.http.base.internal.runtime.ListenerInfo; +import org.apache.felix.http.base.internal.handler.FilterHandler; +import org.apache.felix.http.base.internal.runtime.FilterInfo; import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder; import org.junit.Test; import org.mockito.ArgumentMatchers; @@ -42,88 +42,119 @@ import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceObjects; import org.osgi.framework.ServiceReference; import org.osgi.service.http.runtime.dto.ServletContextDTO; +import org.osgi.service.http.whiteboard.HttpWhiteboardConstants; public class FilterRegistryTest { + private final FilterRegistry reg = new FilterRegistry(); + private void assertEmpty(final ServletContextDTO dto, final FailedDTOHolder holder) { - assertNull(dto.listenerDTOs); - assertTrue(holder.failedListenerDTOs.isEmpty()); + assertNull(dto.filterDTOs); + assertTrue(holder.failedFilterDTOs.isEmpty()); } private void clear(final ServletContextDTO dto, final FailedDTOHolder holder) { - dto.listenerDTOs = null; - holder.failedListenerDTOs.clear(); + dto.filterDTOs = null; + holder.failedFilterDTOs.clear(); } - @Test public void testSingleListener() throws InvalidSyntaxException, ServletException + @Test public void testSingleFilter() throws InvalidSyntaxException, ServletException { - final EventListenerRegistry reg = new EventListenerRegistry(); final FailedDTOHolder holder = new FailedDTOHolder(); final ServletContextDTO dto = new ServletContextDTO(); // check DTO - reg.getRuntimeInfo(dto, holder.failedListenerDTOs); + reg.getRuntimeInfo(dto, holder.failedFilterDTOs); assertEmpty(dto, holder); - // register listener - final ListenerHandler h1 = createListenerHandler(1L, 0, ServletContextListener.class); - reg.addListeners(h1); + // register filter + final FilterHandler h1 = createFilterHandler(1L, 0, "/foo"); + reg.addFilter(h1); + + verify(h1.getFilter()).init(ArgumentMatchers.any(FilterConfig.class)); // one entry in DTO clear(dto, holder); - reg.getRuntimeInfo(dto, holder.failedListenerDTOs); - assertTrue(holder.failedListenerDTOs.isEmpty()); - assertNotNull(dto.listenerDTOs); - assertEquals(1, dto.listenerDTOs.length); - assertEquals(1, dto.listenerDTOs[0].types.length); - assertEquals(ServletContextListener.class.getName(), dto.listenerDTOs[0].types[0]); - - // remove listener - reg.removeListeners(h1.getListenerInfo()); + reg.getRuntimeInfo(dto, holder.failedFilterDTOs); + assertTrue(holder.failedFilterDTOs.isEmpty()); + assertNotNull(dto.filterDTOs); + assertEquals(1, dto.filterDTOs.length); + assertEquals(1, dto.filterDTOs[0].patterns.length); + assertEquals("/foo", dto.filterDTOs[0].patterns[0]); + + // remove filter + final Filter f = h1.getFilter(); + reg.removeFilter(h1.getFilterInfo(), true); + verify(f).destroy(); // empty again clear(dto, holder); - reg.getRuntimeInfo(dto, holder.failedListenerDTOs); + reg.getRuntimeInfo(dto, holder.failedFilterDTOs); assertEmpty(dto, holder); } - private static ListenerInfo createListenerInfo(final long id, final int ranking, final Class<? extends EventListener> type) throws InvalidSyntaxException + @Test public void testFilterOrdering() throws InvalidSyntaxException { - final String[] typeNames = new String[1]; - int index = 0; - typeNames[index++] = type.getName(); + final FilterHandler h1 = createFilterHandler(1L, 20, "/foo"); + reg.addFilter(h1); + final FilterHandler h2 = createFilterHandler(2L, 10, "/foo"); + reg.addFilter(h2); + final FilterHandler h3 = createFilterHandler(3L, 30, "/foo"); + reg.addFilter(h3); + final FilterHandler h4 = createFilterHandler(4L, 0, "/other"); + reg.addFilter(h4); + final FilterHandler h5 = createFilterHandler(5L, 90, "/foo"); + reg.addFilter(h5); + + final FilterHandler[] handlers = reg.getFilterHandlers(null, DispatcherType.REQUEST, "/foo"); + assertEquals(4, handlers.length); + assertEquals(h5.getFilterInfo(), handlers[0].getFilterInfo()); + assertEquals(h3.getFilterInfo(), handlers[1].getFilterInfo()); + assertEquals(h1.getFilterInfo(), handlers[2].getFilterInfo()); + assertEquals(h2.getFilterInfo(), handlers[3].getFilterInfo()); + + // cleanup + reg.removeFilter(h1.getFilterInfo(), true); + reg.removeFilter(h2.getFilterInfo(), true); + reg.removeFilter(h3.getFilterInfo(), true); + reg.removeFilter(h4.getFilterInfo(), true); + reg.removeFilter(h5.getFilterInfo(), true); + } + private static FilterInfo createFilterInfo(final long id, final int ranking, final String... paths) throws InvalidSyntaxException + { final BundleContext bCtx = mock(BundleContext.class); when(bCtx.createFilter(ArgumentMatchers.anyString())).thenReturn(null); final Bundle bundle = mock(Bundle.class); when(bundle.getBundleContext()).thenReturn(bCtx); @SuppressWarnings("unchecked") - final ServiceReference<EventListener> ref = mock(ServiceReference.class); + final ServiceReference<Filter> ref = mock(ServiceReference.class); when(ref.getBundle()).thenReturn(bundle); when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(id); when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(ranking); - when(ref.getProperty(Constants.OBJECTCLASS)).thenReturn(typeNames); + when(ref.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN)).thenReturn(paths); when(ref.getPropertyKeys()).thenReturn(new String[0]); + final FilterInfo si = new FilterInfo(ref); - final EventListener listener = mock(type); - @SuppressWarnings("unchecked") - final ServiceObjects<EventListener> so = mock(ServiceObjects.class); - when(bCtx.getServiceObjects(ref)).thenReturn(so); - when(so.getService()).thenReturn(listener); - - final ListenerInfo info = new ListenerInfo(ref); - - return info; + return si; } - private static ListenerHandler createListenerHandler(final long id, final int ranking, final Class<? extends EventListener> type) throws InvalidSyntaxException + private static FilterHandler createFilterHandler(final long id, final int ranking, final String... paths) throws InvalidSyntaxException { - final ListenerInfo info = createListenerInfo(id, ranking, type); - final ExtServletContext ctx = mock(ExtServletContext.class); + final FilterInfo si = createFilterInfo(id, ranking, paths); + @SuppressWarnings("unchecked") + final ServiceObjects<Filter> so = mock(ServiceObjects.class); + final BundleContext ctx = mock(BundleContext.class); + when(ctx.getServiceObjects(si.getServiceReference())).thenReturn(so); + + final Filter filter = mock(Filter.class); + when(so.getService()).thenReturn(filter); - return new WhiteboardListenerHandler(1L, ctx, info, info.getServiceReference().getBundle().getBundleContext()); + final ExtServletContext context = mock(ExtServletContext.class); + return new FilterHandler(-1, context, si, ctx); } + } diff --git a/http/jetty/pom.xml b/http/jetty/pom.xml index d9e97d5..9587234 100644 --- a/http/jetty/pom.xml +++ b/http/jetty/pom.xml @@ -399,7 +399,7 @@ <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.http.base</artifactId> - <version>4.1.6</version> + <version>4.1.7-SNAPSHOT</version> </dependency> <dependency> <groupId>commons-fileupload</groupId>