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:com.salesmanager.core.service.ws.impl.SalesManagerInvoiceWSImpl.java

/**
 * createInvoice is the WebMethod to create an invoice, this
 * method is accessed by the webservice client supplying the credentails and invoice
 * as arguments.//w w w  .j  a va2 s  .  c o m
 * @param WebServiceCredentials credentials
 * @param Invoice invoice
 * @return CreateInvoiceWebServiceResponse
 */
@WebMethod
public @WebResult CreateInvoiceWebServiceResponse createInvoice(
        @WebParam(name = "credentials") WebServiceCredentials credentials,
        @WebParam(name = "invoice") Invoice invoice) {
    MessageSource messageSource = (MessageSource) SpringUtil.getBean("messageSource");
    Locale locale = LocaleUtil.getLocale(invoice.getLanguage());

    CreateInvoiceWebServiceResponse response = new CreateInvoiceWebServiceResponse();
    try {
        //Validate if the user is authorized to do a create invoice or not.
        WebServiceUtils.validateCredentials(locale, credentials, log);

        //If Business validations fail then return.
        if (!isValidInvoice(credentials.getMerchantId(), invoice, messageSource, locale, response)) {
            return response;
        }
        //Process invoice
        response = processNewInvoice(credentials.getMerchantId(), invoice, messageSource, locale, response);
        //set success status and success message.
        response.setStatus(1);

    } catch (Exception e) {
        if (e instanceof ServiceException) {
            //If instanceof ServiceException then there
            //is a user readable message set so return this message with response.
            String msg[] = { ((ServiceException) e).getMessage() };
            response.setMessages(msg);
            response.setStatus(0);
        } else {

            log.error("Exception occurred while creating Invoice", e);
            response.setMessages(new String[] { messageSource.getMessage("errors.technical", null, locale) });
            response.setStatus(0);
        }
    }
    return response;
}

From source file:com.salesmanager.core.service.ws.utils.WebServiceUtils.java

/**
 * Validates web service credentials//from  www.  j  ava  2s  .  com
 * @param locale
 * @param credentials
 * @throws ServiceException
 */
public static void validateCredentials(Locale locale, WebServiceCredentials credentials, Logger log)
        throws ServiceException {
    MessageSource messageSource = (MessageSource) SpringUtil.getBean("messageSource");

    try {

        int merchantId = credentials.getMerchantId();

        String k = EncryptionUtil.generatekey(String.valueOf(merchantId));
        String apiKeyGen = EncryptionUtil.encrypt(k, String.valueOf(merchantId));

        if (StringUtils.isBlank(apiKeyGen) || apiKeyGen.length() < 16) {
            log.error("Problem with API KEY GENERATION " + apiKeyGen);
            throw new ServiceException(messageSource.getMessage("errors.technical", null, locale));
        }

        String apiKey = credentials.getApiKey();

        if (StringUtils.isBlank(apiKey)) {
            throw new ServiceException(
                    messageSource.getMessage("messages.error.ws.invalidcredentials", null, locale));
        }

        if (!apiKeyGen.equals(apiKey)) {
            throw new ServiceException(
                    messageSource.getMessage("messages.error.ws.invalidcredentials", null, locale));
        }

    } catch (Exception e) {

        if (e instanceof ServiceException) {
            throw (ServiceException) e;
        }

        log.error(e);
        throw new ServiceException(messageSource.getMessage("errors.technical", null, locale));
    }

}

From source file:com.salesmanager.core.service.ws.utils.WebServiceUtils.java

public static void setStatusMsg(MessageSource messageSource, Locale locale, WebServiceResponse response,
        String messageKey, Object args[], int status) {
    response.setMessages(new String[] { messageSource.getMessage(messageKey, args, locale) });
    response.setStatus(status);/*from  www. ja va2s.  c o  m*/
}

From source file:gr.abiss.calipso.util.ItemUtils.java

public static String fmt(String key, Object[] args, MessageSource messageSource, Locale locale) {
    try {/* w  w  w.ja  va2s.c  o m*/
        return messageSource.getMessage(key, args, locale);
    } catch (Exception e) {
        return "???item_view." + key + "???";
    }
}

From source file:grails.web.servlet.mvc.GrailsParameterMap.java

private String lookupFormat(String name) {
    String format = CACHED_DATE_FORMATS.get(name);
    if (format == null) {
        GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
        if (webRequest != null) {
            MessageSource messageSource = webRequest.getApplicationContext();
            if (messageSource != null) {
                format = messageSource.getMessage("date." + name + ".format", EMPTY_ARGS,
                        webRequest.getLocale());
                if (format != null) {
                    CACHED_DATE_FORMATS.put(name, format);
                }/*from  w  w  w.j  av a2  s .  c  o m*/
            }
        }
    }
    return format;
}

From source file:jp.co.ctc_g.jfw.core.resource.Rs.java

