Search in sources :

Example 1 with PreprocessorHandler

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

the class WhiteboardManager method getRuntimeInfo.

public RegistryRuntime getRuntimeInfo() {
    final FailedDTOHolder failedDTOHolder = new FailedDTOHolder();
    final Collection<ServletContextDTO> contextDTOs = new ArrayList<>();
    // get sort list of context handlers
    final List<WhiteboardContextHandler> contextHandlerList = new ArrayList<>();
    synchronized (this.contextMap) {
        for (final List<WhiteboardContextHandler> list : this.contextMap.values()) {
            if (!list.isEmpty()) {
                contextHandlerList.add(list.get(0));
            }
        }
        this.failureStateHandler.getRuntimeInfo(failedDTOHolder);
    }
    Collections.sort(contextHandlerList);
    for (final WhiteboardContextHandler handler : contextHandlerList) {
        final ServletContextDTO scDTO = ServletContextDTOBuilder.build(handler.getContextInfo(), handler.getSharedContext(), -1);
        if (registry.getRuntimeInfo(scDTO, failedDTOHolder)) {
            contextDTOs.add(scDTO);
        }
    }
    final List<PreprocessorDTO> preprocessorDTOs = new ArrayList<>();
    final List<PreprocessorHandler> localHandlers = this.preprocessorHandlers;
    for (final PreprocessorHandler handler : localHandlers) {
        preprocessorDTOs.add(PreprocessorDTOBuilder.build(handler.getPreprocessorInfo(), -1));
    }
    return new RegistryRuntime(failedDTOHolder, contextDTOs, preprocessorDTOs);
}
Also used : FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) PreprocessorDTO(org.osgi.service.http.runtime.dto.PreprocessorDTO) PreprocessorHandler(org.apache.felix.http.base.internal.handler.PreprocessorHandler) ArrayList(java.util.ArrayList) RegistryRuntime(org.apache.felix.http.base.internal.runtime.dto.RegistryRuntime)

Example 2 with PreprocessorHandler

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

the class WhiteboardManager method invokePreprocessors.

/**
 * Invoke all preprocessors
 *
 * @param req The request
 * @param res The response
 * @return {@code true} to continue with dispatching, {@code false} to terminate the request.
 * @throws IOException
 * @throws ServletException
 */
