Example usage for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentContextPath

List of usage examples for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentContextPath

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentContextPath.

Prototype

public static ServletUriComponentsBuilder fromCurrentContextPath() 

Source Link

Document

Same as #fromContextPath(HttpServletRequest) except the request is obtained through RequestContextHolder .

Usage

From source file:org.cloudfoundry.identity.uaa.login.EmailResetPasswordService.java

private String getResetUnavailableEmailHtml(String email) {
    String hostname = ServletUriComponentsBuilder.fromCurrentContextPath().build().getHost();

    final Context ctx = new Context();
    ctx.setVariable("serviceName", brand.equals("pivotal") ? "Pivotal " : "");
    ctx.setVariable("email", email);
    ctx.setVariable("hostname", hostname);
    return templateEngine.process("reset_password_unavailable", ctx);
}

From source file:org.wallride.service.UserService.java

@CacheEvict(value = WallRideCacheConfiguration.USER_CACHE, allEntries = true)
public User updatePassword(PasswordUpdateRequest request, PasswordResetToken passwordResetToken) {
    User user = userRepository.findOneForUpdateById(request.getUserId());
    if (user == null) {
        throw new IllegalArgumentException("The user does not exist");
    }/*w  w  w. j a  v a  2  s . c o  m*/
    PasswordEncoder passwordEncoder = new StandardPasswordEncoder();
    user.setLoginPassword(passwordEncoder.encode(request.getPassword()));
    user.setUpdatedAt(LocalDateTime.now());
    user.setUpdatedBy(passwordResetToken.getUser().toString());
    user = userRepository.saveAndFlush(user);

    passwordResetTokenRepository.delete(passwordResetToken);

    try {
        Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
        String blogTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage());

        ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
        if (blog.isMultiLanguage()) {
            builder.path("/{language}");
        }
        builder.path("/login");

        Map<String, Object> urlVariables = new LinkedHashMap<>();
        urlVariables.put("language", request.getLanguage());
        urlVariables.put("token", passwordResetToken.getToken());
        String loginLink = builder.buildAndExpand(urlVariables).toString();

        Context ctx = new Context(LocaleContextHolder.getLocale());
        ctx.setVariable("passwordResetToken", passwordResetToken);
        ctx.setVariable("resetLink", loginLink);

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart
        message.setSubject(MessageFormat.format(
                messageSourceAccessor.getMessage("PasswordChangedSubject", LocaleContextHolder.getLocale()),
                blogTitle));
        message.setFrom(mailProperties.getProperties().get("mail.from"));
        message.setTo(passwordResetToken.getEmail());

        String htmlContent = templateEngine.process("password-changed", ctx);
        message.setText(htmlContent, true); // true = isHtml

        mailSender.send(mimeMessage);
    } catch (MessagingException e) {
        throw new ServiceException(e);
    }

    return user;
}

From source file:com.example.bot.spring.KitchenSinkController.java

private static String createUri(String path) {
    return ServletUriComponentsBuilder.fromCurrentContextPath().path(path).build().toUriString();
}

From source file:org.wallride.service.UserService.java

@CacheEvict(value = WallRideCacheConfiguration.USER_CACHE, allEntries = true)
public List<UserInvitation> inviteUsers(UserInvitationCreateRequest form, BindingResult result,
        AuthorizedUser authorizedUser) throws MessagingException {
    String[] recipients = StringUtils.commaDelimitedListToStringArray(form.getInvitees());

    LocalDateTime now = LocalDateTime.now();

    List<UserInvitation> invitations = new ArrayList<>();
    for (String recipient : recipients) {
        UserInvitation invitation = new UserInvitation();
        invitation.setEmail(recipient);/*from  w w  w .ja  v  a  2  s .c om*/
        invitation.setMessage(form.getMessage());
        invitation.setExpiredAt(now.plusHours(72));
        invitation.setCreatedAt(now);
        invitation.setCreatedBy(authorizedUser.toString());
        invitation.setUpdatedAt(now);
        invitation.setUpdatedBy(authorizedUser.toString());
        invitation = userInvitationRepository.saveAndFlush(invitation);
        invitations.add(invitation);
    }

    Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
    for (UserInvitation invitation : invitations) {
        String websiteTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage());
        String signupLink = ServletUriComponentsBuilder.fromCurrentContextPath().path("/_admin/signup")
                .queryParam("token", invitation.getToken()).buildAndExpand().toString();

        final Context ctx = new Context(LocaleContextHolder.getLocale());
        ctx.setVariable("websiteTitle", websiteTitle);
        ctx.setVariable("authorizedUser", authorizedUser);
        ctx.setVariable("signupLink", signupLink);
        ctx.setVariable("invitation", invitation);

        final MimeMessage mimeMessage = mailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart
        message.setSubject(MessageFormat.format(
                messageSourceAccessor.getMessage("InvitationMessageTitle", LocaleContextHolder.getLocale()),
                authorizedUser.toString(), websiteTitle));
        message.setFrom(authorizedUser.getEmail());
        message.setTo(invitation.getEmail());

        final String htmlContent = templateEngine.process("user-invite", ctx);
        message.setText(htmlContent, true); // true = isHtml

        mailSender.send(mimeMessage);
    }

    return invitations;
}

From source file:org.wallride.service.UserService.java

@CacheEvict(value = WallRideCacheConfiguration.USER_CACHE, allEntries = true)
public UserInvitation inviteAgain(UserInvitationResendRequest form, BindingResult result,
        AuthorizedUser authorizedUser) throws MessagingException {
    LocalDateTime now = LocalDateTime.now();

    UserInvitation invitation = userInvitationRepository.findOneForUpdateByToken(form.getToken());
    invitation.setExpiredAt(now.plusHours(72));
    invitation.setUpdatedAt(now);/*from   ww w  .j av a  2s.c  om*/
    invitation.setUpdatedBy(authorizedUser.toString());
    invitation = userInvitationRepository.saveAndFlush(invitation);

    Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
    String websiteTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage());
    String signupLink = ServletUriComponentsBuilder.fromCurrentContextPath().path("/_admin/signup")
            .queryParam("token", invitation.getToken()).buildAndExpand().toString();

    final Context ctx = new Context(LocaleContextHolder.getLocale());
    ctx.setVariable("websiteTitle", websiteTitle);
    ctx.setVariable("authorizedUser", authorizedUser);
    ctx.setVariable("signupLink", signupLink);
    ctx.setVariable("invitation", invitation);

    final MimeMessage mimeMessage = mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart
    message.setSubject(MessageFormat.format(
            messageSourceAccessor.getMessage("InvitationMessageTitle", LocaleContextHolder.getLocale()),
            authorizedUser.toString(), websiteTitle));
    message.setFrom(authorizedUser.getEmail());
    message.setTo(invitation.getEmail());

    final String htmlContent = templateEngine.process("user-invite", ctx);
    message.setText(htmlContent, true); // true = isHtml

    mailSender.send(mimeMessage);

    return invitation;
}