• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanopub-registry / 20413250445

21 Dec 2025 05:19PM UTC coverage: 0.924% (-0.03%) from 0.954%
20413250445

push

github

ashleycaselli
chore: minor code cleanup

6 of 592 branches covered (1.01%)

Branch coverage included in aggregate %.

16 of 1789 relevant lines covered (0.89%)

0.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/main/java/com/knowledgepixels/registry/NanopubPage.java
1
package com.knowledgepixels.registry;
2

3
import com.github.jsonldjava.shaded.com.google.common.base.Charsets;
4
import com.mongodb.client.ClientSession;
5
import eu.neverblink.jelly.core.utils.IoUtils;
6
import io.vertx.ext.web.RoutingContext;
7
import org.apache.commons.lang.StringEscapeUtils;
8
import org.bson.Document;
9
import org.bson.types.Binary;
10
import org.eclipse.rdf4j.common.exception.RDF4JException;
11
import org.eclipse.rdf4j.rio.RDFFormat;
12
import org.nanopub.MalformedNanopubException;
13
import org.nanopub.Nanopub;
14
import org.nanopub.NanopubImpl;
15
import org.nanopub.NanopubUtils;
16

17
import java.io.IOException;
18

19
import static com.knowledgepixels.registry.RegistryDB.collection;
20
import static com.knowledgepixels.registry.RegistryDB.isSet;
21
import static com.knowledgepixels.registry.Utils.*;
22

