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