Search in sources :

Example 1 with HttpServiceServletHandler

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

the class SharedHttpServiceImpl method registerServlet.

/**
 * Register a servlet
 */
public void registerServlet(@Nonnull final String alias, @Nonnull final ExtServletContext httpContext, @Nonnull final Servlet servlet, @Nonnull final ServletInfo servletInfo) throws ServletException, NamespaceException {
    final ServletHandler handler = new HttpServiceServletHandler(httpContext, servletInfo, servlet);
    synchronized (this.aliasMap) {
        if (this.aliasMap.containsKey(alias)) {
            throw new NamespaceException("Alias " + alias + " is already in use.");
        }
        this.handlerRegistry.getRegistry(handler.getContextServiceId()).registerServlet(handler);
        this.aliasMap.put(alias, handler);
    }
}
Also used : 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) NamespaceException(org.osgi.service.http.NamespaceException)

Example 2 with HttpServiceServletHandler

use of org.apache.felix.http.base.internal.handler.HttpServiceServletHandler 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 3 with HttpServiceServletHandler

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

the class ErrorPageRegistryTest method createServletHandler.

private static ServletHandler createServletHandler(final long id, final int ranking, final String... codes) throws InvalidSyntaxException {
    final ServletInfo si = createServletInfo(id, ranking, codes);
    final ExtServletContext ctx = mock(ExtServletContext.class);
    final Servlet servlet = mock(Servlet.class);
    return new HttpServiceServletHandler(ctx, si, servlet);
}
Also used : ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) Servlet(javax.servlet.Servlet)

Example 4 with HttpServiceServletHandler

use of org.apache.felix.http.base.internal.handler.HttpServiceServletHandler 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)

Example 5 with HttpServiceServletHandler

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

the class ServletRegistryTest method createServletHandler.

private static ServletHandler createServletHandler(final long id, final int ranking, final String... paths) throws InvalidSyntaxException {
    final ServletInfo si = createServletInfo(id, ranking, paths);
    final ExtServletContext ctx = mock(ExtServletContext.class);
    final Servlet servlet = mock(Servlet.class);
    return new HttpServiceServletHandler(ctx, si, servlet);
}
Also used : ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) Servlet(javax.servlet.Servlet)

Aggregations

HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)5 ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)4 Servlet (javax.servlet.Servlet)3 ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)3 ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)3 IOException (java.io.IOException)1 ServletConfig (javax.servlet.ServletConfig)1 ServletException (javax.servlet.ServletException)1 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)1 ListenerHandler (org.apache.felix.http.base.internal.handler.ListenerHandler)1 WhiteboardFilterHandler (org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler)1 WhiteboardListenerHandler (org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler)1 WhiteboardServletHandler (org.apache.felix.http.base.internal.handler.WhiteboardServletHandler)1 FilterInfo (org.apache.felix.http.base.internal.runtime.FilterInfo)1 ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)1 ResourceInfo (org.apache.felix.http.base.internal.runtime.ResourceInfo)1 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)1 ResourceServlet (org.apache.felix.http.base.internal.service.ResourceServlet)1 Test (org.junit.Test)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1