/** * Creates a new {@link Resource} with the given content and {@link Link}s. * * @param content must not be {@literal null}. * @param links the links to add to the {@link Resource}. */ public Resource(T content, Iterable<Link> links) { Assert.notNull(content, "Content must not be null!"); Assert.isTrue(!(content instanceof Collection), "Content must not be a collection! Use Resources instead!"); this.content = content; this.add(links); }
@Override public Resource<Order> process(Resource<Order> resource) { Order order = resource.getContent(); if (!order.isPaid()) { resource.add(entityLinks.linkForSingleResource(order).withRel(CANCEL_REL)); resource.add(entityLinks.linkForSingleResource(order).withRel(UPDATE_REL)); } return resource; } }
/** * Renders the given {@link Receipt} including links to the associated {@link Order} as well as a self link in case * the {@link Receipt} is still available. * * @param receipt * @return */ private HttpEntity<Resource<Receipt>> createReceiptResponse(Receipt receipt) { Order order = receipt.getOrder(); Resource<Receipt> resource = new Resource<>(receipt); resource.add(entityLinks.linkToSingleResource(order)); if (!order.isTaken()) { resource.add(entityLinks.linkForSingleResource(order).slash("receipt").withSelfRel()); } return ResponseEntity.ok(resource); }
resource.add(resourceLinks); content.add(resource); } else {
/** * Creates a new {@link Resource} with the given content and {@link Link}s. * * @param content must not be {@literal null}. * @param links the links to add to the {@link Resource}. */ public Resource(T content, Iterable<Link> links) { Assert.notNull(content, "Content must not be null!"); Assert.isTrue(!(content instanceof Collection), "Content must not be a collection! Use Resources instead!"); this.content = content; this.add(links); }
@GetMapping("/students/{id}") @ApiOperation(value = "Find student by id", notes = "Also returns a link to retrieve all students with rel - all-students") public Resource<Student> retrieveStudent(@PathVariable long id) { Optional<Student> student = studentRepository.findById(id); if (!student.isPresent()) throw new StudentNotFoundException("id-" + id); Resource<Student> resource = new Resource<Student>(student.get()); ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllStudents()); resource.add(linkTo.withRel("all-students")); return resource; }
@GetMapping("/students/{id}") public Resource<Student> retrieveStudent(@PathVariable long id) { Optional<Student> student = studentRepository.findById(id); if (!student.isPresent()) throw new StudentNotFoundException("id-" + id); Resource<Student> resource = new Resource<Student>(student.get()); ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllStudents()); resource.add(linkTo.withRel("all-students")); return resource; }
@GetMapping("/students/{id}") public Resource<Student> retrieveStudent(@PathVariable long id) { Optional<Student> student = studentRepository.findById(id); if (!student.isPresent()) throw new StudentNotFoundException("id-" + id); Resource<Student> resource = new Resource<Student>(student.get()); ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllStudents()); resource.add(linkTo.withRel("all-students")); return resource; }
@GetMapping("/students/{id}") public Resource<Student> retrieveStudent(@PathVariable long id) { Optional<Student> student = studentRepository.findById(id); if (!student.isPresent()) throw new StudentNotFoundException("id-" + id); Resource<Student> resource = new Resource<Student>(student.get()); ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllStudents()); resource.add(linkTo.withRel("all-students")); return resource; }
@Override public Resource<PackageSummary> process(Resource<PackageSummary> packageSummaryResource) { Link link = linkTo( methodOn(PackageController.class).install(Long.valueOf(packageSummaryResource.getContent().getId()), null)) .withRel("install"); packageSummaryResource.add(link); return packageSummaryResource; } }
@Override protected void addLinks(Resource<PackageMetadata> resource) { super.addLinks(resource); resource.add(linkTo(methodOn(PackageController.class).install(null)).withRel("install")); } }
@RequestMapping(value = "/{eventId}", method = RequestMethod.GET) public @ResponseBody Resource<Event> getEvent(@PathVariable Integer eventId) { Resource<Event> resource = new Resource<Event>(getEvents().get(eventId)); resource.add(linkTo(ReviewController.class).withRel("review")); return resource; }
@RequestMapping(value = "/regex/{eventId:.+}", method = RequestMethod.GET) public @ResponseBody Resource<Event> getEventWithRegexPathVariableMapping(@PathVariable Integer eventId) { Resource<Event> resource = new Resource<Event>(getEvents().get(eventId)); resource.add(linkTo(ReviewController.class).withRel("review")); return resource; }
@Override protected void addLinks(Resource<PackageMetadata> resource) { super.addLinks(resource); resource.add(linkTo(methodOn(PackageController.class).install(null)).withRel("install")); } }
@Override protected void addLinks(Resource<Release> resource) { super.addLinks(resource); resource.add(linkTo(methodOn(ReleaseController.class).status(null)).withRel("status")); } }
@Override protected void addLinks(Resource<Info> resource) { super.addLinks(resource); resource.add(linkTo(methodOn(ReleaseController.class).manifest(null)).withRel("manifest")); } }
@Override protected void addLinks(Resource<Manifest> resource) { super.addLinks(resource); resource.add(linkTo(methodOn(ReleaseController.class).status(null)).withRel("status")); } }
@Override public Resource<Customer> process(Resource<Customer> resource) { String id = resource.getContent().getId(); resource.add(linkTo(methodOn(CustomerCommandsController.class).getCommands(id)).withRel("commands")); return resource; } }
private void addAffordances(Event event) { event.add(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(this.getClass()) .getEvent(event.id)) .and(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(EventController.class) .updateEvent(event.id, event))) .and(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(EventController.class) .deleteEvent(event.id))) .withSelfRel()); event.workPerformed.add(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(ReviewController.class) .addReview(event.id, new Review(null, new Rating(3)))) .withRel("review")); }