Refine search
/** * Exposes a JSON schema for the repository referenced. * * @param resourceInformation will never be {@literal null}. * @return */ @RequestMapping(value = ProfileController.RESOURCE_PROFILE_MAPPING, method = GET, produces = RestMediaTypes.SCHEMA_JSON_VALUE) public HttpEntity<JsonSchema> schema(RootResourceInformation resourceInformation) { JsonSchema schema = jsonSchemaConverter.convert(resourceInformation.getDomainType()); return new ResponseEntity<JsonSchema>(schema, HttpStatus.OK); } }
@Override public RootResourceInformation resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { ResourceMetadata resourceMetadata = resourceMetadataResolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory); Class<?> domainType = resourceMetadata.getDomainType(); RepositoryInvoker repositoryInvoker = invokerFactory.getInvokerFor(domainType); PersistentEntity<?, ?> persistentEntity = repositories.getPersistentEntity(domainType); // TODO reject if ResourceMetadata cannot be resolved return new RootResourceInformation(resourceMetadata, persistentEntity, postProcess(parameter, repositoryInvoker, domainType, webRequest.getParameterMap())); }
/** * <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)); }
/** * Returns the object backing the item resource for the given {@link RootResourceInformation} and id. * * @param resourceInformation * @param id * @return * @throws HttpRequestMethodNotSupportedException * @throws {@link ResourceNotFoundException} */ private Optional<Object> getItemResource(RootResourceInformation resourceInformation, Serializable id) throws HttpRequestMethodNotSupportedException, ResourceNotFoundException { resourceInformation.verifySupportedMethod(HttpMethod.GET, ResourceType.ITEM); return resourceInformation.getInvoker().invokeFindById(id); } }
/** * <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)); }
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(); }
@RequestMapping(value = BASE_MAPPING + "/{propertyId}", method = DELETE) public ResponseEntity<ResourceSupport> deletePropertyReferenceId(RootResourceInformation repoRequest, @BackendId Serializable backendId, @PathVariable String property, @PathVariable String propertyId) Object result = repoRequest.getInvoker().invokeSave(prop.accessor.getBean()); publisher.publishEvent(new AfterLinkDeleteEvent(result, it));
/** * <code>OPTIONS /{repository}</code>. * * @param information * @return * @since 2.2 */ @RequestMapping(value = BASE_MAPPING, method = RequestMethod.OPTIONS) public ResponseEntity<?> optionsForCollectionResource(RootResourceInformation information) { HttpHeaders headers = new HttpHeaders(); SupportedHttpMethods supportedMethods = information.getSupportedMethods(); headers.setAllow(supportedMethods.getMethodsFor(ResourceType.COLLECTION).toSet()); return new ResponseEntity<Object>(headers, HttpStatus.OK); }
/** * <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)); }
/** * <code>GET /{repository}</code> - Returns the collection resource (paged or unpaged). * * @param resourceInformation * @param pageable * @param sort * @param assembler * @return * @throws ResourceNotFoundException * @throws HttpRequestMethodNotSupportedException */ @ResponseBody @RequestMapping(value = BASE_MAPPING, method = RequestMethod.GET) public Resources<?> getCollectionResource(@QuerydslPredicate RootResourceInformation resourceInformation, DefaultedPageable pageable, Sort sort, PersistentEntityResourceAssembler assembler) throws ResourceNotFoundException, HttpRequestMethodNotSupportedException { resourceInformation.verifySupportedMethod(HttpMethod.GET, ResourceType.COLLECTION); RepositoryInvoker invoker = resourceInformation.getInvoker(); if (null == invoker) { throw new ResourceNotFoundException(); } Iterable<?> results = pageable.getPageable() != null ? invoker.invokeFindAll(pageable.getPageable()) : invoker.invokeFindAll(sort); ResourceMetadata metadata = resourceInformation.getResourceMetadata(); Optional<Link> baseLink = Optional.of(entityLinks.linkToPagedResource(resourceInformation.getDomainType(), pageable.isDefault() ? null : pageable.getPageable())); Resources<?> result = toResources(results, assembler, metadata.getDomainType(), baseLink); result.add(getCollectionResourceLinks(resourceInformation, pageable)); return result; }
/** * Executes the search with the given name. * * @param resourceInformation * @param parameters * @param search * @param pageable * @param sort * @param assembler * @return * @throws ResourceNotFoundException */ @ResponseBody @RequestMapping(value = BASE_MAPPING + "/{search}", method = RequestMethod.GET) public ResponseEntity<?> executeSearch(RootResourceInformation resourceInformation, @RequestParam MultiValueMap<String, Object> parameters, @PathVariable String search, DefaultedPageable pageable, Sort sort, PersistentEntityResourceAssembler assembler, @RequestHeader HttpHeaders headers) { Method method = checkExecutability(resourceInformation, search); Optional<Object> result = executeQueryMethod(resourceInformation.getInvoker(), parameters, method, pageable, sort, assembler); SearchResourceMappings searchMappings = resourceInformation.getSearchMappings(); MethodResourceMapping methodMapping = searchMappings.getExportedMethodMappingForPath(search); Class<?> domainType = methodMapping.getReturnedDomainType(); return toResource(result, assembler, domainType, Optional.empty(), headers, resourceInformation); }
@RequestMapping(value = BASE_MAPPING, method = GET, produces = { SPRING_DATA_COMPACT_JSON_VALUE, TEXT_URI_LIST_VALUE }) public ResponseEntity<ResourceSupport> followPropertyReferenceCompact(RootResourceInformation repoRequest, ResourceMetadata repoMapping = repoRequest.getResourceMetadata(); PersistentProperty<?> persistentProp = repoRequest.getPersistentEntity().getRequiredPersistentProperty(property); ResourceMapping propertyMapping = repoMapping.getMappingFor(persistentProp);
@RequestMapping(value = BASE_MAPPING + "/{search}", method = RequestMethod.GET, // produces = { "application/x-spring-data-compact+json" }) public ResourceSupport executeSearchCompact(RootResourceInformation resourceInformation, Optional<Object> result = executeQueryMethod(resourceInformation.getInvoker(), parameters, method, pageable, sort, assembler); ResourceMetadata metadata = resourceInformation.getResourceMetadata(); ResponseEntity<?> entity = toResource(result, assembler, metadata.getDomainType(), Optional.empty(), headers, resourceInformation);
IncomingRequest incoming = new IncomingRequest(request); Class<?> domainType = resourceInformation.getDomainType(); MediaType contentType = request.getHeaders().getContentType(); Optional<Object> objectToUpdate = id.flatMap(it -> resourceInformation.getInvoker().invokeFindById(it)); PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity(); boolean forUpdate = objectToUpdate.isPresent(); Optional<Object> entityIdentifier = objectToUpdate.map(it -> entity.getIdentifierAccessor(it).getIdentifier());
private Optional<ResourceSupport> doWithReferencedProperty(RootResourceInformation resourceInformation, Serializable id, String propertyPath, Function<ReferencedProperty, ResourceSupport> handler, HttpMethod method) throws Exception { ResourceMetadata metadata = resourceInformation.getResourceMetadata(); PropertyAwareResourceMapping mapping = metadata.getProperty(propertyPath); if (mapping == null || !mapping.isExported()) { throw new ResourceNotFoundException(); } PersistentProperty<?> property = mapping.getProperty(); resourceInformation.verifySupportedMethod(method, property); RepositoryInvoker invoker = resourceInformation.getInvoker(); Optional<Object> domainObj = invoker.invokeFindById(id); domainObj.orElseThrow(() -> new ResourceNotFoundException()); return domainObj.map(it -> { PersistentPropertyAccessor accessor = property.getOwner().getPropertyAccessor(it); return handler.apply(new ReferencedProperty(property, accessor.getProperty(property), accessor)); }); }
private Object read(IncomingRequest request, HttpMessageConverter<Object> converter, RootResourceInformation information) { try { return converter.read(information.getDomainType(), request.getServerHttpRequest()); } catch (IOException o_O) { throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, information.getDomainType()), o_O); } } }
public void verifyPutForCreation() throws HttpRequestMethodNotSupportedException { SupportedHttpMethods supportedHttpMethods = resourceMetadata.getSupportedHttpMethods(); if (!supportedHttpMethods.allowsPutForCreation()) { reject(HttpMethod.PUT, supportedHttpMethods.getMethodsFor(ResourceType.ITEM)); } }
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); }
/** * 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()); }
/** * Verifies that the given {@link RootResourceInformation} has searches exposed. * * @param resourceInformation */ private static SearchResourceMappings verifySearchesExposed(RootResourceInformation resourceInformation) { SearchResourceMappings resourceMappings = resourceInformation.getSearchMappings(); if (!resourceMappings.isExported()) { throw new ResourceNotFoundException(); } return resourceMappings; }