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

knowledgepixels / nanodash / 18276189551

06 Oct 2025 09:23AM UTC coverage: 13.658% (-1.0%) from 14.684%
18276189551

push

github

tkuhn
feat: Redirect to user/space page from ExplorePage when applicable

446 of 4124 branches covered (10.81%)

Branch coverage included in aggregate %.

1154 of 7591 relevant lines covered (15.2%)

0.68 hits per line

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

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

3
import java.util.HashMap;
4
import java.util.Map;
5

6
import org.apache.wicket.RestartResponseException;
7
import org.apache.wicket.markup.html.WebMarkupContainer;
8
import org.apache.wicket.markup.html.basic.Label;
9
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
10
import org.apache.wicket.markup.html.link.ExternalLink;
11
import org.apache.wicket.request.flow.RedirectToUrlException;
12
import org.apache.wicket.request.mapper.parameter.PageParameters;
13
import org.commonjava.mimeparse.MIMEParse;
14
import org.nanopub.Nanopub;
15
import org.nanopub.extra.security.SignatureUtils;
16
import org.nanopub.extra.services.ApiResponse;
17
import org.nanopub.extra.services.QueryRef;
18
import org.nanopub.vocabulary.NTEMPLATE;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

22
import com.google.common.collect.ArrayListMultimap;
23
import com.google.common.collect.Multimap;
24
import com.knowledgepixels.nanodash.GrlcQuery;
25
import com.knowledgepixels.nanodash.NanopubElement;
26
import com.knowledgepixels.nanodash.QueryApiAccess;
27
import com.knowledgepixels.nanodash.Space;
28
import com.knowledgepixels.nanodash.User;
29
import com.knowledgepixels.nanodash.Utils;
30
import com.knowledgepixels.nanodash.component.ExploreDataTable;
31
import com.knowledgepixels.nanodash.component.IriItem;
32
import com.knowledgepixels.nanodash.component.NanopubItem;
33
import com.knowledgepixels.nanodash.component.StatusLine;
34
import com.knowledgepixels.nanodash.component.ThingListPanel;
35
import com.knowledgepixels.nanodash.component.TitleBar;
36

37
import jakarta.servlet.http.HttpServletRequest;
38
import net.trustyuri.TrustyUriUtils;
39

40
/**
41
 * ExplorePage is a page that allows users to explore a specific Nanopublication or Thing.
42
 */
