Skip to content

Commit d3994ac

Browse files
committed
FELIX-6745 : HttpService: Cannot unregister javaxwrappers.ServletWrapper
1 parent 29d4ea1 commit d3994ac

6 files changed

Lines changed: 146 additions & 21 deletions

File tree

http/base/src/main/java/org/apache/felix/http/base/internal/handler/HttpServiceServletHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public HttpServiceServletHandler(final ExtServletContext context,
3838
final ServletInfo servletInfo,
3939
final javax.servlet.Servlet servlet)
4040
{
41-
this(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID, context, servletInfo, ServletWrapper.getRegisteredServlet(servlet));
41+
this(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID, context, servletInfo, new ServletWrapper(servlet));
4242
}
4343

4444
/**

http/itest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<properties>
4646
<felix.java.version>17</felix.java.version>
4747
<http.servlet.api.version>3.0.0</http.servlet.api.version>
48-
<http.jetty.version>1.0.17-SNAPSHOT</http.jetty.version>
48+
<http.jetty.version>1.0.20-SNAPSHOT</http.jetty.version>
4949
<http.jetty.id>org.apache.felix.http.jetty12</http.jetty.id>
5050
</properties>
5151
</profile>

http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/HttpServiceTest.java

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import javax.servlet.http.HttpServletRequest;
4141
import javax.servlet.http.HttpServletResponse;
4242

43+
import org.apache.felix.http.javaxwrappers.ServletWrapper;
4344
import org.junit.After;
4445
import org.junit.Test;
4546
import org.junit.runner.RunWith;
@@ -54,6 +55,8 @@
5455
import org.osgi.service.http.HttpService;
5556
import org.osgi.service.http.runtime.HttpServiceRuntime;
5657

58+
import jakarta.servlet.http.HttpServlet;
59+
5760
@RunWith(PaxExam.class)
5861
@ExamReactorStrategy(PerMethod.class)
5962
public class HttpServiceTest extends Servlet3BaseIntegrationTest {
@@ -139,35 +142,90 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
139142
@Test
140143
public void testHttpServiceCapabiltiy() throws Exception {
141144
setupLatches(0);
142-
145+
143146
Bundle httpJettyBundle = getHttpJettyBundle();
144-
147+
145148
BundleWiring wiring = httpJettyBundle.adapt(BundleWiring.class);
146-
149+
147150
List<BundleCapability> capabilities = wiring.getCapabilities("osgi.service");
148-
151+
149152
assertFalse(capabilities.isEmpty());
150-
153+
151154
boolean found = false;
152-
155+
153156
for (BundleCapability capability : capabilities) {
154157
@SuppressWarnings("unchecked")
155158
List<String> objectClass = (List<String>) capability.getAttributes().get(Constants.OBJECTCLASS);
156-
159+
157160
assertNotNull(objectClass);
158-
161+
159162
if(objectClass.contains(HttpService.class.getName())) {
160163
String uses = capability.getDirectives().get("uses");
161-
164+
162165
assertNotNull(uses);
163-
166+
164167
assertTrue(uses.contains(HttpService.class.getPackage().getName()));
165-
168+
166169
found = true;
167170
break;
168171
}
169172
}
170-
173+
171174
assertTrue("Missing HttpService capability", found);
172175
}
176+
177+
private class JakartaServlet extends HttpServlet {
178+
private static final long serialVersionUID = 1L;
179+
180+
@Override
181+
public void init() throws jakarta.servlet.ServletException {
182+
super.init();
183+
initLatch.countDown();
184+
}
185+
186+
@Override
187+
protected void doGet(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)
188+
throws IOException {
189+
resp.getWriter().print("helloworld");
190+
resp.flushBuffer();
191+
}
192+
193+
@Override
194+
public void destroy() {
195+
destroyLatch.countDown();
196+
}
197+
}
198+
199+
@Test
200+
public void testRegisteringWrapperAsServlet() throws Exception {
201+
this.setupLatches(1);
202+
final HttpService service = this.getHttpService();
203+
service.registerServlet("/testjakarta", new ServletWrapper(new JakartaServlet()), null, null);
204+
205+
assertTrue(initLatch.await(5, TimeUnit.SECONDS));
206+
207+
assertContent("helloworld", createURL("/testjakarta"));
208+
209+
service.unregister("/testjakarta");
210+
}
211+
212+
@Test
213+
public void testRegisteringCustomWrapperAsServlet() throws Exception {
214+
this.setupLatches(1);
215+
final HttpService service = this.getHttpService();
216+
service.registerServlet("/testjakarta", new ServletWrapper(new JakartaServlet()) {
217+
@Override
218+
public void service(ServletRequest req, ServletResponse resp)
219+
throws IOException {
220+
resp.getWriter().print("helloworldwrapped");
221+
resp.flushBuffer();
222+
}
223+
}, null, null);
224+
225+
assertTrue(initLatch.await(5, TimeUnit.SECONDS));
226+
227+
assertContent("helloworldwrapped", createURL("/testjakarta"));
228+
229+
service.unregister("/testjakarta");
230+
}
173231
}

http/itest/src/test/java/org/apache/felix/http/itest/servletapi3/ServletContentTest.java

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertTrue;
2323
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
24-
24+
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.io.PrintWriter;
@@ -32,19 +32,24 @@
3232
import java.util.List;
3333
import java.util.concurrent.CountDownLatch;
3434
import java.util.concurrent.TimeUnit;
35-
35+
3636
import javax.servlet.Servlet;
37+
import javax.servlet.ServletRequest;
38+
import javax.servlet.ServletResponse;
3739
import javax.servlet.http.HttpServletRequest;
3840
import javax.servlet.http.HttpServletResponse;
3941

42+
import org.apache.felix.http.javaxwrappers.ServletWrapper;
4043
import org.junit.After;
4144
import org.junit.Test;
4245
import org.junit.runner.RunWith;
4346
import org.ops4j.pax.exam.junit.PaxExam;
4447
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
4548
import org.ops4j.pax.exam.spi.reactors.PerMethod;
4649
import org.osgi.framework.ServiceRegistration;
47-
50+
51+
import jakarta.servlet.http.HttpServlet;
52+
4853
@RunWith(PaxExam.class)
4954
@ExamReactorStrategy(PerMethod.class)
5055
public class ServletContentTest extends Servlet3BaseIntegrationTest {
@@ -81,7 +86,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
8186

8287
registrations.add(m_context.registerService(Servlet.class.getName(), servletWithErrorCode, servletProps));
8388
}
84-
89+
8590
@After
8691
public void unregisterServices() throws InterruptedException {
8792
for (ServiceRegistration<?> serviceRegistration : registrations) {
@@ -117,5 +122,67 @@ public void testContentAndHeaders() throws Exception {
117122

118123
assertContent("/myservlet");
119124
}
125+
126+
private class JakartaServlet extends HttpServlet {
127+
private static final long serialVersionUID = 1L;
128+
129+
@Override
130+
public void init() throws jakarta.servlet.ServletException {
131+
super.init();
132+
initLatch.countDown();
133+
}
134+
135+
@Override
136+
protected void doGet(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)
137+
throws IOException {
138+
resp.getWriter().print("helloworld");
139+
resp.flushBuffer();
140+
}
141+
142+
@Override
143+
public void destroy() {
144+
destroyLatch.countDown();
145+
}
146+
}
147+
148+
@Test
149+
public void testRegisteringWrapperAsServlet() throws Exception {
150+
this.setupLatches(1);
151+
152+
final Dictionary<String, Object> servletProps = new Hashtable<>();
153+
servletProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, "/testjakarta");
154+
155+
final ServiceRegistration<Servlet> reg = m_context.registerService(Servlet.class, new ServletWrapper(new JakartaServlet()), servletProps);
156+
157+
assertTrue(initLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS));
158+
159+
assertContent("helloworld", createURL("/testjakarta"));
160+
161+
reg.unregister();
162+
assertTrue(destroyLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS));
163+
}
164+
165+
@Test
166+
public void testRegisteringCustomWrapperAsServlet() throws Exception {
167+
this.setupLatches(1);
168+
169+
final Dictionary<String, Object> servletProps = new Hashtable<>();
170+
servletProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, "/testjakarta");
171+
172+
final ServiceRegistration<Servlet> reg = m_context.registerService(Servlet.class, new ServletWrapper(new JakartaServlet()) {
173+
@Override
174+
public void service(ServletRequest req, ServletResponse resp)
175+
throws IOException {
176+
resp.getWriter().print("helloworldwrapped");
177+
resp.flushBuffer();
178+
}
179+
}, servletProps);
180+
181+
assertTrue(initLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS));
182+
183+
assertContent("helloworldwrapped", createURL("/testjakarta"));
184+
185+
reg.unregister();
186+
assertTrue(destroyLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS));
187+
}
120188
}
121-

http/jetty/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@
494494
<dependency>
495495
<groupId>org.apache.felix</groupId>
496496
<artifactId>org.apache.felix.http.base</artifactId>
497-
<version>5.1.8</version>
497+
<version>5.1.9-SNAPSHOT</version>
498498
</dependency>
499499
<dependency>
500500
<groupId>org.apache.felix</groupId>

http/jetty12/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@
724724
<dependency>
725725
<groupId>org.apache.felix</groupId>
726726
<artifactId>org.apache.felix.http.base</artifactId>
727-
<version>5.1.8</version>
727+
<version>5.1.9-SNAPSHOT</version>
728728
</dependency>
729729
<dependency>
730730
<groupId>org.apache.felix</groupId>

0 commit comments

Comments
 (0)