Search in sources :

Example 6 with FilterHandler

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

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

the class EventListenerRegistryTest method testSingleFilter.

@Test
public void testSingleFilter() throws InvalidSyntaxException, ServletException {
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // check DTO
    reg.getRuntimeInfo(dto, holder.failedFilterDTOs);
    assertEmpty(dto, holder);
    // register filter
    final FilterHandler h1 = createFilterHandler(1L, 0, "/foo");
    reg.addFilter(h1);
    verify(h1.getFilter()).init(Matchers.any(FilterConfig.class));
    // 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();
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedFilterDTOs);
    assertEmpty(dto, holder);
}
Also used : FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) Filter(javax.servlet.Filter) FilterConfig(javax.servlet.FilterConfig) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler) HttpServiceFilterHandler(org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler) Test(org.junit.Test)

Example 8 with FilterHandler

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

the class EventListenerRegistryTest method testFilterOrdering.

@Test
public void testFilterOrdering() 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);
}
Also used : FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler) HttpServiceFilterHandler(org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler) Test(org.junit.Test)

Example 9 with FilterHandler

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

the class RequestInfoDTOBuilder method build.

public RequestInfoDTO build() {
    final RequestInfoDTO requestInfoDTO = new RequestInfoDTO();
    requestInfoDTO.path = path;
    final PathResolution pr = registry.resolveServlet(path);
    if (pr == null) {
        // no servlet found, return empty DTO
        requestInfoDTO.filterDTOs = FILTER_DTO_ARRAY;
        return requestInfoDTO;
    }
    requestInfoDTO.servletContextId = pr.handler.getContextServiceId();
    if (pr.handler.getServletInfo().isResource()) {
        requestInfoDTO.resourceDTO = ResourceDTOBuilder.build(pr.handler, -1);
        requestInfoDTO.resourceDTO.patterns = pr.patterns;
    } else {
        requestInfoDTO.servletDTO = ServletDTOBuilder.build(pr.handler, -1);
        requestInfoDTO.servletDTO.patterns = pr.patterns;
    }
    final FilterHandler[] filterHandlers = registry.getFilters(pr, DispatcherType.REQUEST, path);
    requestInfoDTO.filterDTOs = FilterDTOBuilder.build(filterHandlers);
    return requestInfoDTO;
}
Also used : RequestInfoDTO(org.osgi.service.http.runtime.dto.RequestInfoDTO) PathResolution(org.apache.felix.http.base.internal.registry.PathResolution) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler)

Example 10 with FilterHandler

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

the class PerBundleHttpServiceImpl method registerFilter.

/**
 * @see org.apache.felix.http.api.ExtHttpService#registerFilter(javax.servlet.Filter, java.lang.String, java.util.Dictionary, int, org.osgi.service.http.HttpContext)
 */
@Override
public void registerFilter(final Filter filter, final String pattern, final Dictionary initParams, final int ranking, final HttpContext context) throws ServletException {
    if (filter == null) {
        throw new IllegalArgumentException("Filter must not be null");
    }
    final Map<String, String> paramMap = new HashMap<String, String>();
    if (initParams != null && initParams.size() > 0) {
        Enumeration e = initParams.keys();
        while (e.hasMoreElements()) {
            Object key = e.nextElement();
            Object value = initParams.get(key);
            if ((key instanceof String) && (value instanceof String)) {
                paramMap.put((String) key, (String) value);
            }
        }
    }
    final FilterInfo filterInfo = new FilterInfo(String.format("%s_%d", filter.getClass(), this.hashCode()), pattern, ranking, paramMap);
    if (!filterInfo.isValid()) {
        throw new ServletException("Invalid registration information for filter.");
    }
    final ExtServletContext httpContext = getServletContext(context);
    final FilterHandler holder = new HttpServiceFilterHandler(httpContext, filterInfo, filter);
    if (this.sharedHttpService.registerFilter(holder)) {
        synchronized (this.localFilters) {
            this.localFilters.add(holder);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) Enumeration(java.util.Enumeration) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) HashMap(java.util.HashMap) HttpServiceFilterHandler(org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler) FilterInfo(org.apache.felix.http.base.internal.runtime.FilterInfo) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler) HttpServiceFilterHandler(org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler)

Aggregations

FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)10 HttpServiceFilterHandler (org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler)5 ServletException (javax.servlet.ServletException)4 FilterChain (javax.servlet.FilterChain)3 IOException (java.io.IOException)2 ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)2 ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)2 FilterInfo (org.apache.felix.http.base.internal.runtime.FilterInfo)2 Test (org.junit.Test)2 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Filter (javax.servlet.Filter)1 FilterConfig (javax.servlet.FilterConfig)1 Servlet (javax.servlet.Servlet)1 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)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