Example usage for org.springframework.context.support MessageSourceAccessor MessageSourceAccessor

List of usage examples for org.springframework.context.support MessageSourceAccessor MessageSourceAccessor

Introduction

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

Prototype

public MessageSourceAccessor(MessageSource messageSource, Locale defaultLocale) 

Source Link

Document

Create a new MessageSourceAccessor, using the given default locale.

Usage

From source file:org.openmrs.contrib.metadatarepository.webapp.controller.PasswordHintController.java

@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request) throws Exception {
    log.debug("entering 'handleRequest' method...");

    String username = request.getParameter("username");
    MessageSourceAccessor text = new MessageSourceAccessor(messageSource, request.getLocale());

    // ensure that the username has been sent
    if (username == null) {
        log.warn("Username not specified, notifying user that it's a required field.");
        request.setAttribute("error", text.getMessage("errors.required", text.getMessage("user.username")));
        return new ModelAndView("login");
    }/*from w  w  w.  j a v a  2 s  . c o  m*/

    log.debug("Processing Password Hint...");

    // look up the user's information
    try {
        User user = userManager.getUserByUsername(username);

        StringBuffer msg = new StringBuffer();
        msg.append("Your password hint is: ").append(user.getPasswordHint());
        msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(request));

        message.setTo(user.getEmail());
        String subject = '[' + text.getMessage("webapp.name") + "] " + text.getMessage("user.passwordHint");
        message.setSubject(subject);
        message.setText(msg.toString());
        mailEngine.send(message);

        saveMessage(request,
                text.getMessage("login.passwordHint.sent", new Object[] { username, user.getEmail() }));
    } catch (UsernameNotFoundException e) {
        log.warn(e.getMessage());
        saveError(request, text.getMessage("login.passwordHint.error", new Object[] { username }));
    } catch (MailException me) {
        log.warn(me.getMessage());
        saveError(request, me.getCause().getLocalizedMessage());
    }

    return new ModelAndView(new RedirectView(request.getContextPath()));
}

From source file:ca.flop.jpublish.i18n.SpringI18nSupport.java

/**
 * Execute the action using the given context.
 *
 * @param context       The current context
 * @param configuration The configuration
 * @throws Exception Any error//from  w  ww .ja  va  2  s. c o m
 */
public void execute(JPublishContext context, Configuration configuration) throws Exception {
    HttpServletRequest request = (HttpServletRequest) context.get("request");
    HttpServletResponse response = (HttpServletResponse) context.get("response");
    Page page = ((Page) context.get("page"));

    if (page != null) {
        Locale locale = page.getLocale();

        ApplicationContext applicationContext = getApplicationContext(context);
        context.put(SPRING, applicationContext);
        HandlerInterceptor localeChangeInterceptor = (HandlerInterceptor) applicationContext
                .getBean("localeChangeInterceptor");

        if (localeChangeInterceptor != null) {
            try {
                LocaleResolver localeResolver = (LocaleResolver) applicationContext.getBean("localeResolver");
                if (localeResolver != null) {
                    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, localeResolver);
                    localeChangeInterceptor.preHandle(request, response, null);
                    locale = localeResolver.resolveLocale(request);
                    MessageSource messageSource = (MessageSource) applicationContext.getBean("messageSource");
                    if (messageSource != null) {
                        context.put(I18N, new MessageSourceAccessor(messageSource, locale));
                    } else {
                        log.error("There is no 'messageSource' defined in your application context."
                                + " Please define one.");
                    }
                } else {
                    log.error("please define a 'localeResolver' bean in your Application context");
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("Cannot handle the locale change event");
            }
        }
        page.setLocale(locale);
    }
}

From source file:alpha.portal.webapp.controller.PasswordHintController.java

/**
 * Handle request./*  w  w w.  j ava  2s  .c  o m*/
 * 
 * @param request
 *            the request
 * @return the model and view
 * @throws Exception
 *             the exception
 */
@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(final HttpServletRequest request) throws Exception {
    this.log.debug("entering 'handleRequest' method...");

    final String username = request.getParameter("username");
    final MessageSourceAccessor text = new MessageSourceAccessor(this.messageSource, request.getLocale());

    // ensure that the username has been sent
    if (username == null) {
        this.log.warn("Username not specified, notifying user that it's a required field.");
        request.setAttribute("error", text.getMessage("errors.required", text.getMessage("user.username")));
        return new ModelAndView("login");
    }

    this.log.debug("Processing Password Hint...");

    // look up the user's information
    try {
        final User user = this.userManager.getUserByUsername(username);

        final StringBuffer msg = new StringBuffer();
        msg.append("Your password hint is: ").append(user.getPasswordHint());
        msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(request));

        this.message.setTo(user.getEmail());
        final String subject = '[' + text.getMessage("webapp.name") + "] "
                + text.getMessage("user.passwordHint");
        this.message.setSubject(subject);
        this.message.setText(msg.toString());
        this.mailEngine.send(this.message);

        this.saveMessage(request,
                text.getMessage("login.passwordHint.sent", new Object[] { username, user.getEmail() }));
    } catch (final UsernameNotFoundException e) {
        this.log.warn(e.getMessage());
        this.saveError(request, text.getMessage("login.passwordHint.error", new Object[] { username }));
    } catch (final MailException me) {
        this.log.warn(me.getMessage());
        this.saveError(request, me.getCause().getLocalizedMessage());
    }

    return new ModelAndView(new RedirectView(request.getContextPath()));
}

