aboutsummaryrefslogtreecommitdiff
path: root/connector/src/main/java/at/asitplus/eidas/specific/connector/storage/eIDASCacheTransactionStoreDecorator.java
diff options
context:
space:
mode:
Diffstat (limited to 'connector/src/main/java/at/asitplus/eidas/specific/connector/storage/eIDASCacheTransactionStoreDecorator.java')
-rw-r--r--connector/src/main/java/at/asitplus/eidas/specific/connector/storage/eIDASCacheTransactionStoreDecorator.java124
1 files changed, 124 insertions, 0 deletions
diff --git a/connector/src/main/java/at/asitplus/eidas/specific/connector/storage/eIDASCacheTransactionStoreDecorator.java b/connector/src/main/java/at/asitplus/eidas/specific/connector/storage/eIDASCacheTransactionStoreDecorator.java
new file mode 100644
index 00000000..f1ffba6f
--- /dev/null
+++ b/connector/src/main/java/at/asitplus/eidas/specific/connector/storage/eIDASCacheTransactionStoreDecorator.java
@@ -0,0 +1,124 @@
+package at.asitplus.eidas.specific.connector.storage;
+
+import java.util.Date;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import at.gv.egiz.eaaf.core.api.storage.ITransactionStorage;
+import at.gv.egiz.eaaf.core.exceptions.EAAFException;
+import at.gv.egiz.eaaf.core.exceptions.EAAFStorageException;
+
+public class eIDASCacheTransactionStoreDecorator implements ITransactionStorage{
+ private static final Logger log = LoggerFactory.getLogger(eIDASCacheTransactionStoreDecorator.class);
+
+ @Autowired(required=true) private CacheWitheIDASBackend storage;
+
+ @Override
+ public void changeKey(String oldKey, String newKey, Object value) throws EAAFException {
+ if (containsKey(oldKey)) {
+ TransactionStoreElement el = storage.get(oldKey);
+ el.setKey(newKey);
+ storage.put(newKey, el);
+ storage.remove(oldKey);
+
+ } else
+ throw new EAAFStorageException("No element in TransactionStorage with key: " + oldKey);
+
+ }
+
+ @Override
+ public List<String> clean(Date now, long dataTimeOut) {
+ return storage.clean(now, dataTimeOut);
+
+ }
+
+ @Override
+ public boolean containsKey(String key) {
+ if (key != null) {
+ if (storage.get(key) != null)
+ return true;
+
+ }
+
+ return false;
+
+ }
+
+ @Override
+ public Object get(String key) throws EAAFException {
+ if (key != null && containsKey(key)) {
+ TransactionStoreElement element = storage.get(key);
+ return element.getData();
+
+ } else
+ return null;
+ }
+
+ @Override
+ public <T> T get(String key, Class<T> type) throws EAAFException {
+ return get(key, type, -1);
+
+ }
+
+ @Override
+ public <T> T get(String key, Class<T> type, long dataTimeOut) throws EAAFException {
+ if (key != null && containsKey(key)) {
+ TransactionStoreElement value = storage.get(key);
+
+ if (dataTimeOut > -1) {
+ long now = new Date().getTime();
+ if (now - value.getCreated().getTime() > dataTimeOut) {
+ log.info("Transaction-Data with key: " + key + " is out of time.");
+ throw new EAAFStorageException("Transaction-Data with key: " + key + " is out of time.");
+
+ }
+ }
+
+ if (type.isAssignableFrom(value.getData().getClass())) {
+ return (T) value.getData();
+
+ } else
+ log.warn("Can NOT cast '" + value.getClass() + "' to '" + type + "'");
+
+ }
+
+ return null;
+ }
+
+ @Override
+ public Object getRaw(String key) throws EAAFException {
+ return storage.get(key);
+
+ }
+
+ @Override
+ public void put(String key, Object value, int dataTimeOut) throws EAAFException {
+ TransactionStoreElement element = new TransactionStoreElement();
+ element.setKey(key);
+ element.setData(value);
+ storage.put(key, element);
+
+ }
+
+ @Override
+ public void putRaw(String key, Object value) throws EAAFException {
+ if (value instanceof TransactionStoreElement)
+ storage.put(((TransactionStoreElement) value).getKey(), (TransactionStoreElement) value);
+ else
+ log.info(value.getClass().getName() + " is NOT a RAW element of " + ITransactionStorage.class.getName());
+
+ }
+
+ @Override
+ public void remove(String key) {
+ if (containsKey(key)) {
+ log.debug("Remove element with key: " + key + " from " + ITransactionStorage.class.getName());
+ storage.remove(key);
+
+ }
+ }
+
+}