23
public class NanopubPage extends Page {
24

25
    public static void show(RoutingContext context) {
26
        NanopubPage page;
27
        try (ClientSession s = RegistryDB.getClient().startSession()) {
×
28
            s.startTransaction();
×
29
            page = new NanopubPage(s, context);
×
30
            page.show();
×
31
        } catch (IOException ex) {
×
32
            ex.printStackTrace();
×
33
        } finally {
34
            context.response().end();
×
35
            // TODO Clean-up here?
36
        }
37
    }
×
38

39
    private NanopubPage(ClientSession mongoSession, RoutingContext context) {
40
        super(mongoSession, context);
×
41
    }
×
42

43
    protected void show() throws IOException {
44
        RoutingContext c = getContext();
×
45
        String ext = getExtension();
×
46
        String format = Utils.getType(ext);
×
47
        final String req = getRequestString();
×
48
        if (format == null) {
×
49
            format = Utils.getMimeType(c, SUPPORTED_TYPES_NANOPUB);
×
50
        }
51
        if (format == null) {
×
52
            c.response().setStatusCode(400).setStatusMessage("Invalid request: " + getFullRequest());
×
53
            return;
×
54
        }
55

56
        var presentationFormat = getPresentationFormat();
×
57
        if (presentationFormat != null) {
×
58
            setRespContentType(presentationFormat);
×
59
        } else {
60
            setRespContentType(format);
×
61
        }
62

63
        if (req.matches("/np/RA[a-zA-Z0-9-_]{43}(\\.[a-z]+)?")) {
×
64
            String ac = req.replaceFirst("/np/(RA[a-zA-Z0-9-_]{43})(\\.[a-z]+)?", "$1");
×
65
            Document npDoc = collection("nanopubs").find(new Document("_id", ac)).first();
×
66
            if (npDoc == null) {
×
67
                if (!isSet(mongoSession, "serverInfo", "testInstance")) {
×
68
                    //getResp().sendError(404, "Not found: " + ac);
69
                    c.response().setStatusCode(307);
×
70
                    c.response().putHeader("Location", "https://np.knowledgepixels.com/" + ac);
×
71
                    return;
×
72
                } else {
73
                    c.response().setStatusCode(404).setStatusMessage("Not found: " + getFullRequest());
×
74
                    return;
×
75
                }
76
            }
77
            //                String url = ServerConf.getInfo().getPublicUrl();
78
            if (TYPE_TRIG.equals(format)) {
×
79
                println(npDoc.getString("content"));
×
80
            } else if (TYPE_JELLY.equals(format)) {
×
81
                if (presentationFormat != null && presentationFormat.startsWith("text")) {
×
82
                    // Parse the Jelly frame and return it as Protobuf Text Format Language
83
                    // https://protobuf.dev/reference/protobuf/textformat-spec/
84
                    // It's better than bombarding the browser with a binary file.
85
                    var frame = eu.neverblink.jelly.core.proto.google.v1.RdfStreamFrame
×
86
                            .parseFrom(((Binary) npDoc.get("jelly")).getData());
×
87
                    println(frame.toString());
×
88
                } else {
×
89
                    // To return this correctly, we would need to prepend the delimiter byte before the Jelly frame
90
                    // (the DB stores is non-delimited and the HTTP response must be delimited).
91
                    BufferOutputStream outputStream = new BufferOutputStream();
×
92
                    IoUtils.writeFrameAsDelimited(
×
93
                            ((Binary) npDoc.get("jelly")).getData(),
×
94
                            outputStream
95
                    );
96
                    c.response().write(outputStream.getBuffer());
×
97
                }
×
98
            } else if (format != null && format.equals(TYPE_NQUADS)) {
×
99
                outputNanopub(npDoc, RDFFormat.NQUADS);
×
100
            } else if (format != null && format.equals(TYPE_JSONLD)) {
×
101
                outputNanopub(npDoc, RDFFormat.JSONLD);
×
102
            } else if (format != null && format.equals(TYPE_TRIX)) {
×
103
                outputNanopub(npDoc, RDFFormat.TRIX);
×
104
            } else {
105
                printHtmlHeader("Nanopublication " + ac + " - Nanopub Registry");
×
106
                println("<h1>Nanopublication</h1>");
×
107
                println("<p><a href=\"/\">&lt; Home</a></p>");
×
108
                println("<h3>ID</h3>");
×
109
                String fullId = npDoc.getString("fullId");
×
110
                println("<p><a href=\"" + fullId + "\"><code>" + fullId + "</code></a></p>");
×
111
                println("<h3>Formats</h3>");
×
112
                println("<p>");
×
113
                println("<a href=\"/np/" + ac + ".trig\">.trig</a> |");
×
114
                println("<a href=\"/np/" + ac + ".trig.txt\">.trig.txt</a> |");
×
115
                println("<a href=\"/np/" + ac + ".jelly\">.jelly</a> |");
×
116
                println("<a href=\"/np/" + ac + ".jelly.txt\">.jelly.txt</a> |");
×
117
                println("<a href=\"/np/" + ac + ".jsonld\">.jsonld</a> |");
×
118
                println("<a href=\"/np/" + ac + ".jsonld.txt\">.jsonld.txt</a> |");
×
119
                println("<a href=\"/np/" + ac + ".nq\">.nq</a> |");
×
120
                println("<a href=\"/np/" + ac + ".nq.txt\">.nq.txt</a> |");
×
121
                println("<a href=\"/np/" + ac + ".xml\">.xml</a> |");
×
122
                println("<a href=\"/np/" + ac + ".xml.txt\">.xml.txt</a>");
×
123
                println("</p>");
×
124
                println("<h3>Content</h3>");
×
125
                println("<pre>");
×
126
                println(StringEscapeUtils.escapeHtml(npDoc.getString("content")));
×
127
                println("</pre>");
×
128
                printHtmlFooter();
×
129
            }
130
        } else {
×
131
            c.response().setStatusCode(400).setStatusMessage("Invalid request: " + getFullRequest());
×
132
        }
133
    }
×
134

135
    private void outputNanopub(Document npDoc, RDFFormat rdfFormat) {
136
        RoutingContext c = getContext();
×
137
        try {
138
            Nanopub np = new NanopubImpl(npDoc.getString("content"), RDFFormat.TRIG);
×
139
            c.response().write(NanopubUtils.writeToString(np, rdfFormat), Charsets.UTF_8.toString());
×
140
        } catch (RDF4JException | MalformedNanopubException | IOException ex) {
×
141
            c.response().setStatusCode(500).setStatusMessage("Failed transforming nanopub: " + getFullRequest());
×
142
            ex.printStackTrace();
×
143
        }
×
144
    }
×
145

146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc