001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.request;
016    
017    import java.io.IOException;
018    
019    import javax.servlet.ServletException;
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.tapestry.Tapestry;
024    import org.apache.tapestry.services.ServletRequestServicer;
025    import org.apache.tapestry.services.ServletRequestServicerFilter;
026    import org.apache.tapestry.spec.ILibrarySpecification;
027    
028    /**
029     * Checks to see if a {@link org.apache.tapestry.request.IRequestDecoder}has been provided as an
030     * application extension, and (if so), creates a new HttpServletRequest wrapper around the decoded
031     * request.
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    public class DecodedRequestInjector implements ServletRequestServicerFilter
037    {
038        private ILibrarySpecification _applicationSpecification;
039    
040        private IRequestDecoder _decoder;
041    
042        public void initializeService()
043        {
044            if (_applicationSpecification.checkExtension(Tapestry.REQUEST_DECODER_EXTENSION_NAME))
045                _decoder = (IRequestDecoder) _applicationSpecification.getExtension(
046                        Tapestry.REQUEST_DECODER_EXTENSION_NAME,
047                        IRequestDecoder.class);
048        }
049    
050        public void service(HttpServletRequest request, HttpServletResponse response,
051                ServletRequestServicer servicer) throws IOException, ServletException
052        {
053            HttpServletRequest decodedRequest = _decoder == null ? request : wrapRequest(request);
054    
055            servicer.service(decodedRequest, response);
056        }
057    
058        public HttpServletRequest wrapRequest(HttpServletRequest request)
059        {
060            DecodedRequest decodedRequest = _decoder.decodeRequest(request);
061    
062            return new DecodedRequestWrapper(request, decodedRequest);
063        }
064    
065        public void setApplicationSpecification(ILibrarySpecification applicationSpecification)
066        {
067            _applicationSpecification = applicationSpecification;
068        }
069    
070    }