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

knowledgepixels / nanodash / 28515002478

01 Jul 2026 11:43AM UTC coverage: 28.026% (-0.01%) from 28.036%
28515002478

Pull #524

github

web-flow
Merge 053a42488 into 3f9733773
Pull Request #524: fix: guard ViewList view rendering so one broken view can't 500 the page

1727 of 7023 branches covered (24.59%)

Branch coverage included in aggregate %.

3608 of 12013 relevant lines covered (30.03%)

2.97 hits per line

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

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

3
import com.google.common.collect.ArrayListMultimap;
4
import com.google.common.collect.Multimap;
5
import com.knowledgepixels.nanodash.View;
6
import com.knowledgepixels.nanodash.ViewDisplay;
7
import com.knowledgepixels.nanodash.domain.AbstractResourceWithProfile;
8
import com.knowledgepixels.nanodash.domain.Space;
9
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
10
import org.apache.wicket.Component;
11
import org.apache.wicket.markup.html.WebMarkupContainer;
12
import org.apache.wicket.markup.html.basic.Label;
13
import org.apache.wicket.markup.html.link.AbstractLink;
14
import org.apache.wicket.markup.html.list.ListItem;
15
import org.apache.wicket.markup.html.list.ListView;
16
import org.apache.wicket.markup.html.panel.Panel;
17
import org.eclipse.rdf4j.model.IRI;
18
import org.nanopub.extra.services.QueryRef;
19
import org.nanopub.extra.services.QueryTemplate;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22

23
import java.util.ArrayList;
24
import java.util.List;
25
import java.util.Set;
26

