Search in sources :

Example 6 with RsRoot

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

the class RsExplorerTest method receiveStatusError.

@Test
public void receiveStatusError() throws Exception {
    String path = "/.well-known/resourcesync";
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(path), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(404).withBody("Document not found"));
    RsExplorer explorer = new RsExplorer(getHttpclient(), getRsContext());
    ResultIndex index = new ResultIndex();
    Result<RsRoot> result = explorer.explore(composeUri(path), index);
    assertThat(result.getUri(), equalTo(composeUri(path)));
    assertThat(result.getStatusCode(), equalTo(404));
    assertThat(result.getErrors().isEmpty(), is(false));
    assertThat(result.getErrors().get(0), instanceOf(RemoteException.class));
    assertThat(result.getContent().isPresent(), is(false));
// result.getDataSetErrors().forEach(Throwable::printStackTrace);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) RsRoot(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot) Test(org.junit.Test)

Example 7 with RsRoot

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

the class RsExplorerTest method findInvalidSitemapDocument.

@Test
public void findInvalidSitemapDocument() throws Exception {
    String path = "/.well-known/resourcesync";
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(path), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(200).withBody(createValidXml()));
    RsExplorer explorer = new RsExplorer(getHttpclient(), getRsContext());
    ResultIndex index = new ResultIndex();
    Result<RsRoot> result = explorer.explore(composeUri(path), index);
    assertThat(result.getUri(), equalTo(composeUri(path)));
    assertThat(result.getStatusCode(), equalTo(200));
    assertThat(result.getErrors().isEmpty(), is(false));
    assertThat(result.getErrors().get(0), instanceOf(JAXBException.class));
    assertThat(result.getContent().isPresent(), is(false));
// result.listErrors().forEach(Throwable::printStackTrace);
}
Also used : JAXBException(javax.xml.bind.JAXBException) Matchers.containsString(org.hamcrest.Matchers.containsString) RsRoot(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot) Test(org.junit.Test)

Example 8 with RsRoot

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

the class RsExplorerTest method findDescribedByDocuments.

@Test
@SuppressWarnings({ "unchecked" })
public void findDescribedByDocuments() throws Exception {
    String path = "/.well-known/resourcesync";
    String pathDescriptionOfSource = "/info_about_source.xml";
    String pathDescriptionOfSet1 = "/info_about_set1_of_resources.xml";
    String pathDescriptionOfSet2 = "/info_about_set2_of_resources.xml";
    String pathDescriptionOfSet3 = "/info_about_set3_of_resources.xml";
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(path), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(200).withBody(createValidSourceDescription()));
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(pathDescriptionOfSet1), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(200).withBody(createDescriptionDocument()));
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(pathDescriptionOfSet2), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(404).withBody("Not Found"));
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(pathDescriptionOfSet3), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(200).withBody(createDescriptionDocument()));
    getMockServer().when(HttpRequest.request().withMethod("GET").withPath(pathDescriptionOfSource), Times.exactly(1)).respond(HttpResponse.response().withStatusCode(200).withBody(createDescriptionDocument()));
    RsExplorer explorer = new RsExplorer(getHttpclient(), getRsContext());
    ResultIndex index = new ResultIndex();
    Result<RsRoot> result = explorer.explore(composeUri(path), index);
    assertThat(result.getStatusCode(), equalTo(200));
    assertThat(result.getDescriptionResult().isPresent(), is(true));
    Result<Description> describedByResult = result.getDescriptionResult().get();
    assertThat(describedByResult.getContent().isPresent(), is(true));
    assertThat(describedByResult.getContent().get().getRawContent(), equalTo(createDescriptionDocument()));
    Result<RsRoot> child1 = (Result<RsRoot>) result.getChildren().get(URI.create("http://example.com/capabilitylist1.xml"));
    assertThat(child1.getContent().isPresent(), is(false));
    assertThat(child1.getDescriptionResult().isPresent(), is(true));
    Result<Description> descriptionResult1 = child1.getDescriptionResult().get();
    descriptionResult1.getErrors().forEach(Throwable::printStackTrace);
    assertThat(descriptionResult1.getContent().isPresent(), is(true));
    assertThat(descriptionResult1.getContent().get().getRawContent(), equalTo(createDescriptionDocument()));
    Result<RsRoot> child3 = (Result<RsRoot>) result.getChildren().get(URI.create("http://example.com/capabilitylist3.xml"));
    assertThat(child3.getContent().isPresent(), is(false));
    assertThat(child3.getDescriptionResult().isPresent(), is(true));
    Result<Description> descriptionResult3 = child3.getDescriptionResult().get();
    descriptionResult3.getErrors().forEach(Throwable::printStackTrace);
    assertThat(descriptionResult3.getContent().isPresent(), is(true));
    assertThat(descriptionResult3.getContent().get().getRawContent(), equalTo(createDescriptionDocument()));
    Result<RsRoot> child2 = (Result<RsRoot>) result.getChildren().get(URI.create("http://example.com/capabilitylist2.xml"));
    assertThat(child2.getContent().isPresent(), is(false));
    assertThat(child2.getDescriptionResult().isPresent(), is(true));
    Result<Description> descriptionResult2 = child2.getDescriptionResult().get();
    assertThat(descriptionResult2.getContent().isPresent(), is(false));
    assertThat(descriptionResult2.getStatusCode(), is(404));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) RsRoot(nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot) Test(org.junit.Test)

Example 9 with RsRoot

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

Aggregations

RsRoot (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsRoot)9 URI (java.net.URI)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.junit.Test)4 RsMd (nl.knaw.huygens.timbuctoo.remote.rs.xml.RsMd)3 List (java.util.List)2 Optional (java.util.Optional)2 Capability (nl.knaw.huygens.timbuctoo.remote.rs.xml.Capability)2 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 JAXBException (javax.xml.bind.JAXBException)1 Description (nl.knaw.huygens.timbuctoo.remote.rs.discover.Description)1 LinkList (nl.knaw.huygens.timbuctoo.remote.rs.discover.LinkList)1 Result (nl.knaw.huygens.timbuctoo.remote.rs.discover.Result)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