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

knowledgepixels / nanodash / 17380144000

01 Sep 2025 02:12PM UTC coverage: 12.03% (+0.05%) from 11.978%
17380144000

push

github

ashleycaselli
refactor: replace printStackTrace with logger.error for better error handling

330 of 3850 branches covered (8.57%)

Branch coverage included in aggregate %.

958 of 6857 relevant lines covered (13.97%)

0.62 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 com.beust.jcommander.Strings;
4
import com.github.openjson.JSONArray;
5
import com.github.openjson.JSONObject;
6
import com.knowledgepixels.nanodash.component.QueryParamField;
7
import org.apache.commons.io.IOUtils;
8
import org.apache.http.HttpHeaders;
9
import org.apache.http.HttpResponse;
10
import org.apache.http.NameValuePair;
11
import org.apache.http.client.methods.HttpGet;
12
import org.apache.http.client.methods.HttpPost;
13
import org.apache.http.client.utils.URLEncodedUtils;
14
import org.apache.http.impl.client.CloseableHttpClient;
15
import org.apache.http.impl.client.HttpClientBuilder;
16
import org.nanopub.extra.services.ApiResponse;
17
import org.nanopub.extra.services.ApiResponseEntry;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

21
import java.io.InputStream;
22
import java.net.URLEncoder;
23
import java.nio.charset.StandardCharsets;
24
import java.util.HashMap;
25
import java.util.List;
26
import java.util.Map;
27

28
/**
29
 * Utility class for APIs look up and parsing.
30
 */
31
public class LookupApis {
32

33
    private static final Logger logger = LoggerFactory.getLogger(LookupApis.class);
×
34

35
    private LookupApis() {
36
    }  // no instances allowed
37

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

59
    /**
60
     * Fetches possible values from an API based on the provided search term.
61
     *
62
     * @param apiString  the API endpoint URL to query
63
     * @param searchterm the search term to use for querying the API
64
     * @param labelMap   a map to store URIs and their corresponding labels
65
     * @param values     a list to store the extracted URIs
66
     */
67
    public static void getPossibleValues(String apiString, String searchterm, Map<String, String> labelMap, List<String> values) {
68
        // TODO This method is a mess and needs some serious clean-up and structuring...
69
        try {
70
            if (apiString.startsWith("https://w3id.org/np/l/nanopub-query-1.1/api/") || apiString.startsWith("http://purl.org/nanopub/api/find_signed_things?")) {
×
71
                String queryName = "find-things";
×
72
                if (apiString.startsWith("https://w3id.org/np/l/nanopub-query-1.1/api/")) {
×
73
                    queryName = apiString.replace("https://w3id.org/np/l/nanopub-query-1.1/api/", "");
×
74
                    if (queryName.contains("?")) queryName = queryName.substring(0, queryName.indexOf("?"));
×
75
                }
76
                Map<String, String> params = new HashMap<>();
×
77
                if (apiString.contains("?")) {
×
78
                    List<NameValuePair> urlParams = URLEncodedUtils.parse(apiString.substring(apiString.indexOf("?") + 1), StandardCharsets.UTF_8);
×
79
                    for (NameValuePair p : urlParams) {
×
80
                        params.put(p.getName(), p.getValue());
×
81
                    }
×
82
                }
83
                String queryId = queryName;
×
84
                if (!queryName.contains("/")) {
×
85
                    queryId = QueryApiAccess.getQueryId(queryName);
×
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 = QueryApiAccess.get(queryName, params);
×
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
            String respString;
124
            try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
×
125
                HttpResponse resp = client.execute(get);
×
126
                if (resp.getStatusLine().getStatusCode() == 405) {
×
127
                    // Method not allowed, trying POST
128
                    HttpPost post = new HttpPost(apiString + URLEncoder.encode(searchterm, StandardCharsets.UTF_8.toString()));
×
129
                    resp = client.execute(post);
×
130
                }
131
                // TODO: support other content types (CSV, XML, ...)
132
                // System.err.println(resp.getHeaders("Content-Type")[0]);
133
                try (InputStream in = resp.getEntity().getContent()) {
×
134
                    respString = IOUtils.toString(in, StandardCharsets.UTF_8);
×
135
                }
136
            }
137
            // System.out.println(respString);
138

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

255
                    // TODO: It seems this is triggered too often and adds 'https://identifiers.org/search' when it
256
                    //       shouldn't. Manually filtering these out for now...
257
                    for (String key : json.keySet()) {
×
258
                        if (!(json.get(key) instanceof JSONArray)) continue;
×
259
                        if ("search".equals(key)) continue;
×
260
                        JSONArray labelArray = json.getJSONArray(key);
×
261
                        String uri = key;
×
262
                        String label = "";
×
263
                        String desc = "";
×
264
                        if (labelArray.length() > 0) label = labelArray.getString(0);
×
265
                        if (labelArray.length() > 1) desc = labelArray.getString(1);
×
266
                        if (desc.length() > 80) desc = desc.substring(0, 77) + "...";
×
267
                        if (!label.isEmpty() && !desc.isEmpty()) desc = " - " + desc;
×
268
                        // Quick fix to convert CURIE to URI, as Nanodash only accepts URIs here
269
                        if (!(uri.startsWith("http://") || uri.startsWith("https://"))) {
×
270
                            uri = "https://identifiers.org/" + uri;
×
271
                        }
272
                        values.add(uri);
×
273
                        labelMap.put(uri, label + desc);
×
274
                    }
×
275
                }
276
            }
277
        } catch (Exception ex) {
×
278
            logger.error("Error fetching possible values from API: {}", apiString, ex);
×
279
        }
×
280
    }
×
281

282
    private static String expandSearchTerm(String searchTerm) {
283
        String expanded = "";
×
284
        boolean insideQuotes = false;
×
285
        searchTerm = searchTerm.replaceAll("\\s+", " ").trim();
×
286
        for (char c : searchTerm.toCharArray()) {
×
287
            if (c == '\n') {
×
288
                continue;
×
289
            } else if (c == '"') {
×
290
                expanded += '"';
×
291
                insideQuotes = !insideQuotes;
×
292
            } else if (c == ' ') {
×
293
                if (insideQuotes) {
×
294
                    expanded += ' ';
×
295
                } else {
296
                    expanded += '\n';
×
297
                }
298
            } else if (("" + c).matches("\\w") || c == '-' || c == '_') {
×
299
                expanded += c;
×
300
            } else {
301
                if (insideQuotes) {
×
302
                    expanded += ' ';
×
303
                } else {
304
                    expanded += '\n';
×
305
                }
306
            }
307
        }
308
        String extra = "*";
×
309
        expanded = expanded.replaceAll("\\n+", "\n").replaceAll("\"", "\\\\\\\"").trim();
×
310
        if (expanded.endsWith("\"") || insideQuotes) extra = "";
×
311
        return "( " + Strings.join(" AND ", expanded.split("\n")) + extra + " )";
×
312
    }
313

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