org.apache.catalina.Valve Java Examples

The following examples show how to use org.apache.catalina.Valve. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MBeanFactory.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new Valve and associate it with a {@link Container}.
 *
 * @param className The fully qualified class name of the {@link Valve} to
 *                  create
 * @param parent    The MBean name of the associated parent
 *                  {@link Container}.
 *
 * @return  The MBean name of the {@link Valve} that was created or
 *          <code>null</code> if the {@link Valve} does not implement
 *          {@link LifecycleMBeanBase}.
 */
public String createValve(String className, String parent)
        throws Exception {

    // Look for the parent
    ObjectName parentName = new ObjectName(parent);
    Container container = getParentContainerFromParent(parentName);

    if (container == null) {
        // TODO
        throw new IllegalArgumentException();
    }

    Valve valve = (Valve) Class.forName(className).newInstance();

    container.getPipeline().addValve(valve);

    if (valve instanceof LifecycleMBeanBase) {
        return ((LifecycleMBeanBase) valve).getObjectName().toString();
    } else {
        return null;
    }
}
 
Example #2
Source File: ContainerBase.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public AccessLog getAccessLog() {
    
    if (accessLogScanComplete) {
        return accessLog;
    }

    AccessLogAdapter adapter = null;
    Valve valves[] = getPipeline().getValves();
    for (Valve valve : valves) {
        if (valve instanceof AccessLog) {
            if (adapter == null) {
                adapter = new AccessLogAdapter((AccessLog) valve);
            } else {
                adapter.add((AccessLog) valve);
            }
        }
    }
    if (adapter != null) {
        accessLog = adapter;
    }
    accessLogScanComplete = true;
    return accessLog;
}
 
Example #3
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
public ObjectName[] getValveObjectNames() {

        ArrayList<ObjectName> valveList = new ArrayList<ObjectName>();
        Valve current = first;
        if (current == null) {
            current = basic;
        }
        while (current != null) {
            if (current instanceof ValveBase) {
                valveList.add(((ValveBase) current).getObjectName());
            }
            current = current.getNext();
        }

        return valveList.toArray(new ObjectName[0]);

    }
 
Example #4
Source File: RedisStore.java    From session-managers with Apache License 2.0 6 votes vote down vote up
@Override
protected void initInternal() {
    this.lockTemplate.withReadLock(new LockTemplate.LockedOperation<Void>() {

        @Override
        public Void invoke() {
            for (Valve valve : RedisStore.this.manager.getContext().getPipeline().getValves()) {
                if (valve instanceof SessionFlushValve) {
                    RedisStore.this.logger.debug("Setting '{}' as the store for '{}'", this, valve);
                    ((SessionFlushValve) valve).setStore(RedisStore.this);
                }
            }

            return null;
        }

    });
}
 
Example #5
Source File: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Create a new Valve and associate it with a {@link Container}.
 *
 * @param className The fully qualified class name of the {@link Valve} to
 *                  create
 * @param parent    The MBean name of the associated parent
 *                  {@link Container}.
 *
 * @return  The MBean name of the {@link Valve} that was created or
 *          <code>null</code> if the {@link Valve} does not implement
 *          {@link JmxEnabled}.
 * @exception Exception if an MBean cannot be created or registered
 */
public String createValve(String className, String parent)
        throws Exception {

    // Look for the parent
    ObjectName parentName = new ObjectName(parent);
    Container container = getParentContainerFromParent(parentName);

    if (container == null) {
        // TODO
        throw new IllegalArgumentException();
    }

    Valve valve = (Valve) Class.forName(className).getConstructor().newInstance();

    container.getPipeline().addValve(valve);

    if (valve instanceof JmxEnabled) {
        return ((JmxEnabled) valve).getObjectName().toString();
    } else {
        return null;
    }
}
 
Example #6
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Return the set of Valves in the pipeline associated with this
 * Container, including the basic Valve (if any).  If there are no
 * such Valves, a zero-length array is returned.
 */
@Override
public Valve[] getValves() {

    ArrayList<Valve> valveList = new ArrayList<Valve>();
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        valveList.add(current);
        current = current.getNext();
    }

    return valveList.toArray(new Valve[0]);

}
 
Example #7
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Stop {@link Valve}s) in this pipeline and implement the requirements
 * of {@link LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void stopInternal() throws LifecycleException {

    setState(LifecycleState.STOPPING);

    // Stop the Valves in our pipeline (including the basic), if any
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        if (current instanceof Lifecycle)
            ((Lifecycle) current).stop();
        current = current.getNext();
    }
}
 
Example #8
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Start {@link Valve}s) in this pipeline and implement the requirements
 * of {@link LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void startInternal() throws LifecycleException {

    // Start the Valves in our pipeline (including the basic), if any
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        if (current instanceof Lifecycle)
            ((Lifecycle) current).start();
        current = current.getNext();
    }

    setState(LifecycleState.STARTING);
}
 
