Search in sources :

Example 61 with Wrapper

use of org.apache.catalina.Wrapper in project tomcat by apache.

the class StandardContext method loadOnStartup.

/**
     * Load and initialize all servlets marked "load on startup" in the
     * web application deployment descriptor.
     *
     * @param children Array of wrappers for all currently defined
     *  servlets (including those not declared load on startup)
     * @return <code>true</code> if load on startup was considered successful
     */
public boolean loadOnStartup(Container[] children) {
    // Collect "load on startup" servlets that need to be initialized
    TreeMap<Integer, ArrayList<Wrapper>> map = new TreeMap<>();
    for (int i = 0; i < children.length; i++) {
        Wrapper wrapper = (Wrapper) children[i];
        int loadOnStartup = wrapper.getLoadOnStartup();
        if (loadOnStartup < 0)
            continue;
        Integer key = Integer.valueOf(loadOnStartup);
        ArrayList<Wrapper> list = map.get(key);
        if (list == null) {
            list = new ArrayList<>();
            map.put(key, list);
        }
        list.add(wrapper);
    }
    // Load the collected "load on startup" servlets
    for (ArrayList<Wrapper> list : map.values()) {
        for (Wrapper wrapper : list) {
            try {
                wrapper.load();
            } catch (ServletException e) {
                getLogger().error(sm.getString("standardContext.loadOnStartup.loadException", getName(), wrapper.getName()), StandardWrapper.getRootCause(e));
                // unless failCtxIfServletStartFails="true" is specified
                if (getComputedFailCtxIfServletStartFails()) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : ServletException(javax.servlet.ServletException) Wrapper(org.apache.catalina.Wrapper) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 62 with Wrapper

use of org.apache.catalina.Wrapper in project tomcat by apache.

the class StandardContext method createWrapper.

/**
     * Factory method to create and return a new Wrapper instance, of
     * the Java implementation class appropriate for this Context
     * implementation.  The constructor of the instantiated Wrapper
     * will have been called, but no properties will have been set.
     */
@Override
public Wrapper createWrapper() {
    Wrapper wrapper = null;
    if (wrapperClass != null) {
        try {
            wrapper = (Wrapper) wrapperClass.newInstance();
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error("createWrapper", t);
            return (null);
        }
    } else {
        wrapper = new StandardWrapper();
    }
    synchronized (wrapperLifecyclesLock) {
        for (int i = 0; i < wrapperLifecycles.length; i++) {
            try {
                Class<?> clazz = Class.forName(wrapperLifecycles[i]);
                LifecycleListener listener = (LifecycleListener) clazz.newInstance();
                wrapper.addLifecycleListener(listener);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                log.error("createWrapper", t);
                return (null);
            }
        }
    }
    synchronized (wrapperListenersLock) {
        for (int i = 0; i < wrapperListeners.length; i++) {
            try {
                Class<?> clazz = Class.forName(wrapperListeners[i]);
                ContainerListener listener = (ContainerListener) clazz.newInstance();
                wrapper.addContainerListener(listener);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                log.error("createWrapper", t);
                return (null);
            }
        }
    }
    return (wrapper);
}
Also used : Wrapper(org.apache.catalina.Wrapper) ContainerListener(org.apache.catalina.ContainerListener) LifecycleListener(org.apache.catalina.LifecycleListener) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 63 with Wrapper

use of org.apache.catalina.Wrapper in project tomcat by apache.

the class StandardContext method addChild.

/**
     * Add a child Container, only if the proposed child is an implementation
     * of Wrapper.
     *
     * @param child Child container to be added
     *
     * @exception IllegalArgumentException if the proposed container is
     *  not an implementation of Wrapper
     */
@Override
public void addChild(Container child) {
    // Global JspServlet
    Wrapper oldJspServlet = null;
    if (!(child instanceof Wrapper)) {
        throw new IllegalArgumentException(sm.getString("standardContext.notWrapper"));
    }
    boolean isJspServlet = "jsp".equals(child.getName());
    // Allow webapp to override JspServlet inherited from global web.xml.
    if (isJspServlet) {
        oldJspServlet = (Wrapper) findChild("jsp");
        if (oldJspServlet != null) {
            removeChild(oldJspServlet);
        }
    }
    super.addChild(child);
    if (isJspServlet && oldJspServlet != null) {
        /*
             * The webapp-specific JspServlet inherits all the mappings
             * specified in the global web.xml, and may add additional ones.
             */
        String[] jspMappings = oldJspServlet.findMappings();
        for (int i = 0; jspMappings != null && i < jspMappings.length; i++) {
            addServletMappingDecoded(jspMappings[i], child.getName());
        }
    }
}
Also used : Wrapper(org.apache.catalina.Wrapper) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 64 with Wrapper

use of org.apache.catalina.Wrapper in project tomcat by apache.

the class StandardContext method addServletMappingDecoded.

/**
     * Add a new servlet mapping, replacing any existing mapping for
     * the specified pattern.
     *
     * @param pattern URL pattern to be mapped
     * @param name Name of the corresponding servlet to execute
     * @param jspWildCard true if name identifies the JspServlet
     * and pattern contains a wildcard; false otherwise
     *
     * @exception IllegalArgumentException if the specified servlet name
     *  is not known to this Context
     */
@Override
public void addServletMappingDecoded(String pattern, String name, boolean jspWildCard) {
    // Validate the proposed mapping
    if (findChild(name) == null)
        throw new IllegalArgumentException(sm.getString("standardContext.servletMap.name", name));
    String adjustedPattern = adjustURLPattern(pattern);
    if (!validateURLPattern(adjustedPattern))
        throw new IllegalArgumentException(sm.getString("standardContext.servletMap.pattern", adjustedPattern));
    // Add this mapping to our registered set
    synchronized (servletMappingsLock) {
        String name2 = servletMappings.get(adjustedPattern);
        if (name2 != null) {
            // Don't allow more than one servlet on the same pattern
            Wrapper wrapper = (Wrapper) findChild(name2);
            wrapper.removeMapping(adjustedPattern);
        }
        servletMappings.put(adjustedPattern, name);
    }
    Wrapper wrapper = (Wrapper) findChild(name);
    wrapper.addMapping(adjustedPattern);
    fireContainerEvent("addServletMapping", adjustedPattern);
}
Also used : Wrapper(org.apache.catalina.Wrapper)

Example 65 with Wrapper

use of org.apache.catalina.Wrapper in project tomcat by apache.

the class StandardContextValve method invoke.

/**
     * Select the appropriate child Wrapper to process this request,
     * based on the specified request URI.  If no matching Wrapper can
     * be found, return an appropriate HTTP error.
     *
     * @param request Request to be processed
     * @param response Response to be produced
     *
     * @exception IOException if an input/output error occurred
     * @exception ServletException if a servlet error occurred
     */
@Override
public final void invoke(Request request, Response response) throws IOException, ServletException {
    // Disallow any direct access to resources under WEB-INF or META-INF
    MessageBytes requestPathMB = request.getRequestPathMB();
    if ((requestPathMB.startsWithIgnoreCase("/META-INF/", 0)) || (requestPathMB.equalsIgnoreCase("/META-INF")) || (requestPathMB.startsWithIgnoreCase("/WEB-INF/", 0)) || (requestPathMB.equalsIgnoreCase("/WEB-INF"))) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    // Select the Wrapper to be used for this Request
    Wrapper wrapper = request.getWrapper();
    if (wrapper == null || wrapper.isUnavailable()) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    // Acknowledge the request
    try {
        response.sendAcknowledgement();
    } catch (IOException ioe) {
        container.getLogger().error(sm.getString("standardContextValve.acknowledgeException"), ioe);
        request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, ioe);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    if (request.isAsyncSupported()) {
        request.setAsyncSupported(wrapper.getPipeline().isAsyncSupported());
    }
    wrapper.getPipeline().getFirst().invoke(request, response);
}
Also used : Wrapper(org.apache.catalina.Wrapper) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) IOException(java.io.IOException)

Aggregations

Wrapper (org.apache.catalina.Wrapper)109 Context (org.apache.catalina.Context)57 Tomcat (org.apache.catalina.startup.Tomcat)48 AsyncContext (javax.servlet.AsyncContext)33 Test (org.junit.Test)31 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)28 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)24 TesterContext (org.apache.tomcat.unittest.TesterContext)24 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)22 IOException (java.io.IOException)18 StandardWrapper (org.apache.catalina.core.StandardWrapper)16 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)14 File (java.io.File)13 Container (org.apache.catalina.Container)13 ServletException (javax.servlet.ServletException)9 HashMap (java.util.HashMap)7 StandardContext (org.apache.catalina.core.StandardContext)7 Servlet (javax.servlet.Servlet)6 ArrayList (java.util.ArrayList)5