001 package org.apache.myfaces.tobago.util;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022
023 import javax.faces.context.ExternalContext;
024 import javax.faces.context.FacesContext;
025 import javax.servlet.http.HttpServletResponse;
026
027 public class ResponseUtils {
028
029 private static final Log LOG = LogFactory.getLog(ResponseUtils.class);
030
031 @Deprecated
032 public static void ensureNoCacheHeader(ExternalContext externalContext) {
033 FacesContext facesContext = FacesContext.getCurrentInstance();
034 if (facesContext.getExternalContext() != externalContext) {
035 throw new RuntimeException("Unexpected behaviour.");
036 }
037 ensureNoCacheHeader(facesContext);
038 }
039
040 public static void ensureNoCacheHeader(FacesContext facesContext) {
041 // TODO PortletRequest
042 ExternalContext externalContext = facesContext.getExternalContext();
043 if (externalContext.getResponse() instanceof HttpServletResponse) {
044 HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
045 response.setHeader("Cache-Control", "no-cache,no-store,max-age=0,must-revalidate");
046 response.setHeader("Pragma", "no-cache");
047 response.setDateHeader("Expires", 0);
048 response.setDateHeader("max-age", 0);
049 }
050 }
051
052 public static void ensureContentTypeHeader(FacesContext facesContext, String contentType) {
053 // TODO PortletRequest
054 if (facesContext.getExternalContext().getResponse() instanceof HttpServletResponse) {
055 HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
056 if (!response.containsHeader("Content-Type")) {
057 response.setContentType(contentType);
058 } else {
059 try {
060 String responseContentType = response.getContentType();
061 if (!responseContentType.equalsIgnoreCase(contentType)) {
062 response.setContentType(contentType);
063 if (LOG.isInfoEnabled()) {
064 LOG.info("Reponse already contains Header Content-Type '" + responseContentType
065 + "'. Setting Content-Type to '" + contentType + "'");
066 }
067 }
068 } catch (Error e) {
069 LOG.warn("The method ServletResponse.getContentType() is not available before Servlet 2.4");
070 }
071 }
072 }
073 }
074 }