use of org.nanopub.Nanopub 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.Nanopub in project nanopub-server by tkuhn.
the class LoadFiles method checkFilesToLoad.
private void checkFilesToLoad() {
logger.info("Check whether there are files to load...");
for (File f : loadDir.listFiles()) {
stillAlive();
if (f.isDirectory())
continue;
logger.info("Try to load file: " + f);
try {
final File processingFile = new File(processingDir, f.getName());
f.renameTo(processingFile);
RDFFormat format = Rio.getParserFormatForFileName(processingFile.getName());
MultiNanopubRdfHandler.process(format, processingFile, new NanopubHandler() {
@Override
public void handleNanopub(Nanopub np) {
if (!ourPattern.matchesUri(np.getUri().toString()))
return;
try {
db.loadNanopub(np);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
stillAlive();
}
});
processingFile.renameTo(new File(doneDir, f.getName()));
logger.info("File loaded: " + processingFile);
} catch (Exception ex) {
logger.error("Failed to load file: " + f, ex);
}
}
}
use of org.nanopub.Nanopub in project nanopub-server by tkuhn.
the class NanopubDb method writePackageToStream.
public void writePackageToStream(long pageNo, boolean gzipped, OutputStream out) throws IOException {
if (pageNo < 1 || pageNo >= journal.getCurrentPageNo()) {
throw new IllegalArgumentException("Not a complete page: " + pageNo);
}
GridFSDBFile f = packageGridFs.findOne(pageNo + "");
OutputStream packageOut = null;
InputStream packageAsStream = null;
try {
if (f == null) {
if (gzipped) {
out = new GZIPOutputStream(out);
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
packageOut = new GZIPOutputStream(bOut);
String pageContent = journal.getPageContent(pageNo);
for (String uri : pageContent.split("\\n")) {
Nanopub np = getNanopub(TrustyUriUtils.getArtifactCode(uri));
String s;
try {
s = NanopubUtils.writeToString(np, RDFFormat.TRIG);
} catch (RDFHandlerException ex) {
throw new RuntimeException("Unexpected RDF handler exception", ex);
}
byte[] bytes = (s + "\n").getBytes();
out.write(bytes);
packageOut.write(bytes);
}
packageOut.close();
packageAsStream = new ByteArrayInputStream(bOut.toByteArray());
GridFSInputFile i = packageGridFs.createFile(packageAsStream);
i.setFilename(pageNo + "");
i.save();
} else {
if (gzipped) {
f.writeTo(out);
} else {
GZIPInputStream in = new GZIPInputStream(f.getInputStream());
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
}
}
} finally {
if (out != null)
out.close();
if (packageOut != null)
packageOut.close();
if (packageAsStream != null)
packageAsStream.close();
}
}
use of org.nanopub.Nanopub 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.Nanopub 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();
}
Aggregations