Skip to content

Commit c225171

Browse files
committed
Make code easier to use; add quickstart example to README
1 parent 2bc7bd4 commit c225171

File tree

7 files changed

+119
-19
lines changed

7 files changed

+119
-19
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ file:
2323
Alternatively, you might want to use one of the [pre-built
2424
jar files](https://github.com/Nanopublication/nanopub-java/releases).
2525

26+
## Quickstart Java Instructions
27+
28+
In a nutshell, this is how nanopublications can be created and published
29+
programmatically:
30+
31+
System.err.println("# Creating nanopub...");
32+
NanopubCreator npCreator = new NanopubCreator(true);
33+
final IRI anne = vf.createIRI("https://example.com/anne");
34+
npCreator.addAssertionStatement(anne, RDF.TYPE, vf.createIRI("https://schema.org/Person"));
35+
npCreator.addProvenanceStatement(PROV.WAS_ATTRIBUTED_TO, anne);
36+
npCreator.addPubinfoStatement(RDF.TYPE, vf.createIRI("http://purl.org/nanopub/x/ExampleNanopub"));
37+
Nanopub np = npCreator.finalizeNanopub(true);
38+
System.err.println("# Nanopub before signing:");
39+
NanopubUtils.writeToStream(np, System.err, RDFFormat.TRIG);
40+
Nanopub signedNp = SignNanopub.signAndTransform(np, TransformContext.makeDefault());
41+
System.err.println("# Final nanopub after signing:");
42+
NanopubUtils.writeToStream(signedNp, System.err, RDFFormat.TRIG);
43+
System.err.println("# Publishing to test server...");
44+
PublishNanopub.publishToTestServer(signedNp);
45+
//System.err.println("# Publishing to real server...");
46+
//PublishNanopub.publish(signedNp);
2647

2748
## Usage on Unix Command-Line
2849

src/main/java/org/nanopub/NanopubCreator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public NanopubCreator() {
4747
init();
4848
}
4949

50+
public NanopubCreator(boolean initWithTempNanopubIris) {
51+
this();
52+
if (initWithTempNanopubIris) {
53+
setNanopubUri(NanopubUtils.createTempNanopubIri());
54+
}
55+
}
56+
5057
public NanopubCreator(IRI nanopubUri) {
5158
this();
5259
setNanopubUri(nanopubUri);

src/main/java/org/nanopub/NanopubUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.HashSet;
1414
import java.util.List;
1515
import java.util.Map;
16+
import java.util.Random;
1617
import java.util.Set;
1718

1819
import org.apache.commons.lang3.tuple.Pair;
@@ -393,4 +394,11 @@ public static CloseableHttpClient getHttpClient() {
393394
return httpClient;
394395
}
395396

397+
398+
private static Random random = new Random();
399+
400+
public static IRI createTempNanopubIri() {
401+
return vf.createIRI("http://purl.org/nanopub/temp/" + Math.abs(random.nextInt()) + "/");
402+
}
403+
396404
}

src/main/java/org/nanopub/extra/security/TransformContext.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package org.nanopub.extra.security;
22

3+
import static org.nanopub.extra.security.SignatureAlgorithm.RSA;
4+
35
import java.security.KeyPair;
46
import java.util.HashMap;
57
import java.util.HashSet;
68
import java.util.Map;
79

810
import org.eclipse.rdf4j.model.IRI;
911
import org.eclipse.rdf4j.model.Resource;
12+
import org.eclipse.rdf4j.model.ValueFactory;
13+
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
1014
import org.eclipse.rdf4j.rio.RDFFormat;
15+
import org.nanopub.NanopubProfile;
1116
import org.nanopub.trusty.CrossRefResolver;
1217

1318
import net.trustyuri.TrustyUriUtils;
@@ -17,6 +22,23 @@ public class TransformContext {
1722

1823
// TODO: Use this also for MakeTrustyNanopub
1924

25+
private static ValueFactory vf = SimpleValueFactory.getInstance();
26+
27+
public static TransformContext makeDefault() {
28+
IRI signerIri = null;
29+
NanopubProfile profile = new NanopubProfile(NanopubProfile.IMPLICIT_PROFILE_FILE_NAME);
30+
if (profile.getOrcidId() != null) {
31+
signerIri = vf.createIRI(profile.getOrcidId());
32+
}
33+
KeyPair key = null;
34+
try {
35+
key = SignNanopub.loadKey("~/.nanopub/id_rsa", RSA);
36+
} catch (Exception ex) {
37+
ex.printStackTrace();
38+
}
39+
return new TransformContext(RSA, key, signerIri, false, false, false);
40+
}
41+
2042
private SignatureAlgorithm algorithm;
2143
private KeyPair key;
2244
private IRI signer;

src/main/java/org/nanopub/extra/server/PublishNanopub.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ public static String publish(Nanopub nanopub) throws IOException {
5050
return new PublishNanopub().publishNanopub(nanopub);
5151
}
5252

53+
public static String publish(Nanopub nanopub, String serverUrl) throws IOException {
54+
return new PublishNanopub().publishNanopub(nanopub, serverUrl);
55+
}
56+
57+
// TODO Make this dynamic/configureable:
58+
public static final String TEST_SERVER_URL = "https://test.registry.knowledgepixels.com/";
59+
60+
public static String publishToTestServer(Nanopub nanopub) throws IOException {
61+
return new PublishNanopub().publishNanopub(nanopub, TEST_SERVER_URL);
62+
}
63+
5364
private ServerIterator serverIterator = null;
5465
private RegistryInfo registryInfo = null;
5566
private Map<String,Integer> usedServers = new HashMap<>();

src/main/java/org/nanopub/op/Create.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package org.nanopub.op;
22

3-
import com.beust.jcommander.ParameterException;
4-
import net.trustyuri.TrustyUriException;
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
import java.util.zip.GZIPOutputStream;
8+
59
import org.eclipse.rdf4j.model.IRI;
610
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
711
import org.eclipse.rdf4j.model.vocabulary.RDFS;
812
import org.eclipse.rdf4j.rio.RDFFormat;
913
import org.eclipse.rdf4j.rio.RDFHandlerException;
1014
import org.eclipse.rdf4j.rio.RDFParseException;
1115
import org.eclipse.rdf4j.rio.Rio;
12-
import org.nanopub.*;
16+
import org.nanopub.CliRunner;
17+
import org.nanopub.MalformedNanopubException;
18+
import org.nanopub.Nanopub;
19+
import org.nanopub.NanopubCreator;
20+
import org.nanopub.NanopubUtils;
1321

14-
import java.io.File;
15-
import java.io.FileOutputStream;
16-
import java.io.IOException;
17-
import java.io.OutputStream;
18-
import java.util.Random;
19-
import java.util.zip.GZIPOutputStream;
22+
import com.beust.jcommander.ParameterException;
23+
24+
import net.trustyuri.TrustyUriException;
2025

2126
public class Create extends CliRunner {
2227

@@ -40,7 +45,6 @@ public static void main(String[] args) {
4045

4146
private RDFFormat rdfOutFormat;
4247
private OutputStream outputStream = System.out;
43-
private Random random = new Random();
4448

4549
private void run() throws IOException, RDFParseException, RDFHandlerException,
4650
MalformedNanopubException, TrustyUriException {
@@ -59,9 +63,8 @@ private void run() throws IOException, RDFParseException, RDFHandlerException,
5963
}
6064
}
6165

62-
String npUri = "http://purl.org/nanopub/temp/" + Math.abs(random.nextInt()) + "/";
63-
IRI nanopubIri = vf.createIRI(npUri);
64-
IRI creatorIri = vf.createIRI(npUri + "creator");
66+
IRI nanopubIri = NanopubUtils.createTempNanopubIri();
67+
IRI creatorIri = vf.createIRI(nanopubIri.stringValue() + "creator");
6568
NanopubCreator npCreator = new NanopubCreator(nanopubIri);
6669
npCreator.addAssertionStatement(npCreator.getAssertionUri(), RDFS.COMMENT, vf.createLiteral("Replace this with your assertion content."));
6770
npCreator.addProvenanceStatement(vf.createIRI("http://www.w3.org/ns/prov#hadPrimarySource"), creatorIri);

src/test/java/org/nanopub/NanopubUtilsTest.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package org.nanopub;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
import java.util.Set;
8+
39
import org.apache.http.impl.client.CloseableHttpClient;
410
import org.eclipse.rdf4j.model.IRI;
511
import org.eclipse.rdf4j.model.Statement;
612
import org.eclipse.rdf4j.model.ValueFactory;
713
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
814
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
15+
import org.eclipse.rdf4j.model.vocabulary.PROV;
16+
import org.eclipse.rdf4j.model.vocabulary.RDF;
917
import org.eclipse.rdf4j.model.vocabulary.RDFS;
1018
import org.eclipse.rdf4j.rio.RDFFormat;
1119
import org.junit.jupiter.api.Test;
12-
13-
import java.io.ByteArrayOutputStream;
14-
import java.io.IOException;
15-
import java.util.Set;
16-
17-
import static org.assertj.core.api.Assertions.assertThat;
20+
import org.nanopub.extra.security.SignNanopub;
21+
import org.nanopub.extra.security.TransformContext;
22+
import org.nanopub.extra.server.PublishNanopub;
1823

1924
public class NanopubUtilsTest {
2025

@@ -172,5 +177,28 @@ void getHttpClient() {
172177
client = NanopubUtils.getHttpClient();
173178
assertThat(client).isNotNull();
174179
}
180+
181+
// TODO: Using this as quickstart code in the README. Should probably be made executable somewhere, but not sure where...
182+
// @Test
183+
// void demoNanopubCreationExample() throws Exception {
184+
// System.err.println("==========");
185+
// System.err.println("# Creating nanopub...");
186+
// NanopubCreator npCreator = new NanopubCreator(true);
187+
// final IRI anne = vf.createIRI("https://example.com/anne");
188+
// npCreator.addAssertionStatement(anne, RDF.TYPE, vf.createIRI("https://schema.org/Person"));
189+
// npCreator.addProvenanceStatement(PROV.WAS_ATTRIBUTED_TO, anne);
190+
// npCreator.addPubinfoStatement(RDF.TYPE, vf.createIRI("http://purl.org/nanopub/x/ExampleNanopub"));
191+
// Nanopub np = npCreator.finalizeNanopub(true);
192+
// System.err.println("# Nanopub before signing:");
193+
// NanopubUtils.writeToStream(np, System.err, RDFFormat.TRIG);
194+
// Nanopub signedNp = SignNanopub.signAndTransform(np, TransformContext.makeDefault());
195+
// System.err.println("# Final nanopub after signing:");
196+
// NanopubUtils.writeToStream(signedNp, System.err, RDFFormat.TRIG);
197+
// System.err.println("# Publishing to test server...");
198+
// PublishNanopub.publishToTestServer(signedNp);
199+
// //System.err.println("# Publishing to real server...");
200+
// //PublishNanopub.publish(signedNp);
201+
// System.err.println("==========");
202+
// }
175203

176204
}

0 commit comments

Comments
 (0)