public void invokePreprocessors(final HttpServletRequest req, final HttpServletResponse res, final Preprocessor dispatcher) throws ServletException, IOException {
    final List<PreprocessorHandler> localHandlers = this.preprocessorHandlers;
    if (localHandlers.isEmpty()) {
        // no preprocessors, we can directly execute
        dispatcher.doFilter(req, res, null);
    } else {
        final FilterChain chain = new FilterChain() {

            private int index = 0;

            @Override
            public void doFilter(final ServletRequest request, final ServletResponse response) throws IOException, ServletException {
                if (index == localHandlers.size()) {
                    dispatcher.doFilter(request, response, null);
                } else {
                    final PreprocessorHandler handler = localHandlers.get(index);
                    index++;
                    handler.handle(request, response, this);
                }
            }
        };
        chain.doFilter(req, res);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) PreprocessorHandler(org.apache.felix.http.base.internal.handler.PreprocessorHandler) FilterChain(javax.servlet.FilterChain)

Example 3 with PreprocessorHandler

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

the class WhiteboardManager method removeWhiteboardService.

/**
 * Remove whiteboard service from the registry.
 *
 * @param info The service id of the whiteboard service
 */
public void removeWhiteboardService(final WhiteboardServiceInfo<?> info) {
    synchronized (this.contextMap) {
        if (!failureStateHandler.remove(info)) {
            if (info instanceof PreprocessorInfo) {
                synchronized (this.preprocessorHandlers) {
                    final List<PreprocessorHandler> newList = new ArrayList<>(this.preprocessorHandlers);
                    final Iterator<PreprocessorHandler> iter = newList.iterator();
                    while (iter.hasNext()) {
                        final PreprocessorHandler handler = iter.next();
                        if (handler.getPreprocessorInfo().compareTo((PreprocessorInfo) info) == 0) {
                            iter.remove();
                            this.preprocessorHandlers = newList;
                            updateRuntimeChangeCount();
                            return;
                        }
                    }
                // not found, nothing to do
                }
                return;
            }
            final List<WhiteboardContextHandler> handlerList = this.servicesMap.remove(info);
            if (handlerList != null) {
                for (final WhiteboardContextHandler h : handlerList) {
                    if (!failureStateHandler.remove(info, h.getContextInfo().getServiceId())) {
                        if (info instanceof ListenerInfo && ((ListenerInfo) info).isListenerType(ServletContextListener.class.getName())) {
                            final ListenerHandler handler = h.getRegistry().getEventListenerRegistry().getServletContextListener((ListenerInfo) info);
                            if (handler != null) {
                                final ServletContextListener listener = (ServletContextListener) handler.getListener();
                                if (listener != null) {
                                    EventListenerRegistry.contextDestroyed(handler.getListenerInfo(), listener, new ServletContextEvent(handler.getContext()));
                                }
                            }
                        }
                        this.unregisterWhiteboardService(h, info);
                    }
                }
            }
        }
        this.failureStateHandler.removeAll(info);
    }
    updateRuntimeChangeCount();
}
Also used : ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ServletContextListener(javax.servlet.ServletContextListener) PreprocessorHandler(org.apache.felix.http.base.internal.handler.PreprocessorHandler) ArrayList(java.util.ArrayList) PreprocessorInfo(org.apache.felix.http.base.internal.runtime.PreprocessorInfo) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 4 with PreprocessorHandler

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

the class WhiteboardManager method addWhiteboardService.

/**
 * Add new whiteboard service to the registry
 *
 * @param info Whiteboard service info
 * @return {@code true} if it matches this http service runtime
 */
public boolean addWhiteboardService(@Nonnull final WhiteboardServiceInfo<?> info) {
    // no logging and no DTO if other target service
    if (isMatchingService(info)) {
        if (info.isValid()) {
            if (info instanceof PreprocessorInfo) {
                final PreprocessorHandler handler = new PreprocessorHandler(this.httpBundleContext, this.webContext, ((PreprocessorInfo) info));
                final int result = handler.init();
                if (result == -1) {
                    synchronized (this.preprocessorHandlers) {
                        final List<PreprocessorHandler> newList = new ArrayList<>(this.preprocessorHandlers);
                        newList.add(handler);
                        Collections.sort(newList);
                        this.preprocessorHandlers = newList;
                    }
                } else {
                    this.failureStateHandler.addFailure(info, FAILURE_REASON_VALIDATION_FAILED);
                }
                updateRuntimeChangeCount();
                return true;
            }
            synchronized (this.contextMap) {
                final List<WhiteboardContextHandler> handlerList = this.getMatchingContexts(info);
                this.servicesMap.put(info, handlerList);
                if (handlerList.isEmpty()) {
                    this.failureStateHandler.addFailure(info, FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING);
                } else {
                    for (final WhiteboardContextHandler h : handlerList) {
                        final int result = this.checkForServletRegistrationInHttpServiceContext(h, info);
                        if (result == -1) {
                            this.registerWhiteboardService(h, info);
                            if (info instanceof ListenerInfo && ((ListenerInfo) info).isListenerType(ServletContextListener.class.getName())) {
                                final ListenerHandler handler = h.getRegistry().getEventListenerRegistry().getServletContextListener((ListenerInfo) info);
                                if (handler != null) {
                                    final ServletContextListener listener = (ServletContextListener) handler.getListener();
                                    if (listener != null) {
                                        EventListenerRegistry.contextInitialized(handler.getListenerInfo(), listener, new ServletContextEvent(handler.getContext()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            this.failureStateHandler.addFailure(info, FAILURE_REASON_VALIDATION_FAILED);
        }
        updateRuntimeChangeCount();
        return true;
    }
    return false;
}
Also used : ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ServletContextListener(javax.servlet.ServletContextListener) PreprocessorHandler(org.apache.felix.http.base.internal.handler.PreprocessorHandler) ArrayList(java.util.ArrayList) PreprocessorInfo(org.apache.felix.http.base.internal.runtime.PreprocessorInfo) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) ServletContextEvent(javax.servlet.ServletContextEvent)

Aggregations

PreprocessorHandler (org.apache.felix.http.base.internal.handler.PreprocessorHandler)4 ArrayList (java.util.ArrayList)3 ServletContextEvent (javax.servlet.ServletContextEvent)2 ServletContextListener (javax.servlet.ServletContextListener)2 ListenerHandler (org.apache.felix.http.base.internal.handler.ListenerHandler)2 WhiteboardListenerHandler (org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler)2 ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)2 PreprocessorInfo (org.apache.felix.http.base.internal.runtime.PreprocessorInfo)2 FilterChain (javax.servlet.FilterChain)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)1 RegistryRuntime (org.apache.felix.http.base.internal.runtime.dto.RegistryRuntime)1 PreprocessorDTO (org.osgi.service.http.runtime.dto.PreprocessorDTO)1 ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)1