diff --git a/src/main/java/it/gov/pagopa/wispconverter/config/RedisConfig.java b/src/main/java/it/gov/pagopa/wispconverter/config/RedisConfig.java index 56251ca7..562393df 100644 --- a/src/main/java/it/gov/pagopa/wispconverter/config/RedisConfig.java +++ b/src/main/java/it/gov/pagopa/wispconverter/config/RedisConfig.java @@ -40,4 +40,23 @@ public RedisTemplate registerRedisSimpleTemplate(final LettuceCo template.setConnectionFactory(connectionFactory); return template; } + + @Bean(name = "redisBinaryTemplate") + public RedisTemplate registerRedisBinaryTemplate(final LettuceConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new org.springframework.data.redis.serializer.RedisSerializer() { + @Override + public byte[] serialize(byte[] bytes) { + return bytes; + } + + @Override + public byte[] deserialize(byte[] bytes) { + return bytes; + } + }); + template.setConnectionFactory(connectionFactory); + return template; + } } \ No newline at end of file diff --git a/src/main/java/it/gov/pagopa/wispconverter/repository/CacheRepository.java b/src/main/java/it/gov/pagopa/wispconverter/repository/CacheRepository.java index 6b1ce7a8..1be03a60 100644 --- a/src/main/java/it/gov/pagopa/wispconverter/repository/CacheRepository.java +++ b/src/main/java/it/gov/pagopa/wispconverter/repository/CacheRepository.java @@ -17,6 +17,10 @@ public class CacheRepository { @Qualifier("redisSimpleTemplate") private RedisTemplate redisSimpleTemplate; + @Autowired + @Qualifier("redisBinaryTemplate") + private RedisTemplate redisBinaryTemplate; + public void insert(String key, String value, long ttlInMinutes) { this.redisSimpleTemplate.opsForValue().set(key, value, Duration.ofMinutes(ttlInMinutes)); } @@ -36,6 +40,16 @@ public T read(String key, Class clazz) { return result; } + public byte[] readByte(String key) { + byte[] result = {}; + try { + result = this.redisBinaryTemplate.opsForValue().get(key); + } catch (Exception e) { + log.error(String.format("Cannot correctly extract binary object retrieved with key [%s]", key)); + } + return result; + } + public boolean delete(String key) { Boolean isDeleted = this.redisSimpleTemplate.delete(key); return isDeleted != null && isDeleted; diff --git a/src/main/java/it/gov/pagopa/wispconverter/service/ReceiptTimerService.java b/src/main/java/it/gov/pagopa/wispconverter/service/ReceiptTimerService.java index 1aa0134b..83cb4d11 100644 --- a/src/main/java/it/gov/pagopa/wispconverter/service/ReceiptTimerService.java +++ b/src/main/java/it/gov/pagopa/wispconverter/service/ReceiptTimerService.java @@ -4,7 +4,6 @@ import com.azure.messaging.servicebus.ServiceBusMessage; import com.azure.messaging.servicebus.ServiceBusSenderClient; import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; import it.gov.pagopa.wispconverter.controller.model.ReceiptTimerRequest; import it.gov.pagopa.wispconverter.repository.CacheRepository; import it.gov.pagopa.wispconverter.service.model.ReceiptDto; @@ -19,11 +18,10 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; +import java.nio.charset.StandardCharsets; import java.time.OffsetDateTime; import java.time.temporal.ChronoUnit; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; @Service @Slf4j @@ -34,7 +32,6 @@ public class ReceiptTimerService { public static final String PAYMENT_TOKEN_CACHING_KEY_TEMPLATE = "2_wisp_%s"; private final CacheRepository cacheRepository; private final ReService reService; - private final Pattern paymentTokenCachingKeyTemplatePattern = Pattern.compile("\\s*(\\{.*\\})\\s*"); @Value("${azure.sb.wisp-payment-timeout-queue.connectionString}") private String connectionString; @Value("${azure.sb.queue.receiptTimer.name}") @@ -141,17 +138,14 @@ private void populateMDC(String paymentToken) { String domainId = null; String noticeNumber = null; try { - String cacheInfo = cacheRepository.read(String.format(PAYMENT_TOKEN_CACHING_KEY_TEMPLATE, paymentToken), String.class); - if (cacheInfo != null) { - Matcher matcher = paymentTokenCachingKeyTemplatePattern.matcher(cacheInfo); - if (matcher.matches()) { - ReceiptTimerRequest receiptTimerRequest = new Gson().fromJson(matcher.group(1), ReceiptTimerRequest.class); - MDC.put(Constants.MDC_SESSION_ID, receiptTimerRequest.getSessionId()); - domainId = receiptTimerRequest.getFiscalCode(); - noticeNumber = receiptTimerRequest.getNoticeNumber(); - } - } - } catch (JsonSyntaxException e) { + byte[] primitiveByteArray = cacheRepository.readByte(String.format(PAYMENT_TOKEN_CACHING_KEY_TEMPLATE, paymentToken)); + String byteArrayAsString = new String(primitiveByteArray, StandardCharsets.UTF_8); + String objectAsString = byteArrayAsString.substring(byteArrayAsString.indexOf('{'), byteArrayAsString.lastIndexOf('}') + 1); + ReceiptTimerRequest receiptTimerRequest = new Gson().fromJson(objectAsString, ReceiptTimerRequest.class); + MDC.put(Constants.MDC_SESSION_ID, receiptTimerRequest.getSessionId()); + domainId = receiptTimerRequest.getFiscalCode(); + noticeNumber = receiptTimerRequest.getNoticeNumber(); + } catch (Exception e) { log.debug("Impossible to generate data for MDC from cached payment token.", e); } MDCUtil.setReceiptTimerInfoInMDC(domainId, noticeNumber, paymentToken);