Search in sources :

Example 6 with Urlset

use of nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset 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);
}
Also used : CachedFile(nl.knaw.huygens.timbuctoo.v5.filestorage.dto.CachedFile) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) RsLn(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn) UrlItem(nl.knaw.huygens.timbuctoo.remote.rs.xml.UrlItem) LogList(nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogList) Urlset(nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset) FileStorage(nl.knaw.huygens.timbuctoo.v5.filestorage.FileStorage) DataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData) RsMd(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd) LogEntry(nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogEntry)

Example 7 with Urlset

use of nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset in project timbuctoo by HuygensING.

the class ResultIndexPivotTest method testSelections.

@SuppressWarnings("unchecked")
@Test
public void testSelections() throws Exception {
    final ResultIndex index = new ResultIndex();
    Result<Urlset> result1 = new Result<>(new URI("doc1"));
    result1.accept(new Urlset(new RsMd((Capability.RESOURCELIST.xmlValue))));
    result1.addError(new RemoteResourceSyncFrameworkException("Bla1"));
    index.add(result1);
    Result<Urlset> result2 = new Result<>(new URI("doc2"));
    result2.accept(new Urlset(new RsMd((Capability.CAPABILITYLIST.xmlValue))));
    index.add(result2);
    Result<Sitemapindex> result3 = new Result<>(new URI("doc3"));
    result3.accept(new Sitemapindex(new RsMd(Capability.CHANGELIST.xmlValue)));
    index.add(result3);
    Result<Sitemapindex> result4 = new Result<>(new URI("doc4"));
    result4.addError(new RemoteResourceSyncFrameworkException("Bla4"));
    index.add(result4);
    Result<Sitemapindex> result5 = new Result<>(new URI("doc5"));
    result5.accept(new Sitemapindex(new RsMd((Capability.CAPABILITYLIST.xmlValue))));
    index.add(result5);
    ResultIndexPivot pivot = new ResultIndexPivot(index);
    List<Throwable> errorList = pivot.listErrors();
    assertThat(errorList.stream().map(Throwable::getMessage).collect(Collectors.toList()), containsInAnyOrder("Bla1", "Bla4"));
    assertThat(errorList.size(), equalTo(2));
    List<Result<?>> errorResultList = pivot.listErrorResults();
    assertThat(errorResultList, containsInAnyOrder(result1, result4));
    assertThat(errorResultList.size(), equalTo(2));
    List<Result<?>> resultList = pivot.listResultsWithContent();
    assertThat(resultList, containsInAnyOrder(result1, result2, result3, result5));
    assertThat(resultList.size(), equalTo(4));
    List<Result<Urlset>> setResultList = pivot.listUrlsetResults();
    assertThat(setResultList, containsInAnyOrder(result1, result2));
    assertThat(setResultList.size(), equalTo(2));
    List<Result<Sitemapindex>> indexResultList = pivot.listSitemapindexResults();
    assertThat(indexResultList.size(), equalTo(2));
    assertThat(indexResultList, containsInAnyOrder(result3, result5));
    List<Result<Urlset>> capabilityListSetResults = pivot.listUrlsetResults(Capability.CAPABILITYLIST);
    assertThat(capabilityListSetResults.size(), equalTo(1));
    assertThat(capabilityListSetResults, containsInAnyOrder(result2));
    List<Result<Sitemapindex>> capabilityListIndexResults = pivot.listSitemapindexResults(Capability.CAPABILITYLIST);
    assertThat(capabilityListIndexResults.size(), equalTo(1));
    assertThat(capabilityListIndexResults, containsInAnyOrder(result5));
    List<Result<RsRoot>> capabilityListResults = pivot.listRsRootResults(Capability.CAPABILITYLIST);
    assertThat(capabilityListResults.size(), equalTo(2));
    assertThat(capabilityListResults, containsInAnyOrder(result2, result5));
    capabilityListResults = pivot.listRsRootResultsByLevel(0);
    // capabilityListResults.stream().forEach(rsRootResult -> System.out.println(rsRootResult.getUri()));
    assertThat(capabilityListResults.size(), equalTo(0));
    capabilityListResults = pivot.listRsRootResultsByLevel(1);
    // capabilityListResults.stream().forEach(rsRootResult -> System.out.println(rsRootResult.getUri()));
    assertThat(capabilityListResults.size(), equalTo(2));
    assertThat(capabilityListResults, containsInAnyOrder(result1, result3));
    capabilityListResults = pivot.listRsRootResultsByLevel(2);
    // capabilityListResults.stream().forEach(rsRootResult -> System.out.println(rsRootResult.getUri()));
    assertThat(capabilityListResults.size(), equalTo(2));
    assertThat(capabilityListResults, containsInAnyOrder(result2, result5));
    capabilityListResults = pivot.listRsRootResultsByLevel(3);
    // capabilityListResults.stream().forEach(rsRootResult -> System.out.println(rsRootResult.getUri()));
    assertThat(capabilityListResults.size(), equalTo(0));
    capabilityListResults = pivot.listRsRootResultsByLevel(-1);
    // capabilityListResults.stream().forEach(rsRootResult -> System.out.println(rsRootResult.getUri()));
    assertThat(capabilityListResults.size(), equalTo(0));
}
Also used : URI(java.net.URI) Urlset(nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset) RsMd(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd) Sitemapindex(nl.knaw.huygens.timbuctoo.remote.rs.xml.Sitemapindex) Test(org.junit.Test)

