001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.tomcat.valve;
018    
019    import java.io.IOException;
020    
021    import javax.servlet.ServletException;
022    
023    import org.apache.catalina.Valve;
024    import org.apache.catalina.connector.Request;
025    import org.apache.catalina.connector.Response;
026    import org.apache.catalina.valves.ValveBase;
027    import org.apache.geronimo.tomcat.interceptor.BeforeAfter;
028    
029    public class GeronimoBeforeAfterValve extends ValveBase{
030        
031        private final BeforeAfter beforeAfter;
032        private final int contextIndexCount;
033    
034        public GeronimoBeforeAfterValve(BeforeAfter beforeAfter, int contextIndexCount) {
035            this.beforeAfter = beforeAfter;
036            this.contextIndexCount = contextIndexCount;
037        }
038    
039        public void invoke(Request request, Response response) throws IOException, ServletException {
040            Object context[] = new Object[contextIndexCount];
041            
042            if (beforeAfter != null){
043                beforeAfter.before(context, request, response, BeforeAfter.EDGE_SERVLET);
044            }
045            
046            // Pass this request on to the next valve in our pipeline
047            getNext().invoke(request, response);
048            
049            if (beforeAfter != null){
050                beforeAfter.after(context, request, response, 0);
051            }
052            
053        }
054    
055    }