    001package org.apache.turbine.util;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022import java.util.Date;
    023import java.util.Locale;
    024import java.util.TimeZone;
    025
    026import javax.servlet.http.HttpServletResponse;
    027
    028import org.apache.commons.lang.time.FastDateFormat;
    029import org.apache.turbine.Turbine;
    030import org.apache.turbine.pipeline.PipelineData;
    031
    032/**
    033 * This class provides utilities for handling some semi-trivial HTTP stuff that
    034 * would otherwise be handled elsewhere.
    035 *
    036 * @author <a href="mailto:magnus@handpoint.com">Magnús Þór Torfason</a>
    037 * @version $Id: HttpUtils.java 1706239 2015-10-01 13:18:35Z tv $
    038 */
    039public class HttpUtils
    040{
    041    /**
    042     * The date format to use for HTTP Dates.
    043     */
    044    private static FastDateFormat httpDateFormat = FastDateFormat.getInstance(
    045                "EEE, dd MMM yyyy HH:mm:ss z",
    046                TimeZone.getTimeZone("GMT"),
    047                Locale.US);
    048
    049    /**
    050     * Formats a java Date according to rfc 1123, the rfc standard for dates in
    051     * http.
    052     *
    053     * @param date The Date to format
    054     * @return A String representation of the date
    055     */
    056    public static String formatHttpDate(Date date)
    057    {
    058        return httpDateFormat.format(date);
    059    }
    060
    061    /**
    062     * This method sets the required expiration headers in the response for a
    063     * given {@link PipelineData} object.  This method attempts to set all relevant headers,
    064     * both for HTTP 1.0 and HTTP 1.1.
    065     *
    066     * @param pipelineData The {@link PipelineData} object we are setting cache information for.
    067     * @param expiry The number of milliseconds until the document should expire,
    068     * <code>0</code> indicating immediate expiration (i.e. no caching).
    069     */
    070    public static void setCacheHeaders(PipelineData pipelineData, int expiry)
    071    {
    072        HttpServletResponse response = pipelineData.get(Turbine.class, HttpServletResponse.class);
    073
    074        if (0 == expiry)
    075        {
    076            response.setHeader("Pragma", "no-cache");
    077            response.setHeader("Cache-Control", "no-cache");
    078            response.setDateHeader("Expires", System.currentTimeMillis());
    079        }
    080        else
    081        {
    082            response.setDateHeader("Expires", System.currentTimeMillis() + expiry);
    083        }
    084    }
    085}