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

Last change on this file since 10308 was 9070, checked in by Don-vip, 8 years ago

Sonar - squid:S2293 - The diamond operator ("<>") should be used

  • Property svn:eol-style set to native
File size: 5.7 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.Set;
10import java.util.concurrent.ConcurrentHashMap;
11
12import org.apache.commons.jcs.engine.ElementAttributes;
13import org.openstreetmap.josm.Main;
14
15/**
16 * Class that contains attributes for JCS cache entries. Parameters are used to properly handle HTTP caching,
17 * and metadata structures, that should be stored together with the cache entry
18 *
19 * @author Wiktor Niesiobędzki
20 * @since 8168
21 */
22public class CacheEntryAttributes extends ElementAttributes {
23 private static final long serialVersionUID = 1L; //version
24 private final Map<String, String> attrs = new ConcurrentHashMap<>(RESERVED_KEYS.size());
25 private static final String NO_TILE_AT_ZOOM = "noTileAtZoom";
26 private static final String ETAG = "Etag";
27 private static final String LAST_MODIFICATION = "lastModification";
28 private static final String EXPIRATION_TIME = "expirationTime";
29 private static final String HTTP_RESPONSE_CODE = "httpResponceCode";
30 private static final String ERROR_MESSAGE = "errorMessage";
31 // this contains all of the above
32 private static final Set<String> RESERVED_KEYS = new HashSet<>(Arrays.asList(new String[]{
33 NO_TILE_AT_ZOOM,
34 ETAG,
35 LAST_MODIFICATION,
36 EXPIRATION_TIME,
37 HTTP_RESPONSE_CODE,
38 ERROR_MESSAGE
39 }));
40
41
42 /**
43 * Constructs a new {@code CacheEntryAttributes}.
44 */
45 public CacheEntryAttributes() {
46 super();
47 attrs.put(NO_TILE_AT_ZOOM, "false");
48 attrs.put(LAST_MODIFICATION, "0");
49 attrs.put(EXPIRATION_TIME, "0");
50 attrs.put(HTTP_RESPONSE_CODE, "200");
51 }
52
53 /**
54 * @return if the entry is marked as "no tile at this zoom level"
55 */
56 public boolean isNoTileAtZoom() {
57 return Boolean.toString(true).equals(attrs.get(NO_TILE_AT_ZOOM));
58 }
59
60 /**
61 * Sets the marker for "no tile at this zoom level"
62 * @param noTileAtZoom true if this entry is "no tile at this zoom level"
63 */
64 public void setNoTileAtZoom(boolean noTileAtZoom) {
65 attrs.put(NO_TILE_AT_ZOOM, Boolean.toString(noTileAtZoom));
66 }
67
68 /**
69 * @return ETag header value, that was returned for this entry.
70 */
71 public String getEtag() {
72 return attrs.get(ETAG);
73 }
74
75 /**
76 * Sets the ETag header that was set with this entry
77 * @param etag Etag header
78 */
79 public void setEtag(String etag) {
80 if (etag != null) {
81 attrs.put(ETAG, etag);
82 }
83 }
84
85 /**
86 * Utility for conversion from String to int, with default to 0, in case of any errors
87 *
88 * @param key - integer as string
89 * @return int value of the string
90 */
91 private long getLongAttr(String key) {
92 String val = attrs.get(key);
93 if (val == null) {
94 attrs.put(key, "0");
95 return 0;
96 }
97 try {
98 return Long.parseLong(val);
99 } catch (NumberFormatException e) {
100 attrs.put(key, "0");
101 return 0;
102 }
103 }
104
105 /**
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 * @return when the object expires in milliseconds from Epoch
123 */
124 public long getExpirationTime() {
125 return getLongAttr(EXPIRATION_TIME);
126 }
127
128 /**
129 * sets expiration time for the object in cache
130 *
131 * @param expirationTime in format of milliseconds from epoch
132 */
133 public void setExpirationTime(long expirationTime) {
134 attrs.put(EXPIRATION_TIME, Long.toString(expirationTime));
135 }
136
137 /**
138 * Sets the HTTP response code that was sent with the cache entry
139 *
140 * @param responseCode http status code
141 * @since 8389
142 */
143 public void setResponseCode(int responseCode) {
144 attrs.put(HTTP_RESPONSE_CODE, Integer.toString(responseCode));
145 }
146
147 /**
148 * @return http status code
149 * @since 8389
150 */
151 public int getResponseCode() {
152 return (int) getLongAttr(HTTP_RESPONSE_CODE);
153 }
154
155 /**
156 * Sets the metadata about cache entry. As it stores all data together, with other attributes
157 * in common map, some keys might not be stored.
158 *
159 * @param map metadata to save
160 * @since 8418
161 */
162 public void setMetadata(Map<String, String> map) {
163 for (Entry<String, String> e: map.entrySet()) {
164 if (RESERVED_KEYS.contains(e.getKey())) {
165 Main.info("Metadata key configuration contains key {0} which is reserved for internal use");
166 } else {
167 attrs.put(e.getKey(), e.getValue());
168 }
169 }
170 }
171
172 /**
173 * Returns an unmodifiable Map containing all metadata. Unmodifiable prevents access to metadata within attributes.
174 *
175 * @return unmodifiable Map with cache element metadata
176 * @since 8418
177 */
178 public Map<String, String> getMetadata() {
179 return Collections.unmodifiableMap(attrs);
180 }
181
182 /**
183 * @return error message returned while retrieving this object
184 */
185 public String getErrorMessage() {
186 return attrs.get(ERROR_MESSAGE);
187 }
188
189 /**
190 * @param message error message related to this object
191 */
192 public void setErrorMessage(String message) {
193 attrs.put(ERROR_MESSAGE, message);
194 }
195}
Note: See TracBrowser for help on using the repository browser.