Search in sources :

Example 66 with Valve

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

the class StandardEngineSF method storeChildren.

/**
 * Store the specified Engine properties.
 *
 * @param aWriter
 *            PrintWriter to which we are storing
 * @param indent
 *            Number of spaces to indent this element
 * @param aEngine
 *            Object whose properties are being stored
 *
 * @exception Exception
 *                if an exception occurs while storing
 */
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aEngine, StoreDescription parentDesc) throws Exception {
    if (aEngine instanceof StandardEngine) {
        StandardEngine engine = (StandardEngine) aEngine;
        // Store nested <Listener> elements
        LifecycleListener[] listeners = engine.findLifecycleListeners();
        storeElementArray(aWriter, indent, listeners);
        // Store nested <Realm> element
        Realm realm = engine.getRealm();
        Realm parentRealm = null;
        // TODO is this case possible? (see it a old Server 5.0 impl)
        if (engine.getParent() != null) {
            parentRealm = engine.getParent().getRealm();
        }
        if (realm != parentRealm) {
            storeElement(aWriter, indent, realm);
        }
        // Store nested <Valve> elements
        Valve[] valves = engine.getPipeline().getValves();
        if (valves != null && valves.length > 0) {
            List<Valve> engineValves = new ArrayList<>();
            for (Valve valve : valves) {
                if (!(valve instanceof ClusterValve)) {
                    engineValves.add(valve);
                }
            }
            storeElementArray(aWriter, indent, engineValves.toArray());
        }
        // store all <Cluster> elements
        Cluster cluster = engine.getCluster();
        if (cluster != null) {
            storeElement(aWriter, indent, cluster);
        }
        // store all <Host> elements
        Container[] children = engine.findChildren();
        storeElementArray(aWriter, indent, children);
    }
}
Also used : Container(org.apache.catalina.Container) StandardEngine(org.apache.catalina.core.StandardEngine) ArrayList(java.util.ArrayList) ClusterValve(org.apache.catalina.ha.ClusterValve) Valve(org.apache.catalina.Valve) Cluster(org.apache.catalina.Cluster) LifecycleListener(org.apache.catalina.LifecycleListener) Realm(org.apache.catalina.Realm) ClusterValve(org.apache.catalina.ha.ClusterValve)

Example 67 with Valve

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

the class AsyncContextImpl method setErrorState.

