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

The following examples show how to use org.springframework.hateoas.mvc.ControllerLinkBuilder. 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: spring-cloud-dashboard   Source File: SecurityController.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return security information. E.g. is security enabled? Which user do you represent?
 */
@ResponseBody
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public SecurityInfoResource getSecurityInfo() {

	final boolean authenticationEnabled = securityProperties.getBasic().isEnabled();

	final SecurityInfoResource securityInfo = new SecurityInfoResource();
	securityInfo.setAuthenticationEnabled(authenticationEnabled);
	securityInfo.add(ControllerLinkBuilder.linkTo(SecurityController.class).withSelfRel());

	if (authenticationEnabled && SecurityContextHolder.getContext() != null) {
		final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (!(authentication instanceof AnonymousAuthenticationToken)) {
			securityInfo.setAuthenticated(authentication.isAuthenticated());
			securityInfo.setUsername(authentication.getName());
		}
	}

	return securityInfo;
}
 
Example 2
Source Project: Mastering-Spring-5.1   Source File: TodoController.java    License: MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}

	Resource<Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 3
Source Project: Mastering-Spring-5.1   Source File: TodoController.java    License: MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}

	Resource<Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 4
Source Project: Mastering-Spring-5.1   Source File: TodoController.java    License: MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}

	Resource<Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 5
Source Project: Mastering-Spring-5.1   Source File: TodoController.java    License: MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}

	Resource<Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 6
Source Project: Spring   Source File: UserJPAController.java    License: Apache License 2.0 5 votes vote down vote up
@GetMapping(path = "/jpa/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id) {
	final User user = userJPAService.findOne(id);
	if (Objects.isNull(user)) {
		throw new UserNotFoundException("There is no user with id: " + id);
	}

	final Resource<User> resource = new Resource<>(user);
	final ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());
	resource.add(linkTo.withRel("all-users"));

	return resource;
}
 
Example 7
Source Project: Mastering-Spring-5.0   Source File: TodoController.java    License: MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}
	
	Resource<com.mastering.spring.springboot.bean.Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 8
Source Project: Mastering-Spring-5.0   Source File: TodoController.java    License: MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}
	
	Resource<com.mastering.spring.springboot.bean.Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 9
Source Project: Mastering-Spring-5.0   Source File: TodoController.java    License: MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}
	
	Resource<com.mastering.spring.springboot.bean.Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 10
Source Project: Showcase   Source File: PageLinksAspect.java    License: Apache License 2.0 5 votes vote down vote up
private UriComponentsBuilder originalUri(Class<?> controller, HttpServletRequest request) {
    UriComponentsBuilder baseUri = ControllerLinkBuilder.linkTo(controller).toUriComponentsBuilder();
    for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
        for (String value : entry.getValue()) {
            baseUri.queryParam(entry.getKey(), value);
        }
    }
    return baseUri;
}
 
Example 11
Source Project: hawkbit   Source File: DataConversionHelper.java    License: Eclipse Public License 1.0 5 votes vote down vote up
static DdiControllerBase fromTarget(final Target target, final Action action,
        final String defaultControllerPollTime, final TenantAware tenantAware) {
    final DdiControllerBase result = new DdiControllerBase(
            new DdiConfig(new DdiPolling(defaultControllerPollTime)));

    if (action != null) {
        if (action.isCancelingOrCanceled()) {
            result.add(ControllerLinkBuilder
                    .linkTo(ControllerLinkBuilder.methodOn(DdiRootController.class, tenantAware.getCurrentTenant())
                            .getControllerCancelAction(tenantAware.getCurrentTenant(), target.getControllerId(),
                                    action.getId()))
                    .withRel(DdiRestConstants.CANCEL_ACTION));
        } else {
            // we need to add the hashcode here of the actionWithStatus
            // because the action might
            // have changed from 'soft' to 'forced' type and we need to
            // change the payload of the
            // response because of eTags.
            result.add(ControllerLinkBuilder
                    .linkTo(ControllerLinkBuilder.methodOn(DdiRootController.class, tenantAware.getCurrentTenant())
                            .getControllerBasedeploymentAction(tenantAware.getCurrentTenant(),
                                    target.getControllerId(), action.getId(), calculateEtag(action), null))
                    .withRel(DdiRestConstants.DEPLOYMENT_BASE_ACTION));
        }
    }

    if (target.isRequestControllerAttributes()) {
        result.add(ControllerLinkBuilder
                .linkTo(ControllerLinkBuilder.methodOn(DdiRootController.class, tenantAware.getCurrentTenant())
                        .putConfigData(null, tenantAware.getCurrentTenant(), target.getControllerId()))
                .withRel(DdiRestConstants.CONFIG_DATA_ACTION));
    }
    return result;
}
 
Example 12
Source Project: in28minutes-spring-microservices   Source File: UserResource.java    License: MIT License 4 votes vote down vote up
@GetMapping("/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id) {
	User user = service.findOne(id);
	
	if(user==null)
		throw new UserNotFoundException("id-"+ id);
	
	
	//"all-users", SERVER_PATH + "/users"
	//retrieveAllUsers
	Resource<User> resource = new Resource<User>(user);
	
	ControllerLinkBuilder linkTo = 
			linkTo(methodOn(this.getClass()).retrieveAllUsers());
	
	resource.add(linkTo.withRel("all-users"));
	
	//HATEOAS
	
	return resource;
}
 
Example 13
Source Project: in28minutes-spring-microservices   Source File: UserJPAResource.java    License: MIT License 4 votes vote down vote up
@GetMapping("/jpa/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id) {
	Optional<User> user = userRepository.findById(id);

	if (!user.isPresent())
		throw new UserNotFoundException("id-" + id);

	// "all-users", SERVER_PATH + "/users"
	// retrieveAllUsers
	Resource<User> resource = new Resource<User>(user.get());

	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());

	resource.add(linkTo.withRel("all-users"));

	// HATEOAS

	return resource;
}
 
Example 14
Source Project: restdocs-raml   Source File: NotesController.java    License: MIT License 4 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
PagedResources<NoteResource> all(Pageable pageable) {
	return pagedResourcesAssembler.toResource(noteRepository.findAll(pageable),
			noteResourceAssembler,
			ControllerLinkBuilder.linkTo(methodOn(this.getClass()).all(null)).withSelfRel());
}
 
Example 15
Source Project: chaos-lemur   Source File: TaskResourceAssembler.java    License: Apache License 2.0 4 votes vote down vote up
private ControllerLinkBuilder getTaskLinkBuilder(Task task) {
    return linkTo(TaskManager.class).slash(task.getId());
}