From source file:org.lanark.jsr303js.taglib.JSR303JSValidateTag.java

public int doEndTag() throws JspException {
    try {//  ww w .  j  a v  a 2  s .  c  o  m
        List<ValidationMetaData> rules = new ArrayList<ValidationMetaData>();
        if (commandName != null) {
            rules.addAll(getValidationMetaDataForCommand());
        }
        String bodyString = null;
        if (bodyContent != null) {
            // body can be a JSON object, specifying date formats, or other extra configuration info
            bodyString = FileCopyUtils.copyToString(bodyContent.getReader());
            bodyString = bodyString.trim().replaceAll("\\s{2,}", " ");
        }

        JspWriter out = pageContext.getOut();
        out.write("<script type=\"text/javascript\" id=\"");
        out.write(commandName + "JSR303JSValidator");
        out.write("\">");
        generator.generateJavaScript(out, commandName, true, bodyString, rules, new MessageSourceAccessor(
                getRequestContext().getWebApplicationContext(), getRequestContext().getLocale()));
        out.write("</script>");
        return EVAL_PAGE;
    } catch (IOException e) {
        throw new JspException("Could not write validation rules", e);
    }
}

From source file:kr.co.skysoft.framework.taglib.JSR303JSValidateTag.java

public int doEndTag() throws JspException {
    try {/*  ww w  .ja v  a  2  s.c  om*/
        List<ValidationMetaData> rules = new ArrayList<ValidationMetaData>();
        if (commandName != null) {
            rules.addAll(getValidationMetaDataForCommand());
            this.addValidVOs(rules);

        }
        String bodyString = null;
        if (bodyContent != null) {
            // body can be a JSON object, specifying date formats, or other extra configuration info
            bodyString = FileCopyUtils.copyToString(bodyContent.getReader());
            bodyString = bodyString.trim().replaceAll("\\s{2,}", " ");
        }

        JspWriter out = pageContext.getOut();
        out.write("<script type=\"text/javascript\" id=\"");
        out.write(commandName + "JSR303JSValidator");
        out.write("\">");
        generator.generateJavaScript(out, commandName, true, bodyString, rules, new MessageSourceAccessor(
                getRequestContext().getWebApplicationContext(), getRequestContext().getLocale()));
        out.write("</script>");
        return EVAL_PAGE;
    } catch (IOException e) {
        throw new JspException("Could not write validation rules", e);
    }
}

From source file:org.springmodules.validation.valang.javascript.taglib.ValangValidateTag.java

public int doEndTag() throws JspException {
    try {/*from   ww  w.  j a  v  a 2 s.c o m*/
        Collection rules = new ArrayList();
        if (commandName != null) {
            rules.addAll(getRulesForCommand());
        }
        if (bodyContent != null) {
            rules.addAll(parseRulesFromBodyContent());
        }
        if (rules.size() == 0) {
            throw new JspException("no valang validation rules were found");
        }

        JspWriter out = pageContext.getOut();
        out.write("<script type=\"text/javascript\" id=\"");
        out.write(commandName + "ValangValidator");
        out.write("\">");
        translator.writeJavaScriptValangValidator(out, commandName, true, rules, new MessageSourceAccessor(
                getRequestContext().getWebApplicationContext(), getRequestContext().getLocale()));
        out.write("</script>");
        return EVAL_PAGE;
    } catch (IOException e) {
        throw new JspException("Could not write validation rules", e);
    }
}

From source file:org.springmodules.validation.bean.annotation.javascript.taglib.BeanAnnotationValidateTag.java

public int doEndTag() throws JspException {
    if (cotvc == null) {
        findConverter();/*w w w.j  a v  a  2s. c  o m*/
    }
    try {
        if (commandObj == null) { // favour commandObj over command name
            commandObj = this.pageContext.getAttribute(commandName, PageContext.REQUEST_SCOPE);
            if (commandName == null || commandObj == null) {
                logger.error("Command object not found");
                return EVAL_PAGE;
            }
        }
        JspWriter out = pageContext.getOut();
        Locale locale = getRequestContext().getLocale();
        WebApplicationContext webApplicationContext = getRequestContext().getWebApplicationContext();
        MessageSourceAccessor messages = new MessageSourceAccessor(webApplicationContext, locale);

        out.write("<script type=\"text/javascript\" id=\"" + commandName + "ValangValidator\">\n");
        cotvc.writeJS(commandName, commandObj, globalVar, validateOnSubmit, out, messages);
        out.write("\n</script>");

        return EVAL_PAGE;

    } catch (IOException e) {
        throw new JspException("Could not write validation rules", e);
    }
}