KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > caching > CachedResponse


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.caching;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.apache.excalibur.source.SourceValidity;
21
22 /**
23  * This is a cached response. This can either contain a byte array with
24  * the complete character response or a byte array with compiled SAX events.
25  *
26  * This class replaces the <code>CachedEventObject</code> and the
27  * <code>CachedStreamObject</code>.
28  *
29  * @since 2.1
30  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
31  * @version CVS $Id: CachedResponse.java 47280 2004-09-27 08:01:15Z cziegeler $
32  */

33 public class CachedResponse
34         implements Serializable JavaDoc {
35
36     protected final SourceValidity[] validityObjects;
37     protected final byte[] response;
38     protected Long JavaDoc expires;
39     protected final long lastModified;
40     protected String JavaDoc contentType;
41     
42     /**
43      * Create a new entry for the cache.
44      *
45      * @param validityObjects The SourceValidity objects in the order
46      * they occured in the pipeline
47      * @param response The cached sax stream or character stream
48      */

49     public CachedResponse(SourceValidity[] validityObjects,
50                           byte[] response) {
51         this(validityObjects, response, null);
52     }
53
54     /**
55      * Create a new entry for the cache.
56      *
57      * @param validityObject The SourceValidity object
58      * @param response The cached sax stream or character stream
59      */

60     public CachedResponse(SourceValidity validityObject,
61                           byte[] response) {
62         this(new SourceValidity[] {validityObject}, response, null);
63     }
64
65     /**
66      * Create a new entry for the cache.
67      *
68      * @param validityObjects The SourceValidity objects in the order
69      * they occured in the pipeline
70      * @param response The cached sax stream or character stream
71      * @param expires The configured expires, or null if no
72      * expires was defined.
73      */

74     public CachedResponse(SourceValidity[] validityObjects,
75                           byte[] response,
76                           Long JavaDoc expires) {
77         this.validityObjects = validityObjects;
78         this.response = response;
79         this.expires = expires;
80         this.lastModified = this.setLastModified(System.currentTimeMillis());
81     }
82
83     /**
84      * Get the validity objects
85      */

86     public SourceValidity[] getValidityObjects() {
87         return this.validityObjects;
88     }
89
90     /**
91      * Get the cached response.
92      *
93      * @return The sax stream or character stream
94      */

95     public byte[] getResponse() {
96         return this.response;
97     }
98
99     /**
100      * Get the configured expires.
101      *
102      * @return The configured expires, or null if no expires was defined
103      */

104     public Long JavaDoc getExpires() {
105         return this.expires;
106     }
107     
108     /**
109      * Set the (newly) configured expires.
110      *
111      */

112     public void setExpires(Long JavaDoc newExpires) {
113         this.expires = newExpires;
114     }
115     
116     /**
117      * Set the (newly) configured last modified.
118      *
119      */

120     protected long setLastModified(long lastModified) {
121         // Return the value rounded to the nearest second.
122
return lastModified - (lastModified % 1000);
123     }
124     
125     /**
126      * @return the last modified time
127      */

128     public long getLastModified() {
129         return lastModified;
130     }
131
132     /**
133      * @return Returns the cached content type (or null).
134      */

135     public String JavaDoc getContentType() {
136         return this.contentType;
137     }
138     /**
139      * @param value The content type to cache.
140      */

141     public void setContentType(String JavaDoc value) {
142         this.contentType = value;
143     }
144 }
145
Popular Tags