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.net.URI;
019    import java.util.Map;
020    import java.security.PermissionCollection;
021    import java.io.File;
022    
023    import net.dpml.logging.Logger;
024    
025    import org.mortbay.jetty.security.SecurityHandler;
026    import org.mortbay.jetty.servlet.SessionHandler;
027    import org.mortbay.jetty.servlet.ServletHandler;
028    
029    /**
030     * Servlet context handler. 
031     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
032     * @version 0.2.0
033     */
034    public class WebAppContextHandler extends org.mortbay.jetty.webapp.WebAppContext
035    {
036       /**
037        * HTTP static resource vontext handler parameters.
038        */
039        public interface Context extends ContextHandlerContext
040        {
041           /**
042            * Get the war artifact uri.
043            * @return the uri identifying the war artifact
044            */
045            URI getWar();
046            
047           /**
048            * Get the assigned temp directory.
049            * @param dir the default temp directory
050            * @return the resolved temp directory
051            */
052            File getTempDirectory( File dir );
053            
054           /**
055            * Get the resource alias map.
056            * @param map the default mapping
057            * @return the resolved map
058            */
059            Map getResourceAliases( Map map );
060            
061           /**
062            * Get the war extraction policy.
063            * @param policy the default policy (true)
064            * @return the resolved policy
065            */
066            boolean getExtractionPolicy( boolean policy );
067            
068           /**
069            * Get the assigned permission collection.
070            * @param permissions the default permissions value
071            * @return the resolved permissions collection
072            */
073            PermissionCollection getPermissions( PermissionCollection permissions );
074            
075           /**
076            * Get the assigned security handler.
077            * @param handler the default value
078            * @return the resolved handler
079            */
080            SecurityHandler getSecurityHandler( SecurityHandler handler );
081            
082           /**
083            * Get the assigned session handler.
084            * @param handler the default value
085            * @return the resolved handler
086            */
087            SessionHandler getSessionHandler( SessionHandler handler );
088            
089           /**
090            * Get the assigned servlet handler.
091            * @param handler the default value
092            * @return the resolved handler
093            */
094            ServletHandler getServletHandler( ServletHandler handler );
095        }
096        
097        private int m_priority = 0;
098        
099       /**
100        * Creation of a new web-application context handler.
101        * @param logger the assigned logging channel
102        * @param context the deployment context
103        * @exception Exception if an instantiation error occurs
104        */
105        public WebAppContextHandler( Logger logger, Context context ) throws Exception
106        {
107            ContextHelper helper = new ContextHelper( logger );
108            helper.contextualize( this, context );
109            
110            SecurityHandler securityHandler = 
111              context.getSecurityHandler( new SecurityHandler() );
112            setSecurityHandler( securityHandler );
113            setHandler( securityHandler );
114            
115            SessionHandler sessionHandler = 
116              context.getSessionHandler( new SessionHandler() );
117            securityHandler.setHandler( sessionHandler );
118            setSessionHandler( sessionHandler );
119            
120            ServletHandler servletHandler = 
121              context.getServletHandler( new ServletHandler() );
122            sessionHandler.setHandler( servletHandler );
123            setServletHandler( servletHandler );
124            
125            URI uri = context.getWar();
126            setWar( uri.toASCIIString() );
127    
128            File temp = context.getTempDirectory( null );
129            if( null != temp )
130            {
131                setTempDirectory( temp );
132            }
133            
134            Map map = context.getResourceAliases( null );
135            if( null != map )
136            {
137                setResourceAliases( map );
138            }
139            
140            boolean extractionPolicy = context.getExtractionPolicy( true );
141            setExtractWAR( extractionPolicy );
142            
143            PermissionCollection permissions = context.getPermissions( null );
144            if( null != permissions )
145            {
146                setPermissions( permissions );
147            }
148        }
149    }