| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| HttpUtils |
|
| 1.5;1,5 |
| 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.util.Date; | |
| 23 | import java.util.Locale; | |
| 24 | import java.util.TimeZone; | |
| 25 | ||
| 26 | import javax.servlet.http.HttpServletResponse; | |
| 27 | ||
| 28 | import org.apache.commons.lang.time.FastDateFormat; | |
| 29 | import org.apache.turbine.Turbine; | |
| 30 | import org.apache.turbine.pipeline.PipelineData; | |
| 31 | ||
| 32 | /** | |
| 33 | * This class provides utilities for handling some semi-trivial HTTP stuff that | |
| 34 | * would otherwise be handled elsewhere. | |
| 35 | * | |
| 36 | * @author <a href="mailto:magnus@handpoint.com">Magnús Þór Torfason</a> | |
| 37 | * @version $Id: HttpUtils.java 1706239 2015-10-01 13:18:35Z tv $ | |
| 38 | */ | |
| 39 | 0 | public class HttpUtils |
| 40 | { | |
| 41 | /** | |
| 42 | * The date format to use for HTTP Dates. | |
| 43 | */ | |
| 44 | 0 | private static FastDateFormat httpDateFormat = FastDateFormat.getInstance( |
| 45 | "EEE, dd MMM yyyy HH:mm:ss z", | |
| 46 | TimeZone.getTimeZone("GMT"), | |
| 47 | Locale.US); | |
| 48 | ||
| 49 | /** | |
| 50 | * Formats a java Date according to rfc 1123, the rfc standard for dates in | |
| 51 | * http. | |
| 52 | * | |
| 53 | * @param date The Date to format | |
| 54 | * @return A String representation of the date | |
| 55 | */ | |
| 56 | public static String formatHttpDate(Date date) | |
| 57 | { | |
| 58 | 0 | return httpDateFormat.format(date); |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * This method sets the required expiration headers in the response for a | |
| 63 | * given {@link PipelineData} object. This method attempts to set all relevant headers, | |
| 64 | * both for HTTP 1.0 and HTTP 1.1. | |
| 65 | * | |
| 66 | * @param pipelineData The {@link PipelineData} object we are setting cache information for. | |
| 67 | * @param expiry The number of milliseconds until the document should expire, | |
| 68 | * <code>0</code> indicating immediate expiration (i.e. no caching). | |
| 69 | */ | |
| 70 | public static void setCacheHeaders(PipelineData pipelineData, int expiry) | |
| 71 | { | |
| 72 | 0 | HttpServletResponse response = pipelineData.get(Turbine.class, HttpServletResponse.class); |
| 73 | ||
| 74 | 0 | if (0 == expiry) |
| 75 | { | |
| 76 | 0 | response.setHeader("Pragma", "no-cache"); |
| 77 | 0 | response.setHeader("Cache-Control", "no-cache"); |
| 78 | 0 | response.setDateHeader("Expires", System.currentTimeMillis()); |
| 79 | } | |
| 80 | else | |
| 81 | { | |
| 82 | 0 | response.setDateHeader("Expires", System.currentTimeMillis() + expiry); |
| 83 | } | |
| 84 | 0 | } |
| 85 | } |