Example #9
Source File: MBeanUtils.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Deregister the MBean for this
 * <code>Valve</code> object.
 *
 * @param valve The Valve to be managed
 *
 * @exception Exception if an MBean cannot be deregistered
 * @deprecated  Unused. Will be removed in Tomcat 8.0.x
 */
@Deprecated
static void destroyMBean(Valve valve, Container container)
    throws Exception {

    ((Contained)valve).setContainer(container);
    String mname = createManagedName(valve);
    ManagedBean managed = registry.findManagedBean(mname);
    if (managed == null) {
        return;
    }
    String domain = managed.getDomain();
    if (domain == null)
        domain = mserver.getDefaultDomain();
    ObjectName oname = createObjectName(domain, valve);
    try {
        ((Contained)valve).setContainer(null);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
    }
    if( mserver.isRegistered(oname) ) {
        mserver.unregisterMBean(oname);
    }

}
 
Example #10
Source File: MBeanFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new Valve and associate it with a {@link Container}.
 *
 * @param className The fully qualified class name of the {@link Valve} to
 *                  create
 * @param parent    The MBean name of the associated parent
 *                  {@link Container}.
 *
 * @return  The MBean name of the {@link Valve} that was created or
 *          <code>null</code> if the {@link Valve} does not implement
 *          {@link LifecycleMBeanBase}.
 */
public String createValve(String className, String parent)
        throws Exception {

    // Look for the parent
    ObjectName parentName = new ObjectName(parent);
    Container container = getParentContainerFromParent(parentName);

    if (container == null) {
        // TODO
        throw new IllegalArgumentException();
    }

    Valve valve = (Valve) Class.forName(className).newInstance();

    container.getPipeline().addValve(valve);

    if (valve instanceof LifecycleMBeanBase) {
        return ((LifecycleMBeanBase) valve).getObjectName().toString();
    } else {
        return null;
    }
}
 
Example #11
Source File: TestCrawlerSessionManagerValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCrawlersSessionIdIsRemovedAfterSessionExpiry() throws IOException, ServletException {
    CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
    valve.setCrawlerIps("216\\.58\\.206\\.174");
    valve.setCrawlerUserAgents(valve.getCrawlerUserAgents());
    valve.setNext(EasyMock.createMock(Valve.class));
    valve.setSessionInactiveInterval(0);
    StandardSession session = new StandardSession(TEST_MANAGER);
    session.setId("id");
    session.setValid(true);

    Request request = createRequestExpectations("216.58.206.174", session, true);

    EasyMock.replay(request);

    valve.invoke(request, EasyMock.createMock(Response.class));

    EasyMock.verify(request);

    MatcherAssert.assertThat(valve.getClientIpSessionId().values(), CoreMatchers.hasItem("id"));

    session.expire();

    Assert.assertEquals(0, valve.getClientIpSessionId().values().size());
}
 
Example #12
Source File: StandardPipeline.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Stop {@link Valve}s) in this pipeline and implement the requirements
 * of {@link LifecycleBase#stopInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void stopInternal() throws LifecycleException {

    setState(LifecycleState.STOPPING);

    // Stop the Valves in our pipeline (including the basic), if any
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        if (current instanceof Lifecycle)
            ((Lifecycle) current).stop();
        current = current.getNext();
    }
}
 
Example #13
Source File: SimpleTomEETcpCluster.java    From tomee with Apache License 2.0 6 votes vote down vote up
public SimpleTomEETcpCluster(final SimpleTcpCluster from) {
    clusterListeners.addAll(Arrays.asList(from.findClusterListeners()));

    setClusterName(from.getClusterName());
    setContainer(from.getContainer());
    setNotifyLifecycleListenerOnFailure(from.isNotifyLifecycleListenerOnFailure());

    setChannelSendOptions(from.getChannelSendOptions());
    setChannelStartOptions(from.getChannelStartOptions());
    setHeartbeatBackgroundEnabled(from.isHeartbeatBackgroundEnabled());
    setChannel(from.getChannel());
    getManagers().putAll(from.getManagers());
    setManagerTemplate(from.getManagerTemplate());
    setClusterDeployer(from.getClusterDeployer());

    for (final Valve valve : from.getValves()) {
        addValve(valve);
    }
}
 
Example #14
Source File: StandardPipeline.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Return the set of Valves in the pipeline associated with this
 * Container, including the basic Valve (if any).  If there are no
 * such Valves, a zero-length array is returned.
 */
@Override
public Valve[] getValves() {

    List<Valve> valveList = new ArrayList<>();
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        valveList.add(current);
        current = current.getNext();
    }

    return valveList.toArray(new Valve[0]);

}
 
