KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > cache > filter > HttpCacheEntry


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.appserv.web.cache.filter;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 /** HttpCacheEntry
31  * Each entry holds cached (HTTP) response:
32  * a) response bytes b) response headers c) expiryTime
33  * d) parameterEncoding used e) entryKey this entry represents,
34  * to match the entry within the hash bucket.
35  *
36  * XXX: should implement methods to enable serialization of cached response?
37  */

38 public class HttpCacheEntry {
39
40     public static final int VALUE_NOT_SET = -1;
41
42     int statusCode;
43     String JavaDoc statusMessage;
44
45     HashMap JavaDoc responseHeaders;
46     HashMap JavaDoc dateHeaders;
47     ArrayList JavaDoc cookies;
48     String JavaDoc contentType;
49     Locale JavaDoc locale;
50
51     int contentLength;
52
53     // XXX: other cacheable response info
54
byte[] bytes;
55
56     int timeout;
57     volatile long expireTime = 0;
58
59     /**
60      * set the real expire time
61      * @param expireTime in milli seconds
62      */

63     public void setExpireTime(long expireTime) {
64         this.expireTime = expireTime;
65     }
66
67     /**
68      * compute when this entry to be expired based on timeout relative to
69      * current time.
70      * @param timeout in seconds
71      */

72     public void computeExpireTime(int timeout) {
73         this.timeout = timeout;
74
75         // timeout is relative to current time
76
this.expireTime = (timeout == -1) ? timeout :
77                           System.currentTimeMillis() + (timeout * 1000);
78     }
79
80     /**
81      * is this response still valid?
82      */

83     public boolean isValid() {
84         return (expireTime > System.currentTimeMillis() || expireTime == -1);
85     }
86
87     /**
88      * clear the contents
89      */

90     public void clear() {
91         bytes = null;
92         responseHeaders = null;
93         cookies = null;
94     }
95
96     /**
97      * get the size
98      * @return size of this entry in bytes
99      * Note: this is only approximate
100      */

101     public int getSize() {
102         int size = 0;
103         if (bytes != null) {
104             size = bytes.length;
105         }
106
107         // size of response bytes plus headers (each approx 20 chars or 40 bytes)
108
return (size + (40 * responseHeaders.size()) );
109     }
110 }
111
Popular Tags