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

Nanopublication / nanopub-java / 17069388833

19 Aug 2025 12:21PM UTC coverage: 45.825% (-1.9%) from 47.675%
17069388833

push

github

Ziroli Plutschow
Fixed some TODOs

825 of 2880 branches covered (28.65%)

Branch coverage included in aggregate %.

4603 of 8965 relevant lines covered (51.34%)

2.46 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.extra.server.GetNanopub;
8
import org.nanopub.extra.services.ApiResponse;
9
import org.nanopub.extra.services.ApiResponseEntry;
10
import org.nanopub.extra.services.FailedApiCallException;
11
import org.nanopub.extra.services.QueryAccess;
12

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

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

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

36
    private RetrieveFdo() {
37
    }  // no instances allowed
38

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

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

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

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

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

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