1 16 package org.apache.cocoon.acting; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.parameters.Parameters; 21 import org.apache.avalon.framework.thread.ThreadSafe; 22 import org.apache.cocoon.environment.ObjectModelHelper; 23 import org.apache.cocoon.environment.Redirector; 24 import org.apache.cocoon.environment.Response; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.apache.commons.lang.time.DateUtils; 27 import org.apache.commons.lang.time.FastDateFormat; 28 29 import java.util.Calendar ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 34 85 public class HttpCacheAction extends AbstractConfigurableAction implements ThreadSafe { 86 87 private FastDateFormat formatter = null; 88 int days = 0; 89 int hours = 0; 90 int minutes = 0; 91 int seconds = 0; 92 93 public void configure(Configuration configuration) 94 throws ConfigurationException { 95 super.configure(configuration); 96 97 this.formatter = FastDateFormat.getInstance("EEE, dd MMM yyyy kk:mm:ss zzz", DateUtils.UTC_TIME_ZONE); 99 this.days = configuration.getChild("days").getValueAsInteger(0); 100 this.hours = configuration.getChild("hours").getValueAsInteger(0); 101 this.minutes = configuration.getChild("minutes").getValueAsInteger(0); 102 this.seconds = configuration.getChild("seconds").getValueAsInteger(0); 103 } 104 105 public Map act(Redirector redirector, SourceResolver resolver, 106 Map objectModel, String source, Parameters parameters) 107 throws Exception { 108 Response response = ObjectModelHelper.getResponse(objectModel); 109 Calendar calendar = Calendar.getInstance(DateUtils.UTC_TIME_ZONE); 110 Map values = new HashMap (3); 111 112 113 String value = this.formatter.format(calendar); 114 long maxage = calendar.getTime().getTime(); 115 response.setHeader("Last-Modified", value); 116 values.put("last-modified", value); 117 118 119 calendar.add(Calendar.DATE, this.days); 120 calendar.add(Calendar.HOUR, this.hours); 121 calendar.add(Calendar.MINUTE, this.minutes); 122 calendar.add(Calendar.SECOND, this.seconds); 123 124 125 maxage = calendar.getTime().getTime() - maxage; 126 127 128 if (maxage > 1000) { 129 value = this.formatter.format(calendar); 130 response.setHeader("Expires", value); 131 values.put("expires", value); 132 133 value = "max-age=" + Long.toString(maxage / 1000l); 134 response.setHeader("Cache-Control", value); 135 values.put("cache-control", value); 136 137 138 } else { 139 140 response.setHeader("Expires", value); 141 values.put("expires", value); 142 143 response.setHeader("Cache-Control", "no-cache"); 144 values.put("cache-control", "no-cache"); 145 } 146 147 148 return(Collections.unmodifiableMap(values)); 149 } 150 } 151 | Popular Tags |