Example usage for org.springframework.context.support ResourceBundleMessageSource getMessage

List of usage examples for org.springframework.context.support ResourceBundleMessageSource getMessage

Introduction

In this page you can find the example usage for org.springframework.context.support ResourceBundleMessageSource getMessage.

Prototype

@Override
    public final String getMessage(String code, @Nullable Object[] args, Locale locale)
            throws NoSuchMessageException 

Source Link

Usage

From source file:eu.eidas.node.utils.EidasNodeErrorUtil.java

private static String prepareErrorMessage(String message, Object[] parameters, Locale locale) {
    try {/*from  w  w w .  j  av a 2  s  .c om*/
        ResourceBundleMessageSource msgResource = (ResourceBundleMessageSource) ApplicationContextProvider
                .getApplicationContext().getBean(NodeBeanNames.SYSADMIN_MESSAGE_RESOURCES.toString());
        final String errorMessage = msgResource.getMessage(message, parameters, locale);
        return errorMessage;
    } catch (NoSuchMessageException e) {
        LOG.warn("ERROR : message not found {} - {}", message, e);
    }
    return null;

}

From source file:eu.eidas.node.utils.EidasNodeErrorUtil.java

/**
 * Method called for processing the SAML error message and specific error behaviour related
 * @param e the exception triggered//from w  w w .ja v  a2 s  . c  o m
 * @param destLog the specific logger
 * @param redirectError the redirected error
 */
public static void processSAMLEngineException(Exception e, Logger destLog, EIDASErrors redirectError) {
    // Special case for propagating the error in case of xxe
    String errorCode = null;
    if (e instanceof EIDASSAMLEngineException) {
        errorCode = ((EIDASSAMLEngineException) e).getErrorCode();
    } else if (e instanceof SAMLEngineException) {
        errorCode = ((SAMLEngineException) e).getErrorCode();
    }
    if (errorCode == null) {
        return;
    }
    if (EIDASErrors.DOC_TYPE_NOT_ALLOWED_CODE.toString().equals(errorCode)) {
        destLog.error(LoggingMarkerMDC.SECURITY_WARNING,
                "Error processing XML : XML entities processing DOCType not allowed, possible XXE attack ");
        throw new InternalErrorEIDASException(EIDASUtil.getConfig(EIDASErrors.DOC_TYPE_NOT_ALLOWED.errorCode()),
                EIDASUtil.getConfig(EIDASErrors.DOC_TYPE_NOT_ALLOWED.errorMessage()), e);
    } else if (EIDASErrors.isErrorCode(errorCode)) {
        EIDASErrors err = EIDASErrors.fromCode(errorCode);
        String message = EIDASUtil.getConfig(err.errorMessage());
        if (ApplicationContextProvider.getApplicationContext() != null) {
            ResourceBundleMessageSource msgResource = (ResourceBundleMessageSource) ApplicationContextProvider
                    .getApplicationContext().getBean(NodeBeanNames.SYSADMIN_MESSAGE_RESOURCES.toString());
            final String errorMessage = msgResource.getMessage(message,
                    new Object[] { EIDASUtil.getConfig(err.errorCode()) }, Locale.getDefault());
            destLog.info(errorMessage);
        }
        throw new InternalErrorEIDASException(EIDASUtil.getConfig(redirectError.errorCode()),
                EIDASUtil.getConfig(redirectError.errorMessage()), e);
    }
}

From source file:org.synyx.sample.ResourceBundleMessageSourceTest.java

@Test
public void test_resource_bundle_msg_source_ok() throws Exception {
    ResourceBundleMessageSource bundle = new ResourceBundleMessageSource();
    bundle.setBasename("ok_messages");
    String msg = bundle.getMessage("test.1", new Object[0], Locale.getDefault());
    assertEquals("david", msg);
}

From source file:org.synyx.sample.ResourceBundleMessageSourceTest.java

@Test
public void test_resource_bundle_msg_source_broken() throws Exception {
    try {// www.j  av a  2s.c  o  m
        ResourceBundleMessageSource bundle = new ResourceBundleMessageSource();
        bundle.setBasename("broken_messages");
        bundle.getMessage("test.1", new Object[0], Locale.getDefault());
        fail("Should throw Exception due to wrong format of file");
    } catch (NoSuchMessageException e) {
        // SHOULD have a cause to indicate why the message is missing
        assertNotNull(e.getCause());
    }
}

From source file:eu.eidas.node.connector.ConnectorExceptionHandlerServlet.java

