Package: DefaultNodeContainerViewController
DefaultNodeContainerViewController
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultNodeContainerViewController(NodeContainerView, SiteNode, RequestLocaleManager) |
|
|
|
|
|
||||||||||||||||||||
computeInlinedScriptsSection() |
|
|
|
|
|
||||||||||||||||||||
computePrintCssSection() |
|
|
|
|
|
||||||||||||||||||||
computeRssFeedsSection() |
|
|
|
|
|
||||||||||||||||||||
computeScreenCssSection() |
|
|
|
|
|
||||||||||||||||||||
computeScriptsSection() |
|
|
|
|
|
||||||||||||||||||||
createLink(String) |
|
|
|
|
|
||||||||||||||||||||
getViewProperties() |
|
|
|
|
|
||||||||||||||||||||
lambda$computeInlinedScriptsSection$12(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$computeInlinedScriptsSection$13(Content) |
|
|
|
|
|
||||||||||||||||||||
lambda$computePrintCssSection$7(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$computeRssFeedsSection$8(Site, String) |
|
|
|
|
|
||||||||||||||||||||
lambda$computeRssFeedsSection$9(Site, SiteNode) |
|
|
|
|
|
||||||||||||||||||||
lambda$computeScreenCssSection$6(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$computeScriptsSection$10(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$computeScriptsSection$11(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderView$0(ResourcePath) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderView$1(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderView$2(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderView$3(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderView$4(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$renderView$5(String) |
|
|
|
|
|
||||||||||||||||||||
renderView(RenderContext) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
streamOf(Key) |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.ui.component.nodecontainer;
28:
29: import javax.annotation.Nonnull;
30: import java.util.List;
31: import java.util.Optional;
32: import java.util.stream.Stream;
33: import it.tidalwave.util.Key;
34: import it.tidalwave.northernwind.core.model.RequestLocaleManager;
35: import it.tidalwave.northernwind.core.model.ResourcePath;
36: import it.tidalwave.northernwind.core.model.ResourceProperties;
37: import it.tidalwave.northernwind.core.model.SiteNode;
38: import it.tidalwave.northernwind.frontend.ui.RenderContext;
39: import lombok.RequiredArgsConstructor;
40: import lombok.extern.slf4j.Slf4j;
41: import static java.util.Collections.emptyList;
42: import static java.util.stream.Collectors.*;
43: import static it.tidalwave.northernwind.core.model.Content.*;
44: import static it.tidalwave.northernwind.core.model.SiteNode._SiteNode_;
45: import static it.tidalwave.northernwind.frontend.ui.component.Properties.P_TEMPLATE_PATH;
46:
47: /***********************************************************************************************************************
48: *
49: * @author Fabrizio Giudici
50: *
51: **********************************************************************************************************************/
52: @RequiredArgsConstructor @Slf4j
53: public class DefaultNodeContainerViewController implements NodeContainerViewController
54: {
55: // TODO: this class should not set the HTML contents... it should be a responsibility of the view.
56: // Instead, it should pass unformatted objects
57: private static final String RSS_MIME_TYPE = "application/rss+xml";
58:
59: private static final String TEMPLATE_LINK_SCREEN_CSS =
60: "<link rel=\"stylesheet\" media=\"screen\" href=\"%s\" type=\"text/css\" />%n";
61:
62: private static final String TEMPLATE_LINK_PRINT_CSS =
63: "<link rel=\"stylesheet\" media=\"print\" href=\"%s\" type=\"text/css\" />%n";
64:
65: // Always use </script> to close, as some browsers break without
66: private static final String TEMPLATE_SCRIPT =
67: "<script type=\"text/javascript\" src=\"%s\"></script>%n";
68:
69: private static final String TEMPLATE_RSS_LINK =
70: "<link rel=\"alternate\" type=\"%s\" title=\"%s\" href=\"%s\" />%n";
71:
72: @Nonnull
73: private final NodeContainerView view;
74:
75: @Nonnull
76: private final SiteNode siteNode;
77:
78: @Nonnull
79: private final RequestLocaleManager requestLocaleManager;
80:
81: /*******************************************************************************************************************
82: *
83: * {@inheritDoc}
84: *
85: ******************************************************************************************************************/
86: @Override
87: public void renderView (@Nonnull final RenderContext context)
88: throws Exception
89: {
90: final var viewProperties = getViewProperties();
91: final var siteNodeProperties = context.getRequestContext().getNodeProperties();
92: viewProperties.getProperty(P_TEMPLATE_PATH).flatMap(p -> siteNode.getSite().getTemplate(getClass(), p))
93: .ifPresent(view::setTemplate);
94:
95: view.addAttribute("language", requestLocaleManager.getLocales().get(0).getLanguage());
96: viewProperties.getProperty(P_DESCRIPTION).ifPresent(d -> view.addAttribute("description", d));
97: viewProperties.getProperty(P_TITLE_PREFIX).ifPresent(p -> view.addAttribute("titlePrefix", p));
98: Stream.of(siteNodeProperties.getProperty(PD_TITLE), siteNodeProperties.getProperty(P_TITLE)) // TODO: use multi-key
99: .filter(Optional::isPresent)
100: .map(Optional::get)
101: .findFirst().ifPresent(s -> view.addAttribute("title", s));
102: view.addAttribute("screenCssSection", computeScreenCssSection());
103: view.addAttribute("printCssSection", computePrintCssSection());
104: view.addAttribute("rssFeeds", computeRssFeedsSection());
105: view.addAttribute("scripts", computeScriptsSection());
106: view.addAttribute("inlinedScripts", computeInlinedScriptsSection());
107: siteNodeProperties.getProperty(PD_IMAGE_ID).ifPresent(id -> view.addAttribute("imageId", id));
108: siteNodeProperties.getProperty(PD_URL).ifPresent(id -> view.addAttribute("url", id));
109: }
110:
111: /*******************************************************************************************************************
112: *
113: * .
114: *
115: ******************************************************************************************************************/
116: @Nonnull
117: protected ResourceProperties getViewProperties()
118: {
119: return siteNode.getPropertyGroup(view.getId());
120: }
121:
122: /*******************************************************************************************************************
123: *
124: * .
125: *
126: ******************************************************************************************************************/
127: @Nonnull
128: private String computeScreenCssSection()
129: {
130: return streamOf(P_SCREEN_STYLE_SHEETS)
131: .map(this::createLink)
132: .map(link -> String.format(TEMPLATE_LINK_SCREEN_CSS, link))
133: .collect(joining());
134: }
135:
136: /*******************************************************************************************************************
137: *
138: * .
139: *
140: ******************************************************************************************************************/
141: @Nonnull
142: private String computePrintCssSection()
143: {
144: return streamOf(P_PRINT_STYLE_SHEETS)
145: .map(this::createLink)
146: .map(link -> String.format(TEMPLATE_LINK_PRINT_CSS, link))
147: .collect(joining());
148: }
149:
150: /*******************************************************************************************************************
151: *
152: * .
153: *
154: ******************************************************************************************************************/
155: @Nonnull
156: private String computeRssFeedsSection()
157: {
158: final var site = siteNode.getSite();
159: return streamOf(P_RSS_FEEDS)
160: .flatMap(relativePath -> site.find(_SiteNode_).withRelativePath(relativePath).stream())
161: .map(node -> String.format(TEMPLATE_RSS_LINK,
162: RSS_MIME_TYPE,
163: node.getProperty(P_TITLE).orElse("RSS"),
164: site.createLink(node.getRelativeUri())))
165: .collect(joining());
166: }
167:
168: /*******************************************************************************************************************
169: *
170: * .
171: *
172: ******************************************************************************************************************/
173: @Nonnull
174: private String computeScriptsSection()
175: {
176: return streamOf(P_SCRIPTS)
177: .map(relativeUri -> siteNode.getSite().createLink(ResourcePath.of(relativeUri)))
178: .map(link -> String.format(TEMPLATE_SCRIPT, link))
179: .collect(joining());
180: }
181:
182: /*******************************************************************************************************************
183: *
184: * .
185: *
186: ******************************************************************************************************************/
187: @Nonnull
188: protected String computeInlinedScriptsSection()
189: {
190: return streamOf(P_INLINED_SCRIPTS)
191: .flatMap(path -> siteNode.getSite().find(_Content_).withRelativePath(path).stream())
192: .flatMap(script -> script.getProperty(P_TEMPLATE).stream())
193: .collect(joining());
194: }
195:
196: /*******************************************************************************************************************
197: *
198: * .
199: *
200: ******************************************************************************************************************/
201: @Nonnull
202: private Stream<String> streamOf (@Nonnull final Key<List<String>> key)
203: {
204: return getViewProperties().getProperty(key).orElse(emptyList()).stream();
205: }
206:
207: /*******************************************************************************************************************
208: *
209: * .
210: *
211: ******************************************************************************************************************/
212: @Nonnull
213: private String createLink (@Nonnull final String relativeUri)
214: {
215:• return relativeUri.startsWith("http") ? relativeUri : siteNode.getSite().createLink(ResourcePath.of(relativeUri));
216: }
217: }