Skip to content

Package: SpringMvcSiteView

SpringMvcSiteView

nameinstructionbranchcomplexitylinemethod
SpringMvcSiteView()
M: 78 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
attach(TextHolder, TextHolder)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createErrorView(Layout, Throwable)
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
renderSiteNode(Request, SiteNode)
M: 64 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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.springmvc;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import org.springframework.beans.factory.annotation.Configurable;
32: import org.springframework.context.annotation.Scope;
33: import org.springframework.http.HttpStatus;
34: import it.tidalwave.northernwind.core.model.HttpStatusException;
35: import it.tidalwave.northernwind.core.model.Request;
36: import it.tidalwave.northernwind.core.model.RequestContext;
37: import it.tidalwave.northernwind.core.model.SiteNode;
38: import it.tidalwave.northernwind.frontend.springmvc.SpringMvcResponseHolder;
39: import it.tidalwave.northernwind.frontend.ui.Layout;
40: import it.tidalwave.northernwind.frontend.ui.RenderContext;
41: import it.tidalwave.northernwind.frontend.ui.SiteView;
42: import it.tidalwave.northernwind.frontend.ui.component.htmltemplate.HtmlHolder;
43: import it.tidalwave.northernwind.frontend.ui.component.htmltemplate.TextHolder;
44: import it.tidalwave.northernwind.frontend.ui.spi.DefaultRenderContext;
45: import it.tidalwave.northernwind.frontend.ui.spi.NodeViewRenderer;
46: import it.tidalwave.northernwind.frontend.ui.spi.ViewAndControllerLayoutBuilder;
47: import lombok.extern.slf4j.Slf4j;
48: import static java.nio.charset.StandardCharsets.UTF_8;
49:
50: /***********************************************************************************************************************
51: *
52: * The Spring MVC implementation of {@link SiteView}.
53: *
54: * @author Fabrizio Giudici
55: *
56: **********************************************************************************************************************/
57: @Configurable @Scope("session") @Slf4j
58:•public class SpringMvcSiteView implements SiteView
59: {
60: @Inject
61: private RequestContext requestContext;
62:
63: @Inject
64: private SpringMvcResponseHolder responseHolder;
65:
66:• private final ThreadLocal<HttpStatus> httpStatus = new ThreadLocal<>();
67:
68: /*******************************************************************************************************************
69: *
70: * {@inheritDoc}
71: *
72: ******************************************************************************************************************/
73: @Override
74: public void renderSiteNode (@Nonnull final Request request, @Nonnull final SiteNode siteNode)
75: {
76: log.info("renderSiteNode({})", siteNode);
77: httpStatus.set(HttpStatus.OK);
78: final RenderContext renderContext = new DefaultRenderContext(request, requestContext);
79: final var vacBuilder = new ViewAndControllerLayoutBuilder(siteNode, renderContext, this::createErrorView);
80: siteNode.getLayout().accept(vacBuilder);
81: final var renderer = new NodeViewRenderer<>(request, requestContext, vacBuilder, SpringMvcSiteView::attach);
82: siteNode.getLayout().accept(renderer);
83: final var textHolder = renderer.getRootComponent();
84: responseHolder.response().withStatus(httpStatus.get().value())
85: .withBody(textHolder.asBytes(UTF_8))
86: .withContentType(textHolder.getMimeType())
87: .put();
88: }
89:
90: /*******************************************************************************************************************
91: *
92: * TODO: sometimes this gets wrapped in a layout (e.g. when it's thrown by a Node Controller), otherwise it just
93: * renders as bare markup. If there is no container, it should be wrapped by a small embedded template.
94: *
95: * Only for codes such as 404 and 500, you could configure a wrapping template from a Node, so that the usual
96: * layout is rendered.
97: *
98: ******************************************************************************************************************/
99: @Nonnull
100: private TextHolder createErrorView (@Nonnull final Layout layout, @Nonnull final Throwable t)
101: {
102: log.warn("While processing " + layout, t);
103:• final var status = (t instanceof HttpStatusException)
104: ? HttpStatus.valueOf(((HttpStatusException)t).getHttpStatus())
105: : HttpStatus.INTERNAL_SERVER_ERROR;
106: httpStatus.set(status);
107: return new HtmlHolder("<div><h1>" + status.getReasonPhrase() + "</h1></div>");
108: }
109:
110: /*******************************************************************************************************************
111: *
112: ******************************************************************************************************************/
113: private static void attach (@Nonnull final TextHolder parent, @Nonnull final TextHolder child)
114: {
115: parent.addComponent(child);
116: }
117: }