private void prepareErrorMessage(AbstractEIDASException exception, HttpServletRequest request) {
    if (exception.getMessage() == null) {
        LOG.info("BUSINESS EXCEPTION : An error occurred on EidasNode! Couldn't get Exception message.");
    } else {/* w  ww. j a v a  2  s .com*/
        if (StringUtils.isBlank(exception.getSamlTokenFail())) {
            ResourceBundleMessageSource msgResource = (ResourceBundleMessageSource) getApplicationContext()
                    .getBean(NodeBeanNames.SYSADMIN_MESSAGE_RESOURCES.toString());
            final String errorMessage = msgResource.getMessage(exception.getErrorMessage(),
                    new Object[] { exception.getErrorCode() }, request.getLocale());
            exception.setErrorMessage(errorMessage);
            EidasNodeErrorUtil.prepareSamlResponseFail(request, exception,
                    EidasNodeErrorUtil.ErrorSource.CONNECTOR);
            LOG.info("BUSINESS EXCEPTION : ", errorMessage);
        } else {
            LOG.info("BUSINESS EXCEPTION : ", exception.getMessage());
        }
    }
}

From source file:mvc.PaymentController.java

private String getErrors(BindingResult result, HttpServletRequest req) {
    String errorString = "";
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(req.getServletContext());
    ResourceBundleMessageSource rbms = applicationContext.getBean(ResourceBundleMessageSource.class);
    for (FieldError error : result.getFieldErrors()) {
        errorString += rbms.getMessage(error.getCode(), null, Locale.US);
    }//from w  ww.j av a 2  s . com
    return errorString;
}

From source file:cu.uci.coj.restapi.controller.RestUserProfileController.java

private String ValidateUser(User user) {
    ResourceBundleMessageSource r = new ResourceBundleMessageSource();
    r.setBasename("messages_en");

    user.setDob(new Date(user.getYear() - 1900, user.getMonth() - 1, user.getDay()));

    if (user.getNick().length() == 0)
        return r.getMessage("judge.register.error.nick", null, new Locale("en"));

    if ((user.getNick().length()) > 15)
        return r.getMessage("judge.register.error.long25charact", null, new Locale("en"));

    if (user.getNick().length() < 3)
        return r.getMessage("judge.register.error.less3charact", null, new Locale("en"));

    if (user.getName().length() < 1)
        return r.getMessage("errormsg.7", null, new Locale("en"));

    if (user.getName().length() > 30)
        return r.getMessage("errormsg.6", null, new Locale("en"));

    if (!user.getName().matches("[a-zA-Z\\.\\-\\'\\s]+"))
        return r.getMessage("errormsg.8", null, new Locale("en"));

    if (user.getLastname().length() < 1)
        return r.getMessage("errormsg.10", null, new Locale("en"));

    if (user.getLastname().length() > 50)
        return r.getMessage("errormsg.9", null, new Locale("en"));

    if (!user.getLastname().matches("[a-zA-Z\\.\\-\\'\\s]+"))
        return r.getMessage("errormsg.11", null, new Locale("en"));

    // si el correo ha sido cambiado y esta en uso por otra persona en el
    // COJ/*from  w w  w  . j  ava2 s  .com*/
    if (user.getEmail().length() == 0)
        return r.getMessage("errormsg.51", null, new Locale("en"));

    if (!StringUtils.isEmpty(user.getEmail()) && userDAO.bool("email.changed", user.getEmail(), user.getUid())
            && userDAO.emailExistUpdate(user.getEmail().trim(), user.getUsername()))
        return r.getMessage("judge.register.error.emailexist", null, new Locale("en"));

    EmailValidator emailValidator = EmailValidator.getInstance(); //ver como inyectar este objeto
    if (!emailValidator.isValid(user.getEmail()))
        return r.getMessage("judge.register.error.bademail", null, new Locale("en"));

    if (user.getCountry_id() == 0)
        return r.getMessage("judge.register.error.country", null, new Locale("en"));

    if (user.getInstitution_id() == 0)
        return r.getMessage("judge.register.error.institution", null, new Locale("en"));

    if (user.getLid() == 0)
        return r.getMessage("judge.register.error.planguage", null, new Locale("en"));

    if (user.getLocale() == 0)
        return r.getMessage("judge.register.error.locale", null, new Locale("en"));

    if (user.getName().length() == 0)
        return r.getMessage("judge.register.error.name", null, new Locale("en"));

    if (user.getGender() == 0)
        return r.getMessage("judge.register.error.gender", null, new Locale("en"));

    return "0";
}