org.jboss.resteasy.client.jaxrs.cache.MapCache Maven / Gradle / Ivy
package org.jboss.resteasy.client.jaxrs.cache;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
public class MapCache implements BrowserCache {
protected Map> cache = null;
public MapCache() {
this(new ConcurrentHashMap>());
}
public MapCache(final Map> cache) {
this.cache = cache;
}
protected Map> createCache() {
return new ConcurrentHashMap>();
}
public Entry get(String key, MediaType accept) {
Map parent = cache.get(key);
if (parent == null || parent.isEmpty()) {
return null;
}
if (accept.isWildcardType()) {
// if the client accepts */*, return just the first entry for requested URL
return parent.entrySet().iterator().next().getValue();
} else if (accept.isWildcardSubtype()) {
// if the client accepts /*, return the first entry which media type starts with /
for (Map.Entry parentEntry : parent.entrySet()) {
if (parentEntry.getKey().startsWith(accept.getType() + "/")) {
return parentEntry.getValue();
}
}
}
return parent.get(accept.toString());
}
public Entry getAny(String key) {
Map parent = cache.get(key);
if (parent == null)
return null;
Iterator iterator = parent.values().iterator();
if (iterator.hasNext())
return iterator.next();
return null;
}
public Entry getEntry(String key, MediaType accept) {
Map parent = cache.get(key);
if (parent == null)
return null;
return parent.get(accept.toString());
}
public Entry remove(String key, MediaType type) {
Map data = cache.get(key);
if (data == null)
return null;
Entry removed = data.remove(type.toString());
if (data.isEmpty()) {
cache.remove(key);
} else {
cache.put(key, data);
}
return removed;
}
public void clear() {
cache.clear();
}
public Entry put(CacheEntry cacheEntry) {
Map map = cache.get(cacheEntry.getKey());
if (map == null) {
map = new ConcurrentHashMap();
}
map.put(cacheEntry.getMediaType().toString(), cacheEntry);
cache.put(cacheEntry.getKey(), map);
return cacheEntry;
}
public Entry put(String key, MediaType mediaType,
MultivaluedMap headers, byte[] cached, int expires,
String etag, String lastModified) {
return put(new CacheEntry(key, headers, cached, expires, etag, lastModified, mediaType));
}
}