KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > handler > ExpiryHandler


1 // ========================================================================
2
// $Id: ExpiryHandler.java,v 1.11 2005/08/13 00:01:26 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http.handler;
17
18 import java.io.IOException JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.mortbay.log.LogFactory;
22 import org.mortbay.http.HttpException;
23 import org.mortbay.http.HttpFields;
24 import org.mortbay.http.HttpRequest;
25 import org.mortbay.http.HttpResponse;
26
27 /* ------------------------------------------------------------ */
28 /**
29  * Handler that allows the default Expiry of all content to be set.
30  *
31  * @version $Id: ExpiryHandler.java,v 1.11 2005/08/13 00:01:26 gregwilkins Exp $
32  * @author Brett Sealey
33  */

34 public class ExpiryHandler extends AbstractHttpHandler
35 {
36     private static Log log = LogFactory.getLog(ExpiryHandler.class);
37
38     /**
39      * The default expiry time in seconds
40      */

41     private long _ttl=-1;
42
43     /* ------------------------------------------------------------ */
44     /**
45      * Set the default expiry time in seconds.
46      *
47      * @param ttl The default time to live in seconds. If negative (the
48      * default) then all content will be set to expire 01Jan1970 by default.
49      */

50     public void setTimeToLive(long ttl)
51     {
52         _ttl=ttl;
53     }
54
55     /* ------------------------------------------------------------ */
56     /** Handle a request by pre-populating the Expires header with a a value
57      * that corresponds to now + ttl. If ttl -s negative then
58      * HttpFields.__01Jan1970 is used.
59      *
60      * Settings made here can be overridden by subsequent handling of the
61      * request.
62      *
63      * @param pathInContext The context path
64      * @param pathParams Path parameters such as encoded Session ID
65      * @param request The HttpRequest request
66      * @param response The HttpResponse response
67      */

68     public void handle(String JavaDoc pathInContext,
69                        String JavaDoc pathParams,
70                        HttpRequest request,
71                        HttpResponse response)
72             throws HttpException,IOException JavaDoc
73     {
74         log.debug("ExpiryHandler.handle()");
75         String JavaDoc expires;
76         if (_ttl<0)
77             expires=HttpFields.__01Jan1970;
78         else
79             expires=HttpFields.formatDate
80               (System.currentTimeMillis()+1000L*_ttl,false);
81         response.setField(HttpFields.__Expires,expires);
82     }
83 }
84
Popular Tags