Example #15
Source File: MBeanUtils.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Deregister the MBean for this
 * <code>Valve</code> object.
 *
 * @param valve The Valve to be managed
 *
 * @exception Exception if an MBean cannot be deregistered
 * @deprecated  Unused. Will be removed in Tomcat 8.0.x
 */
@Deprecated
static void destroyMBean(Valve valve, Container container)
    throws Exception {

    ((Contained)valve).setContainer(container);
    String mname = createManagedName(valve);
    ManagedBean managed = registry.findManagedBean(mname);
    if (managed == null) {
        return;
    }
    String domain = managed.getDomain();
    if (domain == null)
        domain = mserver.getDefaultDomain();
    ObjectName oname = createObjectName(domain, valve);
    try {
        ((Contained)valve).setContainer(null);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
    }
    if( mserver.isRegistered(oname) ) {
        mserver.unregisterMBean(oname);
    }

}
 
Example #16
Source File: ContainerBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public AccessLog getAccessLog() {

    if (accessLogScanComplete) {
        return accessLog;
    }

    AccessLogAdapter adapter = null;
    Valve valves[] = getPipeline().getValves();
    for (Valve valve : valves) {
        if (valve instanceof AccessLog) {
            if (adapter == null) {
                adapter = new AccessLogAdapter((AccessLog) valve);
            } else {
                adapter.add((AccessLog) valve);
            }
        }
    }
    if (adapter != null) {
        accessLog = adapter;
    }
    accessLogScanComplete = true;
    return accessLog;
}
 
Example #17
Source File: TestCrawlerSessionManagerValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testCrawlerMultipleContextsContextAware() throws Exception {
    CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
    valve.setCrawlerUserAgents(valve.getCrawlerUserAgents());
    valve.setHostAware(true);
    valve.setContextAware(true);
    valve.setNext(EasyMock.createMock(Valve.class));

    verifyCrawlingContext(valve, "/examples");
    verifyCrawlingContext(valve, null);
}
 
Example #18
Source File: StandardHost.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
  * Return the MBean Names of the Valves associated with this Host
  *
  * @exception Exception if an MBean cannot be created or registered
  */
 public String [] getValveNames()
     throws Exception
{
     Valve [] valves = this.getPipeline().getValves();
     String [] mbeanNames = new String[valves.length];
     for (int i = 0; i < valves.length; i++) {
         if( valves[i] == null ) continue;
         if( ((ValveBase)valves[i]).getObjectName() == null ) continue;
         mbeanNames[i] = ((ValveBase)valves[i]).getObjectName().toString();
     }

     return mbeanNames;

 }
 
Example #19
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Add a new Valve to the end of the pipeline associated with this
 * Container.  Prior to adding the Valve, the Valve's
 * <code>setContainer()</code> method will be called, if it implements
 * <code>Contained</code>, with the owning Container as an argument.
 * The method may throw an
 * <code>IllegalArgumentException</code> if this Valve chooses not to
 * be associated with this Container, or <code>IllegalStateException</code>
 * if it is already associated with a different Container.</p>
 *
 * @param valve Valve to be added
 *
 * @exception IllegalArgumentException if this Container refused to
 *  accept the specified Valve
 * @exception IllegalArgumentException if the specified Valve refuses to be
 *  associated with this Container
 * @exception IllegalStateException if the specified Valve is already
 *  associated with a different Container
 */
@Override
public void addValve(Valve valve) {

    // Validate that we can add this Valve
    if (valve instanceof Contained)
        ((Contained) valve).setContainer(this.container);

    // Start the new component if necessary
    if (getState().isAvailable()) {
        if (valve instanceof Lifecycle) {
            try {
                ((Lifecycle) valve).start();
            } catch (LifecycleException e) {
                log.error("StandardPipeline.addValve: start: ", e);
            }
        }
    }

    // Add this Valve to the set associated with this Pipeline
    if (first == null) {
        first = valve;
        valve.setNext(basic);
    } else {
        Valve current = first;
        while (current != null) {
            if (current.getNext() == basic) {
                current.setNext(valve);
                valve.setNext(basic);
                break;
            }
            current = current.getNext();
        }
    }
    
    container.fireContainerEvent(Container.ADD_VALVE_EVENT, valve);
}
 
Example #20
Source File: Embedded.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void addAuthenticator(Authenticator authenticator,
                             String loginMethod) {
    if (!(authenticator instanceof Valve)) {
        throw new IllegalArgumentException(
            sm.getString("embedded.authenticatorNotInstanceOfValve"));
    }
    if (authenticators == null) {
        synchronized (this) {
            if (authenticators == null) {
                authenticators = new HashMap<String,Authenticator>();
            }
        }
    }
    authenticators.put(loginMethod, authenticator);
}
 
