Example usage for org.springframework.context MessageSource getMessage

List of usage examples for org.springframework.context MessageSource getMessage

Introduction

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

Prototype

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

Source Link

Document

Try to resolve the message.

Usage

From source file:org.openmrs.module.admintoolsui.page.controller.account.AccountPageController.java

private String getMessageErrors(MessageSource messageSource, List<ObjectError> allErrors) {
    String message = "";
    for (ObjectError error : allErrors) {
        Object[] arguments = error.getArguments();
        String errorMessage = messageSource.getMessage(error.getCode(), arguments, Context.getLocale());
        message = message.concat(replaceArguments(errorMessage, arguments).concat("<br>"));
    }//  ww w  .  jav a 2 s.  co  m
    return message;
}

From source file:org.terasoluna.gfw.common.message.ResultMessageUtilsTest.java

@Test(expected = NoSuchMessageException.class)
public void testNoSuchMessageException() {
    ResultMessage message = mock(ResultMessage.class);
    MessageSource messageSource = mock(MessageSource.class);
    Locale locale = Locale.getDefault();

    when(message.getCode()).thenReturn("MSG001");
    when(message.getArgs()).thenReturn(null);
    when(message.getText()).thenReturn(null);

    when(messageSource.getMessage("MSG001", null, locale)).thenThrow(new NoSuchMessageException("MSG001"));

    ResultMessageUtils.resolveMessage(message, messageSource);
}

From source file:org.terasoluna.gfw.common.message.ResultMessageUtilsTest.java

@Test
public void testResolveMessageWithNoSuchMessageException() {
    ResultMessage message = mock(ResultMessage.class);
    MessageSource messageSource = mock(MessageSource.class);
    Locale locale = Locale.getDefault();

    when(message.getCode()).thenReturn("MSG001");
    when(message.getArgs()).thenReturn(null);
    when(message.getText()).thenReturn("MESSAGE_TEXT");

    when(messageSource.getMessage("MSG001", null, locale)).thenThrow(new NoSuchMessageException("MSG001"));

    String msg = ResultMessageUtils.resolveMessage(message, messageSource);
    assertThat(msg, is("MESSAGE_TEXT"));
}

From source file:egovframework.rte.fdl.cmmn.aspect.ExceptionTransfer.java

protected Exception processException(final Class clazz, final String msgKey, final String[] msgArgs,
        final Exception e, final Locale locale, ExceptionCreator exceptionCreator) {
    getLog(clazz).error(messageSource.getMessage(msgKey, msgArgs, locale), e);
    ExceptionCreator eC = null;/*from w  ww.j  a  va2 s .  c o  m*/
    if (exceptionCreator == null) {
        eC = new ExceptionCreator() {
            public Exception processException(MessageSource messageSource) {
                return new BaseException(messageSource, msgKey, msgArgs, locale, e);
            }
        };
    }
    return eC.processException(messageSource);
}

From source file:com.benfante.taglib.frontend.tags.AlertTag.java

protected final int doStartTagInternal() throws JspException, IOException {
    try {// w  ww . jav a 2s. com
        Map<String, String> flash = (Map<String, String>) pageContext.getRequest().getAttribute("flash");
        if (flash == null)
            flash = (Map<String, String>) pageContext.getSession().getAttribute("flash");
        if (flash != null && flash.get(type) != null) {
            // Resolve the message.
            MessageSource messageSource = getMessageSource();
            if (messageSource == null) {
                throw new JspTagException("No corresponding MessageSource found");
            }
            String msg = "";
            try {
                Object[] argumentsArray = {};
                msg = messageSource.getMessage(flash.get(type), argumentsArray,
                        getRequestContext().getLocale());
            } catch (Exception ex) {
                // If the message is unresolved, use the key as message
                msg = flash.get(type);
            }

            // Write the message
            writeMessage(msg);

            // And clean the session
            flash.remove(type);
        }
        return EVAL_BODY_INCLUDE;
    } catch (NoSuchMessageException ex) {
        throw new JspTagException(getNoSuchMessageExceptionDescription(ex));
    }
}

From source file:org.parancoe.web.tag.FlashTag.java

@Override
protected final int doStartTagInternal() throws JspException, IOException {
    try {/*from   w  w  w  .  ja  v  a  2  s.  c  o m*/
        Map<String, String> flash = new HashMap<String, String>();
        Map<String, String> flashRequest = (Map<String, String>) pageContext.getRequest().getAttribute("flash");
        if (flashRequest != null) {
            flash.putAll(flashRequest);
        }
        Map<String, String> flashSession = (Map<String, String>) pageContext.getSession().getAttribute("flash");
        if (flashSession != null) {
            flash.putAll(flashSession);
            // And clean the session
            flashSession.remove(type);
        }
        if (flash.get(type) != null) {
            // Resolve the message.
            MessageSource messageSource = getMessageSource();
            if (messageSource == null) {
                throw new JspTagException("No corresponding MessageSource found");
            }
            String msg;
            try {
                Object[] argumentsArray = {};
                msg = messageSource.getMessage(flash.get(type), argumentsArray,
                        getRequestContext().getLocale());
            } catch (Exception ex) {
                // If the message is unresolved, use the key as message
                msg = flash.get(type);
            }

            // Write the message
            writeMessage(msg);

        }
        return EVAL_BODY_INCLUDE;
    } catch (NoSuchMessageException ex) {
        throw new JspTagException(getNoSuchMessageExceptionDescription(ex));
    }
}