Example 8 with Urlset

use of nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset 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;
}
Also used : Capability(nl.knaw.huygens.timbuctoo.remote.rs.xml.Capability) LambdaExceptionUtil(nl.knaw.huygens.timbuctoo.util.LambdaExceptionUtil) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) URISyntaxException(java.net.URISyntaxException) RsMd(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Sitemapindex(nl.knaw.huygens.timbuctoo.remote.rs.xml.Sitemapindex) RsRoot(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot) RsLn(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn) Urlset(nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset) HttpResponse(org.apache.http.HttpResponse) Optional(java.util.Optional) ResourceSyncContext(nl.knaw.huygens.timbuctoo.remote.rs.xml.ResourceSyncContext) RsItem(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsItem) RsBuilder(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsBuilder) URI(java.net.URI) Collections(java.util.Collections) InputStream(java.io.InputStream) Capability(nl.knaw.huygens.timbuctoo.remote.rs.xml.Capability) RsLn(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn) RsRoot(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RsItem(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsItem) Sitemapindex(nl.knaw.huygens.timbuctoo.remote.rs.xml.Sitemapindex)

Example 9 with Urlset

use of nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset in project timbuctoo by HuygensING.

the class RsDocumentBuilderTest method sourceDescriptionWithCapabilityLists.

@Test
public void sourceDescriptionWithCapabilityLists() throws Exception {
    when(dataSetRepository.getDataSetsWithReadAccess(null)).thenReturn(Arrays.asList(dataSet, dataSet));
    Urlset sourceDescription = rsDocumentBuilder.getSourceDescription(null);
    String xml = rsBuilder.toXml(sourceDescription, true);
    // System.out.println(xml);
    rsBuilder.setXmlString(xml).build();
    Urlset sd = rsBuilder.getUrlset().get();
    assertThat(sd.getCapability().get(), is(Capability.DESCRIPTION));
    assertThat(sd.getItemList().size(), is(2));
    assertThat(sd.getItemList().get(0).getLoc(), is("http://example.com/v5/resourcesync/u1/ds1/capabilitylist.xml"));
    assertThat(sd.getItemList().get(1).getLoc(), is("http://example.com/v5/resourcesync/u2/ds2/capabilitylist.xml"));
    assertThat(sd.getItemList().get(0).getMetadata().get().getCapability().get(), is(Capability.CAPABILITYLIST.xmlValue));
    assertThat(sd.getItemList().get(1).getLink("describedby").get().getHref(), is("http://example.com/v5/resourcesync/u2/ds2/description.xml"));
    assertThat(sd.getItemList().get(1).getLink("describedby").get().getType().get(), is("application/rdf+xml"));
}
Also used : Urlset(nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset) Test(org.junit.Test)

Example 10 with Urlset

use of nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset in project timbuctoo by HuygensING.

the class RsDocumentBuilderTest method capabilityList.

@Test
public void capabilityList() throws Exception {
    when(dataSetRepository.getDataSet(null, "u1", "ds1")).thenReturn(Optional.of(dataSet));
    Urlset capabilityList = rsDocumentBuilder.getCapabilityList(null, "u1", "ds1").get();
    String xml = rsBuilder.toXml(capabilityList, true);
    // System.out.println(xml);
    rsBuilder.setXmlString(xml).build();
    Urlset cl = rsBuilder.getUrlset().get();
    assertThat(cl.getCapability().get(), is(Capability.CAPABILITYLIST));
    assertThat(cl.getLink("up").get().getHref(), is("http://example.com/.well-known/resourcesync"));
    assertThat(cl.getLink("describedby").get().getHref(), is("http://example.com/v5/resourcesync/u1/ds1/description.xml"));
    assertThat(cl.getLink("describedby").get().getType().get(), is("application/rdf+xml"));
    assertThat(cl.getItemList().size(), is(1));
    assertThat(cl.getItemList().get(0).getLoc(), is("http://example.com/v5/resourcesync/u1/ds1/resourcelist.xml"));
    assertThat(cl.getItemList().get(0).getMetadata().get().getCapability().get(), is(Capability.RESOURCELIST.xmlValue));
}
Also used : Urlset(nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset) Test(org.junit.Test)

Aggregations

Urlset (nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset)10 RsMd (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd)5 RsLn (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn)4 Test (org.junit.Test)4 UrlItem (nl.knaw.huygens.timbuctoo.remote.rs.xml.UrlItem)3 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)3 DataSetMetaData (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData)3 URI (java.net.URI)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Sitemapindex (nl.knaw.huygens.timbuctoo.remote.rs.xml.Sitemapindex)2 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)2 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Capability (nl.knaw.huygens.timbuctoo.remote.rs.xml.Capability)1