Java Code Examples for org.springframework.hateoas.mvc.BasicLinkBuilder

The following examples show how to use org.springframework.hateoas.mvc.BasicLinkBuilder. These examples are extracted from open source projects. 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 Project: micro-ecommerce   Author: idugalic   File: Application.java    License: Apache License 2.0 6 votes vote down vote up
@RequestMapping("/")
public HttpEntity<RootResource> getRootResource() {
	RootResource resource = new RootResource();
	String baseUri = BasicLinkBuilder.linkToCurrentMapping().toString();

	Link catalogServiceLink = new Link(new UriTemplate(baseUri + "/catalog"), RootResource.LINK_NAME_CATALOG);
	resource.add(catalogServiceLink);

	Link reviewServiceLink = new Link(new UriTemplate(baseUri + "/reviews"), RootResource.LINK_NAME_REVIEW);
	resource.add(reviewServiceLink);

	Link recommendationServiceLink = new Link(new UriTemplate(baseUri + "/recommendations"),
			RootResource.LINK_NAME_RECOMMENDATIONS);
	resource.add(recommendationServiceLink);

	Link orderServiceLink = new Link(new UriTemplate(baseUri + "/orders"), RootResource.LINK_NAME_ORDERS);
	resource.add(orderServiceLink);

	Link productLink = new Link(new UriTemplate(baseUri + "/product/{id}"), RootResource.LINK_NAME_PRODUCT_DETAIL);
	resource.add(productLink);

	return new ResponseEntity<>(resource, HttpStatus.OK);
}
 
Example #2
Source Project: JiwhizBlogWeb   Author: jiwhiz   File: AuthorAccountResourceAssembler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public AuthorAccountResource toResource(UserAccount entity) {
    AuthorAccountResource resource = new AuthorAccountResource(entity);

    try {
        resource.add(linkTo(methodOn(AuthorAccountRestController.class).getCurrentAuthorAccount())
                .withSelfRel());
        
        String baseUri = BasicLinkBuilder.linkToCurrentMapping().toString();
        Link blogsLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_AUTHOR_BLOGS), AuthorAccountResource.LINK_NAME_BLOGS);
        resource.add(assembler.appendPaginationParameterTemplates(blogsLink));
        
        Link blogLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_AUTHOR_BLOGS_BLOG), AuthorAccountResource.LINK_NAME_BLOG);
        resource.add(blogLink);

    } catch (Exception ex) {
        //do nothing
    }
    
    return resource;

}
 
Example #3
Source Project: Microservices-with-Spring-Cloud   Author: PacktPublishing   File: ServiceDocumentController.java    License: MIT License 5 votes vote down vote up
@GetMapping("/")
public Resource<String> getServiceDocument() throws URISyntaxException {
    return new Resource<>("Yummy is the best service",
            BasicLinkBuilder.linkToCurrentMapping().withSelfRel(),
            linkTo(methodOn(BookmarksController.class).addBookmark(null)).withRel("bookmarks")
            );
}
 
Example #4
Source Project: Microservices-with-Spring-Cloud   Author: PacktPublishing   File: BookmarksController.java    License: MIT License 5 votes vote down vote up
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> addBookmark(@RequestBody @Valid Bookmark bookmark){
    UUID uuid = bookmarkService.addBookmark(bookmark);
    return ResponseEntity.created(
            BasicLinkBuilder.linkToCurrentMapping()
                    .slash("bookmark")
                    .slash(uuid)
                    .toUri())
            .build();
}
 
Example #5
Source Project: Microservices-with-Spring-Cloud   Author: PacktPublishing   File: BookmarksController.java    License: MIT License 5 votes vote down vote up
@GetMapping
public Resources<Resource<Bookmark>> findAllBookmarks(HttpServletRequest request){
    Resources<Resource<Bookmark>> bookmarks = new Resources<>(
            resourceAssembler.toResourceList(bookmarkService.findAll()),
            BasicLinkBuilder.linkToCurrentMapping()
                    .slash("bookmarks").withSelfRel()
    );
    if(request.isUserInRole("ADMIN")){
        bookmarks.add(
                linkTo(methodOn(BookmarksController.class).addBookmark(null)).withRel("add"),
                linkTo(methodOn(BookmarkController.class).getBookmarkTemplate()).withRel("template")
        );
    }
    return bookmarks;
}
 
