View Javadoc

1   /*
2    * $Id: ExpirationDate.java,v 1.18 2011/06/16 09:29:41 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.webapp;
6   
7   import java.io.Serializable;
8   import java.text.DateFormat;
9   import java.text.ParseException;
10  import java.text.SimpleDateFormat;
11  import java.util.Date;
12  
13  import javax.servlet.http.HttpServletResponse;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.rcfaces.core.internal.lang.StringAppender;
18  import org.rcfaces.core.internal.util.Delay;
19  
20  /**
21   * 
22   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
23   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
24   */
25  public class ExpirationDate implements Serializable {
26  
27      private static final String REVISION = "$Revision: 1.18 $";
28  
29      private static final long serialVersionUID = 8408879197958606825L;
30  
31      private static final Log LOG = LogFactory.getLog(ExpirationDate.class);
32  
33      private final long expiresDate;
34  
35      private final long expiresDelay;
36  
37      private final String expiresMaxAge;
38  
39      protected ExpirationDate(long expires, long expiresDate) {
40          this.expiresDate = expiresDate;
41  
42          this.expiresDelay = expires;
43          if (expires >= Delay.SECOND) {
44              this.expiresMaxAge = "max-age=" + (expires / Delay.SECOND); // en
45  
46          } else {
47              this.expiresMaxAge = null;
48          }
49      }
50  
51      public static ExpirationDate parse(String servletName,
52              String expireProperty, String expiresValue) {
53  
54          if (expiresValue.indexOf('/') >= 0) {
55              DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
56  
57              long expiresDate;
58              try {
59                  Date date = dateFormat.parse(expiresValue);
60  
61                  expiresDate = date.getTime();
62  
63              } catch (ParseException e) {
64                  LOG.error("Can not parse date attribute ('" + expireProperty
65                          + "') value='" + expiresValue + "'  for sevlet '"
66                          + servletName + "'.", e);
67  
68                  IllegalArgumentException ex = new IllegalArgumentException(
69                          "Can not parse date attribute (" + expireProperty
70                                  + "') value='" + expiresValue + "'.");
71  
72                  ex.initCause(e);
73  
74                  throw ex;
75              }
76  
77              return new ExpirationDate(-1, expiresDate);
78          }
79  
80          long expires;
81  
82          try {
83              expires = Delay.parseDelay(expiresValue);
84  
85          } catch (ParseException e) {
86              LOG.error("Can not parse expire attribute ('" + expireProperty
87                      + "') value='" + expiresValue + "'  for sevlet '"
88                      + servletName + "'.", e);
89  
90              IllegalArgumentException ex = new IllegalArgumentException(
91                      "Can not parse expire attribute ('" + expireProperty
92                              + "') value='" + expiresValue + "'.");
93  
94              ex.initCause(e);
95  
96              throw ex;
97          }
98  
99          return new ExpirationDate(expires, -1);
100     }
101 
102     public static ExpirationDate fromDelay(long delay) {
103         return new ExpirationDate(delay, -1);
104     }
105 
106     public static ExpirationDate noExpiration() {
107         return new ExpirationDate(-1, -1);
108     }
109 
110     public void sendExpires(HttpServletResponse response) {
111 
112         long d = 0;
113         if (expiresDate > 0) {
114             d = expiresDate;
115 
116             if (LOG.isTraceEnabled()) {
117                 LOG.trace("Expiration date is setted.");
118             }
119 
120         } else if (expiresDelay > 0) {
121             d = System.currentTimeMillis() + expiresDelay;
122 
123             if (expiresMaxAge != null) {
124                 response.setHeader(ExtendedHttpServlet.HTTP_CACHE_CONTROL,
125                         expiresMaxAge);
126             }
127 
128             if (LOG.isTraceEnabled()) {
129                 LOG.trace("Compute expiration date from delay: " + expiresDelay
130                         / Delay.SECOND + "s");
131             }
132         }
133 
134         if (d > 0) {
135             d -= (d % 1000); // Retire les ms !
136 
137             if (LOG.isDebugEnabled()) {
138                 LOG.debug("Set expiration date to " + d + " (" + new Date(d)
139                         + ")");
140             }
141 
142             response.setDateHeader(ExtendedHttpServlet.HTTP_EXPIRES, d);
143 
144         } else if (LOG.isDebugEnabled()) {
145             LOG.debug("No expiration defined");
146         }
147 
148     }
149 
150     public long getExpiresDate() {
151         return expiresDate;
152     }
153 
154     public long getExpiresDelay() {
155         return expiresDelay;
156     }
157 
158     public String toString() {
159         StringAppender sa = new StringAppender("[ExpirationDate", 128);
160 
161         if (expiresDate > 0) {
162             sa.append(" expiresDate='");
163             sa.append(new SimpleDateFormat().format(new Date(expiresDate)));
164             sa.append('\'');
165         }
166 
167         if (expiresDelay > 0) {
168             sa.append(" expiresDelay='");
169             long d = expiresDelay;
170             if (d >= Delay.YEAR) {
171                 sa.append(" YEAR=").append(d / Delay.YEAR);
172                 d %= Delay.YEAR;
173             }
174             if (d >= Delay.MONTH) {
175                 sa.append(" MONTH=").append(d / Delay.MONTH);
176                 d %= Delay.MONTH;
177             }
178             if (d >= Delay.DAY) {
179                 sa.append(" DAY=").append(d / Delay.DAY);
180                 d %= Delay.DAY;
181             }
182             if (d >= Delay.HOUR) {
183                 sa.append(" HOUR=").append(d / Delay.HOUR);
184                 d %= Delay.HOUR;
185             }
186             if (d >= Delay.MINUTE) {
187                 sa.append(" MINUTE=").append(d / Delay.MINUTE);
188                 d %= Delay.MINUTE;
189             }
190             if (d >= Delay.SECOND) {
191                 sa.append(" SECOND=").append(d / Delay.SECOND);
192                 d %= Delay.SECOND;
193             }
194             if (d > 0) {
195                 sa.append(" MILLIS=").append(d);
196             }
197 
198             sa.append('\'');
199         }
200 
201         sa.append(']');
202 
203         return sa.toString();
204     }
205 
206 }