From 4af92cb886592bd379ed2e338a383c7f350f5104 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Tue, 3 May 2022 16:03:59 +0200 Subject: feature(core): add simple implementation of a Spring based message source --- .../logging/SpringBasedBasicStatusMessager.java | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/logging/SpringBasedBasicStatusMessager.java (limited to 'eaaf_core_utils') diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/logging/SpringBasedBasicStatusMessager.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/logging/SpringBasedBasicStatusMessager.java new file mode 100644 index 00000000..a62b8c9b --- /dev/null +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/logging/SpringBasedBasicStatusMessager.java @@ -0,0 +1,115 @@ +package at.gv.egiz.eaaf.core.impl.logging; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; + +import org.springframework.context.MessageSource; +import org.springframework.context.MessageSourceAware; +import org.springframework.context.NoSuchMessageException; +import org.springframework.context.i18n.LocaleContextHolder; + +import at.gv.egiz.eaaf.core.api.IStatusMessenger; +import lombok.extern.slf4j.Slf4j; + +/** + * Basic {@link IStatusMessenger} implementation that operates on Spring {@link MessageSource}. + * + * @author tlenz + * + */ +@Slf4j +public class SpringBasedBasicStatusMessager implements IStatusMessenger, MessageSourceAware { + + private static final String ERROR_MESSAGES_UNAVAILABLE = "Error messages can NOT be load from application." + + " Only errorCode: {0} is availabe"; + private static final String ERROR_NO_MESSAGE = "No errormesseage for error with number.={0}"; + + private static final String MSG_WARN_NO_SOURCE = "MessageCode: {} is NOT SET for locale: {}"; + private static final String MSG_INFO = "Use locale: {} as default"; + + private MessageSource messageSource; + + @Override + public String getMessage(String messageId, Object[] parameters) { + if (messageSource == null) { + return MessageFormat.format(ERROR_MESSAGES_UNAVAILABLE, new Object[]{messageId}); + + } else { + try { + final Locale locale = LocaleContextHolder.getLocale(); + return messageSource.getMessage(messageId, parameters, locale); + + } catch (final NoSuchMessageException e) { + log.info(MSG_WARN_NO_SOURCE, messageId, LocaleContextHolder.getLocale()); + log.debug(MSG_INFO, Locale.ENGLISH); + + try { + return messageSource.getMessage(messageId, parameters, Locale.ENGLISH); + + } catch (final NoSuchMessageException e2) { + return MessageFormat.format(ERROR_NO_MESSAGE, new Object[]{messageId}); + + } + + } catch (final MissingResourceException e2) { + return MessageFormat.format(ERROR_NO_MESSAGE, new Object[]{messageId}); + + } + } + } + + @Override + public String getMessageWithoutDefault(String messageId, Object[] parameters) { + if (messageSource == null) { + return null; + + } else { + try { + final Locale locale = LocaleContextHolder.getLocale(); + return messageSource.getMessage(messageId, parameters, locale); + + } catch (final NoSuchMessageException e) { + log.info(MSG_WARN_NO_SOURCE, messageId, LocaleContextHolder.getLocale()); + log.debug(MSG_INFO, Locale.ENGLISH); + + try { + return messageSource.getMessage(messageId, parameters, Locale.ENGLISH); + + } catch (final NoSuchMessageException e2) { + log.info(MSG_WARN_NO_SOURCE, messageId, Locale.ENGLISH); + + } + + } catch (final MissingResourceException e2) { + log.warn("No message source", e2); + + } + } + + return null; + + } + + @Override + public String getResponseErrorCode(Throwable throwable) { + return IStatusMessenger.CODES_INTERNAL_ERROR_GENERIC; + + } + + @Override + public String mapInternalErrorToExternalError(String intErrorCode) { + return IStatusMessenger.CODES_EXTERNAL_ERROR_GENERIC; + + } + + @Override + public void setMessageSource(final MessageSource messageSource) { + this.messageSource = messageSource; + + log.info("Injecting '{}' into 'LogMessageProviderFactory'", this.getClass().getName()); + LogMessageProviderFactory.setStatusMessager(this); + + } + +} -- cgit v1.2.3