/** * Returns the {@link Link}s contained in the {@link ResourceSupport} of the given {@link ResponseEntity} as * {@link HttpHeaders}. * * @param entity must not be {@literal null}. * @return */ private static <T extends ResourceSupport> HttpHeaders getHeadersWithLinks(ResponseEntity<T> entity) { List<Link> links = entity.getBody().getLinks(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.putAll(entity.getHeaders()); httpHeaders.add("Link", new Links(links).toString()); return httpHeaders; } }
/** * <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); }
/** * <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 {@link Link}s contained in the {@link ResourceSupport} of the given {@link ResponseEntity} as * {@link HttpHeaders}. * * @param entity must not be {@literal null}. * @return */ private static <T extends ResourceSupport> HttpHeaders getHeadersWithLinks(ResponseEntity<T> entity) { List<Link> links = entity.getBody().getLinks(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.putAll(entity.getHeaders()); httpHeaders.add("Link", new Links(links).toString()); return httpHeaders; } }
@RequestMapping(method = POST) HttpEntity<Void> writeUserProfilePhoto(@PathVariable Long user, @RequestParam MultipartFile file) throws Throwable { byte bytesForProfilePhoto[] = FileCopyUtils.copyToByteArray(file.getInputStream()); this.crmService.writeUserProfilePhoto(user, MediaType.parseMediaType(file.getContentType()), bytesForProfilePhoto); Resource<User> userResource = this.userResourceAssembler.toResource(crmService.findById(user)); List<Link> linkCollection = userResource.getLinks(); Links wrapperOfLinks = new Links(linkCollection); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Link", wrapperOfLinks.toString()); // we can't encode the links in the body of the response, so we put them in the "Links:" header. httpHeaders.setLocation(URI.create(userResource.getLink("photo").getHref())); // "Location: /users/{userId}/photo" return new ResponseEntity<>(httpHeaders, HttpStatus.ACCEPTED); }
@RequestMapping(method = POST) HttpEntity<Void> writeUserProfilePhoto(@PathVariable Long user, @RequestParam MultipartFile file) throws Throwable { byte bytesForProfilePhoto[] = FileCopyUtils.copyToByteArray(file.getInputStream()); this.crmService.writeUserProfilePhoto(user, MediaType.parseMediaType(file.getContentType()), bytesForProfilePhoto); Resource<User> userResource = this.userResourceAssembler.toResource(crmService.findById(user)); List<Link> linkCollection = userResource.getLinks(); Links wrapperOfLinks = new Links(linkCollection); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Link", wrapperOfLinks.toString()); // we can't encode the links in the body of the response, so we put them in the "Links:" header. httpHeaders.setLocation(URI.create(userResource.getLink("photo").getHref())); // "Location: /users/{userId}/photo" return new ResponseEntity<>(httpHeaders, HttpStatus.ACCEPTED); }
/** * <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); }
/** * <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()); }