/* * Copyright 2018 A-SIT Plus GmbH * AT-specific eIDAS Connector has been developed in a cooperation between EGIZ, * A-SIT Plus GmbH, A-SIT, and Graz University of Technology. * * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "License"); * You may not use this work except in compliance with the License. * You may obtain a copy of the License at: * https://joinup.ec.europa.eu/news/understanding-eupl-v12 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.asitplus.eidas.specific.connector.provider; import java.text.MessageFormat; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceAware; import org.springframework.context.NoSuchMessageException; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Service; import at.gv.egiz.eaaf.core.api.IStatusMessenger; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.impl.logging.LogMessageProviderFactory; @Service("StatusMessageProvider") public class StatusMessageProvider implements IStatusMessenger, MessageSourceAware { private static final Logger log = LoggerFactory.getLogger(StatusMessageProvider.class); 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 ERROR_EXTERNALERROR_CODES_UNAVAILABLE = "External error-codes can NOT be load from application. Only internal errorCode: {0} is availabe"; private static final String ERROR_NO_EXTERNALERROR_CODE = "No external error for internal 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"; // external error codes private static final String DEFAULT_EXTERNALERROR_RESOURCES = "properties/external_statuscodes_map"; private static final Locale DEFAULT_EXTERNALERROR_LOCALES = new Locale("en", "GB"); private ResourceBundle externalError = null; //internal messanges private MessageSource messageSource; @Override public String getMessageWithoutDefault(final String messageId, final 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 getMessage(final String messageId, final 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 getResponseErrorCode(Throwable throwable) { String errorCode = IStatusMessenger.CODES_EXTERNAL_ERROR_GENERIC; if (throwable instanceof EaafException) { errorCode = ((EaafException) throwable).getErrorId(); } return errorCode; } @Override public String mapInternalErrorToExternalError(String intErrorCode) { // initialize messages if (externalError == null) { this.externalError = ResourceBundle.getBundle( DEFAULT_EXTERNALERROR_RESOURCES, DEFAULT_EXTERNALERROR_LOCALES); } // create the message if (externalError == null) { log.warn(MessageFormat.format(ERROR_EXTERNALERROR_CODES_UNAVAILABLE, new Object[] { intErrorCode })); return IStatusMessenger.CODES_EXTERNAL_ERROR_GENERIC; } else { try { if (StringUtils.isNotEmpty(intErrorCode)) { return externalError.getString(intErrorCode); } else { return IStatusMessenger.CODES_EXTERNAL_ERROR_GENERIC; } } catch (final MissingResourceException e2) { log.info(MessageFormat.format(ERROR_NO_EXTERNALERROR_CODE, new Object[] { intErrorCode })); return IStatusMessenger.CODES_EXTERNAL_ERROR_GENERIC; } } } @Override public void setMessageSource(MessageSource messageSource) { this.messageSource = messageSource; log.info("Injecting 'StatusMessanger' into 'LogMessageProviderFactory'"); LogMessageProviderFactory.setStatusMessager(this); } }