View Javadoc

1   /*
2    * Copyright 2007 Hippo
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" 
12   * BASIS, 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 nl.hippo.client.api.caching;
17  
18  import java.io.Serializable;
19  
20  import nl.hippo.client.api.event.EventValidity;
21  
22  public class CachedResponseImpl implements Serializable, CachedResponse {
23  
24      private static final long serialVersionUID = 1L;
25  
26      protected final EventValidity[] validityObjects;
27  
28      protected final Object response;
29  
30      protected Long expires;
31  
32      protected final long lastModified;
33  
34      protected String contentType;
35  
36      /**
37       * Create a new entry for the cache.
38       * 
39       * @param validityObjects
40       *            The SourceValidity objects in the order they occured in the
41       *            pipeline
42       * @param response
43       *            The cached sax stream or character stream
44       */
45      public CachedResponseImpl(EventValidity[] validityObjects, Object response) {
46          this(validityObjects, response, null);
47      }
48  
49      /**
50       * Create a new entry for the cache.
51       * 
52       * @param validityObject
53       *            The SourceValidity object
54       * @param response
55       *            The cached sax stream or character stream
56       */
57      public CachedResponseImpl(EventValidity validityObject, Object response) {
58          this(new EventValidity[] { validityObject }, response, null);
59      }
60  
61      /**
62       * Create a new entry for the cache.
63       * 
64       * @param validityObjects
65       *            The SourceValidity objects in the order they occured in the
66       *            pipeline
67       * @param response
68       *            The cached sax stream or character stream
69       * @param expires
70       *            The configured expires, or null if no expires was defined.
71       */
72      public CachedResponseImpl(EventValidity[] validityObjects, Object response, Long expires) {
73          this.validityObjects = validityObjects;
74          this.response = response;
75          this.expires = expires;
76          this.lastModified = this.setLastModified(System.currentTimeMillis());
77      }
78  
79      /**
80       * Get the validity objects
81       */
82      public SourceValidity[] getValidityObjects() {
83          return this.validityObjects;
84      }
85  
86      /**
87       * Get the cached response.
88       * 
89       * @return The sax stream or character stream
90       */
91      public Object getResponse() {
92          return this.response;
93      }
94  
95      /**
96       * Get the configured expires.
97       * 
98       * @return The configured expires, or null if no expires was defined
99       */
100     public Long getExpires() {
101         return this.expires;
102     }
103 
104     /**
105      * Set the (newly) configured expires.
106      *  
107      */
108     public void setExpires(Long newExpires) {
109         this.expires = newExpires;
110     }
111 
112     /**
113      * Set the (newly) configured last modified.
114      *  
115      */
116     protected long setLastModified(long lastModified) {
117         // Return the value rounded to the nearest second.
118         return lastModified - (lastModified % 1000);
119     }
120 
121     /**
122      * @return the last modified time
123      */
124     public long getLastModified() {
125         return lastModified;
126     }
127 
128     /**
129      * @return Returns the cached content type (or null).
130      */
131     public String getContentType() {
132         return this.contentType;
133     }
134 
135     /**
136      * @param value
137      *            The content type to cache.
138      */
139     public void setContentType(String value) {
140         this.contentType = value;
141     }
142 }