Search in sources :

Example 16 with ServletHandler

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

the class ErrorPageRegistry method addErrorHandling.

private void addErrorHandling(final ServletHandler handler, final ErrorRegistrationStatus status, final long code, final String exception) {
    final String key = (exception != null ? exception : String.valueOf(code));
    final List<ServletHandler> newList;
    final List<ServletHandler> list = errorMapping.get(key);
    if (list == null) {
        newList = Collections.singletonList(handler);
    } else {
        newList = new ArrayList<ServletHandler>(list);
        newList.add(handler);
        Collections.sort(newList);
    }
    if (newList.get(0) == handler) {
        // try to activate (and deactivate old handler)
        final int result = handler.init();
        addReason(status, code, exception, result);
        if (result == -1) {
            if (list != null) {
                final ServletHandler old = list.get(0);
                old.destroy();
                errorMapping.put(key, newList);
                ErrorRegistrationStatus oldStatus = null;
                final Iterator<ErrorRegistrationStatus> i = this.status.iterator();
                while (oldStatus == null && i.hasNext()) {
                    final ErrorRegistrationStatus current = i.next();
                    if (current.handler.getServletInfo().equals(old.getServletInfo())) {
                        oldStatus = current;
                    }
                }
                if (oldStatus != null) {
                    removeReason(oldStatus, code, exception, -1);
                    boolean addReason = true;
                    if (exception == null) {
                        if (code >= 400 && code < 500 && oldStatus.usesClientErrorCodes && !status.usesClientErrorCodes) {
                            addReason = false;
                        } else if (code >= 500 && code < 600 && oldStatus.usesServerErrorCodes && !status.usesServerErrorCodes) {
                            addReason = false;
                        }
                    }
                    if (addReason) {
                        addReason(oldStatus, code, exception, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
                    }
                }
            } else {
                errorMapping.put(key, newList);
            }
        }
    } else {
        // failure
        boolean addReason = true;
        if (exception == null) {
            if (code >= 400 && code < 500 && status.usesClientErrorCodes && !hasErrorCode(newList.get(0), CLIENT_ERROR)) {
                addReason = false;
            } else if (code >= 500 && code < 600 && status.usesServerErrorCodes && !hasErrorCode(newList.get(0), SERVER_ERROR)) {
                addReason = false;
            }
        }
        if (addReason) {
            addReason(status, code, exception, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
        }
        errorMapping.put(key, newList);
    }
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler)

Example 17 with ServletHandler

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

the class HandlerRegistry method resolveServletByName.

/**
 * Get the servlet handler for a servlet by name
 * @param contextId The context id
 * @param name The servlet name
 * @return The servlet handler or {@code null}
 */
public ServletResolution resolveServletByName(final long contextId, @Nonnull final String name) {
    final PerContextHandlerRegistry reg = this.getRegistry(contextId);
    if (reg != null) {
        final ServletHandler handler = reg.resolveServletByName(name);
        if (handler != null) {
            final ServletResolution resolution = new ServletResolution();
            resolution.handler = handler;
            resolution.handlerRegistry = reg;
            return resolution;
        }
    }
    return null;
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler)

Example 18 with ServletHandler

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

the class HandlerRegistry method getErrorHandler.

@CheckForNull
public ServletResolution getErrorHandler(@Nonnull final String requestURI, final Long serviceId, final int code, final Throwable exception) {
    final PerContextHandlerRegistry reg;
    if (serviceId == null) {
        // if the context is unknown, we use the first matching one!
        PerContextHandlerRegistry found = null;
        final List<PerContextHandlerRegistry> regs = this.registrations;
        for (final PerContextHandlerRegistry r : regs) {
            final String path = r.isMatching(requestURI);
            if (path != null) {
                found = r;
                break;
            }
        }
        reg = found;
    } else {
        reg = this.getRegistry(serviceId);
    }
    if (reg != null) {
        final ServletHandler handler = reg.getErrorHandler(code, exception);
        if (handler != null) {
            final ServletResolution res = new ServletResolution();
            res.handler = handler;
            res.handlerRegistry = reg;
            return res;
        }
    }
    return null;
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) CheckForNull(javax.annotation.CheckForNull)

Example 19 with ServletHandler

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

the class ServletRegistry method removeFromNameMapping.

private void removeFromNameMapping(final String servletName, final ServletHandler handler) {
    if (!handler.getServletInfo().isResource()) {
        List<ServletHandler> list = this.servletsByName.get(servletName);
        if (list != null) {
            final List<ServletHandler> newList = new ArrayList<ServletHandler>(list);
            final Iterator<ServletHandler> i = newList.iterator();
            while (i.hasNext()) {
                final ServletHandler s = i.next();
                if (s == handler) {
                    i.remove();
                    break;
                }
            }
            if (newList.isEmpty()) {
                this.servletsByName.remove(servletName);
            } else {
                this.servletsByName.put(servletName, newList);
            }
        }
    }
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) ArrayList(java.util.ArrayList)

Example 20 with ServletHandler

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

the class ServletRegistry method removeServlet.

/**
 * Remove a servlet
 * @param info The servlet info
 */
public synchronized void removeServlet(@Nonnull final ServletInfo info, final boolean destroy) {
    if (info.getPatterns() != null) {
        final List<PathResolver> resolvers = new ArrayList<PathResolver>(this.activeResolvers);
        final Map<ServletInfo, RegistrationStatus> newMap = new TreeMap<ServletInfo, ServletRegistry.RegistrationStatus>(this.mapping);
        newMap.remove(info);
        ServletHandler cleanupHandler = null;
        // used for detecting duplicates
        final Set<String> patterns = new HashSet<String>();
        for (final String pattern : info.getPatterns()) {
            if (patterns.contains(pattern)) {
                continue;
            }
            patterns.add(pattern);
            final PathResolver regHandler = this.findResolver(resolvers, pattern);
            if (regHandler != null && regHandler.getServletHandler().getServletInfo().equals(info)) {
                cleanupHandler = regHandler.getServletHandler();
                removeFromNameMapping(cleanupHandler.getName(), cleanupHandler);
                final List<ServletHandler> inactiveList = this.inactiveServletMappings.get(pattern);
                if (inactiveList == null) {
                    resolvers.remove(regHandler);
                } else {
                    boolean done = false;
                    while (!done) {
                        final ServletHandler h = inactiveList.remove(0);
                        boolean activate = h.getServlet() == null;
                        final RegistrationStatus oldStatus = newMap.get(h.getServletInfo());
                        if (oldStatus != null) {
                            final RegistrationStatus newOldStatus = new RegistrationStatus();
                            newOldStatus.handler = oldStatus.handler;
                            newOldStatus.statusToPath = new HashMap<Integer, String[]>(oldStatus.statusToPath);
                            removePattern(newOldStatus, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, pattern);
                            newMap.put(h.getServletInfo(), newOldStatus);
                            done = this.tryToActivate(resolvers, pattern, h, newOldStatus, regHandler);
                            if (done && activate) {
                                this.addToNameMapping(h);
                            }
                        }
                        if (!done) {
                            done = inactiveList.isEmpty();
                        }
                    }
                    if (inactiveList.isEmpty()) {
                        this.inactiveServletMappings.remove(pattern);
                    }
                }
            } else {
                final List<ServletHandler> inactiveList = this.inactiveServletMappings.get(pattern);
                if (inactiveList != null) {
                    final Iterator<ServletHandler> i = inactiveList.iterator();
                    while (i.hasNext()) {
                        final ServletHandler h = i.next();
                        if (h.getServletInfo().equals(info)) {
                            i.remove();
                            cleanupHandler = h;
                            break;
                        }
                    }
                    if (inactiveList.isEmpty()) {
                        this.inactiveServletMappings.remove(pattern);
                    }
                }
            }
        }
        Collections.sort(resolvers);
        this.activeResolvers = resolvers;
        this.mapping = newMap;
        if (cleanupHandler != null) {
            cleanupHandler.dispose();
        }
    } else if (!info.isResource() && info.getName() != null) {
        final Map<ServletInfo, RegistrationStatus> newMap = new TreeMap<ServletInfo, ServletRegistry.RegistrationStatus>(this.mapping);
        final RegistrationStatus status = newMap.remove(info);
        if (status != null) {
            removeFromNameMapping(info.getName(), status.handler);
            this.mapping = newMap;
            status.handler.dispose();
        }
    }
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashSet(java.util.HashSet)

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