private void verifyAlpsEnabled() { if (!configuration.getMetadataConfiguration().alpsEnabled()) { throw 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; }
/** * Checks that the given request is actually executable. Will reject execution if we don't find a search with the * given name. * * @param resourceInformation * @param searchName * @return */ private Method checkExecutability(RootResourceInformation resourceInformation, String searchName) { SearchResourceMappings searchMapping = verifySearchesExposed(resourceInformation); Method method = searchMapping.getMappedMethod(searchName); if (method == null) { throw new ResourceNotFoundException(); } return method; }
/** * <code>HEAD /{repository}</code> * * @param resourceInformation * @return * @throws HttpRequestMethodNotSupportedException * @since 2.2 */ @RequestMapping(value = BASE_MAPPING, method = RequestMethod.HEAD) public ResponseEntity<?> headCollectionResource(RootResourceInformation resourceInformation, DefaultedPageable pageable) throws HttpRequestMethodNotSupportedException { resourceInformation.verifySupportedMethod(HttpMethod.HEAD, ResourceType.COLLECTION); RepositoryInvoker invoker = resourceInformation.getInvoker(); if (null == invoker) { throw new ResourceNotFoundException(); } List<Link> links = getCollectionResourceLinks(resourceInformation, pageable); links.add(0, getDefaultSelfLink()); HttpHeaders headers = new HttpHeaders(); headers.add(LINK_HEADER, new Links(links).toString()); return new ResponseEntity<Object>(headers, HttpStatus.NO_CONTENT); }
/** * 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()); }
@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()); }
/** * Verifies that the given {@link HttpMethod} is supported for the given {@link ResourceType}. * * @param httpMethod must not be {@literal null}. * @param resourceType must not be {@literal null}. * @throws ResourceNotFoundException if the repository is not exported at all. * @throws HttpRequestMethodNotSupportedException if the {@link ResourceType} does not support the given * {@link HttpMethod}. Will contain all supported methods as indicators for clients. */ public void verifySupportedMethod(HttpMethod httpMethod, ResourceType resourceType) throws HttpRequestMethodNotSupportedException, ResourceNotFoundException { Assert.notNull(httpMethod, "HTTP method must not be null!"); Assert.notNull(resourceType, "Resource type must not be null!"); if (!resourceMetadata.isExported()) { throw new ResourceNotFoundException(); } SupportedHttpMethods httpMethods = resourceMetadata.getSupportedHttpMethods(); HttpMethods supportedMethods = httpMethods.getMethodsFor(resourceType); if (!supportedMethods.contains(httpMethod)) { reject(httpMethod, supportedMethods); } }
/** * Verifies that the given {@link HttpMethod} is supported for the given {@link PersistentProperty}. * * @param httpMethod must not be {@literal null}. * @param property must not be {@literal null}. * @throws ResourceNotFoundException if the repository is not exported at all. * @throws HttpRequestMethodNotSupportedException if the {@link PersistentProperty} does not support the given * {@link HttpMethod}. Will contain all supported methods as indicators for clients. */ public void verifySupportedMethod(HttpMethod httpMethod, PersistentProperty<?> property) throws HttpRequestMethodNotSupportedException { Assert.notNull(httpMethod, "HTTP method must not be null!"); Assert.notNull(property, "Resource type must not be null!"); if (!resourceMetadata.isExported()) { throw new ResourceNotFoundException(); } SupportedHttpMethods httpMethods = resourceMetadata.getSupportedHttpMethods(); HttpMethods supportedMethods = httpMethods.getMethodsFor(property); if (!supportedMethods.contains(httpMethod)) { reject(httpMethod, supportedMethods); } }
throw new ResourceNotFoundException(); }).orElseThrow(() -> new ResourceNotFoundException());
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)); }); }
/** * Reads the given {@link ServerHttpRequest} into an object of the type of the given {@link RootResourceInformation}, * potentially applying the content to an object of the given id. * * @param information must not be {@literal null}. * @param request must not be {@literal null}. * @param converter must not be {@literal null}. * @param id must not be {@literal null}. * @return */ private Object read(RootResourceInformation information, IncomingRequest request, HttpMessageConverter<Object> converter, Optional<Object> objectToUpdate) { // JSON + PATCH request if (request.isPatchRequest() && converter instanceof MappingJackson2HttpMessageConverter) { return objectToUpdate.map(it -> { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); return readPatch(request, mapper, it); }).orElseThrow(() -> new ResourceNotFoundException()); // JSON + PUT request } else if (converter instanceof MappingJackson2HttpMessageConverter) { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); return objectToUpdate.map(it -> readPutForUpdate(request, mapper, it))// .orElseGet(() -> read(request, converter, information)); } // Catch all return read(request, converter, information); }
/** * <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()); }
/** * <code>GET /{repository}/search</code> - Exposes links to the individual search resources exposed by the backing * repository. * * @param resourceInformation * @return */ @ResponseBody @RequestMapping(value = BASE_MAPPING, method = RequestMethod.GET) public RepositorySearchesResource listSearches(RootResourceInformation resourceInformation) { verifySearchesExposed(resourceInformation); Links queryMethodLinks = entityLinks.linksToSearchResources(resourceInformation.getDomainType()); if (queryMethodLinks.isEmpty()) { throw new ResourceNotFoundException(); } RepositorySearchesResource result = new RepositorySearchesResource(resourceInformation.getDomainType()); result.add(queryMethodLinks); result.add(getDefaultSelfLink()); return result; }
/** * <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; }
@Override public Market get(MarketId marketId) { Market market = marketRepository.findOne(marketId); if(market == null){ logger.debug("The requested market " + marketId + " was not found"); throw new ResourceNotFoundException("No market has been found for the provided market ID: "+marketId); } return market; } }
@GetMapping("/{id}") public TvSeries getOne(@PathVariable int id){ if(log.isTraceEnabled()) { log.trace("getOne " + id); } TvSeries ts = tvSeriesService.getTvSeriesById(id); if(ts == null) { throw new ResourceNotFoundException(); } return ts; }
@GetMapping("/{id}") public TvSeries getOne(@PathVariable int id){ if(log.isTraceEnabled()) { log.trace("getOne " + id); } TvSeries ts = tvSeriesService.getTvSeriesById(id); if(ts == null) { throw new ResourceNotFoundException(); } return ts; }
@GetMapping("/{id}") public TvSeriesDto getOne(@PathVariable int id){ if(log.isTraceEnabled()) { log.trace("getOne " + id); } TvSeriesDto ts = tvSeriesService.getTvSeriesById(id); if(ts == null) { throw new ResourceNotFoundException(); } return ts; }
@Override public Industry get(Long id) { Industry industry = industryRepository.findOne(id); if(industry == null){ throw new ResourceNotFoundException("No industry has been found for the provided industry ID: "+id); } return industry; } }