27
public class ViewList extends Panel {
28

29
    private static final Logger logger = LoggerFactory.getLogger(ViewList.class);
×
30

31
    // The ref (root definition) this list is pinned to (?root=), used to gate each view's
32
    // action visibility against the claimant being viewed rather than the resource's
33
    // representative ref. Null = representative ref. Read at render time by the inner
34
    // ListView, so it may be set after the delegating constructor. See docs/space-ref-identity.md.
35
    private String refRoot;
36

37
    public ViewList(String markupId, AbstractResourceWithProfile resourceWithProfile) {
38
        this(markupId, resourceWithProfile, null, null, null, null, null, true, null);
×
39
    }
×
40

41
    public ViewList(String markupId, AbstractResourceWithProfile resourceWithProfile, String partId, String nanopubId, Set<IRI> partClasses) {
42
        this(markupId, resourceWithProfile, partId, nanopubId, partClasses, null, null, true, null);
×
43
    }
×
44

45
    public ViewList(String markupId, AbstractResourceWithProfile resourceWithProfile, String partId, String nanopubId, Set<IRI> partClasses, AbstractResourceWithProfile footerResource, List<AbstractLink> footerAdminButtons) {
46
        this(markupId, resourceWithProfile, partId, nanopubId, partClasses, footerResource, footerAdminButtons, true, null);
×
47
    }
×
48

49
    public ViewList(String markupId, AbstractResourceWithProfile resourceWithProfile, String partId, String nanopubId, Set<IRI> partClasses, AbstractResourceWithProfile footerResource, List<AbstractLink> footerAdminButtons, boolean showEmptyNotice) {
50
        this(markupId, resourceWithProfile, partId, nanopubId, partClasses, footerResource, footerAdminButtons, showEmptyNotice, null);
×
51
    }
×
52

53
    public ViewList(String markupId, AbstractResourceWithProfile resourceWithProfile, List<ViewDisplay> explicitViewDisplays) {
54
        this(markupId, resourceWithProfile, null, null, null, null, null, false, explicitViewDisplays);
×
55
    }
×
56

57
    /**
58
     * As {@link #ViewList(String, AbstractResourceWithProfile, List)} but pinned to a specific
59
     * ref (root definition), so each view's action visibility is gated against that claimant's
60
     * authority. Used for {@code ?root=}-pinned space pages. See docs/space-ref-identity.md.
61
     */
62
    public ViewList(String markupId, AbstractResourceWithProfile resourceWithProfile, List<ViewDisplay> explicitViewDisplays, String refRoot) {
63
        this(markupId, resourceWithProfile, null, null, null, null, null, false, explicitViewDisplays);
×
64
        this.refRoot = refRoot;
×
65
    }
×
66

67
    public ViewList(String markupId, AbstractResourceWithProfile resourceWithProfile, List<ViewDisplay> explicitViewDisplays, AbstractResourceWithProfile footerResource, List<AbstractLink> footerAdminButtons) {
68
        this(markupId, resourceWithProfile, null, null, null, footerResource, footerAdminButtons, false, explicitViewDisplays);
×
69
    }
×
70

71
    private ViewList(String markupId, AbstractResourceWithProfile resourceWithProfile, String partId, String nanopubId, Set<IRI> partClasses, AbstractResourceWithProfile footerResource, List<AbstractLink> footerAdminButtons, boolean showEmptyNotice, List<ViewDisplay> explicitViewDisplays) {
72
        super(markupId);
×
73

74
        final String id = (partId == null ? resourceWithProfile.getId() : partId);
×
75
        final String npId = (nanopubId == null ? resourceWithProfile.getNanopubId() : nanopubId);
×
76
        final List<ViewDisplay> viewDisplays;
77
        if (explicitViewDisplays != null) {
×
78
            viewDisplays = explicitViewDisplays;
×
79
        } else if (partId == null) {
×
80
            viewDisplays = resourceWithProfile.getTopLevelViewDisplays();
×
81
        } else {
82
            viewDisplays = resourceWithProfile.getPartLevelViewDisplays(partId, partClasses);
×
83
        }
84

85
        // Group viewDisplays by the first segment of their structural position (e.g. "4" from "4.4.1.papers")
86
        List<List<ViewDisplay>> groups = new ArrayList<>();
×
87
        String currentGroupKey = null;
×
88
        List<ViewDisplay> currentGroup = null;
×
89
        for (ViewDisplay vd : viewDisplays) {
×
90
            String pos = vd.getStructuralPosition();
×
91
            int firstDot = pos.indexOf('.');
×
92
            String key = firstDot > 0 ? pos.substring(0, firstDot) : pos;
×
93
            if (!key.equals(currentGroupKey)) {
×
94
                currentGroup = new ArrayList<>();
×
95
                groups.add(currentGroup);
×
96
                currentGroupKey = key;
×
97
            }
98
            currentGroup.add(vd);
×
99
        }
×
100

101
        add(new ListView<List<ViewDisplay>>("groups", groups) {
×
102
            @Override
103
            protected void populateItem(ListItem<List<ViewDisplay>> groupItem) {
104
                List<ViewDisplay> group = groupItem.getModelObject();
×
105
                groupItem.add(new ListView<ViewDisplay>("views", group) {
×
106
                    @Override
107
                    protected void populateItem(ListItem<ViewDisplay> item) {
108
                        // This populate runs at render time (onBeforeRender) and is the only
109
                        // view-rendering path without a guard; every other one (the QueryResult
110
                        // builders, populateComponent, per-cell populateItem) already degrades a
111
                        // failure to an inline error. Without this catch, a single view whose
112
                        // build/render dereferences a null takes down the whole page render.
113
                        try {
114
                            View view = item.getModelObject().getView();
×
115
                            Multimap<String, String> queryRefParams = ArrayListMultimap.create();
×
116
                            for (String p : view.getQuery().getPlaceholdersList()) {
×
117
                                String paramName = QueryTemplate.getParamName(p);
×
118
                                if (paramName.equals(view.getQueryField())) {
×
119
                                    queryRefParams.put(view.getQueryField(), id);
×
120
                                    if (QueryTemplate.isMultiPlaceholder(p) && resourceWithProfile instanceof Space space) {
×
121
                                        // TODO Support this also for maintained resources and users.
122
                                        for (String altId : space.getAltIDs()) {
×
123
                                            queryRefParams.put(view.getQueryField(), altId);
×
124
                                        }
×
125
                                    }
126
                                } else if (paramName.equals(view.getQueryField() + "Namespace") && resourceWithProfile.getNamespace() != null) {
×
127
                                    queryRefParams.put(view.getQueryField() + "Namespace", resourceWithProfile.getNamespace());
×
128
                                } else if (paramName.equals(view.getQueryField() + "Np")) {
×
129
                                    if (!QueryTemplate.isOptionalPlaceholder(p) && npId == null) {
×
130
                                        queryRefParams.put(view.getQueryField() + "Np", "x:");
×
131
                                    } else {
132
                                        queryRefParams.put(view.getQueryField() + "Np", npId);
×
133
                                    }
134
                                } else if (paramName.equals("root_np")) {
×
135
                                    // Auto-fill the ref scope (root nanopub) from the page's effective ref,
136
                                    // the same way the resource IRI above is filled, so a content-tab view
137
                                    // whose query opts into ref-scoping is scoped without the panel threading
138
                                    // it. Left empty when no ref is known (an optional placeholder tolerates
139
                                    // the empty VALUES; the ref-scoped query then yields its no-ref result).
140
                                    if (refRoot != null && !refRoot.isEmpty()) {
×
141
                                        queryRefParams.put("root_np", refRoot);
×
142
                                    }
143
                                } else if (!QueryTemplate.isOptionalPlaceholder(p)) {
×
144
                                    item.add(new Label("view", "<span class=\"negative\">Error: Query has non-optional parameter</span>").setEscapeModelStrings(false));
×
145
                                    logger.error("Error: Query has non-optional parameter: {} {}", view.getQuery().getQueryId(), p);
×
146
                                    return;
×
147
                                }
148
                            }
×
149
                            QueryRef queryRef = new QueryRef(view.getQuery().getQueryId(), queryRefParams);
×
150
                            if (view.getViewType() != null && View.getSupportedViewTypes().contains(view.getViewType())) {
×
151
                                if (view.getViewType().equals(KPXL_TERMS.LIST_VIEW)) {
×
152
                                    item.add(QueryResultListBuilder.create("view", queryRef, item.getModelObject())
×
153
                                            .resourceWithProfile(resourceWithProfile)
×
154
                                            .pageResource(resourceWithProfile)
×
155
                                            .id(id)
×
156
                                            .contextId(resourceWithProfile.getId())
×
157
                                            .refRoot(refRoot)
×
158
                                            .build());
×
159
                                } else if (view.getViewType().equals(KPXL_TERMS.TABULAR_VIEW)) {
×
160
                                    item.add(QueryResultTableBuilder.create("view", queryRef, item.getModelObject())
×
161
                                            .resourceWithProfile(resourceWithProfile)
×
162
                                            .contextId(resourceWithProfile.getId())
×
163
                                            .id(id)
×
164
                                            .refRoot(refRoot)
×
165
                                            .build());
×
166
                                } else if (view.getViewType().equals(KPXL_TERMS.PLAIN_PARAGRAPH_VIEW)) {
×
167
                                    item.add(QueryResultPlainParagraphBuilder.create("view", queryRef, item.getModelObject())
×
168
                                            .pageResource(resourceWithProfile)
×
169
                                            .contextId(resourceWithProfile.getId())
×
170
                                            .id(id)
×
171
                                            .refRoot(refRoot)
×
172
                                            .build());
×
173
                                } else if (view.getViewType().equals(KPXL_TERMS.NANOPUB_SET_VIEW)) {
×
174
                                    item.add(QueryResultNanopubSetBuilder.create("view", queryRef, item.getModelObject())
×
175
                                            .pageResource(resourceWithProfile)
×
176
                                            .contextId(resourceWithProfile.getId())
×
177
                                            .build());
×
178
                                } else if (view.getViewType().equals(KPXL_TERMS.ITEM_LIST_VIEW)) {
×
179
                                    item.add(QueryResultItemListBuilder.create("view", queryRef, item.getModelObject())
×
180
                                            .resourceWithProfile(resourceWithProfile)
×
181
                                            .pageResource(resourceWithProfile)
×
182
                                            .id(id)
×
183
                                            .contextId(resourceWithProfile.getId())
×
184
                                            .build());
×
185
                                } else {
186
                                    item.add(new Label("view", "<span class=\"negative\">View type \"" + view.getViewType().stringValue() + "\" is supported but its view is not implemented yet</span>").setEscapeModelStrings(false));
×
187
                                    logger.error("View type \"{}\" is supported but its view is not implemented yet", view.getViewType().stringValue());
×
188
                                }
189
                            } else {
190
                                item.add(new Label("view", "<span class=\"negative\">Unsupported view type</span>").setEscapeModelStrings(false));
×
191
                                logger.error("Unsupported view type.");
×
192
                            }
193
                        } catch (Exception ex) {
×
194
                            logger.error("Failed to render view display", ex);
×
195
                            // Guard against a partial add before the failure so we never add a
196
                            // second component with the same id.
197
                            if (item.get("view") == null) {
×
198
                                item.add(new Label("view", "<span class=\"negative\">Error rendering this view</span>").setEscapeModelStrings(false));
×
199
                            }
200
                        }
×
201
                    }
×
202
                });
203
            }
×
204
        });
205

206
        add(new WebMarkupContainer("emptynotice").setVisible(showEmptyNotice && viewDisplays.isEmpty()));
×
207

208
        WebMarkupContainer footerSection = new WebMarkupContainer("footer-section");
×
209
        if (footerAdminButtons != null) {
×
210
            footerSection.add(new ButtonList("footer-buttons",
×
211
                    footerResource != null ? footerResource : resourceWithProfile,
×
212
                    null, null, footerAdminButtons));
213
        } else {
214
            footerSection.setVisible(false);
×
215
            footerSection.add(new Label("footer-buttons").setVisible(false));
×
216
        }
217
        add(footerSection);
×
218

219
        add(new WebMarkupContainer("page-footer").setVisible(false));
×
220
    }
×
221

222
    public void setPageFooter(Component footer) {
223
        replace(footer);
×
224
    }
×
225

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