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

knowledgepixels / nanopub-registry / 20413250445

21 Dec 2025 05:19PM UTC coverage: 0.924% (-0.03%) from 0.954%
20413250445

push

github

ashleycaselli
chore: minor code cleanup

6 of 592 branches covered (1.01%)

Branch coverage included in aggregate %.

16 of 1789 relevant lines covered (0.89%)

0.11 hits per line

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

14.01
src/main/java/com/knowledgepixels/registry/Utils.java
1
package com.knowledgepixels.registry;
2

3
import com.github.jsonldjava.shaded.com.google.common.base.Charsets;
4
import com.github.jsonldjava.shaded.com.google.common.reflect.TypeToken;
5
import com.google.common.hash.Hashing;
6
import com.google.gson.Gson;
7
import com.google.gson.JsonIOException;
8
import com.google.gson.JsonSyntaxException;
9
import com.mongodb.client.ClientSession;
10
import io.vertx.ext.web.RoutingContext;
11
import net.trustyuri.TrustyUriUtils;
12
import org.apache.commons.lang.StringUtils;
13
import org.commonjava.mimeparse.MIMEParse;
14
import org.eclipse.rdf4j.common.exception.RDF4JException;
15
import org.eclipse.rdf4j.model.IRI;
16
import org.eclipse.rdf4j.model.Resource;
17
import org.eclipse.rdf4j.model.Statement;
18
import org.eclipse.rdf4j.model.ValueFactory;
19
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
20
import org.nanopub.MalformedNanopubException;
21
import org.nanopub.Nanopub;
22
import org.nanopub.NanopubImpl;
23
import org.nanopub.NanopubUtils;
24
import org.nanopub.extra.setting.NanopubSetting;
25
import org.nanopub.vocabulary.NPX;
26

27
import java.io.File;
28
import java.io.IOException;
29
import java.io.InputStreamReader;
30
import java.lang.reflect.Type;
31
import java.net.URI;
32
import java.net.URISyntaxException;
33
import java.net.URLEncoder;
34
import java.nio.charset.StandardCharsets;
35
import java.util.*;
36

