use of nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn in project timbuctoo by HuygensING.
the class RsDocumentBuilder method getCapabilityList.
/**
* Get the capability list for the dataSet denoted by <code>ownerId</code> and <code>dataSetId</code>.
* The {@link Optional} is empty if the dataSet is not published and the given <code>user</code> == <code>null</code>
* or has no read access for the dataSet or the dataSet does not exist.
*
* @param user User that requests the list, may be <code>null</code>
* @param ownerId ownerId
* @param dataSetId dataSetId
* @return the capability list for the dataSet denoted by <code>ownerId</code> and <code>dataSetId</code>
*/
public Optional<Urlset> getCapabilityList(@Nullable User user, String ownerId, String dataSetId) {
Urlset capabilityList = null;
Optional<DataSet> maybeDataSet = dataSetRepository.getDataSet(user, ownerId, dataSetId);
if (maybeDataSet.isPresent()) {
RsMd rsMd = new RsMd(Capability.CAPABILITYLIST.xmlValue);
capabilityList = new Urlset(rsMd).addLink(new RsLn(REL_UP, rsUriHelper.uriForWellKnownResourceSync()));
DataSetMetaData dataSetMetaData = maybeDataSet.get().getMetadata();
String descriptionUrl = rsUriHelper.uriForRsDocument(dataSetMetaData, DESCRIPTION_FILENAME);
capabilityList.addLink(new RsLn(REL_DESCRIBED_BY, descriptionUrl).withType(DESCRIPTION_TYPE));
String loc = rsUriHelper.uriForRsDocument(dataSetMetaData, Capability.RESOURCELIST);
UrlItem item = new UrlItem(loc).withMetadata(new RsMd(Capability.RESOURCELIST.xmlValue));
capabilityList.addItem(item);
}
return Optional.ofNullable(capabilityList);
}
use of nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn in project timbuctoo by HuygensING.
the class RsDocumentBuilder method getSourceDescription.
/**
* Get the source description document. If <code>user</code> == <code>null</code> the source description will
* only have links to capability lists of published dataSets. Otherwise the source description will have
* links to capability lists of published dataSets and the dataSets for which the user has read access.
*
* @param user User that requests the document, may be <code>null</code>
* @return the source description document.
*/
public Urlset getSourceDescription(@Nullable User user) {
RsMd rsMd = new RsMd(Capability.DESCRIPTION.xmlValue);
Urlset sourceDescription = new Urlset(rsMd);
for (DataSet dataSet : dataSetRepository.getDataSetsWithReadAccess(user)) {
DataSetMetaData dataSetMetaData = dataSet.getMetadata();
String loc = rsUriHelper.uriForRsDocument(dataSetMetaData, Capability.CAPABILITYLIST);
String descriptionUrl = rsUriHelper.uriForRsDocument(dataSetMetaData, DESCRIPTION_FILENAME);
UrlItem item = new UrlItem(loc).withMetadata(new RsMd(Capability.CAPABILITYLIST.xmlValue)).addLink(new RsLn(REL_DESCRIBED_BY, descriptionUrl).withType(DESCRIPTION_TYPE));
sourceDescription.addItem(item);
}
return sourceDescription;
}
use of nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn in project timbuctoo by HuygensING.
the class RsDocumentBuilder method getResourceList.
/**
* Get the resource list for the dataSet denoted by <code>ownerId</code> and <code>dataSetId</code>.
* The {@link Optional} is empty if the dataSet is not published and the given <code>user</code> == <code>null</code>
* or has no read access for the dataSet or the dataSet does not exist.
*
* @param user User that requests the list, may be <code>null</code>
* @param ownerId ownerId
* @param dataSetId dataSetId
* @return the resource list for the dataSet denoted by <code>ownerId</code> and <code>dataSetId</code>
*/
public Optional<Urlset> getResourceList(@Nullable User user, String ownerId, String dataSetId) throws IOException {
Urlset resourceList = null;
Optional<DataSet> maybeDataSet = dataSetRepository.getDataSet(user, ownerId, dataSetId);
if (maybeDataSet.isPresent()) {
DataSetMetaData dataSetMetaData = maybeDataSet.get().getMetadata();
LogList loglist = maybeDataSet.get().getImportManager().getLogList();
RsMd rsMd = new RsMd(Capability.RESOURCELIST.xmlValue).withAt(// lastImportDate set on server startup?
ZonedDateTime.parse(loglist.getLastImportDate()));
resourceList = new Urlset(rsMd).addLink(new RsLn(REL_UP, rsUriHelper.uriForRsDocument(dataSetMetaData, Capability.CAPABILITYLIST)));
FileStorage fileStorage = maybeDataSet.get().getFileStorage();
List<LogEntry> entries = loglist.getEntries();
entries.sort((e1, e2) -> {
if (e1.getImportStatus().isPresent() && e2.getImportStatus().isPresent()) {
return e1.getImportStatus().get().getDate().compareTo(e2.getImportStatus().get().getDate());
} else if (e1.getImportStatus().isPresent()) {
return 1;
} else {
return -1;
}
});
for (LogEntry logEntry : entries) {
Optional<String> maybeToken = logEntry.getLogToken();
if (maybeToken.isPresent()) {
String loc = rsUriHelper.uriForToken(dataSetMetaData, maybeToken.get());
Optional<CachedFile> maybeCachedFile = fileStorage.getFile(maybeToken.get());
if (maybeCachedFile.isPresent()) {
UrlItem item = new UrlItem(loc).withMetadata(new RsMd().withType(maybeCachedFile.get().getMimeType().toString()));
resourceList.addItem(item);
}
}
}
rsMd.withCompleted(ZonedDateTime.now(ZoneOffset.UTC));
}
return Optional.ofNullable(resourceList);
}
use of nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn in project timbuctoo by HuygensING.
the class RsExplorer method explore.
@SuppressWarnings("unchecked")
@Override
public Result<RsRoot> explore(URI uri, ResultIndex index) {
LOG.debug("Exploring URI " + uri);
Result<RsRoot> result = execute(uri, getSitemapConverter());
index.add(result);
Capability capability = extractCapability(result);
if (followParentLinks) {
// rs:ln rel="up" -> points to parent document, a urlset.
String parentLink = result.getContent().map(rsRoot -> rsRoot.getLinkHref("up")).orElse(null);
if (parentLink != null && !index.contains(parentLink)) {
try {
URI parentUri = new URI(parentLink);
Result<RsRoot> parentResult = explore(parentUri, index);
result.addParent(parentResult);
verifyUpRelation(result, parentResult, capability);
} catch (URISyntaxException e) {
index.addInvalidUri(parentLink);
result.addError(e);
result.addInvalidUri(parentLink);
}
}
}
if (followIndexLinks) {
// rs:ln rel="index" -> points to parent index, a sitemapindex.
String indexLink = result.getContent().map(rsRoot -> rsRoot.getLinkHref("index")).orElse(null);
if (indexLink != null && !index.contains(indexLink)) {
try {
URI indexUri = new URI(indexLink);
Result<RsRoot> indexResult = explore(indexUri, index);
result.addParent(indexResult);
verifyIndexRelation(result, indexResult, capability);
} catch (URISyntaxException e) {
index.addInvalidUri(indexLink);
result.addError(e);
result.addInvalidUri(indexLink);
}
}
}
if (followChildLinks) {
// elements <url> or <sitemap> have the location of the children of result.
// children of Urlset with capability resourcelist, resourcedump, changelist, changedump
// are the resources them selves. do not explore these with this explorer.
String xmlString = result.getContent().map(RsRoot::getMetadata).flatMap(RsMd::getCapability).orElse("invalid");
boolean isSitemapindex = result.getContent().map(rsRoot -> rsRoot instanceof Sitemapindex).orElse(false);
if (Capability.levelfor(xmlString) > Capability.RESOURCELIST.level || isSitemapindex) {
List<RsItem> itemList = result.getContent().map(RsRoot::getItemList).orElse(Collections.emptyList());
for (RsItem item : itemList) {
String childLink = item.getLoc();
if (childLink != null && !index.contains(childLink)) {
try {
URI childUri = new URI(childLink);
Result<RsRoot> childResult = explore(childUri, index);
result.addChild(childResult);
verifyChildRelation(result, childResult, capability);
Optional<RsLn> maybeDescribedByLink = item.getLink("describedBy");
maybeDescribedByLink.ifPresent(rsLn -> loadDescriptionIfApplicable(childResult, rsLn, index));
} catch (URISyntaxException e) {
index.addInvalidUri(childLink);
result.addError(e);
result.addInvalidUri(childLink);
}
}
}
}
}
Optional<RsLn> maybeDescribedByLink = result.getContent().flatMap(rsRoot -> rsRoot.getLink("describedBy"));
maybeDescribedByLink.ifPresent(rsLn -> loadDescriptionIfApplicable(result, rsLn, index));
return result;
}
Aggregations