Search in sources :

Example 1 with ServletHandler

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

the class ServletResponseWrapper method sendError.

@Override
public void sendError(final int code, final String message) throws IOException {
    resetBuffer();
    setStatus(code);
    boolean invokeSuper = true;
    if (invocationCount.incrementAndGet() == 1) {
        // If we are allowed to have a body
        if (code != SC_NO_CONTENT && code != SC_NOT_MODIFIED && code != SC_PARTIAL_CONTENT && code >= SC_OK) {
            final Throwable exception = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
            final ServletHandler errorResolution = (errorRegistry == null ? null : errorRegistry.getErrorHandler(code, exception));
            if (errorResolution != null) {
                try {
                    request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, new Integer(code));
                    if (message != null) {
                        request.setAttribute(RequestDispatcher.ERROR_MESSAGE, message);
                    }
                    request.setAttribute(RequestDispatcher.ERROR_REQUEST_URI, request.getRequestURI());
                    if (this.servletName != null) {
                        request.setAttribute(RequestDispatcher.ERROR_SERVLET_NAME, this.servletName);
                    }
                    final String servletPath = null;
                    final String pathInfo = request.getRequestURI();
                    // XXX
                    final String queryString = null;
                    final RequestInfo requestInfo = new RequestInfo(servletPath, pathInfo, queryString, pathInfo);
                    final FilterHandler[] filterHandlers = errorRegistry.getFilterHandlers(errorResolution, DispatcherType.ERROR, request.getRequestURI());
                    final ServletRequestWrapper reqWrapper = new ServletRequestWrapper(request, errorResolution.getContext(), requestInfo, null, false, null, null);
                    final FilterChain filterChain = new InvocationChain(errorResolution, filterHandlers);
                    filterChain.doFilter(reqWrapper, this);
                    invokeSuper = false;
                } catch (final ServletException e) {
                // ignore
                } finally {
                    request.removeAttribute(RequestDispatcher.ERROR_STATUS_CODE);
                    request.removeAttribute(RequestDispatcher.ERROR_MESSAGE);
                    request.removeAttribute(RequestDispatcher.ERROR_REQUEST_URI);
                    request.removeAttribute(RequestDispatcher.ERROR_SERVLET_NAME);
                    request.removeAttribute(RequestDispatcher.ERROR_EXCEPTION);
                    request.removeAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE);
                }
            }
        }
    }
    if (invokeSuper) {
        super.sendError(code, message);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletException(javax.servlet.ServletException) ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) FilterChain(javax.servlet.FilterChain) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler)

Example 2 with ServletHandler

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

the class ErrorPageRegistry method get.

/**
 * Get the servlet handling the exception
 * @param exception Error exception
 * @return The servlet handling the error or {@code null}
 */
private ServletHandler get(final Throwable exception) {
    if (exception == null) {
        return null;
    }
    ServletHandler servletHandler = null;
    Class<?> throwableClass = exception.getClass();
    while (servletHandler == null && throwableClass != null) {
        final List<ServletHandler> list = this.errorMapping.get(throwableClass.getName());
        if (list != null) {
            servletHandler = list.get(0);
        }
        if (servletHandler == null) {
            throwableClass = throwableClass.getSuperclass();
            if (!Throwable.class.isAssignableFrom(throwableClass)) {
                throwableClass = null;
            }
        }
    }
    return servletHandler;
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler)

Example 3 with ServletHandler

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

the class ErrorPageRegistry method removeErrorHandling.

private void removeErrorHandling(final ServletInfo info, final long code, final String exception) {
    final String key = (exception != null ? exception : String.valueOf(code));
    final List<ServletHandler> list = errorMapping.get(key);
    if (list != null) {
        int index = 0;
        final Iterator<ServletHandler> i = list.iterator();
        while (i.hasNext()) {
            final ServletHandler handler = i.next();
            if (handler.getServletInfo().equals(info)) {
                final List<ServletHandler> newList = new ArrayList<ServletHandler>(list);
                newList.remove(handler);
                if (index == 0) {
                    handler.destroy();
                    index++;
                    while (index < list.size()) {
                        final ServletHandler next = list.get(index);
                        ErrorRegistrationStatus nextStatus = null;
                        for (final ErrorRegistrationStatus s : this.status) {
                            if (s.handler.getServletInfo().equals(next.getServletInfo())) {
                                nextStatus = s;
                                break;
                            }
                        }
                        this.removeReason(nextStatus, code, exception, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
                        final int reason = next.init();
                        this.addReason(nextStatus, code, exception, reason);
                        if (reason == -1) {
                            break;
                        } else {
                            newList.remove(next);
                        }
                    }
                }
                if (newList.isEmpty()) {
                    errorMapping.remove(key);
                } else {
                    errorMapping.put(key, newList);
                }
                break;
            }
            index++;
        }
    }
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) ArrayList(java.util.ArrayList)

Example 4 with ServletHandler

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

the class ServletContextImpl method getNamedDispatcher.

@Override
public RequestDispatcher getNamedDispatcher(final String name) {
    if (name == null) {
        return null;
    }
    final RequestDispatcher dispatcher;
    final ServletHandler servletHandler = this.handlerRegistry.resolveServletByName(name);
    if (servletHandler != null) {
        final ServletResolution resolution = new ServletResolution();
        resolution.handler = servletHandler;
        resolution.handlerRegistry = this.handlerRegistry;
        // TODO - what is the path of a named servlet?
        final RequestInfo requestInfo = new RequestInfo("", null, null, null);
        dispatcher = new RequestDispatcherImpl(resolution, requestInfo);
    } else {
        dispatcher = null;
    }
    return dispatcher;
}
Also used : ServletResolution(org.apache.felix.http.base.internal.registry.ServletResolution) ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) RequestInfo(org.apache.felix.http.base.internal.dispatch.RequestInfo) RequestDispatcher(javax.servlet.RequestDispatcher) RequestDispatcherImpl(org.apache.felix.http.base.internal.dispatch.RequestDispatcherImpl)

Example 5 with ServletHandler

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

the class SharedHttpServiceImpl method registerServlet.

/**
 * Register a servlet
 */
public void registerServlet(@Nonnull final String alias, @Nonnull final ExtServletContext httpContext, @Nonnull final Servlet servlet, @Nonnull final ServletInfo servletInfo) throws ServletException, NamespaceException {
    final ServletHandler handler = new HttpServiceServletHandler(httpContext, servletInfo, servlet);
    synchronized (this.aliasMap) {
        if (this.aliasMap.containsKey(alias)) {
            throw new NamespaceException("Alias " + alias + " is already in use.");
        }
        this.handlerRegistry.getRegistry(handler.getContextServiceId()).registerServlet(handler);
        this.aliasMap.put(alias, handler);
    }
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) NamespaceException(org.osgi.service.http.NamespaceException)

Aggregations

ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)22 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)12 Test (org.junit.Test)9 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)7 ServletContextDTO (org.osgi.service.http.runtime.dto.ServletContextDTO)7 Servlet (javax.servlet.Servlet)6 ServletConfig (javax.servlet.ServletConfig)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)3 FileNotFoundException (java.io.FileNotFoundException)2 RequestDispatcher (javax.servlet.RequestDispatcher)2 ServletException (javax.servlet.ServletException)2 RequestDispatcherImpl (org.apache.felix.http.base.internal.dispatch.RequestDispatcherImpl)2 RequestInfo (org.apache.felix.http.base.internal.dispatch.RequestInfo)2 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)2 ServletResolution (org.apache.felix.http.base.internal.registry.ServletResolution)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1