Java Code Examples for org.springframework.web.servlet.support.ServletUriComponentsBuilder#fromCurrentContextPath()

The following examples show how to use org.springframework.web.servlet.support.ServletUriComponentsBuilder#fromCurrentContextPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RssFeedView.java    From wallride with Apache License 2.0 6 votes vote down vote up
private String link(Article article) {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	Map<String, Object> params = new HashMap<>();

	Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
	if (blog.getLanguages().size() > 1) {
		builder.path("/{language}");
		params.put("language", LocaleContextHolder.getLocale().getLanguage());
	}
	builder.path("/{year}/{month}/{day}/{code}");
	params.put("year", String.format("%04d", article.getDate().getYear()));
	params.put("month", String.format("%02d", article.getDate().getMonth().getValue()));
	params.put("day", String.format("%02d", article.getDate().getDayOfMonth()));
	params.put("code", article.getCode());
	return builder.buildAndExpand(params).encode().toUriString();
}
 
Example 2
Source File: AtomFeedView.java    From wallride with Apache License 2.0 6 votes vote down vote up
protected void buildFeedMetadata(
			Map<String, Object> model,
			Feed feed,
			HttpServletRequest request) {
		Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
		String language = LocaleContextHolder.getLocale().getLanguage();

		feed.setTitle(blog.getTitle(language));
		Content info = new Content();
		info.setValue(blog.getTitle(language));
		feed.setInfo(info);

		ArrayList<Link> links = new ArrayList<>();
		Link link = new Link();
		UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
		link.setHref(builder.buildAndExpand().toUriString());
		links.add(link);
		feed.setOtherLinks(links);
//		feed.setIcon("http://" + settings.getAsString(Setting.Key.SITE_URL) + "resources/default/img/favicon.ico");
	}
 
Example 3
Source File: AtomFeedView.java    From wallride with Apache License 2.0 6 votes vote down vote up
private String link(Article article) {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	Map<String, Object> params = new HashMap<>();

	Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
	if (blog.getLanguages().size() > 1) {
		builder.path("/{language}");
		params.put("language", LocaleContextHolder.getLocale().getLanguage());
	}
	builder.path("/{year}/{month}/{day}/{code}");
	params.put("year", String.format("%04d", article.getDate().getYear()));
	params.put("month", String.format("%02d", article.getDate().getMonth().getValue()));
	params.put("day", String.format("%02d", article.getDate().getDayOfMonth()));
	params.put("code", article.getCode());
	return builder.buildAndExpand(params).encode().toUriString();
}
 
Example 4
Source File: GenApiService.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
private ResponseEntity<ResponseCode> getResponse(String filename, String friendlyName) {
    String host = System.getenv("GENERATOR_HOST");

    UriComponentsBuilder uriBuilder;
    if (!StringUtils.isBlank(host)) {
        uriBuilder = UriComponentsBuilder.fromUriString(host);
    } else {
        uriBuilder = ServletUriComponentsBuilder.fromCurrentContextPath();
    }

    if (filename != null) {
        String code = UUID.randomUUID().toString();
        Generated g = new Generated();
        g.setFilename(filename);
        g.setFriendlyName(friendlyName);
        fileMap.put(code, g);
        System.out.println(code + ", " + filename);
        String link = uriBuilder.path("/api/gen/download/").path(code).toUriString();
        return ResponseEntity.ok().body(new ResponseCode(code, link));
    } else {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
 
Example 5
Source File: UiApplication.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String upload() {
	ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	return "<html><body>" + "<form method='post' enctype='multipart/form-data' action='"
			+ builder.path("/upload").build().toUriString() + "'>"
			+ "File to upload: <input type='file' name='file'>" + "<input type='submit' value='Upload'></form>"
			+ "</body></html>";
}
 
Example 6
Source File: FeatureServiceImpl.java    From pazuzu-registry with MIT License 5 votes vote down vote up
@RolesAllowed({Roles.USER})
public ResponseEntity<Feature> featuresPost(Feature feature) {
    if (feature.getMeta() == null)
        feature.setMeta(new FeatureMeta());
    ServletUriComponentsBuilder servletUriComponentsBuilder = ServletUriComponentsBuilder.fromCurrentContextPath();
    Feature newFeature = featureService.createFeature(
            feature.getMeta().getName(), feature.getMeta().getDescription(), getAuthenticatedUserName(),
            feature.getSnippet(), feature.getTestSnippet(), feature.getMeta().getDependencies(), FeatureConverter::asDto);
    URI uri = servletUriComponentsBuilder.path("/api/features/{featureName}").buildAndExpand(newFeature.getMeta().getName()).toUri();
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
    responseHeaders.setLocation(uri);
    return new ResponseEntity<>(newFeature, responseHeaders, HttpStatus.CREATED);
}
 
Example 7
Source File: RssFeedView.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Override
protected void buildFeedMetadata(
		Map<String, Object> model, 
		Channel feed,
		HttpServletRequest request) {
	Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
	String language = LocaleContextHolder.getLocale().getLanguage();

	feed.setTitle(blog.getTitle(language));
	feed.setDescription(blog.getTitle(language));

	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	feed.setLink(builder.buildAndExpand().toUriString());
}
 
Example 8
Source File: Posts.java    From wallride with Apache License 2.0 5 votes vote down vote up
public String ogImage(Post post) {
	String path = thumbnail(post);
	if (path == null) {
		return null;
	}
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	builder.path(path);
	return builder.buildAndExpand().encode().toUriString();
}
 
Example 9
Source File: UserService.java    From wallride with Apache License 2.0 4 votes vote down vote up
@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");
	}
	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;
}
 
