/** * Finally creates the {@link PersistentEntityResource} instance. * * @return */ public PersistentEntityResource build() { return new PersistentEntityResource(entity, content, links, embeddeds, false, false); }
/** * Creates a new {@link ETag} for the given {@link PersistentEntityResource}. * * @param resource must not be {@literal null}. * @return */ public static ETag from(PersistentEntityResource resource) { Assert.notNull(resource, "PersistentEntityResource must not be null!"); return from(resource.getPersistentEntity(), resource.getContent()); }
@JsonUnwrapped public Iterable<?> getEmbedded() { return resource.getEmbeddeds(); } };
private Links getLinks(PersistentEntityResource resource) { Object source = getLinkSource(resource.getContent()); return resource.isNested() ? collector.getLinksForNested(source, resource.getLinks()) : collector.getLinksFor(source, resource.getLinks()); }
/** * Returns the {@link PersistentPropertyAccessor} for the underlying content bean. * * @return */ public PersistentPropertyAccessor getPropertyAccessor() { return entity.getPropertyAccessor(getContent()); }
/** * <code>PUT /{repository}/{id}</code> - Updates an existing entity or creates one at exactly that place. * * @param resourceInformation * @param payload * @param id * @param assembler * @param eTag * @param acceptHeader * @return * @throws HttpRequestMethodNotSupportedException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.PUT) public ResponseEntity<? extends ResourceSupport> putItemResource(RootResourceInformation resourceInformation, PersistentEntityResource payload, @BackendId Serializable id, PersistentEntityResourceAssembler assembler, ETag eTag, @RequestHeader(value = ACCEPT_HEADER, required = false) String acceptHeader) throws HttpRequestMethodNotSupportedException { resourceInformation.verifySupportedMethod(HttpMethod.PUT, ResourceType.ITEM); if (payload.isNew()) { resourceInformation.verifyPutForCreation(); } RepositoryInvoker invoker = resourceInformation.getInvoker(); Object objectToSave = payload.getContent(); eTag.verify(resourceInformation.getPersistentEntity(), objectToSave); return payload.isNew() ? createAndReturn(objectToSave, invoker, assembler, config.returnBodyOnCreate(acceptHeader)) : saveAndReturn(objectToSave, invoker, PUT, assembler, config.returnBodyOnUpdate(acceptHeader)); }
private Resource<Object> toResource(Object value) { PersistentEntity<?, ?> entity = entities.getRequiredPersistentEntity(value.getClass()); return invoker.invokeProcessorsFor(PersistentEntityResource.build(value, entity).// withEmbedded(assembler.getEmbeddedResources(value)).// buildNested()); } }
@RequestMapping(value = BASE_MAPPING, method = GET) public ResponseEntity<ResourceSupport> followPropertyReference(final RootResourceInformation repoRequest, @BackendId Serializable id, final @PathVariable String property, final PersistentEntityResourceAssembler assembler) throws Exception { HttpHeaders headers = new HttpHeaders(); Function<ReferencedProperty, ResourceSupport> handler = prop -> prop.mapValue(it -> { if (prop.property.isCollectionLike()) { return toResources((Iterable<?>) it, assembler, prop.propertyType, Optional.empty()); } else if (prop.property.isMap()) { Map<Object, Resource<?>> resources = new HashMap<Object, Resource<?>>(); for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) it).entrySet()) { resources.put(entry.getKey(), assembler.toResource(entry.getValue())); } return new Resource<Object>(resources); } else { PersistentEntityResource resource = assembler.toResource(it); headers.set("Content-Location", resource.getId().getHref()); return resource; } }).orElseThrow(() -> new ResourceNotFoundException()); return ControllerUtils.toResponseEntity(HttpStatus.OK, headers, // doWithReferencedProperty(repoRequest, id, property, handler, HttpMethod.GET)); }
/** * <code>HEAD /{repository}/{id}</code> * * @param resourceInformation * @param id * @return * @throws HttpRequestMethodNotSupportedException * @since 2.2 */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.HEAD) public ResponseEntity<?> headForItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id, PersistentEntityResourceAssembler assembler) throws HttpRequestMethodNotSupportedException { return getItemResource(resourceInformation, id).map(it -> { Links links = new Links(assembler.toResource(it).getLinks()); HttpHeaders headers = headersPreparer.prepareHeaders(resourceInformation.getPersistentEntity(), it); headers.add(LINK_HEADER, links.toString()); return new ResponseEntity<Object>(headers, HttpStatus.NO_CONTENT); }).orElseThrow(() -> new ResourceNotFoundException()); }
private Links getLinks(PersistentEntityResource resource) { Object source = getLinkSource(resource.getContent()); return resource.isNested() ? collector.getLinksForNested(source, resource.getLinks()) : collector.getLinksFor(source, resource.getLinks()); }
@Override public PersistentEntityResource process(PersistentEntityResource resource) { Object content = resource.getContent(); if (null == content) { return resource; } Class<?> domainType = content.getClass(); for (DomainTypeResourceProcessor rp : resourceProcessors) { if (isAssignable(domainType, rp.domainType)) { rp.resourceProcessor.process(resource); } } return resource; }
/** * <code>PUT /{repository}/{id}</code> - Updates an existing entity or creates one at exactly that place. * * @param resourceInformation * @param payload * @param id * @param assembler * @param eTag * @param acceptHeader * @return * @throws HttpRequestMethodNotSupportedException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.PUT) public ResponseEntity<? extends ResourceSupport> putItemResource(RootResourceInformation resourceInformation, PersistentEntityResource payload, @BackendId Serializable id, PersistentEntityResourceAssembler assembler, ETag eTag, @RequestHeader(value = ACCEPT_HEADER, required = false) String acceptHeader) throws HttpRequestMethodNotSupportedException { resourceInformation.verifySupportedMethod(HttpMethod.PUT, ResourceType.ITEM); if (payload.isNew()) { resourceInformation.verifyPutForCreation(); } RepositoryInvoker invoker = resourceInformation.getInvoker(); Object objectToSave = payload.getContent(); eTag.verify(resourceInformation.getPersistentEntity(), objectToSave); return payload.isNew() ? createAndReturn(objectToSave, invoker, assembler, config.returnBodyOnCreate(acceptHeader)) : saveAndReturn(objectToSave, invoker, PUT, assembler, config.returnBodyOnUpdate(acceptHeader)); }
private Builder wrap(Object instance, Object source) { PersistentEntity<?, ?> entity = entities.getRequiredPersistentEntity(source.getClass()); return PersistentEntityResource.build(instance, entity).// withEmbedded(getEmbeddedResources(source)).// withLink(getSelfLinkFor(source)).// withLink(linkProvider.createSelfLinkFor(source)); }
/** * <code>HEAD /{repository}/{id}</code> * * @param resourceInformation * @param id * @return * @throws HttpRequestMethodNotSupportedException * @since 2.2 */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.HEAD) public ResponseEntity<?> headForItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id, PersistentEntityResourceAssembler assembler) throws HttpRequestMethodNotSupportedException { return getItemResource(resourceInformation, id).map(it -> { Links links = new Links(assembler.toResource(it).getLinks()); HttpHeaders headers = headersPreparer.prepareHeaders(resourceInformation.getPersistentEntity(), it); headers.add(LINK_HEADER, links.toString()); return new ResponseEntity<Object>(headers, HttpStatus.NO_CONTENT); }).orElseThrow(() -> new ResourceNotFoundException()); }
/** * Returns the default headers to be returned for the given {@link PersistentEntityResource}. Will set {@link ETag} * and {@code Last-Modified} headers if applicable. * * @param resource can be {@literal null}. * @return */ public HttpHeaders prepareHeaders(Optional<PersistentEntityResource> resource) { return resource// .map(it -> prepareHeaders(it.getPersistentEntity(), it.getContent()))// .orElseGet(() -> new HttpHeaders()); }
/** * <code>POST /{repository}</code> - Creates a new entity instances from the collection resource. * * @param resourceInformation * @param payload * @param assembler * @param acceptHeader * @return * @throws HttpRequestMethodNotSupportedException */ @ResponseBody @RequestMapping(value = BASE_MAPPING, method = RequestMethod.POST) public ResponseEntity<ResourceSupport> postCollectionResource(RootResourceInformation resourceInformation, PersistentEntityResource payload, PersistentEntityResourceAssembler assembler, @RequestHeader(value = ACCEPT_HEADER, required = false) String acceptHeader) throws HttpRequestMethodNotSupportedException { resourceInformation.verifySupportedMethod(HttpMethod.POST, ResourceType.COLLECTION); return createAndReturn(payload.getContent(), resourceInformation.getInvoker(), assembler, config.returnBodyOnCreate(acceptHeader)); }
}); Builder build = PersistentEntityResource.build(obj, entity); return forUpdate ? build.build() : build.forCreation();
/** * Finally creates the {@link PersistentEntityResource} instance to symbolize the contained entity is about to be * created. * * @return */ public PersistentEntityResource forCreation() { return new PersistentEntityResource(entity, content, links, embeddeds, true, false); }
@RequestMapping(value = BASE_MAPPING, method = GET) public ResponseEntity<ResourceSupport> followPropertyReference(final RootResourceInformation repoRequest, @BackendId Serializable id, final @PathVariable String property, final PersistentEntityResourceAssembler assembler) throws Exception { HttpHeaders headers = new HttpHeaders(); Function<ReferencedProperty, ResourceSupport> handler = prop -> prop.mapValue(it -> { if (prop.property.isCollectionLike()) { return toResources((Iterable<?>) it, assembler, prop.propertyType, Optional.empty()); } else if (prop.property.isMap()) { Map<Object, Resource<?>> resources = new HashMap<Object, Resource<?>>(); for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) it).entrySet()) { resources.put(entry.getKey(), assembler.toResource(entry.getValue())); } return new Resource<Object>(resources); } else { PersistentEntityResource resource = assembler.toResource(it); headers.set("Content-Location", resource.getId().getHref()); return resource; } }).orElseThrow(() -> new ResourceNotFoundException()); return ControllerUtils.toResponseEntity(HttpStatus.OK, headers, // doWithReferencedProperty(repoRequest, id, property, handler, HttpMethod.GET)); }