Java Code Examples for org.springframework.hateoas.EntityModel#add()

The following examples show how to use org.springframework.hateoas.EntityModel#add() . 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: JobSearchResultModelAssembler.java    From genie with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@Nonnull
public EntityModel<JobSearchResult> toModel(final JobSearchResult job) {
    final EntityModel<JobSearchResult> jobSearchResultModel = new EntityModel<>(job);

    try {
        jobSearchResultModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(JobRestController.class)
                    .getJob(job.getId())
            ).withSelfRel()
        );
    } catch (final GenieException ge) {
        // If we can't convert it we might as well force a server exception
        throw new RuntimeException(ge);
    }

    return jobSearchResultModel;
}
 
Example 2
Source File: ApplicationModelAssembler.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@Nonnull
public EntityModel<Application> toModel(final Application application) {
    final String id = application.getId().orElseThrow(IllegalArgumentException::new);
    final EntityModel<Application> applicationModel = new EntityModel<>(application);

    try {
        applicationModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(ApplicationRestController.class)
                    .getApplication(id)
            ).withSelfRel()
        );

        applicationModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(ApplicationRestController.class)
                    .getCommandsForApplication(id, null)
            ).withRel(COMMANDS_LINK)
        );
    } catch (final GenieException | NotFoundException ge) {
        // If we can't convert it we might as well force a server exception
        throw new RuntimeException(ge);
    }

    return applicationModel;
}
 
Example 3
Source File: CommandModelAssembler.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@Nonnull
public EntityModel<Command> toModel(final Command command) {
    final String id = command.getId().orElseThrow(IllegalArgumentException::new);
    final EntityModel<Command> commandModel = new EntityModel<>(command);

    try {
        commandModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(CommandRestController.class)
                    .getCommand(id)
            ).withSelfRel()
        );

        commandModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(CommandRestController.class)
                    .getApplicationsForCommand(id)
            ).withRel(APPLICATIONS_LINK)
        );

        commandModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(CommandRestController.class)
                    .getClustersForCommand(id, null)
            ).withRel(CLUSTERS_LINK)
        );
    } catch (final GenieException | GenieCheckedException ge) {
        // If we can't convert it we might as well force a server exception
        throw new RuntimeException(ge);
    }

    return commandModel;
}
 
Example 4
Source File: PackageMetadataResourceProcessor.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Override
public EntityModel<PackageMetadata> process(EntityModel<PackageMetadata> packageMetadataResource) {
	Link installLink = linkTo(
			methodOn(PackageController.class).install(packageMetadataResource.getContent().getId(), null))
			.withRel("install");
	packageMetadataResource.add(installLink);
	return packageMetadataResource;
}
 
Example 5
Source File: PackageSummaryResourceProcessor.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Override
public EntityModel<PackageSummary> process(EntityModel<PackageSummary> packageSummaryResource) {
	Link link = linkTo(
			methodOn(PackageController.class).install(Long.valueOf(packageSummaryResource.getContent().getId()),
					null))
			.withRel("install");
	packageSummaryResource.add(link);
	return packageSummaryResource;
}
 
Example 6
Source File: UserResource.java    From spring-web-services with MIT License 5 votes vote down vote up
@GetMapping("/users/{id}")
public EntityModel<User> retrieveUser(@PathVariable int id) {
	User user = service.findOne(id);
	
	if(user==null)
		throw new UserNotFoundException("id-"+ id);
	
	
	//"all-users", SERVER_PATH + "/users"
	//retrieveAllUsers
	EntityModel<User> resource = EntityModel.of(user);
	
	WebMvcLinkBuilder linkTo = 
			linkTo(methodOn(this.getClass()).retrieveAllUsers());
	
	resource.add(linkTo.withRel("all-users"));
	
	//HATEOAS
	
	return resource;
}
 