From source file:com.jidesoft.spring.richclient.docking.JideApplicationLifecycleAdvisor.java

@Override
public boolean onPreWindowClose(ApplicationWindow arg0) {
    this.canCloseWindow = true;
    if (GameHolder.hasInitializedGame()) {
        this.canCloseWindow = false;
        // show warning
        MessageSource ms = (MessageSource) Application.services().getService(MessageSource.class);
        ConfirmationDialog md = new ConfirmationDialog(
                ms.getMessage("confirmCloseAppDialog.title", new String[] {}, Locale.getDefault()),
                ms.getMessage("confirmCloseAppDialog.message", new String[] {}, Locale.getDefault())) {

            @Override/*from  ww w.j a va  2 s .c o m*/
            protected void onConfirm() {
                JideApplicationLifecycleAdvisor.this.canCloseWindow = true;
            }
        };
        md.showDialog();
    }

    return this.canCloseWindow;
}

From source file:org.gvnix.datatables.tags.SpringContextHelper.java

/**
 * Returns the message translation for a code, in the {@link Locale} of the
 * current request./*from ww w .  j  a v a  2  s . com*/
 * 
 * @param pageContext of the current request.
 * @param code to get the message
 * @return the translated message
 * @throws JspException if there is an error getting the message
 */
public String resolveMessage(PageContext pageContext, String code) throws JspException {
    RequestContext requestContext = getRequestContext(pageContext);
    if (requestContext == null) {
        throw new JspTagException("No corresponding RequestContext found");
    }
    MessageSource messageSource = requestContext.getMessageSource();
    if (messageSource == null) {
        throw new JspTagException("No corresponding MessageSource found");
    }

    String resolvedCode = ExpressionEvaluationUtils.evaluateString("code", code, pageContext);

    if (resolvedCode != null) {
        // We have no fallback text to consider.
        try {
            return messageSource.getMessage(resolvedCode, null, requestContext.getLocale());
        } catch (NoSuchMessageException e) {
            LOG.warn("Unable to get message with code " + resolvedCode, e);
        }
    }

    return resolvedCode;
}

From source file:eu.eidas.node.auth.service.tests.AUSERVICESAMLTestCase.java

/**
 * Test method for/*from   w ww.j a va2 s.  c o m*/
 * {@link AUSERVICESAML#processAuthenticationRequest(byte[], IEIDASSession, String)
 * )}. Testing null saml token. Must return and
 * {@link InternalErrorEIDASException}.
 */
@Test(expected = InternalErrorEIDASException.class)
public void testProcessAuthenticationRequestFailSAML() {
    final AUSERVICESAML auconnectorsaml = provideauservicesaml();
    final MessageSource mockMessages = mock(MessageSource.class);
    when(mockMessages.getMessage(anyString(), (Object[]) any(), (Locale) any()))
            .thenReturn(TestingConstants.ERROR_MESSAGE_CONS.toString());
    auconnectorsaml.setMessageSource(mockMessages);

    final IEIDASLogger mockLoggerBean = mock(IEIDASLogger.class);
    auconnectorsaml.setLoggerBean(mockLoggerBean);

    auconnectorsaml.setSamlEngineFactory(new EidasSamlEngineFactory());
    final IEIDASSession session = mock(IEIDASSession.class);
    auconnectorsaml.processAuthenticationRequest(null, session, TestingConstants.USER_IP_CONS.toString());
}

From source file:eu.eidas.node.auth.service.tests.AUSERVICESAMLTestCase.java

/**
 * Test method for//w  w w  .  j  a  v a  2  s.c  om
 * {@link AUSERVICESAML#processAuthenticationRequest(byte[], IEIDASSession, String)
 * )}. Testing invalid country code in class. Must return and
 * {@link EIDASServiceException}.
 */
@Test(expected = EIDASServiceException.class)
public void testProcessAuthenticationRequestFailCountry() {
    final AUSERVICESAML auconnectorsaml = provideauservicesaml();
    final byte[] saml = auconnectorsaml.getSAMLToken(SAML_TOKEN);
    final MessageSource mockMessages = mock(MessageSource.class);
    when(mockMessages.getMessage(anyString(), (Object[]) any(), (Locale) any()))
            .thenReturn(TestingConstants.ERROR_MESSAGE_CONS.toString());
    auconnectorsaml.setMessageSource(mockMessages);

    final IEIDASLogger mockLoggerBean = mock(IEIDASLogger.class);
    auconnectorsaml.setLoggerBean(mockLoggerBean);
    auconnectorsaml.setSamlEngineFactory(new EidasSamlEngineFactory());
    final IEIDASSession session = mock(IEIDASSession.class);
    auconnectorsaml.processAuthenticationRequest(saml, session, TestingConstants.USER_IP_CONS.toString());
}