public StorageItem dereferenceLink( StorageLinkItem link, boolean localOnly, boolean remoteOnly ) throws AccessDeniedException, ItemNotFoundException, IllegalOperationException, StorageException { logger.debug( "Dereferencing link {}", link.getTarget() ); ResourceStoreRequest req = new ResourceStoreRequest( link.getTarget().getPath(), localOnly, remoteOnly ); req.getRequestContext().setParentContext( link.getItemContext() ); return link.getTarget().getRepository().retrieveItem( req ); }
/** * Creates absolute URL (as String) of the passed link's target. To be used in "Location" header of the redirect * message, for example. */ protected String getLinkTargetUrl(final StorageLinkItem link) { final RepositoryItemUid targetUid = link.getTarget(); // TODO: fix this chum return link.getResourceStoreRequest().getRequestAppRootUrl() + "content/repositories/" + targetUid.getRepository().getId() + targetUid.getPath(); }
public void writeLinkContent( final StorageLinkItem link, final OutputStream os ) throws IOException { try { final String linkBody = LINK_PREFIX + link.getTarget().toString(); IOUtil.copy( new ByteArrayInputStream( linkBody.getBytes( UTF8_CHARSET ) ), os ); os.flush(); } finally { IOUtil.close( os ); } } }
/** * Dereferences the passed in link completely (following link-to-links too) as long as non-link item is found as * target. This method will detect cycles, and will fail if such link constellation is found. If any target during * dereference is not found, the usual {@link ItemNotFoundException} will be thrown (by the method used to * dereference). */ protected StorageItem dereferenceLink(final StorageLinkItem link) throws Exception { final List<String> hops = Lists.newArrayList(); StorageLinkItem currentLink = link; while (true) { final String hop = currentLink.getRepositoryItemUid().getKey(); if (!hops.contains(hop)) { hops.add(hop); final StorageItem item = repositoryRouter.dereferenceLink(currentLink); if (!(item instanceof StorageLinkItem)) { return item; } else { currentLink = (StorageLinkItem) item; } } else { // cycle detected, current link already processed throw new ItemNotFoundException(ItemNotFoundException.reasonFor(link.getResourceStoreRequest(), link .getRepositoryItemUid().getRepository(), "Link item %s introduced a cycle while referencing it, cycle is %s", link.getRepositoryItemUid(), hops)); } } }