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);
}
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);
}
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));
}
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;
}
Aggregations