Search in sources :

Example 1 with PathResolution

use of org.apache.felix.http.base.internal.registry.PathResolution in project felix by apache.

the class SharedServletContextImpl method getRequestDispatcher.

@Override
public RequestDispatcher getRequestDispatcher(String path) {
    // See section 9.1 of Servlet 3.x specification...
    if (path == null || (!path.startsWith("/") && !"".equals(path))) {
        return null;
    }
    String query = null;
    int q = 0;
    if ((q = path.indexOf('?')) > 0) {
        query = path.substring(q + 1);
        path = path.substring(0, q);
    }
    // TODO remove path parameters...
    final String encodedRequestURI = path == null ? "" : removeDotSegments(path);
    final String requestURI = decodePath(encodedRequestURI);
    final RequestDispatcher dispatcher;
    final PathResolution pathResolution = this.registry.resolve(requestURI);
    if (pathResolution != null) {
        final ServletResolution resolution = new ServletResolution();
        resolution.handler = pathResolution.handler;
        resolution.handlerRegistry = this.registry;
        final RequestInfo requestInfo = new RequestInfo(pathResolution.servletPath, pathResolution.pathInfo, query, UriUtils.concat(this.contextPath, encodedRequestURI));
        dispatcher = new RequestDispatcherImpl(resolution, requestInfo);
    } else {
        dispatcher = null;
    }
    return dispatcher;
}
Also used : ServletResolution(org.apache.felix.http.base.internal.registry.ServletResolution) RequestInfo(org.apache.felix.http.base.internal.dispatch.RequestInfo) RequestDispatcher(javax.servlet.RequestDispatcher) PathResolution(org.apache.felix.http.base.internal.registry.PathResolution) RequestDispatcherImpl(org.apache.felix.http.base.internal.dispatch.RequestDispatcherImpl)

Example 2 with PathResolution

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

use of org.apache.felix.http.base.internal.registry.PathResolution 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 4 with PathResolution

use of org.apache.felix.http.base.internal.registry.PathResolution in project felix by apache.

the class ServletContextImpl method getRequestDispatcher.

@Override
public RequestDispatcher getRequestDispatcher(String path) {
    // See section 9.1 of Servlet 3.x specification...
    if (path == null || (!path.startsWith("/") && !"".equals(path))) {
        return null;
    }
    String query = null;
    int q = 0;
    if ((q = path.indexOf('?')) > 0) {
        query = path.substring(q + 1);
        path = path.substring(0, q);
    }
    // TODO remove path parameters...
    final String encodedRequestURI = path == null ? "" : removeDotSegments(path);
    final String requestURI = decodePath(encodedRequestURI);
    final RequestDispatcher dispatcher;
    final PathResolution pathResolution = this.handlerRegistry.resolve(requestURI);
    if (pathResolution != null) {
        final ServletResolution resolution = new ServletResolution();
        resolution.handler = pathResolution.handler;
        resolution.handlerRegistry = this.handlerRegistry;
        final RequestInfo requestInfo = new RequestInfo(pathResolution.servletPath, pathResolution.pathInfo, query, UriUtils.concat(this.getContextPath(), encodedRequestURI));
        dispatcher = new RequestDispatcherImpl(resolution, requestInfo);
    } else {
        dispatcher = null;
    }
    return dispatcher;
}
Also used : ServletResolution(org.apache.felix.http.base.internal.registry.ServletResolution) RequestInfo(org.apache.felix.http.base.internal.dispatch.RequestInfo) RequestDispatcher(javax.servlet.RequestDispatcher) PathResolution(org.apache.felix.http.base.internal.registry.PathResolution) RequestDispatcherImpl(org.apache.felix.http.base.internal.dispatch.RequestDispatcherImpl)

Aggregations

PathResolution (org.apache.felix.http.base.internal.registry.PathResolution)4 RequestDispatcher (javax.servlet.RequestDispatcher)2 RequestDispatcherImpl (org.apache.felix.http.base.internal.dispatch.RequestDispatcherImpl)2 RequestInfo (org.apache.felix.http.base.internal.dispatch.RequestInfo)2 ServletResolution (org.apache.felix.http.base.internal.registry.ServletResolution)2 IOException (java.io.IOException)1 FilterChain (javax.servlet.FilterChain)1 FilterConfig (javax.servlet.FilterConfig)1 ServletException (javax.servlet.ServletException)1 ServletRequest (javax.servlet.ServletRequest)1 ServletRequestEvent (javax.servlet.ServletRequestEvent)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)1 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)1 PerContextHandlerRegistry (org.apache.felix.http.base.internal.registry.PerContextHandlerRegistry)1 WhiteboardManager (org.apache.felix.http.base.internal.whiteboard.WhiteboardManager)1 RequestInfoDTO (org.osgi.service.http.runtime.dto.RequestInfoDTO)1