37
public class Utils {
38

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

42
    public static String getMimeType(RoutingContext context, String supported) {
43
        List<String> supportedList = Arrays.asList(StringUtils.split(supported, ','));
×
44
        String mimeType = supportedList.getFirst();
×
45
        try {
46
            mimeType = MIMEParse.bestMatch(supportedList, context.request().getHeader("Accept"));
×
47
        } catch (Exception ex) {
×
48
        }
×
49
        return mimeType;
×
50
    }
51

52
    public static String urlEncode(Object o) {
53
        return URLEncoder.encode((o == null ? "" : o.toString()), StandardCharsets.UTF_8);
×
54
    }
55

56
    public static String getHash(String s) {
57
        return Hashing.sha256().hashString(s, Charsets.UTF_8).toString();
18✔
58
    }
59

60
    public static Set<IRI> getInvalidatedNanopubIds(Nanopub np) {
61
        Set<IRI> l = new HashSet<IRI>();
×
62
        for (Statement st : NanopubUtils.getStatements(np)) {
×
63
            if (!(st.getObject() instanceof IRI)) continue;
×
64
            Resource s = st.getSubject();
×
65
            IRI p = st.getPredicate();
×
66
            if ((p.equals(NPX.RETRACTS) || p.equals(NPX.INVALIDATES)) || (p.equals(NPX.SUPERSEDES) && s.equals(np.getUri()))) {
×
67
                if (TrustyUriUtils.isPotentialTrustyUri(st.getObject().stringValue())) {
×
68
                    l.add((IRI) st.getObject());
×
69
                }
70
            }
71
        }
×
72
        return l;
×
73
    }
74

75
    public static String getEnv(String name, String defaultValue) {
76
        String value = System.getenv(name);
9✔
77
        if (value == null) value = defaultValue;
12!
78
        return value;
6✔
79
    }
80

81
    public static String getTypeHash(ClientSession mongoSession, Object type) {
82
        String typeHash = Utils.getHash(type.toString());
×
83
        if (type.toString().equals("$")) {
×
84
            typeHash = "$";
×
85
        } else {
86
            RegistryDB.recordHash(mongoSession, type.toString());
×
87
        }
88
        return typeHash;
×
89
    }
90

91
    public static String getAgentLabel(String agentId) {
92
        if (agentId == null || agentId.isBlank()) {
15✔
93
            throw new IllegalArgumentException("Agent ID cannot be null or blank");
15✔
94
        }
95
        agentId = agentId.replaceFirst("^https://orcid\\.org/", "orcid:");
15✔
96
        if (agentId.length() > 55) {
12!
97
            return agentId.substring(0, 50) + "...";
×
98
        }
99
        return agentId;
6✔
100
    }
101

102
    public static boolean isUnloadedStatus(String status) {
103
        if (status.equals("seen")) return true;  // only exists in "accounts_loading"?
×
104
        if (status.equals("skipped")) return true;
×
105
        return false;
×
106
    }
107

108
    public static boolean isCoreLoadedStatus(String status) {
109
        if (status.equals("visited")) return true;  // only exists in "accounts_loading"?
×
110
        if (status.equals("expanded")) return true;  // only exists in "accounts_loading"?
×
111
        if (status.equals("processed")) return true;  // only exists in "accounts_loading"?
×
112
        if (status.equals("aggregated")) return true;  // only exists in "accounts_loading"?
×
113
        if (status.equals("approved")) return true;  // only exists in "accounts_loading"?
×
114
        if (status.equals("contested")) return true;
×
115
        if (status.equals("toLoad")) return true;  // only exists in "accounts_loading"?
×
116
        if (status.equals("loaded")) return true;
×
117
        return false;
×
118
    }
119

120
    public static boolean isFullyLoadedStatus(String status) {
121
        if (status.equals("loaded")) return true;
×
122
        return false;
×
123
    }
124

125
    private static ValueFactory vf = SimpleValueFactory.getInstance();
6✔
126
    public static final IRI APPROVES_OF = vf.createIRI("http://purl.org/nanopub/x/approvesOf");
12✔
127

128
    public static final String TYPE_JSON = "application/json";
129
    public static final String TYPE_TRIG = "application/trig";
130
    public static final String TYPE_JELLY = "application/x-jelly-rdf";
131
    public static final String TYPE_JSONLD = "application/ld+json";
132
    public static final String TYPE_NQUADS = "application/n-quads";
133
    public static final String TYPE_TRIX = "application/trix";
134
    public static final String TYPE_HTML = "text/html";
135

136
    // Content types supported on a ListPage
137
    public static final String SUPPORTED_TYPES_LIST = TYPE_JSON + "," + TYPE_JELLY + "," + TYPE_HTML;
138
    // Content types supported on a NanopubPage
139
    public static final String SUPPORTED_TYPES_NANOPUB = TYPE_TRIG + "," + TYPE_JELLY + "," + TYPE_JSONLD + "," + TYPE_NQUADS + "," + TYPE_TRIX + "," + TYPE_HTML;
140

141
    private static Map<String, String> extensionTypeMap;
142

143
    public static String getType(String extension) {
144
        if (extension == null) {
×
145
            return null;
×
146
        }
147
        if (extensionTypeMap == null) {
×
148
            extensionTypeMap = new HashMap<>();
×
149
            extensionTypeMap.put("trig", TYPE_TRIG);
×
150
            extensionTypeMap.put("jelly", TYPE_JELLY);
×
151
            extensionTypeMap.put("jsonld", TYPE_JSONLD);
×
152
            extensionTypeMap.put("nq", TYPE_NQUADS);
×
153
            extensionTypeMap.put("xml", TYPE_TRIX);
×
154
            extensionTypeMap.put("html", TYPE_HTML);
×
155
            extensionTypeMap.put("json", TYPE_JSON);
×
156
        }
157
        return extensionTypeMap.get(extension);
×
158
    }
159

160
    private static List<String> peerUrls;
161

162
    public static List<String> getPeerUrls() {
163
        if (peerUrls == null) {
×
164
            String envPeerUrls = getEnv("REGISTRY_PEER_URLS", "");
×
165
            String thisRegistryUrl = getEnv("REGISTRY_SERVICE_URL", "");
×
166
            if (!envPeerUrls.isEmpty()) {
×
167
                peerUrls = new ArrayList<>();
×
168
                for (String peerUrl : envPeerUrls.split(";")) {
×
169
                    if (thisRegistryUrl.equals(peerUrl)) continue;
×
170
                    peerUrls.add(peerUrl);
×
171
                }
172
            } else {
173
                NanopubSetting setting;
174
                try {
175
                    setting = getSetting();
×
176
                } catch (Exception ex) {
×
177
                    throw new RuntimeException(ex);
×
178
                }
×
179
                peerUrls = new ArrayList<>();
×
180
                for (IRI iri : setting.getBootstrapServices()) {
×
181
                    String peerUrl = iri.stringValue();
×
182
                    if (thisRegistryUrl.equals(peerUrl)) continue;
×
183
                    peerUrls.add(peerUrl);
×
184
                }
×
185
            }
186
        }
187
        return peerUrls;
×
188
    }
189

190
    private static final String SETTING_FILE_PATH = Utils.getEnv("REGISTRY_SETTING_FILE", "/data/setting.trig");
12✔
191
    private static NanopubSetting settingNp;
192

193
    public static NanopubSetting getSetting() throws RDF4JException, MalformedNanopubException, IOException {
194
        if (settingNp == null) {
×
195
            settingNp = new NanopubSetting(new NanopubImpl(new File(SETTING_FILE_PATH)));
×
196
        }
197
        return settingNp;
×
198
    }
199

200
    public static String getRandomPeer() throws RDF4JException {
201
        List<String> peerUrls = getPeerUrls();
×
202
        return peerUrls.get(random.nextInt(peerUrls.size()));
×
203
    }
204

205
    private static Random random = new Random();
12✔
206

207
    public static Random getRandom() {
208
        return random;
×
209
    }
210

211
    private static Gson g = new Gson();
12✔
212
    private static Type listType = new TypeToken<List<String>>() {
21✔
213
    }.getType();
6✔
214

215
    public static List<String> retrieveListFromJsonUrl(String url) throws JsonIOException, JsonSyntaxException, IOException, URISyntaxException {
216
        return g.fromJson(new InputStreamReader(new URI(url).toURL().openStream()), listType);
×
217
    }
218

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