/** * Creates a {@link Links} instance from the given RFC5988-compatible link format. * * @param source a comma separated list of {@link Link} representations. * @return the {@link Links} represented by the given {@link String}. */ public static Links valueOf(String source) { if (!StringUtils.hasText(source)) { return NO_LINKS; } Matcher matcher = LINK_HEADER_PATTERN.matcher(source); List<Link> links = new ArrayList<>(); while (matcher.find()) { Link link = Link.valueOf(matcher.group()); if (link != null) { links.add(link); } } return new Links(links); }
public Links getLinksForNested(Object object, List<Link> existing) { PersistentEntity<?, ?> entity = entities.getRequiredPersistentEntity(object.getClass()); NestedLinkCollectingAssociationHandler handler = new NestedLinkCollectingAssociationHandler(links, entity.getPropertyAccessor(object), associationLinks); entity.doWithAssociations(handler); List<Link> links = new ArrayList<Link>(); links.addAll(existing); links.addAll(handler.getLinks()); return new Links(links); }
/** * 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; } }
/** * Returns all links to search resources of the given type. Pre-expands the template with the given {@link Pageable} * and {@link Sort} if applicable. * * @param type must not be {@literal null}. * @param pageable can be {@literal null}. * @param sort can be {@literal null}. * @return */ private Links linksToSearchResources(Class<?> type, Pageable pageable, Sort sort) { List<Link> links = new ArrayList<Link>(); SearchResourceMappings searchMappings = mappings.getSearchResourceMappings(type); for (MethodResourceMapping mapping : searchMappings.getExportedMappings()) { links.add(getSearchResourceLinkFor(type, mapping.getRel(), pageable, sort)); } return new Links(links); }
/** * <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); }
@Override public void doWithAssociation(final Association<? extends PersistentProperty<?>> association) { if (associationLinks.isLinkableAssociation(association)) { PersistentProperty<?> property = association.getInverse(); Links existingLinks = new Links(links); for (Link link : associationLinks.getLinksFor(association, basePath)) { if (existingLinks.hasLink(link.getRel())) { throw new MappingException(String.format(AMBIGUOUS_ASSOCIATIONS, property.toString())); } else { links.add(link); } } } } }
@Override public void doWithAssociation(final Association<? extends PersistentProperty<?>> association) { PersistentProperty<?> property = association.getInverse(); if (associations.isLinkableAssociation(property)) { Links existingLinks = new Links(links); for (Link link : associations.getLinksFor(association, basePath)) { if (existingLinks.hasLink(link.getRel())) { throw new MappingException(String.format(AMBIGUOUS_ASSOCIATIONS, property.toString())); } else { links.add(link); } } } } }
/** * <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 all {@link Links} for the given object and already existing {@link Link}. * * @param object must not be {@literal null}. * @param existingLinks must not be {@literal null}. * @return */ public Links getLinksFor(Object object, List<Link> existingLinks) { Assert.notNull(object, "Object must not be null!"); Assert.notNull(existingLinks, "Existing links must not be null!"); Links links = new Links(existingLinks); Link selfLink = createSelfLink(object, links); if (selfLink == null) { return links; } Path path = new Path(selfLink.expand().getHref()); LinkCollectingAssociationHandler handler = new LinkCollectingAssociationHandler(entities, path, associationLinks); entities.getRequiredPersistentEntity(object.getClass()).doWithAssociations(handler); List<Link> result = new ArrayList<Link>(existingLinks); result.addAll(handler.getLinks()); return addSelfLinkIfNecessary(object, result); }
curies.add(curieProvider.getCurieInformation(new Links(links)));
$(document).ready(function(){ $(".links").ajaxSuccess(function() { console.log("Hey.") // use links in here: var links = new Links(20,0); }); });
/** * Creates a {@link ProjectionResource} for the given {@link TargetAware}. * * @param value must not be {@literal null}. * @return */ private ProjectionResource toResource(TargetAware value) { Object target = value.getTarget(); ResourceMetadata metadata = associations.getMetadataFor(value.getTargetClass()); Links links = metadata.isExported() ? collector.getLinksFor(target) : new Links(); Resource<TargetAware> resource = invoker.invokeProcessorsFor(new Resource<TargetAware>(value, links)); return new ProjectionResource(resource.getContent(), resource.getLinks()); } }
@Override public Resource<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { ObjectNode node = p.readValueAs(ObjectNode.class); ObjectMapper mapper = (ObjectMapper) p.getCodec(); ResourceSupport resource = mapper.convertValue(node, ResourceSupport.class); Links links = new Links(resource.getLinks()); Object content = mapper.convertValue(node, getResourceDeserializationType(links)); return new Resource<>(content, links); }
@Override public Resource<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { ObjectNode node = p.readValueAs(ObjectNode.class); ObjectMapper mapper = (ObjectMapper) p.getCodec(); ResourceSupport resource = mapper.convertValue(node, ResourceSupport.class); Links links = new Links(resource.getLinks()); Object content = mapper.convertValue(node, getResourceDeserializationType(links)); return new Resource<>(content, links); }
@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); }
@RequestMapping(value = "", method = RequestMethod.GET) public ResourceSupport root() throws Exception { List<Link> links = new ArrayList<>(); links.add(linkWithMethodGet(linkTo(TenantController.class).withRel("iam.tenants"))); links.add(linkWithMethodGet(linkTo(UserController.class).withRel("iam.users"))); links.add(linkWithMethodGet(linkTo(AvatarController.class).withRel("iam.avatars"))); links.add(linkWithMethodGet(linkTo(SessionController.class).withRel("iam.sessions"))); ResourceSupport result = new ResourceSupport(); result.add(new Links(links)); return result; }
@RequestMapping(value = "/projects", method = RequestMethod.GET) public ResourceSupport projects() throws Exception { List<Link> links = new ArrayList<>(); links.add(linkWithMethodPost(linkTo(ProjectController.class).withRel(RestMethods.CREATE.getName()))); links.add(linkWithMethodGet(linkTo(methodOn(ProjectController.class).get("ID")).withRel(RelConstants.PROJECTS_BY_ID))); links.add(linkWithMethodGet(linkTo(methodOn(ProjectController.class).searchProjects(null, null, null)).withRel(RelConstants.PROJECTS_SEARCH))); links.add(linkWithMethodGet(linkTo(methodOn(ProjectController.class).searchProjects(null, "", null)).withRel(RelConstants.PROJECTS_SEARCH_BY))); ResourceSupport result = new ResourceSupport(); result.add(new Links(links)); return result; }