use of org.nanopub.NanopubImpl in project nanopub-server by tkuhn.
the class CollectNanopubs method processPage.
private void processPage(int page, boolean isLastPage, long ignoreBeforePos) throws Exception {
parent.stillAlive();
logger.info("Process page " + page + " from " + peerInfo.getPublicUrl());
loaded = 0;
nextNp = (page - 1) * peerPageSize;
List<String> toLoad = new ArrayList<>();
boolean downloadAsPackage = false;
for (String nanopubUri : NanopubServerUtils.loadNanopubUriList(peerInfo, page)) {
parent.stillAlive();
if (nextNp >= ignoreBeforePos) {
String ac = TrustyUriUtils.getArtifactCode(nanopubUri);
if (ac != null && ourPattern.matchesUri(nanopubUri) && !db.hasNanopub(ac)) {
toLoad.add(ac);
if (!isLastPage && toLoad.size() > 5) {
// Download entire package if more than 5 nanopubs are new
downloadAsPackage = true;
nextNp = (page - 1) * peerPageSize;
break;
}
}
}
nextNp++;
}
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000).build();
HttpClient c = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
watch = new StopWatch();
watch.start();
if (downloadAsPackage) {
logger.info("Download page " + page + " as compressed package...");
HttpGet get = new HttpGet(peerInfo.getPublicUrl() + "package.gz?page=" + page);
get.setHeader("Accept", "application/x-gzip");
HttpResponse resp = c.execute(get);
InputStream in = null;
try {
if (wasSuccessful(resp)) {
in = new GZIPInputStream(resp.getEntity().getContent());
} else {
logger.info("Failed. Trying uncompressed package...");
// This is for compability with older versions; to be removed at some point...
get = new HttpGet(peerInfo.getPublicUrl() + "package?page=" + page);
get.setHeader("Accept", "application/trig");
resp = c.execute(get);
if (!wasSuccessful(resp)) {
logger.error("HTTP request failed: " + resp.getStatusLine().getReasonPhrase());
recordTime();
throw new RuntimeException(resp.getStatusLine().getReasonPhrase());
}
in = resp.getEntity().getContent();
}
MultiNanopubRdfHandler.process(RDFFormat.TRIG, in, new NanopubHandler() {
@Override
public void handleNanopub(Nanopub np) {
nextNp++;
if (watch.getTime() > 5 * 60 * 1000) {
// Downloading the whole package should never take more than 5 minutes.
logger.error("Downloading package took too long; interrupting");
recordTime();
throw new RuntimeException("Downloading package took too long; interrupting");
}
if (!ourPattern.matchesUri(np.getUri().stringValue()))
return;
try {
loadNanopub(np);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
});
} finally {
if (in != null)
in.close();
}
} else {
logger.info("Download " + toLoad.size() + " nanopubs individually...");
for (String ac : toLoad) {
parent.stillAlive();
HttpGet get = new HttpGet(peerInfo.getPublicUrl() + ac);
get.setHeader("Accept", "application/trig");
HttpResponse resp = c.execute(get);
if (!wasSuccessful(resp)) {
logger.error("HTTP request failed: " + resp.getStatusLine().getReasonPhrase());
recordTime();
throw new RuntimeException(resp.getStatusLine().getReasonPhrase());
}
InputStream in = null;
try {
in = resp.getEntity().getContent();
loadNanopub(new NanopubImpl(in, RDFFormat.TRIG));
} finally {
if (in != null)
in.close();
}
}
}
recordTime();
logger.info("Update peer state: " + peerInfo.getPublicUrl() + " at position " + nextNp);
db.updatePeerState(peerInfo, nextNp);
}
use of org.nanopub.NanopubImpl in project nanopub-server by tkuhn.
the class NanopubDb method getNanopub.
public Nanopub getNanopub(String artifactCode) {
BasicDBObject query = new BasicDBObject("_id", artifactCode);
DBCursor cursor = getNanopubCollection().find(query);
if (!cursor.hasNext()) {
return null;
}
String nanopubString = cursor.next().get("nanopub").toString();
Nanopub np = null;
try {
np = new NanopubImpl(nanopubString, internalFormat);
} catch (MalformedNanopubException ex) {
throw new RuntimeException("Stored nanopub is not wellformed (this shouldn't happen)", ex);
} catch (OpenRDFException ex) {
throw new RuntimeException("Stored nanopub is corrupted (this shouldn't happen)", ex);
}
if (ServerConf.get().isCheckNanopubsOnGetEnabled() && !TrustyNanopubUtils.isValidTrustyNanopub(np)) {
throw new RuntimeException("Stored nanopub is not trusty (this shouldn't happen)");
}
return np;
}
use of org.nanopub.NanopubImpl in project nanopub-server by tkuhn.
the class NanopubServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
setGeneralHeaders(resp);
ServerRequest r = new ServerRequest(req);
if (r.isEmpty()) {
if (!ServerConf.getInfo().isPostNanopubsEnabled()) {
resp.sendError(405, "Posting nanopubs is not supported by this nanopub server");
return;
}
Nanopub np = null;
try {
np = new NanopubImpl(req.getInputStream(), Rio.getParserFormatForMIMEType(req.getContentType(), RDFFormat.TRIG));
} catch (Exception ex) {
resp.sendError(400, "Error reading nanopub: " + ex.getMessage());
}
if (np != null) {
if (ourPattern.matchesUri(np.getUri().toString())) {
String code = TrustyUriUtils.getArtifactCode(np.getUri().toString());
try {
if (NanopubDb.get().getNanopub(code) == null) {
NanopubDb.get().loadNanopub(np);
}
resp.setHeader("Location", TrustyUriUtils.getArtifactCode(np.getUri().toString()));
resp.setStatus(201);
} catch (NotTrustyNanopubException ex) {
resp.sendError(400, "Nanopub is not trusty: " + ex.getMessage());
} catch (OversizedNanopubException ex) {
resp.sendError(400, "Nanopub is too large: " + ex.getMessage());
} catch (Exception ex) {
resp.sendError(500, "Error storing nanopub: " + ex.getMessage());
}
} else {
resp.sendError(500, "Nanopub doesn't match pattern for this server: " + np.getUri());
}
}
} else if (r.getRequestString().equals(PeerListPage.PAGE_NAME)) {
if (!ServerConf.getInfo().isPostPeersEnabled()) {
resp.sendError(405, "Posting peers is not supported by this nanopub server");
return;
}
try {
StringWriter sw = new StringWriter();
IOUtils.copy(new InputStreamReader(req.getInputStream(), Charset.forName("UTF-8")), sw);
NanopubDb.get().addPeer(sw.toString().trim());
resp.setStatus(201);
} catch (ServerInfoException ex) {
resp.sendError(400, "Invalid peer URL: " + ex.getMessage());
} catch (IOException ex) {
resp.sendError(500, "Error adding peer: " + ex.getMessage());
}
} else {
resp.sendError(400, "Invalid POST request: " + r.getFullRequest());
}
} finally {
resp.getOutputStream().close();
req.getInputStream().close();
}
check();
}
use of org.nanopub.NanopubImpl in project eol-globi-data by jhpoelen.
the class LinkerTrustyNanoPubs method generateTrustyNanopub.
public Nanopub generateTrustyNanopub(String nanoPubString) throws MalformedNanopubException, OpenRDFException, TrustyUriException {
NanopubImpl nanopub = new NanopubImpl(nanoPubString, RDFFormat.TRIG);
Nanopub trustyNanopub = MakeTrustyNanopub.transform(nanopub);
OutputStream os = osFactory.outputStreamFor(trustyNanopub);
OutputStreamWriter writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
RDFWriter w = Rio.createWriter(RDFFormat.TRIG, writer);
NanopubUtils.propagateToHandler(trustyNanopub, w);
try {
writer.flush();
} catch (IOException e) {
// ignore
} finally {
IOUtils.closeQuietly(writer);
}
return trustyNanopub;
}
use of org.nanopub.NanopubImpl in project eol-globi-data by jhpoelen.
the class LinkerTrustyNanoPubsTest method trustyURI.
@Test
public void trustyURI() throws OpenRDFException, IOException, MalformedNanopubException, TrustyUriException {
NanopubImpl nanopub = new NanopubImpl(getClass().getResourceAsStream("nanopub.trig"), RDFFormat.TRIG);
ByteArrayOutputStream actual = new ByteArrayOutputStream();
Nanopub trustyNanopub = MakeTrustyNanopub.writeAsTrustyNanopub(nanopub, RDFFormat.TRIG, actual);
String artifactCode = TrustyUriUtils.getArtifactCode(trustyNanopub.getUri().toString());
assertThat(artifactCode, is("RA7XQvcOGTux6HTndtjAiVWXjEMZbQMH4yJIxTMCV8sx4"));
String actualTrig = toTrigString(new ByteArrayInputStream(actual.toByteArray()));
String expectedTrig = toTrigString(getClass().getResourceAsStream("trusty.nanopub.trig"));
assertThat(actualTrig, is(expectedTrig));
}
Aggregations