Search in sources :

Example 1 with RsMd

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

Example 2 with RsMd

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

the class RsExplorerTest method verifyFindSourceDescription.

private void verifyFindSourceDescription(String path) {
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(path), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(200).withBody(createValidSourceDescription()));
    URI uri = composeUri(path);
    RsExplorer explorer = new RsExplorer(getHttpclient(), getRsContext());
    ResultIndex index = new ResultIndex();
    Result<RsRoot> result = explorer.explore(uri, index);
    result.getErrors().forEach(Throwable::printStackTrace);
    assertThat(result.getUri(), equalTo(uri));
    assertThat(result.getStatusCode(), equalTo(200));
    assertThat(result.getErrors().isEmpty(), is(true));
    assertThat(result.getContent().isPresent(), is(true));
    assertThat(result.getContent().map(RsRoot::getMetadata).flatMap(RsMd::getCapability).orElse("invalid"), equalTo("description"));
    // index should contain the describedBy uri of the source description:
    assertThat(index.contains(composeUri("/info_about_source.xml")), is(true));
    // result should contain the describedByResult:
    Result<Description> descriptionResult = result.getDescriptionResult().get();
    assertThat(descriptionResult.getUri(), equalTo(composeUri("/info_about_source.xml")));
    assertThat(descriptionResult.getContent().isPresent(), is(false));
    assertThat(descriptionResult.getStatusCode(), equalTo(404));
}
Also used : RsRoot(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot) URI(java.net.URI) RsMd(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd)

Example 3 with RsMd

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

the class RsExplorerTest method findWrongParentDocument.

@Test
public void findWrongParentDocument() throws Exception {
    String path1 = "/foo/resourcedump.xml";
    String path2 = "/bla/resourcedump.xml";
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(path1), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(200).withBody(createValidResourceDump(path2)));
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(path2), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(200).withBody(createValidResourceDump("/bar/whatever.xml")));
    URI uri = composeUri(path1);
    RsExplorer explorer = new RsExplorer(getHttpclient(), getRsContext());
    ResultIndex index = new ResultIndex();
    Result<RsRoot> result = explorer.explore(uri, index);
    // result.listErrors().forEach(Throwable::printStackTrace);
    assertThat(result.getUri(), equalTo(uri));
    assertThat(result.getStatusCode(), equalTo(200));
    assertThat(result.getErrors().isEmpty(), is(false));
    assertThat(result.getErrors().get(0).getMessage(), containsString("invalid up relation:"));
    assertThat(result.getContent().isPresent(), is(true));
    assertThat(result.getContent().map(RsRoot::getMetadata).flatMap(RsMd::getCapability).orElse("invalid"), equalTo("resourcedump"));
    URI uri2 = composeUri(path2);
    Result<RsRoot> parentResult = (Result<RsRoot>) result.getParents().get(uri2);
    parentResult.getErrors().forEach(Throwable::printStackTrace);
    assertThat(parentResult.getUri(), equalTo(uri2));
    assertThat(parentResult.getStatusCode(), equalTo(200));
    assertThat(parentResult.getErrors().isEmpty(), is(true));
    assertThat(parentResult.getContent().isPresent(), is(true));
    assertThat(parentResult.getContent().map(RsRoot::getMetadata).flatMap(RsMd::getCapability).orElse("invalid"), equalTo("resourcedump"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) RsRoot(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot) URI(java.net.URI) RsMd(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd) Test(org.junit.Test)

Example 4 with RsMd

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

Example 5 with RsMd

use of nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd 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)

Aggregations

RsMd (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd)7 Urlset (nl.knaw.huygens.timbuctoo.remote.rs.xml.Urlset)5 URI (java.net.URI)4 RsLn (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsLn)4 RsRoot (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot)3 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 Sitemapindex (nl.knaw.huygens.timbuctoo.remote.rs.xml.Sitemapindex)2 Test (org.junit.Test)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 ResourceSyncContext (nl.knaw.huygens.timbuctoo.remote.rs.xml.ResourceSyncContext)1 RsBuilder (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsBuilder)1 RsItem (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsItem)1