package at.gv.egiz.moazs.repository; import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.egiz.moazs.scheme.MsgResponse; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.app2mzs.xsd.ObjectFactory; import org.apache.tools.ant.filters.StringInputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Repository; import javax.xml.bind.JAXBElement; import java.util.Optional; @Repository @Profile("cluster") public class RedisDeliveryRepository implements DeliveryRepository { private static final Logger logger = LoggerFactory.getLogger(RedisDeliveryRepository.class); private static final String KEY = "MZS_DELIVERY_REQUEST"; private static final String NOT_IMPLEMENTED_MESSAGE = "Not Implemented Yet"; private final RedisTemplate redisTemplate; private final Marshaller marshaller; private final HashOperations hashOperations; private final ObjectFactory factory = new ObjectFactory(); @Autowired public RedisDeliveryRepository(RedisTemplate redisTemplate, Marshaller mzsMarshaller){ logger.info("RedisDeliveryRepository()"); this.redisTemplate = redisTemplate; this.hashOperations = redisTemplate.opsForHash(); this.marshaller = mzsMarshaller; } @Override public void store(DeliveryRequestType request) { var hashKey = request.getMetaData().getAppDeliveryID(); var serializedRequest = marshaller.marshallXml(factory.createDeliveryRequest(request)); logger.info("Save DeliveryRequest with AppDeliveryID={} to redis.", hashKey); hashOperations.put(KEY, hashKey, serializedRequest); } @Override public Optional retrieveDeliveryRequest(String appDeliveryID) { String serializedRequest = hashOperations.get(KEY, appDeliveryID); if (serializedRequest == null) { return Optional.empty(); } var stream = new StringInputStream(serializedRequest); var request = (JAXBElement) marshaller.unmarshallXml(stream); return Optional.of(request.getValue()); } @Override public void store(MsgResponse notification) { throw new UnsupportedOperationException(NOT_IMPLEMENTED_MESSAGE); } @Override public Optional retrieveResponse(String id) { throw new UnsupportedOperationException(NOT_IMPLEMENTED_MESSAGE); } @Override public void store(String responseID, byte[] data) { } @Override public Optional retrieveBinaryResponse(String responseID) { return Optional.empty(); } }