Example 7
Source File: ClusterModelAssembler.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@Nonnull
public EntityModel<Cluster> toModel(final Cluster cluster) {
    final String id = cluster.getId().orElseThrow(IllegalArgumentException::new);
    final EntityModel<Cluster> clusterModel = new EntityModel<>(cluster);

    try {
        clusterModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(ClusterRestController.class)
                    .getCluster(id)
            ).withSelfRel()
        );

        clusterModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(ClusterRestController.class)
                    .getCommandsForCluster(id, null)
            ).withRel(COMMANDS_LINK)
        );
    } catch (final NotFoundException ge) {
        // If we can't convert it we might as well force a server exception
        throw new RuntimeException(ge);
    }

    return clusterModel;
}
 
Example 8
Source File: DemoComponentDtoModelProcessor.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
@Override
public EntityModel<DemoComponentDto> process(EntityModel<DemoComponentDto> model) {
	final String id = model.getContent().getId();

	model.add(linkTo(methodOn(ComponentsController.class).findById(id)).withSelfRel());

	return model;
}
 
Example 9
Source File: EmployeeWithManagerResourceAssembler.java    From spring-hateoas-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Define links to add to every individual {@link EntityModel}.
 *
 * @param resource
 */
@Override
public void addLinks(EntityModel<EmployeeWithManager> resource) {

	resource.add(
			linkTo(methodOn(EmployeeController.class).findDetailedEmployee(resource.getContent().getId())).withSelfRel());
	resource.add(linkTo(methodOn(EmployeeController.class).findOne(resource.getContent().getId())).withRel("summary"));
	resource.add(linkTo(methodOn(EmployeeController.class).findAllDetailedEmployees()).withRel("detailedEmployees"));
}
 
Example 10
Source File: OrderProcessor.java    From spring-hateoas-examples with Apache License 2.0 5 votes vote down vote up
@Override
public EntityModel<Order> process(EntityModel<Order> model) {

	CustomOrderController controller = methodOn(CustomOrderController.class);
	String basePath = configuration.getBasePath().toString();

	// If PAID_FOR is valid, add a link to the `pay()` method
	if (valid(model.getContent().getOrderStatus(), OrderStatus.PAID_FOR)) {
		model.add(applyBasePath( //
				linkTo(controller.pay(model.getContent().getId())) //
						.withRel(IanaLinkRelations.PAYMENT), //
				basePath));
	}

	// If CANCELLED is valid, add a link to the `cancel()` method
	if (valid(model.getContent().getOrderStatus(), OrderStatus.CANCELLED)) {
		model.add(applyBasePath( //
				linkTo(controller.cancel(model.getContent().getId())) //
						.withRel(LinkRelation.of("cancel")), //
				basePath));
	}

	// If FULFILLED is valid, add a link to the `fulfill()` method
	if (valid(model.getContent().getOrderStatus(), OrderStatus.FULFILLED)) {
		model.add(applyBasePath( //
				linkTo(controller.fulfill(model.getContent().getId())) //
						.withRel(LinkRelation.of("fulfill")), //
				basePath));
	}

	return model;
}
 
Example 11
Source File: UserResource.java    From spring-microservices with MIT License 4 votes vote down vote up
@GetMapping("/users/{id}")
public EntityModel<User> retrieveUser(@PathVariable int id) {
	User user = service.findOne(id);
	
	if(user==null)
		throw new UserNotFoundException("id-"+ id);
	
	
	//"all-users", SERVER_PATH + "/users"
	//retrieveAllUsers
	EntityModel<User> resource = EntityModel.of(user);
	
	WebMvcLinkBuilder linkTo = 
			linkTo(methodOn(this.getClass()).retrieveAllUsers());
	
	resource.add(linkTo.withRel("all-users"));
	
	//HATEOAS
	
	return resource;
}
 
