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

knowledgepixels / nanodash / 23112508226

15 Mar 2026 02:37PM UTC coverage: 15.984% (+0.2%) from 15.811%
23112508226

Pull #402

github

web-flow
Merge b1ffd3d2f into 7b510582a
Pull Request #402: Fix unbounded memory growth and resource exhaustion

717 of 5509 branches covered (13.02%)

Branch coverage included in aggregate %.

1809 of 10294 relevant lines covered (17.57%)

2.39 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/LookupApis.java
1
package com.knowledgepixels.nanodash;
2

3
import java.io.InputStream;
4
import java.net.URLEncoder;
5
import java.nio.charset.StandardCharsets;
6
import java.util.List;
7
import java.util.Map;
8

9
import org.apache.commons.io.IOUtils;
10
import org.apache.http.HttpHeaders;
11
import org.apache.http.HttpResponse;
12
import org.apache.http.NameValuePair;
13
import org.apache.http.client.methods.HttpGet;
14
import org.apache.http.client.methods.HttpPost;
15
import org.apache.http.client.utils.URLEncodedUtils;
16
import org.apache.http.util.EntityUtils;
17
import org.apache.http.impl.client.CloseableHttpClient;
18
import org.apache.http.impl.client.HttpClientBuilder;
19
import org.nanopub.extra.services.ApiResponse;
20
import org.nanopub.extra.services.ApiResponseEntry;
21
import org.nanopub.extra.services.QueryRef;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

25
import com.beust.jcommander.Strings;
26
import com.github.openjson.JSONArray;
27
import com.github.openjson.JSONObject;
28
import com.google.common.collect.ArrayListMultimap;
29
import com.google.common.collect.Multimap;
30
import com.knowledgepixels.nanodash.component.QueryParamField;
31

32
/**
33
 * Utility class for APIs look up and parsing.
34
 */