public void setErrorState(Throwable t, boolean fireOnError) {
    if (t != null) {
        request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
    }
    request.getCoyoteRequest().action(ActionCode.ASYNC_ERROR, null);
    if (fireOnError) {
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("asyncContextImpl.fireOnError"));
        }
        AsyncEvent errorEvent = new AsyncEvent(event.getAsyncContext(), event.getSuppliedRequest(), event.getSuppliedResponse(), t);
        List<AsyncListenerWrapper> listenersCopy = new ArrayList<>(listeners);
        for (AsyncListenerWrapper listener : listenersCopy) {
            try {
                listener.fireOnError(errorEvent);
            } catch (Throwable t2) {
                ExceptionUtils.handleThrowable(t2);
                log.warn(sm.getString("asyncContextImpl.onErrorError", listener.getClass().getName()), t2);
            }
        }
    }
    AtomicBoolean result = new AtomicBoolean();
    request.getCoyoteRequest().action(ActionCode.ASYNC_IS_ERROR, result);
    if (result.get()) {
        // No listener called dispatch() or complete(). This is an error.
        // SRV.2.3.3.3 (search for "error dispatch")
        // Take a local copy to avoid threading issues if another thread
        // clears this (can happen during error handling with non-container
        // threads)
        ServletResponse servletResponse = this.servletResponse;
        if (servletResponse instanceof HttpServletResponse) {
            ((HttpServletResponse) servletResponse).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        Host host = (Host) context.getParent();
        Valve stdHostValve = host.getPipeline().getBasic();
        if (stdHostValve instanceof StandardHostValve) {
            ((StandardHostValve) stdHostValve).throwable(request, request.getResponse(), t);
        }
        request.getCoyoteRequest().action(ActionCode.ASYNC_IS_ERROR, result);
        if (result.get()) {
            // Still in the error state. The error page did not call
            // complete() or dispatch(). Complete the async processing.
            complete();
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ArrayList(java.util.ArrayList) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Valve(org.apache.catalina.Valve) Host(org.apache.catalina.Host) AsyncEvent(jakarta.servlet.AsyncEvent)

Example 68 with Valve

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

the class ContainerMBean method removeValve.

/**
 * Remove an existing Valve.
 *
 * @param valveName MBean Name of the Valve to remove
 *
 * @exception MBeanException if a component cannot be removed
 */
public void removeValve(String valveName) throws MBeanException {
    Container container = doGetManagedResource();
    ObjectName oname;
    try {
        oname = new ObjectName(valveName);
    } catch (MalformedObjectNameException | NullPointerException e) {
        throw new MBeanException(e);
    }
    if (container != null) {
        Valve[] valves = container.getPipeline().getValves();
        for (Valve valve : valves) {
            if (valve instanceof JmxEnabled) {
                ObjectName voname = ((JmxEnabled) valve).getObjectName();
                if (voname.equals(oname)) {
                    container.getPipeline().removeValve(valve);
                }
            }
        }
    }
}
Also used : Container(org.apache.catalina.Container) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanException(javax.management.MBeanException) Valve(org.apache.catalina.Valve) ObjectName(javax.management.ObjectName) JmxEnabled(org.apache.catalina.JmxEnabled)

Example 69 with Valve

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

the class OpenWebBeansContextLifecycleListener method lifecycleEvent.

@Override
public void lifecycleEvent(LifecycleEvent event) {
    if (event.getSource() instanceof Context) {
        Context context = (Context) event.getSource();
        if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
            if (getStartWithoutBeansXml() || context.getResources().getResource("/WEB-INF/beans.xml").exists() || context.getResources().getResource("/WEB-INF/classes/META-INF/beans.xml").exists()) {
                // Registering ELResolver with JSP container
                System.setProperty("org.apache.webbeans.application.jsp", "true");
                // Add Listeners
                String[] oldListeners = context.findApplicationListeners();
                LinkedList<String> listeners = new LinkedList<>();
                listeners.addFirst(WebBeansConfigurationListener.class.getName());
                for (String listener : oldListeners) {
                    listeners.add(listener);
                    context.removeApplicationListener(listener);
                }
                for (String listener : listeners) {
                    context.addApplicationListener(listener);
                }
                Pipeline pipeline = context.getPipeline();
                // Add to the corresponding pipeline to get a notification once configure is done
                if (pipeline instanceof Lifecycle) {
                    boolean contextLifecycleListenerFound = false;
                    for (LifecycleListener listener : ((Lifecycle) pipeline).findLifecycleListeners()) {
                        if (listener instanceof OpenWebBeansContextLifecycleListener) {
                            contextLifecycleListenerFound = true;
                        }
                    }
                    if (!contextLifecycleListenerFound) {
                        ((Lifecycle) pipeline).addLifecycleListener(this);
                    }
                }
                if (getAddSecurityValve()) {
                    // Add security valve
                    boolean securityValveFound = false;
                    for (Valve valve : pipeline.getValves()) {
                        if (valve instanceof OpenWebBeansSecurityValve) {
                            securityValveFound = true;
                        }
                    }
                    if (!securityValveFound) {
                        pipeline.addValve(new OpenWebBeansSecurityValve());
                    }
                }
            }
        }
    } else if (event.getSource() instanceof Pipeline && event.getType().equals(Lifecycle.START_EVENT)) {
        // This notification occurs once the configuration is fully done, including naming resources setup
        // Otherwise, the instance manager is not ready for creation
        Pipeline pipeline = (Pipeline) event.getSource();
        if (pipeline.getContainer() instanceof Context) {
            Context context = (Context) pipeline.getContainer();
            if (!(context.getInstanceManager() instanceof OpenWebBeansInstanceManager)) {
                InstanceManager processor = context.getInstanceManager();
                if (processor == null) {
                    processor = context.createInstanceManager();
                }
                InstanceManager custom = new OpenWebBeansInstanceManager(context.getLoader().getClassLoader(), processor);
                context.setInstanceManager(custom);
            }
        }
    }
}
Also used : Context(org.apache.catalina.Context) InstanceManager(org.apache.tomcat.InstanceManager) Lifecycle(org.apache.catalina.Lifecycle) WebBeansConfigurationListener(org.apache.webbeans.servlet.WebBeansConfigurationListener) LifecycleListener(org.apache.catalina.LifecycleListener) LinkedList(java.util.LinkedList) Pipeline(org.apache.catalina.Pipeline) Valve(org.apache.catalina.Valve)

Example 70 with Valve

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

the class TestCrawlerSessionManagerValve method testCrawlerIpsNegative.

@Test
public void testCrawlerIpsNegative() throws Exception {
    CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
    valve.setCrawlerIps("216\\.58\\.206\\.174");
    valve.setCrawlerUserAgents(valve.getCrawlerUserAgents());
    valve.setNext(EasyMock.createMock(Valve.class));
    HttpSession session = createSessionExpectations(valve, false);
    Request request = createRequestExpectations("127.0.0.1", session, false);
    EasyMock.replay(request, session);
    valve.invoke(request, EasyMock.createMock(Response.class));
    EasyMock.verify(request, session);
}
Also used : Response(org.apache.catalina.connector.Response) HttpSession(jakarta.servlet.http.HttpSession) Request(org.apache.catalina.connector.Request) Valve(org.apache.catalina.Valve) Test(org.junit.Test)

Aggregations

Valve (org.apache.catalina.Valve)72 ArrayList (java.util.ArrayList)15 Lifecycle (org.apache.catalina.Lifecycle)14 Container (org.apache.catalina.Container)13 LifecycleException (org.apache.catalina.LifecycleException)13 Pipeline (org.apache.catalina.Pipeline)11 ObjectName (javax.management.ObjectName)9 Realm (org.apache.catalina.Realm)8 AccessLogValve (org.apache.catalina.valves.AccessLogValve)8 Contained (org.apache.catalina.Contained)7 LifecycleListener (org.apache.catalina.LifecycleListener)7 Request (org.apache.catalina.connector.Request)7 ClusterValve (org.apache.catalina.ha.ClusterValve)7 RemoteIpValve (org.apache.catalina.valves.RemoteIpValve)7 Test (org.junit.Test)7 Context (org.apache.catalina.Context)6 JmxEnabled (org.apache.catalina.JmxEnabled)6 Response (org.apache.catalina.connector.Response)6 IOException (java.io.IOException)5 ContainerBase (org.apache.catalina.core.ContainerBase)5