@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { String settedLocale = request.getParameter(this.springInterceptor.getParamName()); if (settedLocale != null) { Boolean isValid = false; for (Language lang : Language.values()) { if (lang.getLanguageCode().equals(settedLocale)) { isValid = true; break; } } if (!isValid) return true; } return this.springInterceptor.preHandle(request, response, handler); }
/** * @return user language */ public Language getLanguage() { // add verification if language is not SPANISH. if it is SPANISH, language changed to default ENGLISH if (language.equals(Language.SPANISH)) { language = Language.ENGLISH; } return language; }
/** * Validates captcha value. * * @param userDto user container with captcha * @param pluginId plugin id * @return validation errors */ public Map<String, String> validateCaptcha(UserDto userDto, Long pluginId) { String captcha = userDto.getCaptchas().get(getFormElementId(String.valueOf(pluginId))); if (!isValid(captcha)) { ResourceBundle resourceBundle = ResourceBundle.getBundle("org.jtalks.jcommune.plugin.kaptcha.messages", userDto.getLanguage().getLocale()); String fieldName = getFormFieldName(String.valueOf(pluginId)); return new ImmutableMap.Builder<String, String>().put(fieldName, resourceBundle.getString("validation.captcha.wrong")).build(); } return new HashMap<>(); }
@RequestMapping(value = "**/language", method = RequestMethod.GET) public String saveUserLanguage(@RequestParam(value = "lang", defaultValue = "en") String lang, HttpServletResponse response, HttpServletRequest request) throws ServletException { final JCUser jcuser = userService.getCurrentUser(); final Language languageFromRequest = Language.byLocale(new Locale(lang)); if (!jcuser.isAnonymous()) { retryTemplate.execute(new RetryCallback<Void, RuntimeException>() { @Override public Void doWithRetry(RetryContext context) throws RuntimeException { userService.changeLanguage(jcuser, languageFromRequest); return null; } }); } LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); localeResolver.setLocale(request, response, languageFromRequest.getLocale()); return "redirect:" + request.getHeader("Referer"); }
/** * Handle user's successful authentication. * Updates last login time for authenticated user. * * @param request http request * @param response http response * @param authentication user's authentication * @throws ServletException forwarded from handler chain * @throws IOException forwarded from handler chain */ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { JCUser user = (JCUser) authentication.getPrincipal(); userService.updateLastLoginTime(user); logger.info("JCUser logged in: " + user.getUsername()); //apply language settings assuming CookieLocaleResolver usage String languageCode = user.getLanguage().getLanguageCode(); LocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setLocale(request, response, user.getLanguage().getLocale()); Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, languageCode); cookie.setPath("/"); response.addCookie(cookie); super.onAuthenticationSuccess(request, response, authentication); } }
/** * Returns all the languages available for the user * to choose from. * * @return array of languages for user to choose */ public Language[] getLanguagesAvailable() { return Language.values(); }
BindingResult errors; try { userDto.setLanguage(Language.byLocale(locale)); errors = authenticator.register(registerUserDto); } catch (NoConnectionException e) {
/** * Sends a password recovery message for the user. * This method does not generate new password, just sends a message. * * @param user a person we will send a mail * @param newPassword new user password to be placed in an email * @throws MailingFailedException when mailing failed */ public void sendPasswordRecoveryMail(JCUser user, String newPassword) throws MailingFailedException { String urlSuffix = "/login"; String url = this.getDeploymentRootUrl() + urlSuffix; String name = user.getUsername(); Locale locale = user.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(NAME, name); model.put("newPassword", newPassword); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); this.sendEmail(user.getEmail(), messageSource.getMessage("passwordRecovery.subject", new Object[]{}, locale), model, "passwordRecovery.vm"); LOGGER.info("Password recovery email sent for {}", name); }
/** * Returns all the languages available for the user * to choose from. * * @return array of languages for user to choose */ public Language[] getLanguagesAvailable() { return Language.values(); }
userDto.setLanguage(Language.byLocale(locale)); errors = authenticator.register(registerUserDto); } catch (NoConnectionException e) {
/** * Send email notification to user when he was mentioned in forum. * Email notification will be sent only when notifications are enabled * in forum, otherwise nothing will happen. * * @param recipient mentioned user who will receive notification * @param postId id of post where user was mentioned */ public void sendUserMentionedNotification(JCUser recipient, long postId) { String urlSuffix = "/posts/" + postId; String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(NAME, recipient.getUsername()); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); try { this.sendEmail(recipient.getEmail(), messageSource.getMessage("userMentioning.subject", new Object[]{}, locale), model, "userMentioning.vm"); } catch (MailingFailedException e) { LOGGER.error("Failed to sent activation mail for user: " + recipient.getUsername()); } }
/** * @param locale locale to select language constant for * @return Language constant matches the locale given. If nothing matches exception is to be raised */ public static Language byLocale(Locale locale){ for (Language language : values()){ if (language.locale.equals(locale)){ return language; } } throw new IllegalArgumentException("Locale does not match any known language"); }
/** * Sends notification to user about received private message. * * @param recipient a person to be notified about received private message by email * @param pm private message itself */ public void sendReceivedPrivateMessageNotification(JCUser recipient, PrivateMessage pm) { try { String urlSuffix = "/pm/inbox/" + pm.getId(); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put("recipient", recipient); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); this.sendEmail(recipient.getEmail(), messageSource.getMessage("receivedPrivateMessageNotification.subject", new Object[]{}, locale), model, "receivedPrivateMessageNotification.vm"); } catch (MailingFailedException e) { LOGGER.error(String.format(LOG_TEMPLATE, "Private message", pm.getId(), recipient.getUsername())); } }
/** * Resolves user locale. If user is logged in locale will be read from database. If user is anonymous locale will be * resolved form request and cookies using standard {@link CookieLocaleResolver} mechanism * * @param request HttpServletRequest * @return user locale */ @Override public Locale resolveLocale(HttpServletRequest request) { Locale locale = (Locale)request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME); if (locale != null) { return locale; } JCUser currentUser = userReader.getCurrentUser(); if (currentUser.isAnonymous()) { locale = super.resolveLocale(request); } else { locale = currentUser.getLanguage().getLocale(); } request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale); return locale; }
/** * Sends email with a hyperlink to activate user account. * * @param recipient user to send activation mail to */ public void sendAccountActivationMail(JCUser recipient) { try { String urlSuffix = "/user/activate/" + recipient.getUuid(); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(NAME, recipient.getUsername()); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); this.sendEmail(recipient.getEmail(), messageSource.getMessage("accountActivation.subject", new Object[]{}, locale), model, "accountActivation.vm"); } catch (MailingFailedException e) { LOGGER.error("Failed to sent activation mail for user: " + recipient.getUsername()); } }
/** * Sends update notification to user specified if * {@link SubscriptionAwareEntity} was updated, e.g. when some new * information were added to the subscribed entity. This method won't check if user * is subscribed to the particular notification or not. * * @param recipient a person to be notified about updates by email * @param entity changed subscribed entity. */ public void sendUpdatesOnSubscription(JCUser recipient, SubscriptionAwareEntity entity) { try { String urlSuffix = entity.getUrlSuffix(); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(LINK_UNSUBSCRIBE, this.getDeploymentRootUrl() + entity.getUnsubscribeLinkForSubscribersOf(entity.getClass())); sendEmailOnForumUpdates(recipient, model, locale, (Entity) entity, "subscriptionNotification.subject", "subscriptionNotification.vm"); } catch (MailingFailedException e) { LOGGER.error(String.format(LOG_TEMPLATE, entity.getClass().getCanonicalName(), ((Entity) entity).getId(), recipient.getUsername())); } }
/** * Set mail about removing topic. * * @param recipient Recipient for which send notification * @param topic Current topic * @param curUser User that removed the topic */ public void sendRemovingTopicMail(JCUser recipient, Topic topic, String curUser) { Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(USER, recipient); model.put(RECIPIENT_LOCALE, locale); model.put(CUR_USER, curUser); //Topic not exist more and user not subscribed to branch, so simply redirect to branch model.put(LINK_UNSUBSCRIBE, this.getDeploymentRootUrl() + "/branches/" + topic.getBranch().getId()); model.put(TOPIC, topic); try { String subjectTemplate = REMOVE_TOPIC_SUBJECT_TEMPLATE; String messageBodyTemplate = REMOVE_TOPIC_MESSAGE_BODY_TEMPLATE; if (topic.isCodeReview()) { subjectTemplate = REMOVE_CODE_REVIEW_SUBJECT_TEMPLATE; messageBodyTemplate = REMOVE_CODE_REVIEW_MESSAGE_BODY_TEMPLATE; } String subject = messageSource.getMessage(subjectTemplate, new Object[]{}, locale); this.sendEmail(recipient.getEmail(), subject, model, messageBodyTemplate); } catch (MailingFailedException e) { LOGGER.error("Failed to sent mail about removing topic or code review for user: " + recipient.getUsername()); } }
/** * Sends email to topic starter that his or her topic was moved * * @param recipient user to send notification * @param topic relocated topic * @param curUser User that moved topic */ public <T extends SubscriptionAwareEntity> void sendTopicMovedMail( JCUser recipient, Topic topic, String curUser, Class<T> subsсriptionTargetClass) { String urlSuffix = getTopicUrlSuffix(topic); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(NAME, recipient.getUsername()); model.put(CUR_USER, curUser); model.put(LINK, url); model.put(LINK_UNSUBSCRIBE, this.getDeploymentRootUrl() + topic.getUnsubscribeLinkForSubscribersOf(subsсriptionTargetClass)); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); try { this.sendEmail(recipient.getEmail(), messageSource.getMessage("moveTopic.subject", new Object[]{}, locale), model, "moveTopic.vm"); } catch (MailingFailedException e) { LOGGER.error("Failed to sent activation mail for user: " + recipient.getUsername()); } }
localeResolver.setLocale(request, response, userService.getCurrentUser().getLanguage().getLocale()); return new ModelAndView("redirect:" + referer); } else {
/** * Send email about new topic in the subscribed branch. * * @param subscriber recipient * @param topic newly created topic */ void sendTopicCreationMail(JCUser subscriber, Topic topic) { try { String urlSuffix = getTopicUrlSuffix(topic); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = subscriber.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(LINK, url); model.put(LINK_UNSUBSCRIBE, this.getDeploymentRootUrl() + topic.getBranch().getUnsubscribeLinkForSubscribersOf(Branch.class)); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); sendEmailOnForumUpdates(subscriber, model, locale, topic.getBranch(), "subscriptionNotification.subject", "branchSubscriptionNotification.vm"); } catch (MailingFailedException e) { LOGGER.error("Failed to sent mail about creation topic for user: " + subscriber.getUsername()); } }