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);
}
}
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);
}
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);
}
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;
}
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);
}
}
}
Aggregations