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

Nanopublication / nanopub-java / 17376844338

01 Sep 2025 11:54AM UTC coverage: 45.468% (-0.03%) from 45.497%
17376844338

push

github

ashleycaselli
refactor: update code to use NanopubAlreadyFinalizedException

825 of 2878 branches covered (28.67%)

Branch coverage included in aggregate %.

4608 of 9071 relevant lines covered (50.8%)

2.43 hits per line

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

86.84
src/main/java/org/nanopub/fdo/RetrieveFdo.java
1
package org.nanopub.fdo;
2

3
import org.eclipse.rdf4j.model.Value;
4
import org.eclipse.rdf4j.model.util.Values;
5
import org.nanopub.MalformedNanopubException;
6
import org.nanopub.Nanopub;
7
import org.nanopub.NanopubAlreadyFinalizedException;
8
import org.nanopub.extra.server.GetNanopub;
9
import org.nanopub.extra.services.*;
10

11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.net.URI;
14
import java.net.URISyntaxException;
15
import java.net.http.HttpClient;
16
import java.net.http.HttpRequest;
17
import java.net.http.HttpResponse;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
21

22
/**
23
 * Retrieve FDOs (FDO Records) from the nanopub network or handle system.
24
 */
25
// TODO class that provides the Op.Retrieve operations.
26
//      See https://fdo-connect.gitlab.io/ap1/architecture-documentation/main/operation-specification/
27
public class RetrieveFdo {
28

29
    /**
30
     * The query ID for retrieving FDOs by their ID from the nanopub network.
31
     */
32
    public final static String GET_FDO_QUERY_ID = "RAs0HI_KRAds4w_OOEMl-_ed0nZHFWdfePPXsDHf4kQkU/get-fdo-by-id";
33

34
    private RetrieveFdo() {
35
    }  // no instances allowed
36

37
    /**
38
     * Retrieve the NP/FdoRecord from the nanopub network or handle system, always check the nanopub network first.
39
     *
40
     * @param iriOrHandle the IRI or handle of the FDO to resolve
41
     * @return the FdoRecord corresponding to the IRI or handle
42
     * @throws org.nanopub.fdo.FdoNotFoundException if the FDO is not found in either the nanopub network or handle system
43
     */
44
    public static FdoRecord resolveId(String iriOrHandle) throws FdoNotFoundException {
45
        try {
46
            Nanopub np = resolveInNanopubNetwork(iriOrHandle);
3✔
47
            if (np != null) {
2✔
48
                return new FdoRecord(np);
5✔
49
            }
50
            if (FdoUtils.looksLikeHandle(iriOrHandle)) {
3✔
51
                return resolveInHandleSystem(iriOrHandle);
3✔
52
            } else if (FdoUtils.isHandleIri(Values.iri(iriOrHandle))) {
4!
53
                return resolveInHandleSystem(FdoUtils.extractHandle(Values.iri(iriOrHandle)));
5✔
54
            }
55
        } catch (Exception e) {
1✔
56
            throw new FdoNotFoundException(e);
5✔
57
        }
×
58
        throw new FdoNotFoundException("Could not find fdo: " + iriOrHandle);
×
59
    }
60

61
    /**
62
     * Retrieve the newest corresponding Nanopub from the Nanopub network.
63
     *
64
     * @param iriOrHandle the IRI or handle of the FDO to resolve
65
     * @return the Nanopub corresponding to the IRI or handle, or null if not found
66
     * @throws org.nanopub.extra.services.FailedApiCallException if the API call fails
67
     */
68
    public static Nanopub resolveInNanopubNetwork(String iriOrHandle) throws FailedApiCallException, APINotReachableException, NotEnoughAPIInstancesException {
69
        Map<String, String> params = new HashMap<>();
4✔
70
        params.put("fdoid", iriOrHandle);
5✔
71
        ApiResponse apiResponse = QueryAccess.get(GET_FDO_QUERY_ID, params);
4✔
72
        List<ApiResponseEntry> data = apiResponse.getData();
3✔
73
        if (data.isEmpty()) {
3✔
74
            return null;
2✔
75
        }
76
        String npRef = data.getFirst().get("np");
6✔
77
        return GetNanopub.get(npRef);
3✔
78
    }
79

80
    /**
81
     * Retrieve the data from the handle system.
82
     *
83
     * @param handle the handle of the FDO to resolve
84
     * @return the FdoRecord corresponding to the handle
85
     * @throws org.nanopub.MalformedNanopubException if the nanopub is malformed
86
     * @throws java.net.URISyntaxException           if the URI is malformed
87
     * @throws java.io.IOException                   if an I/O error occurs
88
     * @throws java.lang.InterruptedException        if the operation is interrupted
89
     */
90
    public static FdoRecord resolveInHandleSystem(String handle) throws MalformedNanopubException, URISyntaxException, IOException, InterruptedException, NanopubAlreadyFinalizedException {
91
        Nanopub np = FdoNanopubCreator.createFromHandleSystem(handle);
3✔
92
        return new FdoRecord(np);
5✔
93
    }
94

95
    /**
96
     * Retrieve the NP/FdoRecord Content (DataRef) from the nanopub network or handle system, always check the nanopub network first.
97
     *
98
     * @param iriOrHandle the IRI or handle of the FDO to resolve
99
     * @return an InputStream containing the content of the FDO
100
     * @throws java.net.URISyntaxException          if the URI is malformed
101
     * @throws java.io.IOException                  if an I/O error occurs
102
     * @throws java.lang.InterruptedException       if the operation is interrupted
103
     * @throws org.nanopub.fdo.FdoNotFoundException if the FDO is not found
104
     */
105
    public static InputStream retrieveContentFromId(String iriOrHandle) throws URISyntaxException, IOException, InterruptedException, FdoNotFoundException {
106
        FdoRecord fdo = resolveId(iriOrHandle);
3✔
107

108
        Value contentUrl = fdo.getDataRef();
3✔
109
        if (contentUrl == null) {
2!
110
            throw new FdoNotFoundException("FDO has no file / DataRef.");
×
111
        }
112
        HttpRequest req = HttpRequest.newBuilder().GET().uri(new URI(contentUrl.stringValue())).build();
10✔
113
        HttpResponse<InputStream> httpResponse = HttpClient.newHttpClient().send(req, responseInfo -> HttpResponse.BodySubscribers.ofInputStream());
5✔
114
        return httpResponse.body();
4✔
115
    }
116

117
}
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