001    /*
002     * Copyright 2006 Stephen McConnell.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package net.dpml.http;
017    
018    import java.io.File;
019    
020    import net.dpml.util.PropertyResolver;
021    
022    import org.mortbay.jetty.handler.RequestLogHandler;
023    import org.mortbay.jetty.NCSARequestLog;
024    
025    /** 
026     * Wrapper for the Jetty NCSA request logger.
027     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
028     * @version 0.2.0
029     */
030    public class NCSARequestLogHandler extends RequestLogHandler
031    {
032       /**
033        * Component context.
034        */
035        public interface Context
036        {
037           /**
038            * Get the array of ignore paths.
039            * @param value the default value
040            * @return the ignore path array
041            */
042            String[] getIgnorePaths( String[] value );
043            
044           /**
045            * Return the append policy.
046            * @param value the default policy value
047            * @return the resolved value
048            */
049            boolean getAppend( boolean value );
050            
051           /**
052            * Return the extended policy.
053            * @param value the default policy value
054            * @return the resolved value
055            */
056            boolean getExtended( boolean value );
057            
058           /**
059            * Return the prefer-proxy-for-address policy.
060            * @param value the default policy value
061            * @return the resolved value
062            */
063            boolean getPreferProxiedForAddress( boolean value );
064            
065           /**
066            * Return the log filename.
067            * @param value the default filename value (may include symbolic
068            *   references to system properties)
069            * @return the resolved filename
070            */
071            String getFilename( String value );
072            
073           /**
074            * Return the log date format.
075            * @param value the default value
076            * @return the resolved value
077            */
078            String getLogDateFormat( String value );
079            
080           /**
081            * Return the log time zone.
082            * @param value the default value
083            * @return the resolved value
084            */
085            String getLogTimeZone( String value );
086            
087           /**
088            * Return the retain days value.
089            * @param value the default value
090            * @return the resolved value
091            */
092            int getRetainDays( int value );
093            
094           /**
095            * Get the log latency policy. Ig true the request processing latency will
096            * included in the reqwuest log messages.
097            * @param flag the log latency default value
098            * @return the resulted log latency policy
099            */
100            boolean getLogLatency( boolean flag );
101            
102           /**
103            * Get the log cookies policy.
104            * @param flag the default policy
105            * @return the resolved policy
106            */
107            boolean getLogCookies( boolean flag );
108        }
109        
110        private final NCSARequestLog m_ncsa;
111    
112       /**
113        * Creation of a new NCSA request log.
114        * @param context the deployment context
115        */
116        public NCSARequestLogHandler( Context context )
117        {
118            m_ncsa = new NCSARequestLog();
119            
120            boolean append = context.getAppend( false );
121            m_ncsa.setAppend( append );
122            
123            boolean extended = context.getExtended( false );
124            m_ncsa.setExtended( extended );
125            
126            boolean preferProxiedFor = context.getPreferProxiedForAddress( false );
127            m_ncsa.setPreferProxiedForAddress( preferProxiedFor );
128            
129            String filename = context.getFilename( null );
130            if( filename != null )
131            {
132                filename = PropertyResolver.resolve( System.getProperties(), filename );
133                File file = new File( filename );
134                File parent = file.getParentFile();
135                if( null != parent )
136                {
137                    parent.mkdirs();
138                }
139                m_ncsa.setFilename( filename );
140            }
141            
142            String dateformat = context.getLogDateFormat( null );
143            if( dateformat != null )
144            {
145                m_ncsa.setLogDateFormat( dateformat );
146            }
147            
148            String[] ignorepaths = context.getIgnorePaths( null );
149            if( ignorepaths != null )
150            {
151                m_ncsa.setIgnorePaths( ignorepaths );
152            }
153            
154            String timezone = context.getLogTimeZone( null );
155            if( timezone != null )
156            {
157                m_ncsa.setLogTimeZone( timezone );
158            }
159            
160            int retain = context.getRetainDays( -1 );
161            if( retain > 0 )
162            {
163                m_ncsa.setRetainDays( retain );
164            }
165            
166            boolean recordLatencyPolicy = context.getLogLatency( false );
167            m_ncsa.setLogLatency( recordLatencyPolicy );
168            
169            boolean cookiesPolicy = context.getLogCookies( false );
170            m_ncsa.setLogCookies( cookiesPolicy );
171            
172            setRequestLog( m_ncsa );
173        }
174    }