Example 12
Source File: JobRequestModelAssembler.java    From genie with Apache License 2.0 4 votes vote down vote up
/**
     * {@inheritDoc}
     */
    @Override
    @Nonnull
    public EntityModel<JobRequest> toModel(final JobRequest jobRequest) {
        final String id = jobRequest.getId().orElseThrow(IllegalArgumentException::new);
        final EntityModel<JobRequest> jobRequestModel = new EntityModel<>(jobRequest);

        try {
            jobRequestModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJobRequest(id)
                ).withSelfRel()
            );

            jobRequestModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJob(id)
                ).withRel(JOB_LINK)
            );

            jobRequestModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJobExecution(id)
                ).withRel(EXECUTION_LINK)
            );

            // TODO: https://github.com/spring-projects/spring-hateoas/issues/186 should be fixed in .20 currently .19
//            jobRequestResource.add(
//                ControllerLinkBuilder.linkTo(
//                    JobRestController.class,
//                    JobRestController.class.getMethod(
//                        "getJobOutput",
//                        String.class,
//                        String.class,
//                        HttpServletRequest.class,
//                        HttpServletResponse.class
//                    ),
//                    id,
//                    null,
//                    null,
//                    null
//                ).withRel("output")
//            );

            jobRequestModel.add(
                WebMvcLinkBuilder
                    .linkTo(JobRestController.class)
                    .slash(id)
                    .slash(OUTPUT_LINK)
                    .withRel(OUTPUT_LINK)
            );

            jobRequestModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJobStatus(id)
                ).withRel(STATUS_LINK)
            );

            jobRequestModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJobMetadata(id)
                ).withRel(METADATA_LINK)
            );
        } catch (final GenieException | GenieCheckedException ge) {
            // If we can't convert it we might as well force a server exception
            throw new RuntimeException(ge);
        }

        return jobRequestModel;
    }
 
Example 13
Source File: JobMetadataModelAssembler.java    From genie with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@Nonnull
public EntityModel<JobMetadata> toModel(final JobMetadata jobMetadata) {
    final String id = jobMetadata.getId().orElseThrow(IllegalArgumentException::new);
    final EntityModel<JobMetadata> jobMetadataModel = new EntityModel<>(jobMetadata);

    try {
        jobMetadataModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(JobRestController.class)
                    .getJobMetadata(id)
            ).withSelfRel()
        );

        jobMetadataModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(JobRestController.class)
                    .getJob(id)
            ).withRel(JOB_LINK)
        );

        jobMetadataModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(JobRestController.class)
                    .getJobRequest(id)
            ).withRel(REQUEST_LINK)
        );

        jobMetadataModel.add(
            WebMvcLinkBuilder
                .linkTo(JobRestController.class)
                .slash(id)
                .slash(OUTPUT_LINK)
                .withRel(OUTPUT_LINK)
        );

        jobMetadataModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(JobRestController.class)
                    .getJobStatus(id)
            ).withRel(STATUS_LINK)
        );

        jobMetadataModel.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(JobRestController.class)
                    .getJobExecution(id)
            ).withRel(EXECUTION_LINK)
        );
    } catch (final GenieException | GenieCheckedException ge) {
        // If we can't convert it we might as well force a server exception
        throw new RuntimeException(ge);
    }

    return jobMetadataModel;
}
 
Example 14
Source File: JobExecutionModelAssembler.java    From genie with Apache License 2.0 4 votes vote down vote up
/**
     * {@inheritDoc}
     */
    @Override
    @Nonnull
    public EntityModel<JobExecution> toModel(final JobExecution jobExecution) {
        final String id = jobExecution.getId().orElseThrow(IllegalArgumentException::new);
        final EntityModel<JobExecution> jobExecutionModel = new EntityModel<>(jobExecution);

        try {
            jobExecutionModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJobExecution(id)
                ).withSelfRel()
            );

            jobExecutionModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJob(id)
                ).withRel(JOB_LINK)
            );

            jobExecutionModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJobRequest(id)
                ).withRel(REQUEST_LINK)
            );

            // TODO: https://github.com/spring-projects/spring-hateoas/issues/186 should be fixed in .20 currently .19