Example #6
Source Project: spring-content   Author: paulcwarren   File: ContentLinksResourceProcessor.java    License: Apache License 2.0 5 votes vote down vote up
private Link shortcutLink(URI baseUri, ContentStoreInfo store, Object id, String linkRel) {
	LinkBuilder builder = BasicLinkBuilder.linkToCurrentMapping();

	if (baseUri != null) {
		builder = builder.slash(baseUri);
	}

	return builder.slash(ContentStoreUtils.storePath(store))
			.slash(id)
			.withRel(linkRel);
}
 
Example #7
Source Project: spring-content   Author: paulcwarren   File: ContentLinksResourceProcessor.java    License: Apache License 2.0 5 votes vote down vote up
private Link fullyQualifiedLink(URI baseUri, ContentStoreInfo store, Object id, String fieldName) {
	LinkBuilder builder = BasicLinkBuilder.linkToCurrentMapping();

	if (baseUri != null) {
		builder = builder.slash(baseUri);
	}

	String property = StringUtils.uncapitalize(ContentStoreUtils.propertyName(fieldName));
	return builder.slash(ContentStoreUtils.storePath(store))
			.slash(id)
			.slash(property)
			.withRel(property);
}
 
Example #8
Source Project: JiwhizBlogWeb   Author: jiwhiz   File: AdminAccountResourceAssembler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public AdminAccountResource toResource(UserAccount userAccount) {
    AdminAccountResource resource = new AdminAccountResource(userAccount);

    try {
        resource.add(linkTo(methodOn(AdminAccountRestController.class).getAdminAccount()).withSelfRel());
        
        String baseUri = BasicLinkBuilder.linkToCurrentMapping().toString();
        
        Link usersLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_ADMIN_USERS), AdminAccountResource.LINK_NAME_USERS);
        resource.add(assembler.appendPaginationParameterTemplates(usersLink));
        
        Link blogsLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_ADMIN_BLOGS), AdminAccountResource.LINK_NAME_BLOGS);
        resource.add(assembler.appendPaginationParameterTemplates(blogsLink));
        
        Link commentsLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_ADMIN_COMMENTS), AdminAccountResource.LINK_NAME_COMMENTS);
        resource.add(assembler.appendPaginationParameterTemplates(commentsLink));

    } catch (Exception ex) {
        //do nothing
    }
    
    return resource;

}
 
Example #9
Source Project: JiwhizBlogWeb   Author: jiwhiz   File: WebsiteResourceAssembler.java    License: Apache License 2.0 4 votes vote down vote up
public WebsiteResource toResource() {
    UserAccount currentUser = this.userAccountService.getCurrentUser();
    WebsiteResource resource = new WebsiteResource();
    resource.setAuthenticated(currentUser != null);
    
    try{
        resource.add(linkTo(methodOn(WebsiteRestController.class).getPublicWebsiteResource())
                .withSelfRel());
        
        String baseUri = BasicLinkBuilder.linkToCurrentMapping().toString();
        
        Link blogsLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_SITE_BLOGS), WebsiteResource.LINK_NAME_BLOGS);
        resource.add(assembler.appendPaginationParameterTemplates(blogsLink));
        
        Link blogLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_SITE_BLOGS_BLOG), WebsiteResource.LINK_NAME_BLOG);
        resource.add(blogLink);
        
        resource.add(linkTo(methodOn(WebsiteRestController.class).getCurrentUserAccount())
                .withRel(WebsiteResource.LINK_NAME_CURRENT_USER));
        resource.add(linkTo(methodOn(WebsiteRestController.class).getLatestBlogPost())
                .withRel(WebsiteResource.LINK_NAME_LATEST_BLOG));
        resource.add(linkTo(methodOn(WebsiteRestController.class).getRecentPublicBlogPosts())
                .withRel(WebsiteResource.LINK_NAME_RECENT_BLOGS));
        resource.add(linkTo(methodOn(WebsiteRestController.class).getRecentPublicCommentPosts())
                .withRel(WebsiteResource.LINK_NAME_RECENT_COMMENTS));
        resource.add(linkTo(methodOn(WebsiteRestController.class).getTagCloud())
                .withRel(WebsiteResource.LINK_NAME_TAG_CLOUD));

        Link profileLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_SITE_PROFILES_USER), WebsiteResource.LINK_NAME_PROFILE);
        resource.add(profileLink);

        Link postCommentLink = new Link(
                new UriTemplate(baseUri + ApiUrls.API_ROOT + ApiUrls.URL_USER_BLOGS_BLOG_COMMENTS), WebsiteResource.LINK_NAME_POST_COMMENT);
        resource.add(postCommentLink);
    
    } catch (ResourceNotFoundException ex) {
    }

    return resource;
}