/** * Converts all given entities into resources and wraps the result in a {@link Resources} instance. * * @see #toListOfResources() and {@link ResourceAssembler#toResource(Object)} * @return */ public Resources<D> toResources() { return new Resources<>(toListOfResources()); } }
@Override public boolean hasSingleElement(Resources<?> value) { return value.getContent().size() == 1; }
/** * Creates a {@link Resources} instance with the given content and {@link Link}s. * * @param content must not be {@literal null}. * @param links the links to be added to the {@link Resources}. */ public Resources(Iterable<T> content, Iterable<Link> links) { Assert.notNull(content, "Content must not be null!"); this.content = new ArrayList<>(); for (T element : content) { this.content.add(element); } this.add(links); }
@Override public PagedResources<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JavaType rootType = JacksonHelper.findRootType(this.contentType); UberDocument doc = p.getCodec().readValue(p, UberDocument.class); Resources<?> resources = extractResources(doc, rootType, this.contentType); PageMetadata pageMetadata = extractPagingMetadata(doc); return new PagedResources<>(resources.getContent(), pageMetadata, resources.getLinks()); }
@ResponseBody @SuppressWarnings({ "unchecked" }) @RequestMapping(value = BASE_MAPPING, method = RequestMethod.GET, produces = { "application/x-spring-data-compact+json", "text/uri-list" }) public Resources<?> getCollectionResourceCompact(@QuerydslPredicate RootResourceInformation resourceinformation, DefaultedPageable pageable, Sort sort, PersistentEntityResourceAssembler assembler) throws ResourceNotFoundException, HttpRequestMethodNotSupportedException { Resources<?> resources = getCollectionResource(resourceinformation, pageable, sort, assembler); List<Link> links = new ArrayList<Link>(resources.getLinks()); for (Resource<?> resource : ((Resources<Resource<?>>) resources).getContent()) { PersistentEntityResource persistentEntityResource = (PersistentEntityResource) resource; links.add(resourceLink(resourceinformation, persistentEntityResource)); } if (resources instanceof PagedResources) { return new PagedResources<Object>(Collections.emptyList(), ((PagedResources<?>) resources).getMetadata(), links); } else { return new Resources<Object>(Collections.emptyList(), links); } }
if (resource instanceof Resources && ((Resources<?>) resource).getContent() != null) { for (Object obj : ((Resources<?>) resource).getContent()) { if (null != obj && obj instanceof Resource) { Resource<?> res = (Resource<?>) obj; return new Resources<Resource<?>>(EMPTY_RESOURCE_LIST, links);
@RequestMapping(method = GET, value = "/{user}/customers") Resources<Resource<Customer>> loadUserCustomers(@PathVariable Long user) { List<Resource<Customer>> customers = this.crmService.loadCustomerAccounts(user).parallelStream() .map(customerResourceAssembler::toResource) .collect(Collectors.toList()); Resources<Resource<Customer>> customerResources = new Resources<Resource<Customer>>(customers); customerResources.add(linkTo(methodOn(UserController.class).loadUserCustomers(user)).withSelfRel()); return customerResources; }
@PathVariable String property) throws Exception { Resources<Object> source = incoming == null ? new Resources<Object>(Collections.emptyList()) : incoming; RepositoryInvoker invoker = resourceInformation.getInvoker(); for (Link l1 : source.getLinks()) { collection.add(loadPropertyValue(prop.propertyType, l1)); for (Link l2 : source.getLinks()) { map.put(l2.getRel(), loadPropertyValue(prop.propertyType, l2)); if (source.getLinks().size() != 1) { throw new IllegalArgumentException( "Must send only 1 link to update a property reference that isn't a List or a Map."); prop.accessor.setProperty(prop.property, loadPropertyValue(prop.propertyType, source.getLinks().get(0)));
@Override public Resources<Resource<Address>> process(Resources<Resource<Address>> resources) { String[] linkParts = resources.getLink("self").getHref().split("/"); String customerId = linkParts[linkParts.length - 2]; try { UUID.fromString(customerId); resources.add(linkTo(methodOn(AddressesCommandsController.class).getCommands(customerId)).withRel("commands")); } catch (Exception e) {} return resources; } }
@Override public Resources<Resource<OrderItem>> process(Resources<Resource<OrderItem>> resources) { String orderId = new ResourceURI(resources.getId().getHref()).getParentId(); try { UUID.fromString(orderId); resources.add(linkTo(methodOn(OrderItemsCommandsController.class).getCommands(orderId)) .withRel("commands").expand(orderId)); } catch (Exception e) {} return resources; } }
@Override @JsonIgnore public List<Link> getLinks() { return super.getLinks(); } }
@Override public void serialize(Resources<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException { CollectionJson<?> collectionJson = new CollectionJson() .withVersion("1.0") .withHref(value.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()) .withLinks(withoutSelfLink(value.getLinks())) .withItems(resourcesToCollectionJsonItems(value)) .withQueries(findQueries(value)) .withTemplate(findTemplate(value)); CollectionJsonDocument<?> doc = new CollectionJsonDocument<>(collectionJson); provider .findValueSerializer(CollectionJsonDocument.class, property) .serialize(doc, jgen, provider); }
@RequestMapping(method = GET, value = "/{user}/customers") Resources<Resource<Customer>> loadUserCustomers(@PathVariable Long user) { List<Resource<Customer>> customers = this.crmService.loadCustomerAccounts(user).parallelStream() .map(customerResourceAssembler::toResource) .collect(Collectors.toList()); Resources<Resource<Customer>> customerResources = new Resources<Resource<Customer>>(customers); customerResources.add(linkTo(methodOn(UserController.class).loadUserCustomers(user)).withSelfRel()); return customerResources; }
} else if (object instanceof Resources) { Resources<?> resources = (Resources<?>) object; if(!visitor.visitLinks(resources.getLinks())) { return; traverseResource(visitor, resources.getContent()); return; } else if (object instanceof ResourceSupport) {
@ResponseBody @SuppressWarnings({ "unchecked" }) @RequestMapping(value = BASE_MAPPING, method = RequestMethod.GET, produces = { "application/x-spring-data-compact+json", "text/uri-list" }) public Resources<?> getCollectionResourceCompact(@QuerydslPredicate RootResourceInformation resourceinformation, DefaultedPageable pageable, Sort sort, PersistentEntityResourceAssembler assembler) throws ResourceNotFoundException, HttpRequestMethodNotSupportedException { Resources<?> resources = getCollectionResource(resourceinformation, pageable, sort, assembler); List<Link> links = new ArrayList<Link>(resources.getLinks()); for (Resource<?> resource : ((Resources<Resource<?>>) resources).getContent()) { PersistentEntityResource persistentEntityResource = (PersistentEntityResource) resource; links.add(resourceLink(resourceinformation, persistentEntityResource)); } if (resources instanceof PagedResources) { return new PagedResources<Object>(Collections.emptyList(), ((PagedResources<?>) resources).getMetadata(), links); } else { return new Resources<Object>(Collections.emptyList(), links); } }
@PathVariable String property) throws Exception { Resources<Object> source = incoming == null ? new Resources<Object>(Collections.emptyList()) : incoming; RepositoryInvoker invoker = resourceInformation.getInvoker(); for (Link l1 : source.getLinks()) { collection.add(loadPropertyValue(prop.propertyType, l1)); for (Link l2 : source.getLinks()) { map.put(l2.getRel(), loadPropertyValue(prop.propertyType, l2)); if (source.getLinks().size() != 1) { throw new IllegalArgumentException( "Must send only 1 link to update a property reference that isn't a List or a Map."); prop.accessor.setProperty(prop.property, loadPropertyValue(prop.propertyType, source.getLinks().get(0)));
@Override public Resources<Address> process(Resources<Address> resources) { String[] linkParts = resources.getLink("self").getHref().split("/"); String customerId = linkParts[linkParts.length - 2]; try { UUID.fromString(customerId); resources.add(linkTo(methodOn(AddressesCommandsController.class).getCommands(customerId)).withRel("commands")); } catch (Exception e) {} return resources; } }
@Override public Resources<OrderItem> process(Resources<OrderItem> resources) { String orderId = new ResourceURI(resources.getId().getHref()).getParentId(); try { UUID.fromString(orderId); resources.add(linkTo(methodOn(OrderItemsCommandsController.class).getCommands(orderId)) .withRel("commands").expand(orderId)); } catch (Exception e) {} return resources; } }
@Override public void serialize(Resources<?> value, JsonGenerator gen, SerializerProvider provider) throws IOException { Map<String, Object> embeddeds = embeddedMapper.map(value); HalFormsDocument<?> doc; if (value instanceof PagedResources) { doc = HalFormsDocument.empty() // .withEmbedded(embeddeds) // .withPageMetadata(((PagedResources<?>) value).getMetadata()) // .withLinks(value.getLinks()) // .withTemplates(findTemplates(value)); } else { doc = HalFormsDocument.empty() // .withEmbedded(embeddeds) // .withLinks(value.getLinks()) // .withTemplates(findTemplates(value)); } provider .findValueSerializer(HalFormsDocument.class, property) .serialize(doc, gen, provider); }
@GetMapping public ResponseEntity<Resources<PersonResource>> all() { final List<PersonResource> collection = personRepository.findAll().stream().map(PersonResource::new).collect(Collectors.toList()); final Resources<PersonResource> resources = new Resources<>(collection); final String uriString = ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString(); resources.add(new Link(uriString, "self")); return ResponseEntity.ok(resources); }