source: josm/trunk/src/org/openstreetmap/josm/data/cache/CacheEntryAttributes.java@ 16553

Last change on this file since 16553 was 16553, checked in by Don-vip, 4 years ago

see #19334 - javadoc fixes + protected constructors for abstract classes

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import java.util.Arrays;
5import java.util.Collections;
6import java.util.HashSet;
7import java.util.Map;
8import java.util.Map.Entry;
9import java.util.Optional;
10import java.util.Set;
11import java.util.concurrent.ConcurrentHashMap;
12
13import org.apache.commons.jcs3.engine.ElementAttributes;
14import org.openstreetmap.josm.tools.Logging;
15
16/**
17 * Class that contains attributes for JCS cache entries. Parameters are used to properly handle HTTP caching,
18 * and metadata structures, that should be stored together with the cache entry
19 *
20 * @author Wiktor Niesiobędzki
21 * @since 8168
22 */
23public class CacheEntryAttributes extends ElementAttributes {
24 private static final long serialVersionUID = 1L; //version
25 private final Map<String, String> attrs = new ConcurrentHashMap<>(RESERVED_KEYS.size());
26 private static final String NO_TILE_AT_ZOOM = "noTileAtZoom";
27 private static final String ETAG = "Etag";
28 private static final String LAST_MODIFICATION = "lastModification";
29 private static final String EXPIRATION_TIME = "expirationTime";
30 private static final String HTTP_RESPONSE_CODE = "httpResponseCode";
31 private static final String ERROR_MESSAGE = "errorMessage";
32 private static final String EXCEPTION = "exception";
33 // this contains all of the above
34 private static final Set<String> RESERVED_KEYS = new HashSet<>(Arrays.asList(
35 NO_TILE_AT_ZOOM,
36 ETAG,
37 LAST_MODIFICATION,
38 EXPIRATION_TIME,
39 HTTP_RESPONSE_CODE,
40 ERROR_MESSAGE,
41 EXCEPTION
42 ));
43
44 /**
45 * Constructs a new {@code CacheEntryAttributes}.
46 */
47 public CacheEntryAttributes() {
48 super();
49 attrs.put(NO_TILE_AT_ZOOM, "false");
50 attrs.put(LAST_MODIFICATION, "0");
51 attrs.put(EXPIRATION_TIME, "0");
52 attrs.put(HTTP_RESPONSE_CODE, "200");
53 }
54
55 /**
56 * Determines if the entry is marked as "no tile at this zoom level".
57 * @return if the entry is marked as "no tile at this zoom level"
58 */
59 public boolean isNoTileAtZoom() {
60 return Boolean.toString(true).equals(attrs.get(NO_TILE_AT_ZOOM));
61 }
62
63 /**
64 * Sets the marker for "no tile at this zoom level"
65 * @param noTileAtZoom true if this entry is "no tile at this zoom level"
66 */
67 public void setNoTileAtZoom(boolean noTileAtZoom) {
68 attrs.put(NO_TILE_AT_ZOOM, Boolean.toString(noTileAtZoom));
69 }
70
71 /**
72 * Returns ETag header value, that was returned for this entry.
73 * @return ETag header value, that was returned for this entry.
74 */
75 public String getEtag() {
76 return attrs.get(ETAG);
77 }
78
79 /**
80 * Sets the ETag header that was set with this entry
81 * @param etag Etag header
82 */
83 public void setEtag(String etag) {
84 if (etag != null) {
85 attrs.put(ETAG, etag);
86 }
87 }
88
89 /**
90 * Utility for conversion from String to int, with default to 0, in case of any errors
91 *
92 * @param key - integer as string
93 * @return int value of the string
94 */
95 private long getLongAttr(String key) {
96 try {
97 return Long.parseLong(attrs.computeIfAbsent(key, k -> "0"));
98 } catch (NumberFormatException e) {
99 attrs.put(key, "0");
100 return 0;
101 }
102 }
103
104 /**
105 * Returns last modification of the object in cache in milliseconds from Epoch.
106 * @return last modification of the object in cache in milliseconds from Epoch
107 */
108 public long getLastModification() {
109 return getLongAttr(LAST_MODIFICATION);
110 }
111
112 /**
113 * sets last modification of the object in cache
114 *
115 * @param lastModification time in format of milliseconds from Epoch
116 */
117 public void setLastModification(long lastModification) {
118 attrs.put(LAST_MODIFICATION, Long.toString(lastModification));
119 }
120
121 /**
122 * Returns when the object expires in milliseconds from Epoch.
123 * @return when the object expires in milliseconds from Epoch
124 */
125 public long getExpirationTime() {
126 return getLongAttr(EXPIRATION_TIME);
127 }
128
129 /**
130 * sets expiration time for the object in cache
131 *
132 * @param expirationTime in format of milliseconds from epoch
133 */
134 public void setExpirationTime(long expirationTime) {
135 attrs.put(EXPIRATION_TIME, Long.toString(expirationTime));
136 }
137
138 /**
139 * Sets the HTTP response code that was sent with the cache entry
140 *
141 * @param responseCode http status code
142 * @since 8389
143 */
144 public void setResponseCode(int responseCode) {
145 attrs.put(HTTP_RESPONSE_CODE, Integer.toString(responseCode));
146 }
147
148 /**
149 * Returns HTTP response code.
150 * @return http status code
151 * @since 8389
152 */
153 public int getResponseCode() {
154 return (int) getLongAttr(HTTP_RESPONSE_CODE);
155 }
156
157 /**
158 * Sets the metadata about cache entry. As it stores all data together, with other attributes
159 * in common map, some keys might not be stored.
160 *
161 * @param map metadata to save
162 * @since 8418
163 */
164 public void setMetadata(Map<String, String> map) {
165 for (Entry<String, String> e: map.entrySet()) {
166 if (RESERVED_KEYS.contains(e.getKey())) {
167 Logging.info("Metadata key configuration contains key {0} which is reserved for internal use");
168 } else {
169 attrs.put(e.getKey(), e.getValue());
170 }
171 }
172 }
173
174 /**
175 * Returns an unmodifiable Map containing all metadata. Unmodifiable prevents access to metadata within attributes.
176 *
177 * @return unmodifiable Map with cache element metadata
178 * @since 8418
179 */
180 public Map<String, String> getMetadata() {
181 return Collections.unmodifiableMap(attrs);
182 }
183
184 /**
185 * Returns error message returned while retrieving this object.
186 * @return error message returned while retrieving this object
187 */
188 public String getErrorMessage() {
189 return attrs.get(ERROR_MESSAGE);
190 }
191
192 /**
193 * Sets error related to this object.
194 * @param error error related to this object
195 * @since 10469
196 */
197 public void setError(Exception error) {
198 setErrorMessage(Logging.getErrorMessage(error));
199 }
200
201 /**
202 * Sets error message related to this object.
203 * @param message error message related to this object
204 */
205 public void setErrorMessage(String message) {
206 attrs.put(ERROR_MESSAGE, message);
207 }
208
209 /**
210 * Sets exception that caused error.
211 * @param e exception that caused error
212 */
213 public void setException(Exception e) {
214 attrs.put(EXCEPTION, e.getClass().getCanonicalName());
215 }
216
217 /**
218 * Returns optional exception that was thrown when fetching resource.
219 * @return Optional exception that was thrown when fetching resource
220 */
221 public Optional<Class<? extends Exception>> getException() {
222 String className = attrs.get(EXCEPTION);
223 if (className == null) {
224 return Optional.empty();
225 }
226 try {
227 Class<?> klass = Class.forName(className);
228 if (Exception.class.isAssignableFrom(klass)) {
229 return Optional.of(klass.asSubclass(Exception.class));
230 }
231 } catch (ClassNotFoundException | ClassCastException ex) {
232 Logging.trace(ex);
233 }
234 return Optional.empty();
235 }
236}
Note: See TracBrowser for help on using the repository browser.