001    // Copyright 2004, 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    package org.apache.tapestry.services.impl;
015    
016    import java.io.IOException;
017    
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.markup.MarkupWriterSource;
020    import org.apache.tapestry.services.RequestLocaleManager;
021    import org.apache.tapestry.services.ResponseBuilder;
022    import org.apache.tapestry.services.ResponseContributor;
023    import org.apache.tapestry.web.WebRequest;
024    import org.apache.tapestry.web.WebResponse;
025    
026    /**
027     * Determines if incoming request is a valid dojo request via the javascript
028     * added parameter of "dojoRequest" = "true".
029     * 
030     * @author jkuhnert
031     */
032    public class JSONResponseContributorImpl implements ResponseContributor
033    {
034        
035        public static final String JSON_HEADER = "json";
036        
037        private RequestLocaleManager _localeManager;
038        
039        private MarkupWriterSource _markupWriterSource;
040        
041        private WebResponse _webResponse;
042        
043        private WebRequest _webRequest;
044        
045        /**
046         * {@inheritDoc}
047         */
048        public ResponseBuilder createBuilder(IRequestCycle cycle)
049        throws IOException
050        {
051            return new JSONResponseBuilder(cycle, _localeManager, _markupWriterSource,
052                    _webResponse);
053        }
054        
055        /**
056         * {@inheritDoc}
057         */
058        public boolean handlesResponse(IRequestCycle cycle)
059        {
060            String parm = cycle.getParameter(JSON_HEADER);
061            
062            if (parm != null && Boolean.valueOf(parm).booleanValue())
063                return true;
064            if (_webRequest.getHeader(JSON_HEADER) != null)
065                return true;
066            
067            return false;
068        }
069        
070        public void setLocaleManager(RequestLocaleManager localeManager)
071        {
072            _localeManager = localeManager;
073        }
074        
075        public void setMarkupWriterSource(MarkupWriterSource markupWriterSource)
076        {
077            _markupWriterSource = markupWriterSource;
078        }
079        
080        public void setWebResponse(WebResponse webResponse)
081        {
082            _webResponse = webResponse;
083        }
084        
085        public void setWebRequest(WebRequest webRequest)
086        {
087            _webRequest  = webRequest;
088        }
089    }