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()); }
@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); }
@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); }
/** * 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()); } }
@Override @JsonSerialize(using = LinkListSerializer.class) @JsonUnwrapped public List<Link> getLinks() { return super.getLinks(); }
@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); } }
@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); }
@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); }
private void traverseSingleSubEntity(SirenEntityContainer objectNode, Object content, String name, String docUrl) throws InvocationTargetException, IllegalAccessException { Object bean; List<Link> links; if (content instanceof Resource) { bean = ((Resource) content).getContent(); links = ((Resource) content).getLinks(); } else if (content instanceof ResourceSupport) { bean = content; links = ((ResourceSupport) content).getLinks(); } else { bean = content; links = Collections.emptyList(); } Map<String, Object> properties = new HashMap<String, Object>(); List<String> rels = Collections.singletonList(docUrl != null ? docUrl : name); SirenEmbeddedRepresentation subEntity = new SirenEmbeddedRepresentation( getSirenClasses(bean), properties, null, toSirenActions(getActions(links)), toSirenLinks(getNavigationalLinks(links)), rels, null); //subEntity.setProperties(properties); objectNode.addSubEntity(subEntity); List<SirenEmbeddedLink> sirenEmbeddedLinks = toSirenEmbeddedLinks(getEmbeddedLinks(links)); for (SirenEmbeddedLink sirenEmbeddedLink : sirenEmbeddedLinks) { subEntity.addSubEntity(sirenEmbeddedLink); } createRecursiveSirenEntitiesFromPropertiesAndFields(subEntity, properties, bean); }
private void traverseSingleSubEntity(SirenEntityContainer objectNode, Object content, String name, String docUrl) throws InvocationTargetException, IllegalAccessException { Object bean; List<Link> links; if (content instanceof Resource) { bean = ((Resource) content).getContent(); links = ((Resource) content).getLinks(); } else if (content instanceof ResourceSupport) { bean = content; links = ((ResourceSupport) content).getLinks(); } else { bean = content; links = Collections.emptyList(); } Map<String, Object> properties = new HashMap<String, Object>(); List<String> rels = Collections.singletonList(docUrl != null ? docUrl : name); SirenEmbeddedRepresentation subEntity = new SirenEmbeddedRepresentation( getSirenClasses(bean), properties, null, toSirenActions(getActions(links)), toSirenLinks(getNavigationalLinks(links)), rels, null); //subEntity.setProperties(properties); objectNode.addSubEntity(subEntity); List<SirenEmbeddedLink> sirenEmbeddedLinks = toSirenEmbeddedLinks(getEmbeddedLinks(links)); for (SirenEmbeddedLink sirenEmbeddedLink : sirenEmbeddedLinks) { subEntity.addSubEntity(sirenEmbeddedLink); } createRecursiveSirenEntitiesFromPropertiesAndFields(subEntity, properties, bean); }
Resource<?> resource = (Resource<?>) object; if(!visitor.visitLinks(resource.getLinks())) { return;
Resource<?> resource = (Resource<?>) object; if(!visitor.visitLinks(resource.getLinks())) { return;
objectNode.addLinks(resource.getLinks()); toUberData(objectNode, resource.getContent()); return;
objectNode.addLinks(resource.getLinks()); toUberData(objectNode, resource.getContent()); return;
/** * 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()); } }
Resource<?> resource = (Resource<?>) object; objectNode.setLinks(this.toSirenLinks( getNavigationalLinks(resource.getLinks()))); objectNode.setEmbeddedLinks(this.toSirenEmbeddedLinks( getEmbeddedLinks(resource.getLinks()))); objectNode.setActions(this.toSirenActions(getActions(resource.getLinks()))); toSirenEntity(objectNode, resource.getContent()); return;
Resource<?> resource = (Resource<?>) object; objectNode.setLinks(this.toSirenLinks( getNavigationalLinks(resource.getLinks()))); objectNode.setEmbeddedLinks(this.toSirenEmbeddedLinks( getEmbeddedLinks(resource.getLinks()))); objectNode.setActions(this.toSirenActions(getActions(resource.getLinks()))); toSirenEntity(objectNode, resource.getContent()); return;
writer.writeLinks(resource.getLinks());
writer.writeLinks(resource.getLinks());