/** * <code>GET /{repository}/{id}</code> - Returns a single entity. * * @param resourceInformation * @param id * @return * @throws HttpRequestMethodNotSupportedException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.GET) public ResponseEntity<Resource<?>> getItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id, final PersistentEntityResourceAssembler assembler, @RequestHeader HttpHeaders headers) throws HttpRequestMethodNotSupportedException { return getItemResource(resourceInformation, id).map(it -> { PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); return resourceStatus.getStatusAndHeaders(headers, it, entity).toResponseEntity(// () -> assembler.toFullResource(it)); }).orElseGet(() -> new ResponseEntity<Resource<?>>(HttpStatus.NOT_FOUND)); }
/** * Turns the given source into a {@link ResourceSupport} if needed and possible. Uses the given * {@link PersistentEntityResourceAssembler} for the actual conversion. * * @param source can be must not be {@literal null}. * @param assembler must not be {@literal null}. * @param domainType the domain type in case the source is an empty iterable, must not be {@literal null}. * @param baseLink can be {@literal null}. * @return */ protected ResponseEntity<?> toResource(Optional<Object> source, final PersistentEntityResourceAssembler assembler, Class<?> domainType, Optional<Link> baseLink, HttpHeaders headers, RootResourceInformation information) { return source.map(it -> { if (it instanceof Iterable) { return ResponseEntity.ok(toResources((Iterable<?>) it, assembler, domainType, baseLink)); } else if (ClassUtils.isPrimitiveOrWrapper(it.getClass())) { return ResponseEntity.ok(it); } PersistentEntity<?, ?> entity = information.getPersistentEntity(); // Returned value is not of the aggregates type - probably some projection if (!entity.getType().isInstance(it)) { return ResponseEntity.ok(it); } return resourceStatus.getStatusAndHeaders(headers, it, entity).toResponseEntity(// () -> assembler.toFullResource(it)); }).orElseThrow(() -> new ResourceNotFoundException()); }
public Alps convert(RootResourceInformation resourceInformation) { Class<?> type = resourceInformation.getDomainType(); List<Descriptor> descriptors = new ArrayList<Descriptor>(); Descriptor representationDescriptor = buildRepresentationDescriptor(type); descriptors.add(representationDescriptor); SupportedHttpMethods supportedHttpMethods = resourceInformation.getSupportedMethods(); for (HttpMethod method : supportedHttpMethods.getMethodsFor(ResourceType.COLLECTION)) { if (!UNDOCUMENTED_METHODS.contains(method)) { descriptors.add(buildCollectionResourceDescriptor(type, resourceInformation, representationDescriptor, method)); } } for (HttpMethod method : supportedHttpMethods.getMethodsFor(ResourceType.ITEM)) { if (!UNDOCUMENTED_METHODS.contains(method)) { descriptors.add(buildItemResourceDescriptor(resourceInformation, representationDescriptor, method)); } } descriptors.addAll(buildSearchResourceDescriptors(resourceInformation.getPersistentEntity())); return Alps.alps().descriptors(descriptors).build(); }
/** * <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()); }
/** * <code>DELETE /{repository}/{id}</code> - Deletes the entity backing the item resource. * * @param resourceInformation * @param id * @param eTag * @return * @throws ResourceNotFoundException * @throws HttpRequestMethodNotSupportedException * @throws ETagDoesntMatchException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.DELETE) public ResponseEntity<?> deleteItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id, ETag eTag) throws ResourceNotFoundException, HttpRequestMethodNotSupportedException { resourceInformation.verifySupportedMethod(HttpMethod.DELETE, ResourceType.ITEM); RepositoryInvoker invoker = resourceInformation.getInvoker(); Optional<Object> domainObj = invoker.invokeFindById(id); return domainObj.map(it -> { PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); eTag.verify(entity, it); publisher.publishEvent(new BeforeDeleteEvent(it)); invoker.invokeDeleteById(entity.getIdentifierAccessor(it).getIdentifier()); publisher.publishEvent(new AfterDeleteEvent(it)); return new ResponseEntity<Object>(HttpStatus.NO_CONTENT); }).orElseThrow(() -> new ResourceNotFoundException()); }
PersistentProperty<?> persistentProp = repoRequest.getPersistentEntity().getRequiredPersistentProperty(property); ResourceMapping propertyMapping = repoMapping.getMappingFor(persistentProp);
/** * <code>PATCH /{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 * @throws ResourceNotFoundException * @throws ETagDoesntMatchException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.PATCH) public ResponseEntity<ResourceSupport> patchItemResource(RootResourceInformation resourceInformation, PersistentEntityResource payload, @BackendId Serializable id, PersistentEntityResourceAssembler assembler, ETag eTag, @RequestHeader(value = ACCEPT_HEADER, required = false) String acceptHeader) throws HttpRequestMethodNotSupportedException, ResourceNotFoundException { resourceInformation.verifySupportedMethod(HttpMethod.PATCH, ResourceType.ITEM); Object domainObject = payload.getContent(); eTag.verify(resourceInformation.getPersistentEntity(), domainObject); return saveAndReturn(domainObject, resourceInformation.getInvoker(), PATCH, assembler, config.returnBodyOnUpdate(acceptHeader)); }
PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); boolean forUpdate = objectToUpdate.isPresent(); Optional<Object> entityIdentifier = objectToUpdate.map(it -> entity.getIdentifierAccessor(it).getIdentifier());
private Descriptor buildItemResourceDescriptor(RootResourceInformation resourceInformation, Descriptor representationDescriptor, HttpMethod method) { PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); ResourceMetadata metadata = associations.getMetadataFor(entity.getType()); return descriptor().// id(prefix(method).concat(metadata.getItemResourceRel())).// name(metadata.getItemResourceRel()).// type(getType(method)).// doc(getDocFor(metadata.getItemResourceDescription())).// rt("#".concat(representationDescriptor.getId())). // descriptors(getProjectionDescriptor(entity.getType(), method)).// build(); }
/** * <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)); }
/** * <code>GET /{repository}/{id}</code> - Returns a single entity. * * @param resourceInformation * @param id * @return * @throws HttpRequestMethodNotSupportedException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.GET) public ResponseEntity<Resource<?>> getItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id, final PersistentEntityResourceAssembler assembler, @RequestHeader HttpHeaders headers) throws HttpRequestMethodNotSupportedException { return getItemResource(resourceInformation, id).map(it -> { PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); return resourceStatus.getStatusAndHeaders(headers, it, entity).toResponseEntity(// () -> assembler.toFullResource(it)); }).orElseGet(() -> new ResponseEntity<Resource<?>>(HttpStatus.NOT_FOUND)); }
/** * Turns the given source into a {@link ResourceSupport} if needed and possible. Uses the given * {@link PersistentEntityResourceAssembler} for the actual conversion. * * @param source can be must not be {@literal null}. * @param assembler must not be {@literal null}. * @param domainType the domain type in case the source is an empty iterable, must not be {@literal null}. * @param baseLink can be {@literal null}. * @return */ protected ResponseEntity<?> toResource(Optional<Object> source, final PersistentEntityResourceAssembler assembler, Class<?> domainType, Optional<Link> baseLink, HttpHeaders headers, RootResourceInformation information) { return source.map(it -> { if (it instanceof Iterable) { return ResponseEntity.ok(toResources((Iterable<?>) it, assembler, domainType, baseLink)); } else if (ClassUtils.isPrimitiveOrWrapper(it.getClass())) { return ResponseEntity.ok(it); } PersistentEntity<?, ?> entity = information.getPersistentEntity(); // Returned value is not of the aggregates type - probably some projection if (!entity.getType().isInstance(it)) { return ResponseEntity.ok(it); } return resourceStatus.getStatusAndHeaders(headers, it, entity).toResponseEntity(// () -> assembler.toFullResource(it)); }).orElseThrow(() -> new ResourceNotFoundException()); }
/** * <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()); }
public Alps convert(RootResourceInformation resourceInformation) { Class<?> type = resourceInformation.getDomainType(); List<Descriptor> descriptors = new ArrayList<Descriptor>(); Descriptor representationDescriptor = buildRepresentationDescriptor(type); descriptors.add(representationDescriptor); SupportedHttpMethods supportedHttpMethods = resourceInformation.getSupportedMethods(); for (HttpMethod method : supportedHttpMethods.getMethodsFor(ResourceType.COLLECTION)) { if (!UNDOCUMENTED_METHODS.contains(method)) { descriptors.add(buildCollectionResourceDescriptor(type, resourceInformation, representationDescriptor, method)); } } for (HttpMethod method : supportedHttpMethods.getMethodsFor(ResourceType.ITEM)) { if (!UNDOCUMENTED_METHODS.contains(method)) { descriptors.add(buildItemResourceDescriptor(resourceInformation, representationDescriptor, method)); } } descriptors.addAll(buildSearchResourceDescriptors(resourceInformation.getPersistentEntity())); return Alps.alps().descriptors(descriptors).build(); }
/** * <code>DELETE /{repository}/{id}</code> - Deletes the entity backing the item resource. * * @param resourceInformation * @param id * @param eTag * @return * @throws ResourceNotFoundException * @throws HttpRequestMethodNotSupportedException * @throws ETagDoesntMatchException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.DELETE) public ResponseEntity<?> deleteItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id, ETag eTag) throws ResourceNotFoundException, HttpRequestMethodNotSupportedException { resourceInformation.verifySupportedMethod(HttpMethod.DELETE, ResourceType.ITEM); RepositoryInvoker invoker = resourceInformation.getInvoker(); Optional<Object> domainObj = invoker.invokeFindById(id); return domainObj.map(it -> { PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); eTag.verify(entity, it); publisher.publishEvent(new BeforeDeleteEvent(it)); invoker.invokeDeleteById(entity.getIdentifierAccessor(it).getIdentifier()); publisher.publishEvent(new AfterDeleteEvent(it)); return new ResponseEntity<Object>(HttpStatus.NO_CONTENT); }).orElseThrow(() -> new ResourceNotFoundException()); }
PersistentProperty<?> persistentProp = repoRequest.getPersistentEntity().getRequiredPersistentProperty(property); ResourceMapping propertyMapping = repoMapping.getMappingFor(persistentProp);
/** * <code>PATCH /{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 * @throws ResourceNotFoundException * @throws ETagDoesntMatchException */ @RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.PATCH) public ResponseEntity<ResourceSupport> patchItemResource(RootResourceInformation resourceInformation, PersistentEntityResource payload, @BackendId Serializable id, PersistentEntityResourceAssembler assembler, ETag eTag, @RequestHeader(value = ACCEPT_HEADER, required = false) String acceptHeader) throws HttpRequestMethodNotSupportedException, ResourceNotFoundException { resourceInformation.verifySupportedMethod(HttpMethod.PATCH, ResourceType.ITEM); Object domainObject = payload.getContent(); eTag.verify(resourceInformation.getPersistentEntity(), domainObject); return saveAndReturn(domainObject, resourceInformation.getInvoker(), PATCH, assembler, config.returnBodyOnUpdate(acceptHeader)); }
PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); boolean forUpdate = objectToUpdate.isPresent(); Optional<Object> entityIdentifier = objectToUpdate.map(it -> entity.getIdentifierAccessor(it).getIdentifier());
private Descriptor buildItemResourceDescriptor(RootResourceInformation resourceInformation, Descriptor representationDescriptor, HttpMethod method) { PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); ResourceMetadata metadata = associations.getMetadataFor(entity.getType()); return descriptor().// id(prefix(method).concat(metadata.getItemResourceRel())).// name(metadata.getItemResourceRel()).// type(getType(method)).// doc(getDocFor(metadata.getItemResourceDescription())).// rt("#".concat(representationDescriptor.getId())). // descriptors(getProjectionDescriptor(entity.getType(), method)).// build(); }
/** * <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)); }