Example #21
Source File: Embedded.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void addAuthenticator(Authenticator authenticator,
                             String loginMethod) {
    if (!(authenticator instanceof Valve)) {
        throw new IllegalArgumentException(
            sm.getString("embedded.authenticatorNotInstanceOfValve"));
    }
    if (authenticators == null) {
        synchronized (this) {
            if (authenticators == null) {
                authenticators = new HashMap<String,Authenticator>();
            }
        }
    }
    authenticators.put(loginMethod, authenticator);
}
 
Example #22
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void destroyInternal() {
    Valve[] valves = getValves();
    for (Valve valve : valves) {
        removeValve(valve);
    }
}
 
Example #23
Source File: SimpleTcpCluster.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * unregister all cluster valve to host or engine
 */
protected void unregisterClusterValve() {
    for (Iterator<Valve> iter = valves.iterator(); iter.hasNext();) {
        ClusterValve valve = (ClusterValve) iter.next();
        if (log.isDebugEnabled())
            log.debug("Invoking removeValve on " + getContainer()
                    + " with class=" + valve.getClass().getName());
        if (valve != null) {
            container.getPipeline().removeValve(valve);
            valve.setCluster(null);
        }
    }
}
 
Example #24
Source File: TestCrawlerSessionManagerValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testCrawlerMultipleHostsHostAware() throws Exception {
    CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
    valve.setCrawlerUserAgents(valve.getCrawlerUserAgents());
    valve.setHostAware(true);
    valve.setContextAware(true);
    valve.setNext(EasyMock.createMock(Valve.class));

    verifyCrawlingLocalhost(valve, "localhost");
    verifyCrawlingLocalhost(valve, "example.invalid");
}
 
Example #25
Source File: TestFormAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private FormAuthClient(boolean clientShouldUseCookies,
        boolean serverShouldUseCookies,
        boolean serverShouldChangeSessid) throws Exception {

    Tomcat tomcat = getTomcatInstance();
    File appDir = new File(getBuildDirectory(), "webapps/examples");
    Context ctx = tomcat.addWebapp(null, "/examples",
            appDir.getAbsolutePath());
    setUseCookies(clientShouldUseCookies);
    ctx.setCookies(serverShouldUseCookies);

    MapRealm realm = new MapRealm();
    realm.addUser("tomcat", "tomcat");
    realm.addUserRole("tomcat", "tomcat");
    ctx.setRealm(realm);

    tomcat.start();

    // perhaps this does not work until tomcat has started?
    ctx.setSessionTimeout(TIMEOUT_MINS);

    // Valve pipeline is only established after tomcat starts
    Valve[] valves = ctx.getPipeline().getValves();
    for (Valve valve : valves) {
        if (valve instanceof AuthenticatorBase) {
            ((AuthenticatorBase)valve)
                    .setChangeSessionIdOnAuthentication(
                                        serverShouldChangeSessid);
            break;
        }
    }

    // Port only known after Tomcat starts
    setPort(getPort());
}
 
Example #26
Source File: TestCrawlerSessionManagerValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@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);
}
 
Example #27
Source File: TestCrawlerSessionManagerValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testCrawlerIpsPositive() 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, true);
    Request request = createRequestExpectations("216.58.206.174", session, true);

    EasyMock.replay(request, session);

    valve.invoke(request, EasyMock.createMock(Response.class));

    EasyMock.verify(request, session);
}
 
Example #28
Source File: SimpleTcpCluster.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * register all cluster valve to host or engine
 */
protected void registerClusterValve() {
    if(container != null ) {
        for (Iterator<Valve> iter = valves.iterator(); iter.hasNext();) {
            ClusterValve valve = (ClusterValve) iter.next();
            if (log.isDebugEnabled())
                log.debug("Invoking addValve on " + getContainer()
                        + " with class=" + valve.getClass().getName());
            if (valve != null) {
                container.getPipeline().addValve(valve);
                valve.setCluster(this);
            }
        }
    }
}
 
Example #29
Source File: SimpleTcpCluster.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * unregister all cluster valve to host or engine
 */
protected void unregisterClusterValve() {
    for (Iterator<Valve> iter = valves.iterator(); iter.hasNext();) {
        ClusterValve valve = (ClusterValve) iter.next();
        if (log.isDebugEnabled())
            log.debug("Invoking removeValve on " + getContainer()
                    + " with class=" + valve.getClass().getName());
        if (valve != null) {
            container.getPipeline().removeValve(valve);
            valve.setCluster(null);
        }
    }
}
 
Example #30
Source File: StandardPipeline.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public Valve getFirst() {
    if (first != null) {
        return first;
    }
    
    return basic;
}