/** * 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); }
@Override public Resource<Order> process(Resource<Order> resource) { Order order = resource.getContent(); if (!order.isPaid()) { resource.add(paymentLinks.getPaymentLink(order)); } if (order.isReady()) { resource.add(paymentLinks.getReceiptLink(order)); } return resource; } }
@Override public void serialize(Resource<?> value, JsonGenerator gen, SerializerProvider provider) throws IOException { HalFormsDocument<?> doc = HalFormsDocument.forResource(value.getContent()) // .withLinks(value.getLinks()) // .withTemplates(findTemplates(value)); provider .findValueSerializer(HalFormsDocument.class, property) .serialize(doc, gen, provider); }
/** * Converts the given entity into a {@link Resource}. * * @param entity * @return */ default Resource<T> toResource(T entity) { Resource<T> resource = new Resource<>(entity); addLinks(resource); return resource; }
@Override public String toString() { return String.format("Resource { content: %s, %s }", getContent(), super.toString()); }
/** * Creates a {@link ProjectionResource} for the given {@link TargetAware}. * * @param value must not be {@literal null}. * @return */ private ProjectionResource toResource(TargetAware value) { Object target = value.getTarget(); ResourceMetadata metadata = associations.getMetadataFor(value.getTargetClass()); Links links = metadata.isExported() ? collector.getLinksFor(target) : new Links(); Resource<TargetAware> resource = invoker.invokeProcessorsFor(new Resource<TargetAware>(value, links)); return new ProjectionResource(resource.getContent(), resource.getLinks()); } }
Object content = ((Resource<?>) resource).getContent(); if (content instanceof Iterable) { Link l = new Link(entry.getValue().getLink("self").getHref(), entry.getKey().toString()); links.add(l); return ControllerUtils.toResponseEntity(HttpStatus.OK, null, new Resource<Object>(EMPTY_RESOURCE_LIST, links));
/** * 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); }
private static List<CollectionJsonItem<?>> resourcesToCollectionJsonItems(Resources<?> resources) { return resources.getContent().stream() .map(content -> { if (ClassUtils.isAssignableValue(Resource.class, content)) { Resource resource = (Resource) content; return new CollectionJsonItem<>() .withHref(resource.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()) .withLinks(withoutSelfLink(resource.getLinks())) .withRawData(resource.getContent()); } else { return new CollectionJsonItem<>().withRawData(content); } }) .collect(Collectors.toList()); }
@RequestMapping(method = POST) HttpEntity<Void> writeUserProfilePhoto(@PathVariable Long user, @RequestParam MultipartFile file) throws Throwable { byte bytesForProfilePhoto[] = FileCopyUtils.copyToByteArray(file.getInputStream()); this.crmService.writeUserProfilePhoto(user, MediaType.parseMediaType(file.getContentType()), bytesForProfilePhoto); Resource<User> userResource = this.userResourceAssembler.toResource(crmService.findById(user)); List<Link> linkCollection = userResource.getLinks(); Links wrapperOfLinks = new Links(linkCollection); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Link", wrapperOfLinks.toString()); // we can't encode the links in the body of the response, so we put them in the "Links:" header. httpHeaders.setLocation(URI.create(userResource.getLink("photo").getHref())); // "Location: /users/{userId}/photo" return new ResponseEntity<>(httpHeaders, HttpStatus.ACCEPTED); }
@Override @JsonSerialize(using = LinkListSerializer.class) @JsonUnwrapped public List<Link> getLinks() { return super.getLinks(); }
protected Link resourceLink(RootResourceInformation resourceLink, Resource resource) { ResourceMetadata repoMapping = resourceLink.getResourceMetadata(); Link selfLink = resource.getLink("self"); String rel = repoMapping.getItemResourceRel(); return new Link(selfLink.getHref(), rel); }
/** * Creates a new {@link Resources} instance by wrapping the given domain class instances into a {@link Resource}. * * @param content must not be {@literal null}. * @return */ @SuppressWarnings("unchecked") public static <T extends Resource<S>, S> Resources<T> wrap(Iterable<S> content) { Assert.notNull(content, "Content must not be null!"); ArrayList<T> resources = new ArrayList<>(); for (S element : content) { resources.add((T) new Resource<>(element)); } return new Resources<>(resources); }
public static Map<String, Object> findProperties(Object object) { if (object.getClass().equals(Resource.class)) { return findProperties(((Resource<?>) object).getContent()); } return getPropertyDescriptors(object.getClass()) .collect(HashMap::new, (hashMap, descriptor) -> { try { Method readMethod = descriptor.getReadMethod(); ReflectionUtils.makeAccessible(readMethod); hashMap.put(descriptor.getName(), readMethod.invoke(object)); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }, HashMap::putAll); }
@GetMapping(value = "/supervisors/{id}", produces = MediaTypes.HAL_JSON_VALUE) public ResponseEntity<Resource<Supervisor>> findOne(@PathVariable Long id) { Resource<Manager> managerResource = controller.findOne(id).getBody(); Resource<Supervisor> supervisorResource = new Resource<>( new Supervisor(managerResource.getContent()), managerResource.getLinks()); return ResponseEntity.ok(supervisorResource); } }
Object content = ((Resource<?>) resource).getContent(); if (content instanceof Iterable) { Link l = new Link(entry.getValue().getLink("self").getHref(), entry.getKey().toString()); links.add(l); return ControllerUtils.toResponseEntity(HttpStatus.OK, null, new Resource<Object>(EMPTY_RESOURCE_LIST, links));
/** * 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 void serialize(Resource<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException { String href = value.getRequiredLink(IanaLinkRelation.SELF.value()).getHref(); CollectionJson<?> collectionJson = new CollectionJson() .withVersion("1.0") .withHref(href) .withLinks(withoutSelfLink(value.getLinks())) .withItems(Collections.singletonList(new CollectionJsonItem<>() .withHref(href) .withLinks(withoutSelfLink(value.getLinks())) .withRawData(value.getContent()))) .withQueries(findQueries(value)) .withTemplate(findTemplate(value)); CollectionJsonDocument<?> doc = new CollectionJsonDocument<>(collectionJson); provider .findValueSerializer(CollectionJsonDocument.class, property) .serialize(doc, jgen, provider); }
@RequestMapping(method = POST) HttpEntity<Void> writeUserProfilePhoto(@PathVariable Long user, @RequestParam MultipartFile file) throws Throwable { byte bytesForProfilePhoto[] = FileCopyUtils.copyToByteArray(file.getInputStream()); this.crmService.writeUserProfilePhoto(user, MediaType.parseMediaType(file.getContentType()), bytesForProfilePhoto); Resource<User> userResource = this.userResourceAssembler.toResource(crmService.findById(user)); List<Link> linkCollection = userResource.getLinks(); Links wrapperOfLinks = new Links(linkCollection); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Link", wrapperOfLinks.toString()); // we can't encode the links in the body of the response, so we put them in the "Links:" header. httpHeaders.setLocation(URI.create(userResource.getLink("photo").getHref())); // "Location: /users/{userId}/photo" return new ResponseEntity<>(httpHeaders, HttpStatus.ACCEPTED); }
@Override public Object invoke(Object self, Method method, Method proceed, Object[] args) { Link selfLink = resource.getLink(Link.REL_SELF); return selfLink == null ? null : URI.create(selfLink.getHref()); } }