001package org.apache.turbine.pipeline;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import java.io.IOException;
025
026import org.apache.turbine.util.TurbineException;
027
028/**
029 * <p>A <b>Valve</b> is a request processing component.  A series of
030 * Valves are generally associated with each other into a Pipeline.
031 * The detailed contract for a Valve is included in the description of
032 * the <code>invoke()</code> method below.</p>
033 *
034 * <b>HISTORICAL NOTE</b>:  The "Valve" name was assigned to this concept
035 * because a valve is what you use in a real world pipeline to control and/or
036 * modify flows through it.
037 *
038 * @author Craig R. McClanahan
039 * @author Gunnar Rjnning
040 * @author Peter Donald
041 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
042 *
043 * @see #invoke(PipelineData, ValveContext)
044 */
045@FunctionalInterface
046public interface Valve
047{
048    /**
049     * <p>Perform request processing as required by this Valve.</p>
050     *
051     * <p>An individual Valve <b>MAY</b> perform the following actions, in
052     * the specified order:</p>
053     * <ul>
054     * <li>Examine and/or modify the properties of the specified Request and
055     *     Response.
056     * <li>Examine the properties of the specified Request, completely generate
057     *     the corresponding Response, and return control to the caller.
058     * <li>Examine the properties of the specified Request and Response, wrap
059     *     either or both of these objects to supplement their functionality,
060     *     and pass them on.
061     * <li>If the corresponding Response was not generated (and control was not
062     *     returned, call the next Valve in the pipeline (if there is one) by
063     *     executing <code>context.invokeNext()</code>.
064     * <li>Examine, but not modify, the properties of the resulting Response
065     *     (which was created by a subsequently invoked Valve via a
066     *     call to <code>context.invokeNext()</code>).
067     * </ul>
068     *
069     * <p>A Valve <b>MUST NOT</b> do any of the following things:</p>
070     * <ul>
071     * <li>Change request properties that have already been used to direct
072     *     the flow of processing control for this request.
073     * <li>Create a completed Response <strong>AND</strong> pass this
074     *     Request and Response on to the next Valve in the pipeline.
075     * <li>Consume bytes from the input stream associated with the Request,
076     *     unless it is completely generating the response, or wrapping the
077     *     request before passing it on.
078     * <li>Modify the HTTP headers included with the Response after the
079     *     <code>invokeNext()</code> method has returned.
080     * <li>Perform any actions on the output stream associated with the
081     *     specified Response after the <code>invokeNext()</code> method has
082     *     returned.
083     * </ul>
084     *
085     * @param pipelineData The run-time information, including the servlet
086     * request and response we are processing.
087     * @param context The valve context used to invoke the next valve
088     *  in the current processing pipeline
089     *
090     * @throws IOException Thrown by a subsequent Valve.
091     * @throws TurbineException Thrown by a subsequent Valve.
092     */
093    void invoke(PipelineData pipelineData, ValveContext context)
094        throws IOException, TurbineException;
095
096    /**
097     * Initialize the valve before using in a pipeline.
098     * @throws Exception if initialization fails
099     */
100    default void initialize() throws Exception
101    {
102        // empty
103    }
104}