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