35
public class LookupApis {
36

37
    private static final Logger logger = LoggerFactory.getLogger(LookupApis.class);
×
38

39
    private LookupApis() {
40
    }  // no instances allowed
41

42
    /**
43
     * Parses a JSON response from a grlc API for nanopublications and extracts URIs and labels.
44
     *
45
     * @param grlcJsonObject the JSON object containing the grlc API response
46
     * @param labelMap       a map to store URIs and their corresponding labels
47
     * @param values         a list to store the extracted URIs
48
     */
49
    public static void parseNanopubGrlcApi(JSONObject grlcJsonObject, Map<String, String> labelMap, List<String> values) {
50
        // Aimed to resolve Nanopub grlc API: http://grlc.nanopubs.lod.labs.vu.nl/api/local/local/find_signed_nanopubs_with_text?text=covid
51
        JSONArray resultsArray = grlcJsonObject.getJSONObject("results").getJSONArray("bindings");
×
52
        for (int i = 0; i < resultsArray.length(); i++) {
×
53
            JSONObject resultObject = resultsArray.getJSONObject(i);
×
54
            // Get the nanopub URI
55
            String uri = resultObject.getJSONObject("thing").getString("value");
×
56
            // Get the string which matched with the search term
57
            String label = resultObject.getJSONObject("label").getString("value");
×
58
            values.add(uri);
×
59
            labelMap.put(uri, label);
×
60
        }
61
    }
×
62

63
    /**
64
     * Fetches possible values from an API based on the provided search term.
65
     *
66
     * @param apiString  the API endpoint URL to query
67
     * @param searchterm the search term to use for querying the API
68
     * @param labelMap   a map to store URIs and their corresponding labels
69
     * @param values     a list to store the extracted URIs
70
     */
71
    public static void getPossibleValues(String apiString, String searchterm, Map<String, String> labelMap, List<String> values) {
72
        // TODO This method is a mess and needs some serious clean-up and structuring...
73
        try {
74
            if (apiString.startsWith("https://w3id.org/np/l/nanopub-query-1.1/api/") || apiString.startsWith("http://purl.org/nanopub/api/find_signed_things?")) {
×
75
                String queryId = QueryApiAccess.FIND_THINGS;
×
76
                if (apiString.startsWith("https://w3id.org/np/l/nanopub-query-1.1/api/")) {
×
77
                    queryId = apiString.replace("https://w3id.org/np/l/nanopub-query-1.1/api/", "");
×
78
                    if (queryId.contains("?")) queryId = queryId.substring(0, queryId.indexOf("?"));
×
79
                }
80
                Multimap<String, String> params = ArrayListMultimap.create();
×
81
                if (apiString.contains("?")) {
×
82
                    List<NameValuePair> urlParams = URLEncodedUtils.parse(apiString.substring(apiString.indexOf("?") + 1), StandardCharsets.UTF_8);
×
83
                    for (NameValuePair p : urlParams) {
×
84
                        params.put(p.getName(), p.getValue());
×
85
                    }
×
86
                }
87
                GrlcQuery q = GrlcQuery.get(queryId);
×
88
                if (q.getEndpoint().stringValue().endsWith("/text")) {
×
89
                    searchterm = expandSearchTerm(searchterm);
×
90
                }
91
                String queryParamName = "query";
×
92
                if (q.getPlaceholdersList().size() == 1) {
×
93
                    queryParamName = QueryParamField.getParamName(q.getPlaceholdersList().get(0));
×
94
                }
95
                params.put(queryParamName, searchterm);
×
96
                ApiResponse result = ApiCache.retrieveResponseSync(new QueryRef(queryId, params), false);
×
97
                int count = 0;
×
98
                for (ApiResponseEntry r : result.getData()) {
×
99
                    String uri = r.get("thing");
×
100
                    values.add(uri);
×
101
                    String desc = r.get("description");
×
102
                    if (desc == null) desc = "";
×
103
                    if (desc.length() > 80) desc = desc.substring(0, 77) + "...";
×
104
                    if (!desc.isEmpty()) desc = " - " + desc;
×
105
                    labelMap.put(uri, r.get("label") + desc);
×
106
                    count++;
×
107
                    if (count > 9) return;
×
108
                }
×
109
                return;
×
110
            }
111

112
            if (apiString.startsWith("https://vodex.")) {
×
113
                searchterm = expandSearchTerm(searchterm);
×
114
            }
115
            String callUrl;
116
            if (apiString.contains(" ")) {
×
117
                callUrl = apiString.replaceAll(" ", URLEncoder.encode(searchterm, StandardCharsets.UTF_8.toString()));
×
118
            } else {
119
                callUrl = apiString + URLEncoder.encode(searchterm, StandardCharsets.UTF_8.toString());
×
120
            }
121
            HttpGet get = new HttpGet(callUrl);
×
122
            get.setHeader(HttpHeaders.ACCEPT, "application/json");
×
123
            get.setHeader("User-Agent", NanodashPreferences.get().getWebsiteUrl() + "#user-agent");
×
124
            String respString;
125
            try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
×
126
                HttpResponse resp = client.execute(get);
×
127
                if (resp.getStatusLine().getStatusCode() == 405) {
×
128
                    // Method not allowed, trying POST
129
                    EntityUtils.consume(resp.getEntity());
×
130
                    HttpPost post = new HttpPost(apiString + URLEncoder.encode(searchterm, StandardCharsets.UTF_8.toString()));
×
131
                    resp = client.execute(post);
×
132
                }
133
                // TODO: support other content types (CSV, XML, ...)
134
                // System.err.println(resp.getHeaders("Content-Type")[0]);
135
                try (InputStream in = resp.getEntity().getContent()) {
×
136
                    respString = IOUtils.toString(in, StandardCharsets.UTF_8);
×
137
                }
138
            }
139
            // System.out.println(respString);
140

141
            if (apiString.startsWith("https://w3id.org/np/l/nanopub-query") || apiString.startsWith("https://grlc.") || apiString.contains("/sparql?")) {
×
142
                JSONArray resultsArray = new JSONObject(respString).getJSONObject("results").getJSONArray("bindings");
×
143
                for (int i = 0; i < resultsArray.length(); i++) {
×
144
                    JSONObject resultObject = resultsArray.getJSONObject(i);
×
145
                    // Get the nanopub URI
146
                    String uri = resultObject.getJSONObject("thing").getString("value");
×
147
                    // Get the string which matched with the search term
148
                    String label = resultObject.getJSONObject("label").getString("value");
×
149
                    values.add(uri);
×
150
                    labelMap.put(uri, label);
×
151
                }
152
            } else if (apiString.startsWith("https://www.ebi.ac.uk/ols/api/select")) {
×
153
                // Resolve EBI Ontology Lookup Service
154
                // e.g. https://www.ebi.ac.uk/ols/api/select?q=interacts%20with
155
                // response.docs.[].iri/label
156
                JSONArray responseArray = new JSONObject(respString).getJSONObject("response").getJSONArray("docs");
×
157
                for (int i = 0; i < responseArray.length(); i++) {
×
158
                    String uri = responseArray.getJSONObject(i).getString("iri");
×
159
                    String label = responseArray.getJSONObject(i).getString("label");
×
160
                    try {
161
                        label += " - " + responseArray.getJSONObject(i).getJSONArray("description").getString(0);
×
162
                    } catch (Exception ex) {
×
163
                        logger.error("No description found for {}", uri, ex);
×
164
                    }
×
165
                    if (!values.contains(uri)) {
×
166
                        values.add(uri);
×
167
                        labelMap.put(uri, label);
×
168
                    }
169
                }
170
            } else if (apiString.startsWith("https://api.openaire.eu/graph/v")) {
×
171
                String type = apiString.replaceFirst("^https://api\\.openaire\\.eu/graph/(v[0-9]+/[a-zA-Z]+).*$", "$1");
×
172
                for (Object obj : new JSONObject(respString).getJSONArray("results")) {
×
173
                    if (obj instanceof JSONObject jsonObj) {
×
174
                        String uri = "https://api.openaire.eu/graph/" + type + "/" + jsonObj.getString("id");
×
175
                        if (!values.contains(uri)) {
×
176
                            values.add(uri);
×
177
                            String label = uri;
×
178
                            if (jsonObj.has("mainTitle")) {
×
179
                                label = jsonObj.getString("mainTitle");
×
180
                            } else if (jsonObj.has("legalShortName")) {
×
181
                                label = jsonObj.getString("legalShortName");
×
182
                            } else if (jsonObj.has("officialName")) {
×
183
                                label = jsonObj.getString("officialName");
×
184
                            } else if (jsonObj.has("title")) {
×
185
                                label = jsonObj.getString("title");
×
186
                            } else if (jsonObj.has("familyName")) {
×
187
                                label = jsonObj.getString("familyName");
×
188
                                if (jsonObj.has("givenName")) {
×
189
                                    label = jsonObj.getString("givenName") + " " + label;
×
190
                                }
191
                            }
192
                            labelMap.put(uri, label);
×
193
                        }
194
                    }
195
                }
×
196
            } else if (apiString.startsWith("https://api.gbif.org/v1/species/suggest")) {
×
197
                JSONArray responseArray = new JSONArray(respString);
×
198
                for (int i = 0; i < responseArray.length(); i++) {
×
199
                    String uri = "https://www.gbif.org/species/" + responseArray.getJSONObject(i).getString("key");
×
200
                    String label = responseArray.getJSONObject(i).getString("scientificName");
×
201
                    if (!values.contains(uri)) {
×
202
                        values.add(uri);
×
203
                        labelMap.put(uri, label);
×
204
                    }
205
                }
206
            } else if (apiString.startsWith("https://api.catalogueoflife.org/dataset/3LR/nameusage/search")) {
×
207
                JSONArray responseArray = new JSONObject(respString).getJSONArray("result");
×
208
                for (int i = 0; i < responseArray.length(); i++) {
×
209
                    String uri = "https://www.catalogueoflife.org/data/taxon/" + responseArray.getJSONObject(i).getString("id");
×
210
                    String label = responseArray.getJSONObject(i).getJSONObject("usage").getString("label");
×
211
                    if (!values.contains(uri)) {
×
212
                        values.add(uri);
×
213
                        labelMap.put(uri, label);
×
214
                    }
215
                }
216
            } else if (apiString.startsWith("https://vodex.")) {
×
217
                // TODO This is just a test and needs to be improved
218
                JSONArray responseArray = new JSONObject(respString).getJSONObject("response").getJSONArray("docs");
×
219
                for (int i = 0; i < responseArray.length(); i++) {
×
220
                    String uri = responseArray.getJSONObject(i).getString("id");
×
221
                    String label = responseArray.getJSONObject(i).getJSONArray("label").get(0).toString();
×
222
                    if (!values.contains(uri)) {
×
223
                        values.add(uri);
×
224
                        labelMap.put(uri, label);
×
225
                    }
226
                }
227
            } else if (apiString.startsWith("https://api.ror.org/organizations")) {
×
228
                // TODO This is just a test and needs to be improved
229
                JSONArray responseArray = new JSONObject(respString).getJSONArray("items");
×
230
                for (int i = 0; i < responseArray.length(); i++) {
×
231
                    String uri = responseArray.getJSONObject(i).getString("id");
×
232
                    JSONArray namesArray = responseArray.getJSONObject(i).getJSONArray("names");
×
233
                    if (namesArray.length() == 0) continue;
×
234
                    String label = namesArray.getJSONObject(0).getString("value");
×
235
                    if (!values.contains(uri)) {
×
236
                        values.add(uri);
×
237
                        labelMap.put(uri, label);
×
238
                    }
239
                }
240
            } else {
×
241
                // TODO: create parseJsonApi() ?
242
                boolean foundId = false;
×
243
                JSONObject json = new JSONObject(respString);
×
244
                for (String key : json.keySet()) {
×
245
                    if (values.size() > 9) break;
×
246
                    if (!(json.get(key) instanceof JSONArray)) continue;
×
247
                    JSONArray a = json.getJSONArray(key);
×
248
                    for (int i = 0; i < a.length(); i++) {
×
249
                        if (values.size() > 9) break;
×
250
                        if (!(a.get(i) instanceof JSONObject)) continue;
×
251
                        JSONObject o = a.getJSONObject(i);
×
252
                        String uri = null;
×
253
                        for (String s : new String[]{"@id", "concepturi", "uri"}) {
×
254
                            if (o.has(s)) {
×
255
                                uri = o.get(s).toString();
×
256
                                foundId = true;
×
257
                                break;
×
258
                            }
259
                        }
260
                        if (uri != null) {
×
261
                            values.add(uri);
×
262
                            String label = "";
×
263
                            for (String s : new String[]{"prefLabel", "label"}) {
×
264
                                if (o.has(s)) {
×
265
                                    label = o.get(s).toString().replaceAll(" - ", " -- ");
×
266
                                    break;
×
267
                                }
268
                            }
269
                            String desc = "";
×
270
                            for (String s : new String[]{"definition", "description"}) {
×
271
                                if (o.has(s)) {
×
272
                                    desc = o.get(s).toString();
×
273
                                    break;
×
274
                                }
275
                            }
276
                            if (!label.isEmpty() && !desc.isEmpty()) desc = " - " + desc;
×
277
                            labelMap.put(uri, label + desc);
×
278
                        }
279
                    }
280
                }
×
281
                if (foundId == false) {
×
282
                    // ID key not found, try to get results for following format
283
                    // {result1: ["label 1", "label 2"], result2: ["label 3", "label 4"]}
284
                    // Aims to resolve https://name-resolution-sri.renci.org/docs#
285

286
                    // TODO: It seems this is triggered too often and adds 'https://identifiers.org/search' when it
287
                    //       shouldn't. Manually filtering these out for now...
288
                    for (String key : json.keySet()) {
×
289
                        if (!(json.get(key) instanceof JSONArray)) continue;
×
290
                        if ("search".equals(key)) continue;
×
291
                        JSONArray labelArray = json.getJSONArray(key);
×
292
                        String uri = key;
×
293
                        String label = "";
×
294
                        String desc = "";
×
295
                        if (labelArray.length() > 0) label = labelArray.getString(0);
×
296
                        if (labelArray.length() > 1) desc = labelArray.getString(1);
×
297
                        if (desc.length() > 80) desc = desc.substring(0, 77) + "...";
×
298
                        if (!label.isEmpty() && !desc.isEmpty()) desc = " - " + desc;
×
299
                        // Quick fix to convert CURIE to URI, as Nanodash only accepts URIs here
300
                        if (!(uri.startsWith("http://") || uri.startsWith("https://"))) {
×
301
                            uri = "https://identifiers.org/" + uri;
×
302
                        }
303
                        values.add(uri);
×
304
                        labelMap.put(uri, label + desc);
×
305
                    }
×
306
                }
307
            }
308
        } catch (Exception ex) {
×
309
            logger.error("Error fetching possible values from API: {}", apiString, ex);
×
310
        }
×
311
    }