//            jobExecutionResource.add(
//                ControllerLinkBuilder.linkTo(
//                    JobRestController.class,
//                    JobRestController.class.getMethod(
//                        "getJobOutput",
//                        String.class,
//                        String.class,
//                        HttpServletRequest.class,
//                        HttpServletResponse.class
//                    ),
//                    id,
//                    null,
//                    null,
//                    null
//                ).withRel("output")
//            );

            jobExecutionModel.add(
                WebMvcLinkBuilder
                    .linkTo(JobRestController.class)
                    .slash(id)
                    .slash(OUTPUT_LINK)
                    .withRel(OUTPUT_LINK)
            );

            jobExecutionModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJobStatus(id)
                ).withRel(STATUS_LINK)
            );

            jobExecutionModel.add(
                WebMvcLinkBuilder.linkTo(
                    WebMvcLinkBuilder
                        .methodOn(JobRestController.class)
                        .getJobMetadata(id)
                ).withRel(METADATA_LINK)
            );
        } catch (final GenieException | GenieCheckedException ge) {
            // If we can't convert it we might as well force a server exception
            throw new RuntimeException(ge);
        }

        return jobExecutionModel;
    }
 
Example 15
Source File: UserJPAResource.java    From spring-web-services with MIT License 4 votes vote down vote up
@GetMapping("/jpa/users/{id}")
public EntityModel<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
	EntityModel<User> resource = EntityModel.of(user.get());//new EntityModel<User>(user.get());

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

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

	// HATEOAS

	return resource;
}
 
Example 16
Source File: ReleaseResourceAssembler.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
@Override
protected void addLinks(EntityModel<Release> resource) {
	super.addLinks(resource);
	resource.add(linkTo(methodOn(ReleaseController.class).status(null)).withRel("status"));
}
 
Example 17
Source File: ManifestResourceAssembler.java    From spring-cloud-skipper with Apache License 2.0 4 votes vote down vote up
@Override
protected void addLinks(EntityModel<Manifest> resource) {
	super.addLinks(resource);
	resource.add(linkTo(methodOn(ReleaseController.class).status(null)).withRel("status"));
}
 
Example 18
Source File: RootModelAssembler.java    From genie with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@SuppressFBWarnings("NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS")
@Nonnull
public EntityModel<JsonNode> toModel(final JsonNode metadata) {
    final EntityModel<JsonNode> rootResource = new EntityModel<>(metadata);

    try {
        rootResource.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(RootRestController.class)
                    .getRoot()
            ).withSelfRel()
        );

        rootResource.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(ApplicationRestController.class)
                    .createApplication(null)
            ).withRel(APPLICATIONS_LINK)
        );

        rootResource.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(CommandRestController.class)
                    .createCommand(null)
            ).withRel(COMMANDS_LINK)
        );

        rootResource.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(ClusterRestController.class)
                    .createCluster(null)
            ).withRel(CLUSTERS_LINK)
        );

        rootResource.add(
            WebMvcLinkBuilder.linkTo(
                WebMvcLinkBuilder
                    .methodOn(JobRestController.class)
                    .submitJob(null, null, null, null)
            ).withRel(JOBS_LINK)
        );
    } catch (final GenieException | GenieCheckedException ge) {
        // If we can't convert it we might as well force a server exception
        throw new RuntimeException(ge);
    }

    return rootResource;
}
 
Example 19
Source File: UserJPAResource.java    From spring-microservices with MIT License 3 votes vote down vote up
@GetMapping("/jpa/users/{id}")
public EntityModel<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
	EntityModel<User> resource = EntityModel.of(user.get());//new EntityModel<User>(user.get());

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

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

	// HATEOAS

	return resource;
}
 
Example 20
Source File: PackageMetadataResourceAssembler.java    From spring-cloud-skipper with Apache License 2.0 3 votes vote down vote up
@Override
protected void addLinks(EntityModel<PackageMetadata> resource) {
	super.addLinks(resource);

	resource.add(linkTo(methodOn(PackageController.class).install(null)).withRel("install"));

}