Search in sources :

Example 6 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class WhiteboardManager method registerWhiteboardService.

/**
 * Register whiteboard service in the http service
 * @param handler Context handler
 * @param info Whiteboard service info
 */
private void registerWhiteboardService(final WhiteboardContextHandler handler, final WhiteboardServiceInfo<?> info) {
    try {
        int failureCode = -1;
        if (info instanceof ServletInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ServletHandler servletHandler = new WhiteboardServletHandler(handler.getContextInfo().getServiceId(), servletContext, (ServletInfo) info, handler.getBundleContext(), info.getServiceReference().getBundle(), this.httpBundleContext.getBundle());
                handler.getRegistry().registerServlet(servletHandler);
            }
        } else if (info instanceof FilterInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final FilterHandler filterHandler = new WhiteboardFilterHandler(handler.getContextInfo().getServiceId(), servletContext, (FilterInfo) info, handler.getBundleContext());
                handler.getRegistry().registerFilter(filterHandler);
            }
        } else if (info instanceof ResourceInfo) {
            final ServletInfo servletInfo = ((ResourceInfo) info).getServletInfo();
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ServletHandler servleHandler = new HttpServiceServletHandler(handler.getContextInfo().getServiceId(), servletContext, servletInfo, new ResourceServlet(servletInfo.getPrefix()));
                handler.getRegistry().registerServlet(servleHandler);
            }
        } else if (info instanceof ListenerInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ListenerHandler listenerHandler = new WhiteboardListenerHandler(handler.getContextInfo().getServiceId(), servletContext, (ListenerInfo) info, handler.getBundleContext());
                handler.getRegistry().registerListeners(listenerHandler);
            }
        } else {
            // This should never happen, but we log anyway
            SystemLogger.error("Unknown whiteboard service " + info.getServiceReference(), null);
        }
        if (failureCode != -1) {
            this.failureStateHandler.addFailure(info, handler.getContextInfo().getServiceId(), failureCode);
        }
    } catch (final Exception e) {
        this.failureStateHandler.addFailure(info, handler.getContextInfo().getServiceId(), FAILURE_REASON_UNKNOWN, e);
    }
}
Also used : WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ResourceInfo(org.apache.felix.http.base.internal.runtime.ResourceInfo) WhiteboardServletHandler(org.apache.felix.http.base.internal.handler.WhiteboardServletHandler) ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) WhiteboardFilterHandler(org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler) WhiteboardFilterHandler(org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler) ServletException(javax.servlet.ServletException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ResourceServlet(org.apache.felix.http.base.internal.service.ResourceServlet) WhiteboardServletHandler(org.apache.felix.http.base.internal.handler.WhiteboardServletHandler) FilterInfo(org.apache.felix.http.base.internal.runtime.FilterInfo) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler)

Example 7 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class ErrorPageRegistryTest method testRangeRegistrationOverlay.

