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 621cbb07a1 FELIX-6673 : Improve resource handling
621cbb07a1 is described below

commit 621cbb07a14b3af7f85ed1b3cfbbb5e507f9d775
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Wed Nov 29 19:13:05 2023 +0100

    FELIX-6673 : Improve resource handling
---
 .../base/internal/whiteboard/ResourceServlet.java  | 92 +++++++++-------------
 http/itest/pom.xml                                 | 40 +++++-----
 .../felix/http/itest/BaseIntegrationTest.java      | 11 +--
 .../itest/servletapi3/HttpServiceRuntimeTest.java  | 27 +++++--
 .../felix/http/itest/servletapi3/ResourceTest.java | 23 +++---
 .../itest/servletapi5/HttpServiceRuntimeTest.java  | 27 +++++--
 .../felix/http/itest/servletapi5/ResourceTest.java | 34 ++++++--
 7 files changed, 141 insertions(+), 113 deletions(-)

diff --git 
a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ResourceServlet.java
 
b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ResourceServlet.java
index 3a4d14341d..3d873feaa7 100644
--- 
a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ResourceServlet.java
+++ 
b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ResourceServlet.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
 
@@ -50,7 +51,7 @@ public class ResourceServlet extends HttpServlet {
     protected void doGet(final HttpServletRequest req, final 
HttpServletResponse res)
             throws ServletException, IOException {
         final String target = req.getPathInfo();
-        final String resName = (target == null ? this.prefix : this.prefix + 
target);
+        final String resName = (target == null ? this.prefix : 
this.prefix.concat(target));
 
         final URL url = getServletContext().getResource(resName);
 
@@ -61,15 +62,16 @@ public class ResourceServlet extends HttpServlet {
         }
     }
 
-    private void handle(final HttpServletRequest req,
-            final HttpServletResponse res, final URL url, final String resName)
+    private void handle(final HttpServletRequest req, final 
HttpServletResponse res, final URL url, final String resName)
     throws IOException {
         final String contentType = getServletContext().getMimeType(resName);
         if (contentType != null) {
             res.setContentType(contentType);
         }
 
-        final long lastModified = getLastModified(url);
+        final URLConnection conn = url.openConnection();
+
+        final long lastModified = getLastModified(conn);
         if (lastModified != 0) {
             res.setDateHeader("Last-Modified", lastModified);
         }
@@ -77,27 +79,28 @@ public class ResourceServlet extends HttpServlet {
         if (!resourceModified(lastModified, 
req.getDateHeader("If-Modified-Since"))) {
             res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
         } else {
-            copyResource(url, res);
+            copyResource(conn, res);
         }
     }
 
-    private long getLastModified(final URL url) {
-        long lastModified = 0;
-
-        try {
-            final URLConnection conn = url.openConnection();
-            lastModified = conn.getLastModified();
-        } catch (final Exception e) {
-            // Do nothing
+    private File getFile(final URL url) {
+        if (url.getProtocol().equals("file")) {
+            try {
+                return new File(url.toURI());
+            } catch (URISyntaxException e) {
+                return new File(url.getPath());
+            }
         }
+        return null;
+    }
+
+    private long getLastModified(final URLConnection conn) {
+        long lastModified = conn.getLastModified();
 
         if (lastModified == 0) {
-            final String filepath = url.getPath();
-            if (filepath != null) {
-                final File f = new File(filepath);
-                if (f.exists()) {
-                    lastModified = f.lastModified();
-                }
+            final File f = getFile(conn.getURL());
+            if ( f != null && f.exists()) {
+                lastModified = f.lastModified();
             }
         }
 
@@ -111,58 +114,35 @@ public class ResourceServlet extends HttpServlet {
         return resTimestamp == 0 || modSince == -1 || resTimestamp > modSince;
     }
 
-    private void copyResource(final URL url, final HttpServletResponse res) 
throws IOException {
-        URLConnection conn = null;
-        OutputStream os = null;
-        InputStream is = null;
-
-        try {
-            conn = url.openConnection();
-
-            is = conn.getInputStream();
-            os = res.getOutputStream();
+    private void copyResource(final URLConnection conn, final 
HttpServletResponse res) throws IOException {
+        try(final InputStream is = conn.getInputStream()) {
             // FELIX-3987 content length should be set *before* any streaming 
is done
             // as headers should be written before the content is actually 
written...
-            int len = getContentLength(conn);
+            final long len = getContentLength(conn);
             if (len >= 0) {
-                res.setContentLength(len);
+                res.setContentLengthLong(len);
             }
 
             byte[] buf = new byte[1024];
             int n;
 
-            while ((n = is.read(buf, 0, buf.length)) >= 0) {
+            // no need to close output stream as this is done by the servlet 
container
+            final OutputStream os = res.getOutputStream();
+            while ((n = is.read(buf, 0, buf.length)) > 0) {
                 os.write(buf, 0, n);
             }
-        } finally {
-            if (is != null) {
-                is.close();
-            }
-
-            if (os != null) {
-                os.close();
-            }
+            os.flush();
         }
     }
 
-    private int getContentLength(final URLConnection conn)
-    {
-        int length = -1;
-
-        length = conn.getContentLength();
-        if (length < 0)
-        {
+    private long getContentLength(final URLConnection conn) {
+        long length = conn.getContentLengthLong();
+        if (length < 0) {
             // Unknown, try whether it is a file, and if so, use the file
             // API to get the length of the content...
-            String path = conn.getURL().getPath();
-            if (path != null)
-            {
-                File f = new File(path);
-                // In case more than 2GB is streamed
-                if (f.length() < Integer.MAX_VALUE)
-                {
-                    length = (int) f.length();
-                }
+            final File f = getFile(conn.getURL());
+            if ( f != null && f.exists()) {
+                length = f.length();
             }
         }
         return length;
diff --git a/http/itest/pom.xml b/http/itest/pom.xml
index bfbc78b005..0984c9c68e 100644
--- a/http/itest/pom.xml
+++ b/http/itest/pom.xml
@@ -32,12 +32,24 @@
 
     <properties>
         <felix.java.version>11</felix.java.version>
-        <pax.exam.version>4.13.1</pax.exam.version>
-        <pax.url.aether.version>2.6.2</pax.url.aether.version>
         <http.servlet.api.version>2.1.0</http.servlet.api.version>
         <http.jetty.version>5.1.5-SNAPSHOT</http.jetty.version>
+        <http.jetty.id>org.apache.felix.http.jetty</http.jetty.id>
+        <pax.exam.version>4.13.1</pax.exam.version>
+        <pax.url.aether.version>2.6.14</pax.url.aether.version>
     </properties>
 
+    <profiles>
+        <profile>
+            <id>jetty12</id>
+            <properties>
+                <felix.java.version>17</felix.java.version>
+                <http.servlet.api.version>3.0.0</http.servlet.api.version>
+                <http.jetty.version>1.0.0-SNAPSHOT</http.jetty.version>
+                <http.jetty.id>org.apache.felix.http.jetty12</http.jetty.id>
+            </properties>
+        </profile>
+    </profiles>
     <build>
         <plugins>
             <plugin>
@@ -47,6 +59,7 @@
                     <systemPropertyVariables>                    
                         
<http.servlet.api.version>${http.servlet.api.version}</http.servlet.api.version>
                         
<http.jetty.version>${http.jetty.version}</http.jetty.version>
+                        <http.jetty.id>${http.jetty.id}</http.jetty.id>
                     </systemPropertyVariables>
                 </configuration>
             </plugin>  
@@ -79,14 +92,14 @@
         </dependency>
         <dependency>
             <groupId>org.apache.geronimo.specs</groupId>
-            <artifactId>geronimo-json_1.0_spec</artifactId>
-            <version>1.0-alpha-1</version>
+            <artifactId>geronimo-json_1.1_spec</artifactId>
+            <version>1.3</version>
             <scope>provided</scope> 
         </dependency>
         <dependency>
             <groupId>org.apache.johnzon</groupId>
             <artifactId>johnzon-core</artifactId>
-            <version>1.0.0</version>
+            <version>1.2.21</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
@@ -108,7 +121,7 @@
         </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
-            <artifactId>org.apache.felix.http.jetty</artifactId>
+            <artifactId>${http.jetty.id}</artifactId>
             <version>${http.jetty.version}</version>
         </dependency>
         <dependency>
@@ -129,13 +142,6 @@
             <version>${pax.exam.version}</version>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.objenesis</groupId>
-            <artifactId>objenesis</artifactId>
-            <version>2.6</version>
-            <scope>test</scope>
-        </dependency>
-
         <dependency>
             <groupId>org.ops4j.pax.url</groupId>
             <artifactId>pax-url-aether</artifactId>
@@ -148,12 +154,6 @@
             <version>4.13.2</version>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.mockito</groupId>
-            <artifactId>mockito-core</artifactId>
-            <version>5.7.0</version>
-            <scope>test</scope>
-        </dependency>
         <dependency>
             <groupId>javax.inject</groupId>
             <artifactId>javax.inject</artifactId>
@@ -163,7 +163,7 @@
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.framework</artifactId>
-            <version>7.0.3</version>
+            <version>7.0.5</version>
             <scope>test</scope>
         </dependency>
     </dependencies>
diff --git 
a/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java 
b/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
index 24cdb850fd..8bc267372e 100644
--- 
a/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
+++ 
b/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
@@ -164,18 +164,15 @@ public abstract class BaseIntegrationTest {
                 mavenBundle("org.apache.sling", 
"org.apache.sling.commons.log", "5.3.0"),
                 mavenBundle("org.apache.sling", 
"org.apache.sling.commons.logservice", "1.1.0"),
 
-                mavenBundle("org.apache.geronimo.specs", 
"geronimo-json_1.0_spec", "1.0-alpha-1").startLevel(START_LEVEL_SYSTEM_BUNDLES),
-                mavenBundle("org.apache.johnzon", "johnzon-core", 
"1.0.0").startLevel(START_LEVEL_SYSTEM_BUNDLES),
+                mavenBundle("org.apache.sling", 
"org.apache.sling.commons.johnzon", 
"1.2.16").startLevel(START_LEVEL_SYSTEM_BUNDLES),
 
                 mavenBundle("org.apache.felix", 
"org.apache.felix.configadmin").version("1.9.22").startLevel(START_LEVEL_SYSTEM_BUNDLES),
                 mavenBundle("org.apache.felix", 
"org.apache.felix.http.servlet-api", 
System.getProperty("http.servlet.api.version")).startLevel(START_LEVEL_SYSTEM_BUNDLES),
-                mavenBundle("org.apache.felix", ORG_APACHE_FELIX_HTTP_JETTY, 
System.getProperty("http.jetty.version")).startLevel(START_LEVEL_SYSTEM_BUNDLES),
+                mavenBundle("org.apache.felix", 
System.getProperty("http.jetty.id"), 
System.getProperty("http.jetty.version")).startLevel(START_LEVEL_SYSTEM_BUNDLES),
                 mavenBundle("org.apache.felix", 
"org.apache.felix.http.whiteboard", 
"4.0.0").startLevel(START_LEVEL_SYSTEM_BUNDLES),
 
                 mavenBundle("org.apache.httpcomponents", "httpcore-osgi", 
"4.4.6").startLevel(START_LEVEL_SYSTEM_BUNDLES),
                 mavenBundle("org.apache.httpcomponents", "httpclient-osgi", 
"4.5.3").startLevel(START_LEVEL_SYSTEM_BUNDLES),
-                mavenBundle("org.mockito", "mockito-all", 
"1.10.19").startLevel(START_LEVEL_SYSTEM_BUNDLES),
-                mavenBundle("org.objenesis", "objenesis", 
"2.6").startLevel(START_LEVEL_SYSTEM_BUNDLES),
 
                 junitBundles(),
                 frameworkStartLevel(START_LEVEL_TEST_BUNDLE));
@@ -265,7 +262,7 @@ public abstract class BaseIntegrationTest {
      */
     protected Bundle findBundle(String bsn) {
         for (Bundle bundle : m_context.getBundles()) {
-            if (bsn.equals(bundle.getSymbolicName())) {
+            if (bsn.equals(bundle.getSymbolicName()) || 
(bundle.getSymbolicName() != null && bundle.getSymbolicName().startsWith(bsn))) 
{
                 return bundle;
             }
         }
@@ -274,7 +271,7 @@ public abstract class BaseIntegrationTest {
 
     protected Bundle getHttpJettyBundle() {
         Bundle b = findBundle(ORG_APACHE_FELIX_HTTP_JETTY);
-        assertNotNull("Apache Felix Jetty bundle not found?!", b);
+        assertNotNull("Apache Felix Jetty bundle not found. Looking for 
symbolic name equal to/starting with " + ORG_APACHE_FELIX_HTTP_JETTY, b);
         return b;
     }
 
diff --git 
a/http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/HttpServiceRuntimeTest.java
 
b/http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/HttpServiceRuntimeTest.java
index 485ca75d7e..45bd407528 100644
--- 
a/http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/HttpServiceRuntimeTest.java
+++ 
b/http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/HttpServiceRuntimeTest.java
@@ -25,7 +25,6 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import static org.mockito.Mockito.mock;
 import static org.osgi.framework.Constants.SERVICE_RANKING;
 import static 
org.osgi.service.http.runtime.HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT;
 import static 
org.osgi.service.http.runtime.HttpServiceRuntimeConstants.HTTP_SERVICE_ID;
@@ -211,7 +210,23 @@ public class HttpServiceRuntimeTest extends 
Servlet3BaseIntegrationTest {
         Dictionary<String, ?> properties = createDictionary(context == null ?
                 propertyEntries.subList(0, 2).toArray() : 
propertyEntries.toArray());
 
-        registrations.add(m_context.registerService(listenerClass.getName(), 
mock(listenerClass), properties));
+        final Object service;
+        if ( 
ServletContextListener.class.getName().equals(listenerClass.getName())) {
+            service = new ServletContextListener() {};
+        } else if ( 
ServletContextAttributeListener.class.getName().equals(listenerClass.getName()))
 {
+            service = new ServletContextAttributeListener() {};
+        } else if ( 
ServletRequestListener.class.getName().equals(listenerClass.getName())) {
+            service = new ServletRequestListener() {};
+        } else if ( 
ServletRequestAttributeListener.class.getName().equals(listenerClass.getName()))
 {
+            service = new ServletRequestAttributeListener() {};
+        } else if ( 
HttpSessionListener.class.getName().equals(listenerClass.getName())) {
+            service = new HttpSessionListener() {};
+        } else if ( 
HttpSessionAttributeListener.class.getName().equals(listenerClass.getName())) {
+            service = new HttpSessionAttributeListener() {};
+        } else {
+            throw new RuntimeException("Unknown listener class " + 
listenerClass.getName());
+        }
+        registrations.add(m_context.registerService(listenerClass.getName(), 
service, properties));
         awaitServiceRegistration();
     }
 
@@ -220,7 +235,7 @@ public class HttpServiceRuntimeTest extends 
Servlet3BaseIntegrationTest {
                 HTTP_WHITEBOARD_CONTEXT_NAME, name,
                 HTTP_WHITEBOARD_CONTEXT_PATH, path);
 
-        ServiceRegistration<?> contextRegistration = 
m_context.registerService(ServletContextHelper.class.getName(), 
mock(ServletContextHelper.class), properties);
+        ServiceRegistration<?> contextRegistration = 
m_context.registerService(ServletContextHelper.class.getName(), new 
ServletContextHelper() {}, properties);
         registrations.add(contextRegistration);
         awaitServiceRegistration();
         return contextRegistration;
@@ -804,7 +819,7 @@ public class HttpServiceRuntimeTest extends 
Servlet3BaseIntegrationTest {
     public void missingContextHelperNameAppearsAsFailure() {
         Dictionary<String, ?> properties = 
createDictionary(HTTP_WHITEBOARD_CONTEXT_PATH, "");
 
-        
registrations.add(m_context.registerService(ServletContextHelper.class.getName(),
 mock(ServletContextHelper.class), properties));
+        
registrations.add(m_context.registerService(ServletContextHelper.class.getName(),
 new ServletContextHelper() {}, properties));
 
         HttpServiceRuntime serviceRuntime = 
getService(HttpServiceRuntime.class);
         assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
@@ -1102,7 +1117,7 @@ public class HttpServiceRuntimeTest extends 
Servlet3BaseIntegrationTest {
     public void invalidListenerPopertyValueAppearsAsFailure() throws Exception 
{
         Dictionary<String, ?> properties = 
createDictionary(HTTP_WHITEBOARD_LISTENER, "invalid");
 
-        
registrations.add(m_context.registerService(ServletRequestListener.class.getName(),
 mock(ServletRequestListener.class), properties));
+        
registrations.add(m_context.registerService(ServletRequestListener.class.getName(),
 new ServletRequestListener() {}, properties));
 
         HttpServiceRuntime serviceRuntime = 
getService(HttpServiceRuntime.class);
         assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
@@ -1144,7 +1159,7 @@ public class HttpServiceRuntimeTest extends 
Servlet3BaseIntegrationTest {
                 HTTP_WHITEBOARD_CONTEXT_PATH, "/second",
                 SERVICE_RANKING, Integer.MAX_VALUE);
 
-        ServiceRegistration<?> secondContext = 
m_context.registerService(ServletContextHelper.class.getName(), 
mock(ServletContextHelper.class), properties);
+        ServiceRegistration<?> secondContext = 
m_context.registerService(ServletContextHelper.class.getName(), new 
ServletContextHelper() {}, properties);
         registrations.add(secondContext);
         Long secondContextId = (Long) 
secondContext.getReference().getProperty(Constants.SERVICE_ID);
 
diff --git 
a/http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/ResourceTest.java
 
b/http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/ResourceTest.java
index 487b9d15ac..6422436626 100644
--- 
a/http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/ResourceTest.java
+++ 
b/http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/ResourceTest.java
@@ -27,6 +27,7 @@ import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.nio.file.Files;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -58,7 +59,7 @@ public class ResourceTest extends Servlet3BaseIntegrationTest 
{
             @Override
             public URL getResource(String name) {
                 try {
-                    File f = new File("src/test/resources/" + name);
+                    File f = new File("src/test/resources" + name);
                     if (f.exists()) {
                         return f.toURI().toURL();
                     }
@@ -76,25 +77,25 @@ public class ResourceTest extends 
Servlet3BaseIntegrationTest {
 
         TestServlet servlet = new TestServlet(initLatch, destroyLatch);
 
-        register("/", "/resource", context);
-        register("/test", servlet, context);
+        register("/files", "/resource", context);
+        register("/files/test", servlet, context);
 
-        URL testHtmlURL = createURL("/test.html");
-        URL testURL = createURL("/test");
+        URL testHtmlURL = createURL("/files/test.html");
+        URL testURL = createURL("/files/test");
 
         assertTrue(initLatch.await(5, TimeUnit.SECONDS));
 
-        assertResponseCode(SC_OK, testHtmlURL);
-        assertResponseCode(SC_OK, testURL);
+        assertContent(SC_OK, Files.readString(new 
File("src/test/resources/resource/test.html").toPath()), testHtmlURL);
+        assertContent(SC_OK, null, testURL);
 
-        unregister("/test");
+        unregister("/files/test");
 
         assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
 
-        assertResponseCode(SC_OK, testHtmlURL);
-        assertResponseCode(SC_OK, testURL);
+        assertContent(SC_OK, Files.readString(new 
File("src/test/resources/resource/test.html").toPath()), testHtmlURL);
+        assertResponseCode(SC_NOT_FOUND, testURL);
 
-        unregister("/");
+        unregister("/files");
 
         assertResponseCode(SC_NOT_FOUND, testHtmlURL);
         assertResponseCode(SC_NOT_FOUND, testURL);
diff --git 
a/http/itest/src/test/java/org/apache/felix/http/itest/servletapi5/HttpServiceRuntimeTest.java
 
b/http/itest/src/test/java/org/apache/felix/http/itest/servletapi5/HttpServiceRuntimeTest.java
index aca969fcfa..434a8b5913 100644
--- 
a/http/itest/src/test/java/org/apache/felix/http/itest/servletapi5/HttpServiceRuntimeTest.java
+++ 
b/http/itest/src/test/java/org/apache/felix/http/itest/servletapi5/HttpServiceRuntimeTest.java
@@ -25,7 +25,6 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import static org.mockito.Mockito.mock;
 import static org.osgi.framework.Constants.SERVICE_RANKING;
 import static 
org.osgi.service.servlet.runtime.HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT;
 import static 
org.osgi.service.servlet.runtime.dto.DTOConstants.FAILURE_REASON_EXCEPTION_ON_INIT;
@@ -167,7 +166,23 @@ public class HttpServiceRuntimeTest extends 
Servlet5BaseIntegrationTest {
         Dictionary<String, ?> properties = createDictionary(context == null ?
                 propertyEntries.subList(0, 2).toArray() : 
propertyEntries.toArray());
 
-        registrations.add(m_context.registerService(listenerClass.getName(), 
mock(listenerClass), properties));
+        final Object service;
+        if ( 
ServletContextListener.class.getName().equals(listenerClass.getName())) {
+            service = new ServletContextListener() {};
+        } else if ( 
ServletContextAttributeListener.class.getName().equals(listenerClass.getName()))
 {
+            service = new ServletContextAttributeListener() {};
+        } else if ( 
ServletRequestListener.class.getName().equals(listenerClass.getName())) {
+            service = new ServletRequestListener() {};
+        } else if ( 
ServletRequestAttributeListener.class.getName().equals(listenerClass.getName()))
 {
+            service = new ServletRequestAttributeListener() {};
+        } else if ( 
HttpSessionListener.class.getName().equals(listenerClass.getName())) {
+            service = new HttpSessionListener() {};
+        } else if ( 
HttpSessionAttributeListener.class.getName().equals(listenerClass.getName())) {
+            service = new HttpSessionAttributeListener() {};
+        } else {
+            throw new RuntimeException("Unknown listener class " + 
listenerClass.getName());
+        }
+        registrations.add(m_context.registerService(listenerClass.getName(), 
service, properties));
     }
 
     private ServiceRegistration<?> registerContext(String name, String path) 
throws InterruptedException {
@@ -175,7 +190,7 @@ public class HttpServiceRuntimeTest extends 
Servlet5BaseIntegrationTest {
                 HTTP_WHITEBOARD_CONTEXT_NAME, name,
                 HTTP_WHITEBOARD_CONTEXT_PATH, path);
 
-        ServiceRegistration<?> contextRegistration = 
m_context.registerService(ServletContextHelper.class.getName(), 
mock(ServletContextHelper.class), properties);
+        ServiceRegistration<?> contextRegistration = 
m_context.registerService(ServletContextHelper.class.getName(), new 
ServletContextHelper() {}, properties);
         registrations.add(contextRegistration);
         return contextRegistration;
     }
@@ -785,7 +800,7 @@ public class HttpServiceRuntimeTest extends 
Servlet5BaseIntegrationTest {
         Dictionary<String, ?> properties = 
createDictionary(HTTP_WHITEBOARD_CONTEXT_PATH, "");
 
         long counter = this.getRuntimeCounter();
-        
registrations.add(m_context.registerService(ServletContextHelper.class.getName(),
 mock(ServletContextHelper.class), properties));
+        
registrations.add(m_context.registerService(ServletContextHelper.class.getName(),
 new ServletContextHelper() {}, properties));
         counter = this.waitForRuntime(counter);
 
         final HttpServiceRuntime serviceRuntime = this.getHttpServiceRuntime();
@@ -1036,7 +1051,7 @@ public class HttpServiceRuntimeTest extends 
Servlet5BaseIntegrationTest {
     public void invalidListenerPopertyValueAppearsAsFailure() throws Exception 
{
         Dictionary<String, ?> properties = 
createDictionary(HTTP_WHITEBOARD_LISTENER, "invalid");
 
-        
registrations.add(m_context.registerService(ServletRequestListener.class.getName(),
 mock(ServletRequestListener.class), properties));
+        
registrations.add(m_context.registerService(ServletRequestListener.class.getName(),
 new ServletRequestListener() {}, properties));
 
         final HttpServiceRuntime serviceRuntime = this.getHttpServiceRuntime();
 
@@ -1077,7 +1092,7 @@ public class HttpServiceRuntimeTest extends 
Servlet5BaseIntegrationTest {
                 HTTP_WHITEBOARD_CONTEXT_PATH, "/second",
                 SERVICE_RANKING, Integer.MAX_VALUE);
 
-        ServiceRegistration<?> secondContext = 
m_context.registerService(ServletContextHelper.class.getName(), 
mock(ServletContextHelper.class), properties);
+        ServiceRegistration<?> secondContext = 
m_context.registerService(ServletContextHelper.class.getName(), new 
ServletContextHelper() {}, properties);
         registrations.add(secondContext);
         Long secondContextId = (Long) 
secondContext.getReference().getProperty(Constants.SERVICE_ID);
         counter = this.waitForRuntime(counter);
diff --git 
a/http/itest/src/test/java/org/apache/felix/http/itest/servletapi5/ResourceTest.java
 
b/http/itest/src/test/java/org/apache/felix/http/itest/servletapi5/ResourceTest.java
index 4126ede202..dccbeb3873 100644
--- 
a/http/itest/src/test/java/org/apache/felix/http/itest/servletapi5/ResourceTest.java
+++ 
b/http/itest/src/test/java/org/apache/felix/http/itest/servletapi5/ResourceTest.java
@@ -26,8 +26,10 @@ import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.nio.file.Files;
 import java.util.Dictionary;
 import java.util.Hashtable;
+import java.util.concurrent.CountDownLatch;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -38,6 +40,7 @@ import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.servlet.context.ServletContextHelper;
 import org.osgi.service.servlet.whiteboard.HttpWhiteboardConstants;
 
+import jakarta.servlet.Servlet;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 
@@ -47,6 +50,7 @@ public class ResourceTest extends Servlet5BaseIntegrationTest 
{
 
     @Test
     public void testHandleResourceRegistrationOk() throws Exception {
+        this.setupLatches(1);
         long counter = this.getRuntimeCounter();
         ServletContextHelper context = new ServletContextHelper() {
             @Override
@@ -57,7 +61,8 @@ public class ResourceTest extends Servlet5BaseIntegrationTest 
{
             @Override
             public URL getResource(String name) {
                 try {
-                    File f = new File("src/test/resources/" + name);
+                    File f = new File("src/test/resources" + name);
+                    System.out.println("********************** " + 
f.getAbsolutePath());
                     if (f.exists()) {
                         return f.toURI().toURL();
                     }
@@ -75,18 +80,33 @@ public class ResourceTest extends 
Servlet5BaseIntegrationTest {
         counter = this.waitForRuntime(counter);
 
         final Dictionary<String, Object> resourcesProps = new Hashtable<>();
-        
resourcesProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PATTERN, 
"/");
+        
resourcesProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PATTERN, 
"/files/*");
         
resourcesProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PREFIX, 
"/resource");
         
resourcesProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" 
+ HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=test)");
         final ServiceRegistration<Object> reg = 
this.m_context.registerService(Object.class, new Object(), resourcesProps);
-        
+    
         counter = this.waitForRuntime(counter);
 
-        URL testHtmlURL = createURL("/test.html");
-        URL testURL = createURL("/test");
+        final TestServlet servlet = new TestServlet();
 
-        assertResponseCode(SC_OK, testHtmlURL);
-        assertResponseCode(SC_OK, testURL);
+        final Dictionary<String, Object> servletProps = new Hashtable<>();
+        
servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, 
"/files/test");
+        
servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + 
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=test)");
+        final ServiceRegistration<Servlet> servletReg = 
this.m_context.registerService(Servlet.class, servlet, servletProps);
+
+        this.waitForInit();
+
+        URL testHtmlURL = createURL("/files/test.html");
+        URL testURL = createURL("/files/test");
+
+        assertContent(SC_OK, Files.readString(new 
File("src/test/resources/resource/test.html").toPath()), testHtmlURL);
+        assertContent(SC_OK, null, testURL);
+
+        servletReg.unregister();
+        this.waitForDestroy();
+
+        assertContent(SC_OK, Files.readString(new 
File("src/test/resources/resource/test.html").toPath()), testHtmlURL);
+        assertResponseCode(SC_NOT_FOUND, testURL);
 
         reg.unregister();
         counter = this.waitForRuntime(counter);

Reply via email to