Example 10
Source File: UserService.java    From wallride with Apache License 2.0 4 votes vote down vote up
public PasswordResetToken createPasswordResetToken(PasswordResetTokenCreateRequest request) {
	User user = userRepository.findOneByEmail(request.getEmail());
	if (user == null) {
		throw new EmailNotFoundException();
	}

	LocalDateTime now = LocalDateTime.now();
	PasswordResetToken passwordResetToken = new PasswordResetToken();
	passwordResetToken.setUser(user);
	passwordResetToken.setEmail(user.getEmail());
	passwordResetToken.setExpiredAt(now.plusHours(24));
	passwordResetToken.setCreatedAt(now);
	passwordResetToken.setCreatedBy(user.toString());
	passwordResetToken.setUpdatedAt(now);
	passwordResetToken.setUpdatedBy(user.toString());
	passwordResetToken = passwordResetTokenRepository.saveAndFlush(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("/password-reset");
		builder.path("/{token}");

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

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

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

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

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

	return passwordResetToken;
}
 
Example 11
Source File: PostUtils.java    From wallride with Apache License 2.0 4 votes vote down vote up
public String link(Page page, boolean encode) {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	return path(builder, page, encode);
}
 
Example 12
Source File: PostUtils.java    From wallride with Apache License 2.0 4 votes vote down vote up
public String link(Page page) {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	return path(builder, page, true);
}
 
Example 13
Source File: PostUtils.java    From wallride with Apache License 2.0 4 votes vote down vote up
public String link(Article article, boolean encode) {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	return path(builder, article, encode);
}
 
Example 14
Source File: PostUtils.java    From wallride with Apache License 2.0 4 votes vote down vote up
public String link(Article article) {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	return path(builder, article, true);
}
 
Example 15
Source File: Users.java    From wallride with Apache License 2.0 4 votes vote down vote up
public String link(User user, boolean encode) {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	return path(builder, user, encode);
}
 
Example 16
Source File: Users.java    From wallride with Apache License 2.0 4 votes vote down vote up
public String link(User user) {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	return path(builder, user, true);
}
 
Example 17
Source File: DefaultModelAttributeInterceptor.java    From wallride with Apache License 2.0 4 votes vote down vote up
private String buildAdminLink() {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	builder.path("/_admin");
	return builder.buildAndExpand().toUriString();
}
 
Example 18
Source File: DefaultModelAttributeInterceptor.java    From wallride with Apache License 2.0 4 votes vote down vote up
private String buildGuestLink() {
	UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
	return builder.buildAndExpand().toUriString();
}