/** * 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); }
/** * 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 whether the {@link Links} container contains a {@link Link} with the given rel. * * @param rel * @return */ public boolean hasLink(String rel) { return getLink(rel).isPresent(); }
RegexRule regexRule = new RegexRule(); { regexRule.addRule("http://bbs.gter.net/thread.*"); } public Links visitAndGetNextLinks(Page page) { String url = page.getUrl(); if (Pattern.matches("http://bbs.gter.net/forum-1033-.*", url)) { Links nextLinks=new Links(); nextLinks.addAllFromDocument(page.getDoc(),regexRule); System.out.println(nextLinks+"\n"); return nextLinks; }else{ byte[] content=page.getContent(); try { FileUtils.writeFileWithParent("/Users/gary/Dropbox/hooom/program/html"+id.incrementAndGet()+".txt", content); System.out.println("save page "+page.getUrl()); } catch (IOException ex) { ex.printStackTrace(); } } return null; }
@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); } } } } }
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); }
/** * <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) { 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); } } } } }
/** * Returns whether the {@link Links} container contains a {@link Link} with the given rel. * * @param rel * @return */ public boolean hasLink(String rel) { return getLink(rel) != null; }
/** * 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}/{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()); }
@Override public <T> Class<? extends T> resolveType(Class<T> declaredType, Links resourceLinks, Configuration configuration) { Link self = resourceLinks.getLink(Link.REL_SELF);
/** * 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); }
/** * 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; } }
@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); } } } } }
@Override public <T> Class<? extends T> resolveType(Class<T> declaredType, Links resourceLinks, Configuration configuration) { Link self = resourceLinks.getLink(Link.REL_SELF);