1 package org.apache.turbine.util; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.text.SimpleDateFormat; 23 import java.util.Date; 24 import java.util.Locale; 25 import java.util.TimeZone; 26 27 /** 28 * This class provides utilities for handling some semi-trivial HTTP stuff that 29 * would otherwise be handled elsewhere. 30 * 31 * @author <a href="mailto:magnus@handpoint.com">Magnús Þór Torfason</a> 32 * @version $Id: HttpUtils.java 1071052 2011-02-15 20:54:47Z tv $ 33 */ 34 public class HttpUtils 35 { 36 /** 37 * The date format to use for HTTP Dates. 38 */ 39 private static SimpleDateFormat httpDateFormat; 40 41 static 42 { 43 httpDateFormat = new SimpleDateFormat( 44 "EEE, dd MMM yyyy HH:mm:ss z", Locale.US); 45 httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 46 } 47 48 /** 49 * Formats a java Date according to rfc 1123, the rfc standard for dates in 50 * http. 51 * 52 * @param date The Date to format 53 * @return A String representation of the date 54 */ 55 public static String formatHttpDate(Date date) 56 { 57 synchronized (httpDateFormat) 58 { 59 return httpDateFormat.format(date); 60 } 61 } 62 63 /** 64 * This method sets the required expiration headers in the response for a 65 * given RunData object. This method attempts to set all relevant headers, 66 * both for HTTP 1.0 and HTTP 1.1. 67 * 68 * @param data The RunData object we are setting cache information for. 69 * @param expiry The number of milliseconds until the document should expire, 70 * <code>0</code> indicating immediate expiration (i.e. no caching). 71 */ 72 public static void setCacheHeaders(RunData data, int expiry) 73 { 74 if (0 == expiry) 75 { 76 data.getResponse().setHeader("Pragma", "no-cache"); 77 data.getResponse().setHeader("Cache-Control", "no-cache"); 78 data.getResponse().setDateHeader("Expires", System.currentTimeMillis()); 79 } 80 else 81 { 82 data.getResponse().setDateHeader("Expires", System.currentTimeMillis() + expiry); 83 } 84 } 85 }