43
public class ExplorePage extends NanodashPage {
44

45
    private static final Logger logger = LoggerFactory.getLogger(ExplorePage.class);
×
46

47
    /**
48
     * The mount path for this page.
49
     */
50
    public static final String MOUNT_PATH = "/explore";
51

52
    /**
53
     * {@inheritDoc}
54
     */
55
    @Override
56
    public String getMountPath() {
57
        return MOUNT_PATH;
×
58
    }
59

60
    /**
61
     * Constructor for ExplorePage.
62
     *
63
     * @param parameters Page parameters containing the ID of the Nanopublication or Thing to explore.
64
     */
65
    public ExplorePage(final PageParameters parameters) {
66
        super(parameters);
×
67

68
        add(new TitleBar("titlebar", this, null));
×
69

70
        String tempRef = parameters.get("id").toString();
×
71

72
        WebMarkupContainer raw = new WebMarkupContainer("raw");
×
73
        add(raw);
×
74

75
        if (User.getUserData().isUser(tempRef)) {
×
76
            throw new RestartResponseException(UserPage.class, parameters);
×
77
        } else if (Space.get(tempRef) != null) {
×
78
            throw new RestartResponseException(SpacePage.class, parameters);
×
79
        }
80

81
        Map<String, String> nanopubParams = new HashMap<>();
×
82
        nanopubParams.put("ref", tempRef);
×
83
        Nanopub np = Utils.getAsNanopub(tempRef);
×
84
        boolean isNanopubId = (np != null);
×
85
        if (isNanopubId) {
×
86
            tempRef = np.getUri().stringValue();
×
87
        }
88
        if (!isNanopubId && tempRef.matches("^(.*[^A-Za-z0-9-_])?RA[A-Za-z0-9-_]{43}[^A-Za-z0-9-_].*$")) {
×
89
            np = Utils.getAsNanopub(tempRef.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})[^A-Za-z0-9-_].*$", "$2"));
×
90
            if (np != null) {
×
91
                String npId = np.getUri().stringValue();
×
92
                tempRef = npId + tempRef.replaceFirst("^(.*[^A-Za-z0-9-_])?(RA[A-Za-z0-9-_]{43})([^A-Za-z0-9-_].*)$", "$3");
×
93
                Multimap<String, String> params = ArrayListMultimap.create();
×
94
                params.put("thing", tempRef);
×
95
                params.put("np", npId);
×
96
                ApiResponse resp = QueryApiAccess.forcedGet(new QueryRef("get-latest-thing-nanopub", params));
×
97
                if (!resp.getData().isEmpty()) {
×
98
                    // TODO We take the most recent in case more than one latest version exists. Make other latest versions visible too.
99
                    npId = resp.getData().get(0).get("latestVersion");
×
100
                }
101
                np = Utils.getAsNanopub(npId);
×
102
            }
103
        }
104
        if (np == null) {
×
105
            raw.setVisible(false);
×
106
            add(new Label("nanopub-header", ""));
×
107
            add(new Label("nanopub", ""));
×
108
            add(new WebMarkupContainer("use-template").setVisible(false));
×
109
            add(new WebMarkupContainer("run-query").setVisible(false));
×
110
        } else {
111

112
            // Check whether we should redirect to Nanopub Registry for machine-friendly formats:
113
            String mimeType = Utils.TYPE_HTML;
×
114
            try {
115
                HttpServletRequest httpRequest = (HttpServletRequest) getRequest().getContainerRequest();
×
116
                mimeType = MIMEParse.bestMatch(Utils.SUPPORTED_TYPES_LIST, httpRequest.getHeader("Accept"));
×
117
            } catch (Exception ex) {
×
118
                logger.error("Error determining MIME type from Accept header.", ex);
×
119
            }
×
120
            if (!mimeType.equals(Utils.TYPE_HTML)) {
×
121
                logger.info("Non-HTML content type: {}", mimeType);
×
122
                // TODO Make this registry URL configurable/dynamic:
123
                String redirectUrl = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
124
                logger.info("Redirecting to: {}", redirectUrl);
×
125
                throw new RedirectToUrlException(redirectUrl, 302);
×
126
            }
127

128
            String nanopubHeaderLabel = "<h4>%s</h4>";
×
129
            if (isNanopubId) {
×
130
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "Nanopublication");
×
131
            } else {
132
                nanopubHeaderLabel = String.format(nanopubHeaderLabel, "Minted in Nanopublication");
×
133
            }
134
            add(new Label("nanopub-header", nanopubHeaderLabel).setEscapeModelStrings(false));
×
135
            add(new NanopubItem("nanopub", NanopubElement.get(np)));
×
136
            String url = Utils.getMainRegistryUrl() + "np/" + TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
×
137
            raw.add(new ExternalLink("trig-txt", url + ".trig.txt"));
×
138
            raw.add(new ExternalLink("jsonld-txt", url + ".jsonld.txt"));
×
139
            raw.add(new ExternalLink("nq-txt", url + ".nq.txt"));
×
140
            raw.add(new ExternalLink("xml-txt", url + ".xml.txt"));
×
141
            raw.add(new ExternalLink("trig", url + ".trig"));
×
142
            raw.add(new ExternalLink("jsonld", url + ".jsonld"));
×
143
            raw.add(new ExternalLink("nq", url + ".nq"));
×
144
            raw.add(new ExternalLink("xml", url + ".xml"));
×
145
            if (Utils.isNanopubOfClass(np, NTEMPLATE.ASSERTION_TEMPLATE)) {
×
146
                add(new WebMarkupContainer("use-template").add(new BookmarkablePageLink<Void>("template-link", PublishPage.class, new PageParameters().add("template", np.getUri()))));
×
147
            } else {
148
                add(new WebMarkupContainer("use-template").setVisible(false));
×
149
            }
150
            if (Utils.isNanopubOfClass(np, GrlcQuery.GRLC_QUERY_CLASS)) {
×
151
                add(new WebMarkupContainer("run-query").add(new BookmarkablePageLink<Void>("query-link", QueryPage.class, new PageParameters().add("id", np.getUri()))));
×
152
            } else {
153
                add(new WebMarkupContainer("run-query").setVisible(false));
×
154
            }
155
        }
156

157
        final String ref = tempRef;
×
158
        final String shortName;
159
        if (parameters.get("label").isEmpty()) {
×
160
            shortName = IriItem.getShortNameFromURI(ref);
×
161
        } else {
162
            shortName = parameters.get("label").toString();
×
163
        }
164
        add(new Label("pagetitle", shortName + " (explore) | nanodash"));
×
165
        add(new Label("termname", shortName));
×
166
        add(new ExternalLink("urilink", ref, ref));
×
167
        if (isNanopubId && SignatureUtils.seemsToHaveSignature(np)) {
×
168
            add(StatusLine.createComponent("statusline", ref));
×
169
        } else {
170
            add(new Label("statusline").setVisible(false));
×
171
        }
172
        add(ThingListPanel.createComponent("classes-panel", ThingListPanel.Mode.CLASSES, ref, "<em>Searching for classes...</em>"));
×
173
        if (isNanopubId) {
×
174
            add(new Label("instances-panel").setVisible(false));
×
175
            add(new Label("parts-panel").setVisible(false));
×
176
            add(new Label("templates-panel").setVisible(false));
×
177
        } else {
178
            add(ThingListPanel.createComponent("instances-panel", ThingListPanel.Mode.INSTANCES, ref, "<em>Searching for instances...</em>"));
×
179
            add(ThingListPanel.createComponent("parts-panel", ThingListPanel.Mode.PARTS, ref, "<em>Searching for parts...</em>"));
×
180
            add(ThingListPanel.createComponent("templates-panel", ThingListPanel.Mode.TEMPLATES, ref, "<em>Searching for templates...</em>"));
×
181
        }
182
        add(ExploreDataTable.createComponent("reftable", ref));
×
183
    }
×
184

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