@Test
public void testRangeRegistrationOverlay() throws InvalidSyntaxException {
    final ErrorPageRegistry reg = new ErrorPageRegistry();
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    final ServletHandler handler4 = createServletHandler(1L, 0, "4xx");
    final ServletHandler handler = createServletHandler(2L, 10, "404", "403");
    reg.addServlet(handler4);
    reg.addServlet(handler);
    // check DTO
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    // a 4xx is only registered as failure DTO if overlayed by a 4xx!
    // -> no failure in this case
    assertEquals(0, holder.failedErrorPageDTOs.size());
    final Set<Long> codes4 = new HashSet<Long>();
    for (final long c : dto.errorPageDTOs[1].errorCodes) {
        assertTrue(c >= 400 && c < 500);
        codes4.add(c);
    }
    assertEquals(98, codes4.size());
    assertFalse(codes4.contains(404L));
    assertFalse(codes4.contains(403L));
    assertEquals(2, dto.errorPageDTOs[0].errorCodes.length);
    final Set<Long> codes = new HashSet<Long>();
    for (final long c : dto.errorPageDTOs[0].errorCodes) {
        assertTrue(c >= 403 && c < 405);
        codes.add(c);
    }
    assertEquals(2, codes.size());
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class ErrorPageRegistryTest method testSingleErrorPage.

@Test
public void testSingleErrorPage() throws InvalidSyntaxException, ServletException {
    final ErrorPageRegistry reg = new ErrorPageRegistry();
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // empty reg
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    assertEmpty(dto, holder);
    // register error page
    final ServletHandler h1 = createServletHandler(1L, 0, "404", "java.io.IOException");
    reg.addServlet(h1);
    verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
    // one entry in reg
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    assertNull(dto.resourceDTOs);
    assertNull(dto.servletDTOs);
    assertNotNull(dto.errorPageDTOs);
    assertEquals(1, dto.errorPageDTOs.length);
    assertEquals(1L, dto.errorPageDTOs[0].serviceId);
    assertEquals(1, dto.errorPageDTOs[0].errorCodes.length);
    assertEquals(404, dto.errorPageDTOs[0].errorCodes[0]);
    assertEquals(1, dto.errorPageDTOs[0].exceptions.length);
    assertEquals("java.io.IOException", dto.errorPageDTOs[0].exceptions[0]);
    assertTrue(holder.failedErrorPageDTOs.isEmpty());
    // test error handling
    assertNotNull(reg.get(new IOException(), 404));
    assertNotNull(reg.get(new RuntimeException(), 404));
    assertNotNull(reg.get(new IOException(), 500));
    assertNotNull(reg.get(new FileNotFoundException(), 500));
    assertNull(reg.get(new RuntimeException(), 500));
    // remove servlet
    final Servlet s = h1.getServlet();
    reg.removeServlet(h1.getServletInfo(), true);
    verify(s).destroy();
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    assertEmpty(dto, holder);
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletConfig(javax.servlet.ServletConfig) FileNotFoundException(java.io.FileNotFoundException) Servlet(javax.servlet.Servlet) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class ErrorPageRegistryTest method testSimpleHiding.

@Test
public void testSimpleHiding() throws InvalidSyntaxException, ServletException {
    final ErrorPageRegistry reg = new ErrorPageRegistry();
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // check DTO
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    assertEmpty(dto, holder);
    // register error pages
    final ServletHandler h1 = createServletHandler(1L, 0, "404", "java.io.IOException");
    reg.addServlet(h1);
    final ServletHandler h2 = createServletHandler(2L, 10, "404", "some.other.Exception");
    reg.addServlet(h2);
    verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
    verify(h2.getServlet()).init(Matchers.any(ServletConfig.class));
    // two entries in DTO
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    assertNull(dto.resourceDTOs);
    assertNull(dto.servletDTOs);
    assertNotNull(dto.errorPageDTOs);
    assertEquals(2, dto.errorPageDTOs.length);
    assertEquals(0, dto.errorPageDTOs[1].errorCodes.length);
    assertEquals(1, dto.errorPageDTOs[0].errorCodes.length);
    assertEquals(404, dto.errorPageDTOs[0].errorCodes[0]);
    assertEquals(1, dto.errorPageDTOs[1].exceptions.length);
    assertEquals(1, dto.errorPageDTOs[0].exceptions.length);
    assertEquals("java.io.IOException", dto.errorPageDTOs[1].exceptions[0]);
    assertEquals("some.other.Exception", dto.errorPageDTOs[0].exceptions[0]);
    assertEquals(1, holder.failedErrorPageDTOs.size());
    assertEquals(1L, holder.failedErrorPageDTOs.get(0).serviceId);
    assertEquals(1, holder.failedErrorPageDTOs.get(0).errorCodes.length);
    assertEquals(404, holder.failedErrorPageDTOs.get(0).errorCodes[0]);
    assertEquals(0, holder.failedErrorPageDTOs.get(0).exceptions.length);
    assertEquals(DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, holder.failedErrorPageDTOs.get(0).failureReason);
    // remove second page
    final Servlet s2 = h2.getServlet();
    reg.removeServlet(h2.getServletInfo(), true);
    verify(s2).destroy();
    // one entry in DTO
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    assertNull(dto.resourceDTOs);
    assertNull(dto.servletDTOs);
    assertNotNull(dto.errorPageDTOs);
    assertEquals(1, dto.errorPageDTOs.length);
    assertEquals(1, dto.errorPageDTOs[0].errorCodes.length);
    assertEquals(404, dto.errorPageDTOs[0].errorCodes[0]);
    assertEquals(1, dto.errorPageDTOs[0].exceptions.length);
    assertEquals("java.io.IOException", dto.errorPageDTOs[0].exceptions[0]);
    assertTrue(holder.failedErrorPageDTOs.isEmpty());
    // test error handling
    assertNotNull(reg.get(new IOException(), 404));
    assertNotNull(reg.get(new RuntimeException(), 404));
    assertNotNull(reg.get(new IOException(), 500));
    assertNotNull(reg.get(new FileNotFoundException(), 500));
    assertNull(reg.get(new RuntimeException(), 500));
    // remove first page
    final Servlet s1 = h1.getServlet();
    reg.removeServlet(h1.getServletInfo(), true);
    verify(s1).destroy();
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
    assertEmpty(dto, holder);
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletConfig(javax.servlet.ServletConfig) FileNotFoundException(java.io.FileNotFoundException) Servlet(javax.servlet.Servlet) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with ServletHandler

use of org.apache.felix.http.base.internal.handler.ServletHandler in project felix by apache.

the class HandlerRegistryTest method testAddRemoveServlet.

@Test
public void testAddRemoveServlet() throws Exception {
    registry.init();
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    dto.serviceId = HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID;
    dto.servletDTOs = new ServletDTO[0];
    Servlet servlet = Mockito.mock(Servlet.class);
    final ServletInfo info = new ServletInfo("foo", "/foo", Collections.<String, String>emptyMap());
    ServletHandler handler = new HttpServiceServletHandler(null, info, servlet);
    assertTrue(registry.getRuntimeInfo(dto, holder));
    assertEquals("Precondition", 0, dto.servletDTOs.length);
    registry.getRegistry(handler.getContextServiceId()).registerServlet(handler);
    Mockito.verify(servlet, Mockito.times(1)).init(Mockito.any(ServletConfig.class));
    assertTrue(registry.getRuntimeInfo(dto, holder));
    assertEquals(1, dto.servletDTOs.length);
    assertEquals(info.getServiceId(), dto.servletDTOs[0].serviceId);
    final ServletInfo info2 = new ServletInfo("bar", "/bar", Collections.<String, String>emptyMap());
    ServletHandler handler2 = new HttpServiceServletHandler(null, info2, Mockito.mock(Servlet.class));
    registry.getRegistry(handler.getContextServiceId()).registerServlet(handler2);
    assertTrue(registry.getRuntimeInfo(dto, holder));
    assertEquals(2, dto.servletDTOs.length);
    final ServletInfo info3 = new ServletInfo("zar", "/foo", Collections.<String, String>emptyMap());
    ServletHandler handler3 = new HttpServiceServletHandler(null, info3, Mockito.mock(Servlet.class));
    registry.getRegistry(handler.getContextServiceId()).registerServlet(handler3);
    assertTrue(registry.getRuntimeInfo(dto, holder));
    assertEquals(2, dto.servletDTOs.length);
    assertEquals(1, holder.failedServletDTOs.size());
    registry.shutdown();
}
Also used : ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletConfig(javax.servlet.ServletConfig) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Aggregations

ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)22 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)12 Test (org.junit.Test)9 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)7 ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)7 Servlet (javax.servlet.Servlet)6 ServletConfig (javax.servlet.ServletConfig)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)3 FileNotFoundException (java.io.FileNotFoundException)2 RequestDispatcher (javax.servlet.RequestDispatcher)2 ServletException (javax.servlet.ServletException)2 RequestDispatcherImpl (org.apache.felix.http.base.internal.dispatch.RequestDispatcherImpl)2 RequestInfo (org.apache.felix.http.base.internal.dispatch.RequestInfo)2 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)2 ServletResolution (org.apache.felix.http.base.internal.registry.ServletResolution)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1