/**
 * ????????????/*  ww  w. j  a va2 s.c o m*/
 * ????????defaultValue????????</strong>
 *
 * @param key
 *            ??
 * @param defaultValue
 *            ?
 * @return ??
 */
public static String find(String key, String defaultValue) {
    try {
        MessageSource source = MessageSourceLocator.get();
        if (Strings.isEmpty(key))
            return "";
        Locale userLocale = LocaleContextHolder.getLocale();
        Locale currentLocale = userLocale != null ? userLocale : Locale.getDefault();
        String value = source.getMessage(key, null, currentLocale);
        if (Strings.isEmpty(value)) {
            return key;
        } else {
            return value;
        }
    } catch (NoSuchMessageException e) {
        return defaultValue;
    }
}

From source file:jp.co.ctc_g.jfw.core.resource.Rs.java

/**
 * ????????????/* w  w w.j  av  a 2s  .  co m*/
 * ????????<strong>????????</strong>
 *
 * @param key
 *            ??
 * @param locale
 *            ??
 * @return ??
 */
public static String find(String key, Locale locale) {
    try {
        MessageSource source = MessageSourceLocator.get();

        if (Strings.isEmpty(key))
            return "";

        Locale currentLocale = locale;
        if (currentLocale == null) {
            Locale userLocale = LocaleContextHolder.getLocale();
            currentLocale = userLocale != null ? userLocale : Locale.getDefault();
        }
        String value = source.getMessage(key, null, currentLocale);
        if (Strings.isEmpty(value)) {
            return key;
        } else {
            return value;
        }
    } catch (NoSuchMessageException e) {
        if (L.isDebugEnabled()) {
            String message = Strings.substitute(R.getString("I-RESOURCE#0001"), Maps.hash("key", key));
            L.debug(message);
        }
        return key;
    }
}

From source file:org.broadleafcommerce.openadmin.server.service.persistence.validation.FieldLengthValidator.java

@Override
public PropertyValidationResult validate(Entity entity, Serializable instance,
        Map<String, FieldMetadata> entityFieldMetadata, BasicFieldMetadata propertyMetadata,
        String propertyName, String value) {
    boolean valid = true;
    String errorMessage = "";
    if (propertyMetadata.getLength() != null) {
        valid = StringUtils.length(value) <= propertyMetadata.getLength();
    }/*  w  w w.  j av a 2  s.  c  om*/

    if (!valid) {
        BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
        MessageSource messages = context.getMessageSource();
        errorMessage = messages.getMessage("fieldLengthValidationFailure",
                new Object[] { propertyMetadata.getLength(), StringUtils.length(value) },
                context.getJavaLocale());
    }

    return new PropertyValidationResult(valid, errorMessage);
}

From source file:org.broadleafcommerce.openadmin.server.service.persistence.validation.RequiredIfPropertyValidator.java

@Override
public PropertyValidationResult validate(Entity entity, Serializable instance,
        Map<String, FieldMetadata> entityFieldMetadata, Map<String, String> validationConfiguration,
        BasicFieldMetadata propertyMetadata, String propertyName, String value) {
    String errorMessage = "";

    String compareFieldName = lookupCompareFieldName(propertyName, validationConfiguration);
    String compareFieldValue = validationConfiguration.get("compareFieldValue");
    String compareFieldRegEx = validationConfiguration.get("compareFieldRegEx");
    Property compareFieldProperty = null;

    boolean valid = true;
    if (StringUtils.isEmpty(value)) {
        compareFieldProperty = entity.getPMap().get(compareFieldName);

        if (compareFieldProperty != null) {
            if (compareFieldValue != null) {
                valid = !compareFieldValue.equals(compareFieldProperty.getValue());
            } else if (compareFieldRegEx != null) {
                String expression = validationConfiguration.get("compareFieldRegEx");
                valid = !compareFieldProperty.getValue().matches(expression);
            }/*from   w  w  w  . j a v  a  2s.  c  om*/

        }
    }

    if (!valid) {
        BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
        MessageSource messages = context.getMessageSource();

        FieldMetadata fmd = entityFieldMetadata.get(compareFieldName);
        String fieldName = messages.getMessage(fmd.getFriendlyName(), null, context.getJavaLocale());
        errorMessage = messages.getMessage("requiredIfValidationFailure",
                new Object[] { fieldName, compareFieldProperty.getValue() }, context.getJavaLocale());
    }

    return new PropertyValidationResult(valid, errorMessage);
}

From source file:org.codehaus.groovy.grails.support.MockApplicationContext.java

public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
    MessageSource messageSource = (MessageSource) getBean("messageSource");
    if (messageSource == null) {
        throw new BeanCreationException("No bean [messageSource] found in MockApplicationContext");
    }/* ww w  . j  a va 2 s.  c o  m*/
    return messageSource.getMessage(code, args, locale);
}