×
312

313
    private static String expandSearchTerm(String searchTerm) {
314
        String expanded = "";
×
315
        boolean insideQuotes = false;
×
316
        searchTerm = searchTerm.replaceAll("\\s+", " ").trim();
×
317
        for (char c : searchTerm.toCharArray()) {
×
318
            if (c == '\n') {
×
319
                continue;
×
320
            } else if (c == '"') {
×
321
                expanded += '"';
×
322
                insideQuotes = !insideQuotes;
×
323
            } else if (c == ' ') {
×
324
                if (insideQuotes) {
×
325
                    expanded += ' ';
×
326
                } else {
327
                    expanded += '\n';
×
328
                }
329
            } else if (("" + c).matches("\\w") || c == '-' || c == '_') {
×
330
                expanded += c;
×
331
            } else {
332
                if (insideQuotes) {
×
333
                    expanded += ' ';
×
334
                } else {
335
                    expanded += '\n';
×
336
                }
337
            }
338
        }
339
        String extra = "*";
×
340
        expanded = expanded.replaceAll("\\n+", "\n").replaceAll("\"", "\\\\\\\"").trim();
×
341
        if (expanded.endsWith("\"") || insideQuotes) extra = "";
×
342
        return "( " + Strings.join(" AND ", expanded.split("\n")) + extra + " )";
×
343
    }
344

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