Search in sources :

Example 6 with ExtServletContext

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

Example 7 with ExtServletContext

use of org.apache.felix.http.base.internal.context.ExtServletContext in project felix by apache.

the class FilterRegistryTest method createListenerHandler.

private static ListenerHandler createListenerHandler(final long id, final int ranking, final Class<? extends EventListener> type) throws InvalidSyntaxException {
    final ListenerInfo info = createListenerInfo(id, ranking, type);
    final ExtServletContext ctx = mock(ExtServletContext.class);
    return new WhiteboardListenerHandler(1L, ctx, info, info.getServiceReference().getBundle().getBundleContext());
}
Also used : WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext)

Example 8 with ExtServletContext

use of org.apache.felix.http.base.internal.context.ExtServletContext in project felix by apache.

the class Dispatcher method dispatch.

/**
 * Responsible for dispatching a given request to the actual applicable servlet and/or filters in the local registry.
 *
 * @param req the {@link ServletRequest} to dispatch;
 * @param res the {@link ServletResponse} to dispatch.
 * @throws ServletException in case of exceptions during the actual dispatching;
 * @throws IOException in case of I/O problems.
 */
public void dispatch(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
    final WhiteboardManager mgr = this.whiteboardManager;
    if (mgr == null) {
        // not active, always return 404
        res.sendError(404);
        return;
    }
    // check for invalidating session(s) first
    final HttpSession session = req.getSession(false);
    if (session != null) {
        final Set<String> names = HttpSessionWrapper.getExpiredSessionContextNames(session);
        mgr.sessionDestroyed(session, names);
    }
    // invoke preprocessors and then dispatching
    mgr.invokePreprocessors(req, res, new Preprocessor() {

        @Override
        public void init(final FilterConfig filterConfig) throws ServletException {
        // nothing to do
        }

        @Override
        public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
            final HttpServletRequest req = (HttpServletRequest) request;
            final HttpServletResponse res = (HttpServletResponse) response;
            // get full decoded path for dispatching
            // we can't use req.getRequestURI() or req.getRequestURL() as these are returning the encoded path
            String path = req.getServletPath();
            if (path == null) {
                path = "";
            }
            if (req.getPathInfo() != null) {
                path = path.concat(req.getPathInfo());
            }
            final String requestURI = path;
            // Determine which servlet we should forward the request to...
            final PathResolution pr = handlerRegistry.resolveServlet(requestURI);
            final PerContextHandlerRegistry errorRegistry = (pr != null ? pr.handlerRegistry : handlerRegistry.getBestMatchingRegistry(requestURI));
            final String servletName = (pr != null ? pr.handler.getName() : null);
            final HttpServletResponse wrappedResponse = new ServletResponseWrapper(req, res, servletName, errorRegistry);
            if (pr == null) {
                wrappedResponse.sendError(404);
                return;
            }
            final ExtServletContext servletContext = pr.handler.getContext();
            final RequestInfo requestInfo = new RequestInfo(pr.servletPath, pr.pathInfo, null, req.getRequestURI());
            final HttpServletRequest wrappedRequest = new ServletRequestWrapper(req, servletContext, requestInfo, null, pr.handler.getServletInfo().isAsyncSupported(), pr.handler.getMultipartConfig(), pr.handler.getMultipartSecurityContext());
            final FilterHandler[] filterHandlers = handlerRegistry.getFilters(pr, req.getDispatcherType(), pr.requestURI);
            try {
                if (servletContext.getServletRequestListener() != null) {
                    servletContext.getServletRequestListener().requestInitialized(new ServletRequestEvent(servletContext, wrappedRequest));
                }
                final FilterChain filterChain = new InvocationChain(pr.handler, filterHandlers);
                filterChain.doFilter(wrappedRequest, wrappedResponse);
            } catch (final Exception e) {
                SystemLogger.error("Exception while processing request to " + requestURI, e);
                req.setAttribute(RequestDispatcher.ERROR_EXCEPTION, e);
                req.setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE, e.getClass().getName());
                wrappedResponse.sendError(500);
            } finally {
                if (servletContext.getServletRequestListener() != null) {
                    servletContext.getServletRequestListener().requestDestroyed(new ServletRequestEvent(servletContext, wrappedRequest));
                }
            }
        }

        @Override
        public void destroy() {
        // nothing to do
        }
    });
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) WhiteboardManager(org.apache.felix.http.base.internal.whiteboard.WhiteboardManager) IOException(java.io.IOException) PerContextHandlerRegistry(org.apache.felix.http.base.internal.registry.PerContextHandlerRegistry) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestEvent(javax.servlet.ServletRequestEvent) FilterConfig(javax.servlet.FilterConfig) Preprocessor(org.osgi.service.http.whiteboard.Preprocessor) PathResolution(org.apache.felix.http.base.internal.registry.PathResolution)

Example 9 with ExtServletContext

use of org.apache.felix.http.base.internal.context.ExtServletContext in project felix by apache.

the class ServletContextManager method addServletContext.

private ExtServletContext addServletContext(HttpContext httpContext) {
    ExtServletContext context = new ServletContextImpl(this.bundle, this.context, httpContext, this.sharedAttributes, handlerRegistry);
    this.contextMap.put(httpContext, context);
    return context;
}
Also used : ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext)

Example 10 with ExtServletContext

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

ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)10 ServletException (javax.servlet.ServletException)4 ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)4 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)3 FilterInfo (org.apache.felix.http.base.internal.runtime.FilterInfo)3 IOException (java.io.IOException)2 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2 Servlet (javax.servlet.Servlet)2 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)2 HttpServiceFilterHandler (org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler)2 WhiteboardListenerHandler (org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler)2 ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)2 Filter (javax.servlet.Filter)1 FilterChain (javax.servlet.FilterChain)1 FilterConfig (javax.servlet.FilterConfig)1 ServletRequest (javax.servlet.ServletRequest)1 ServletRequestEvent (javax.servlet.ServletRequestEvent)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1