    001    package 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
    022    import java.text.SimpleDateFormat;
    023    import java.util.Date;
    024    import java.util.Locale;
    025    import java.util.TimeZone;
    026
    027    /**
    028     * This class provides utilities for handling some semi-trivial HTTP stuff that
    029     * would otherwise be handled elsewhere.
    030     *
    031     * @author <a href="mailto:magnus@handpoint.com">Magnús Þór Torfason</a>
    032     * @version $Id: HttpUtils.java 1071052 2011-02-15 20:54:47Z tv $
    033     */
    034    public class HttpUtils
    035    {
    036        /**
    037         * The date format to use for HTTP Dates.
    038         */
    039        private static SimpleDateFormat httpDateFormat;
    040
    041        static
    042        {
    043            httpDateFormat = new SimpleDateFormat(
    044                    "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    045            httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    046        }
    047
    048        /**
    049         * Formats a java Date according to rfc 1123, the rfc standard for dates in
    050         * http.
    051         *
    052         * @param date The Date to format
    053         * @return A String representation of the date
    054         */
    055        public static String formatHttpDate(Date date)
    056        {
    057            synchronized (httpDateFormat)
    058            {
    059                return httpDateFormat.format(date);
    060            }
    061        }
    062
    063        /**
    064         * This method sets the required expiration headers in the response for a
    065         * given RunData object.  This method attempts to set all relevant headers,
    066         * both for HTTP 1.0 and HTTP 1.1.
    067         *
    068         * @param data The RunData object we are setting cache information for.
    069         * @param expiry The number of milliseconds until the document should expire,
    070         * <code>0</code> indicating immediate expiration (i.e. no caching).
    071         */
    072        public static void setCacheHeaders(RunData data, int expiry)
    073        {
    074            if (0 == expiry)
    075            {
    076                data.getResponse().setHeader("Pragma", "no-cache");
    077                data.getResponse().setHeader("Cache-Control", "no-cache");
    078                data.getResponse().setDateHeader("Expires", System.currentTimeMillis());
    079            }
    080            else
    081            {
    082                data.getResponse().setDateHeader("Expires", System.currentTimeMillis() + expiry);
    083            }
    084        }
    085    }