Package: SpringMvcSiteView
SpringMvcSiteView
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SpringMvcSiteView() |
|
|
|
|
|
||||||||||||||||||||
attach(TextHolder, TextHolder) |
|
|
|
|
|
||||||||||||||||||||
createErrorView(Layout, Throwable) |
|
|
|
|
|
||||||||||||||||||||
renderSiteNode(Request, SiteNode) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * NorthernWind - lightweight CMS
5: * http://tidalwave.it/projects/northernwind
6: *
7: * Copyright (C) 2011 - 2025 Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/northernwind-src
22: * git clone https://github.com/tidalwave-it/northernwind-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.northernwind.frontend.ui.springmvc;
27:
28: import javax.annotation.Nonnull;
29: import javax.inject.Inject;
30: import org.springframework.beans.factory.annotation.Configurable;
31: import org.springframework.context.annotation.Scope;
32: import org.springframework.http.HttpStatus;
33: import it.tidalwave.northernwind.core.model.HttpStatusException;
34: import it.tidalwave.northernwind.core.model.Request;
35: import it.tidalwave.northernwind.core.model.RequestContext;
36: import it.tidalwave.northernwind.core.model.SiteNode;
37: import it.tidalwave.northernwind.frontend.springmvc.SpringMvcResponseHolder;
38: import it.tidalwave.northernwind.frontend.ui.Layout;
39: import it.tidalwave.northernwind.frontend.ui.RenderContext;
40: import it.tidalwave.northernwind.frontend.ui.SiteView;
41: import it.tidalwave.northernwind.frontend.ui.component.htmltemplate.HtmlHolder;
42: import it.tidalwave.northernwind.frontend.ui.component.htmltemplate.TextHolder;
43: import it.tidalwave.northernwind.frontend.ui.spi.DefaultRenderContext;
44: import it.tidalwave.northernwind.frontend.ui.spi.NodeViewRenderer;
45: import it.tidalwave.northernwind.frontend.ui.spi.ViewAndControllerLayoutBuilder;
46: import lombok.extern.slf4j.Slf4j;
47: import static java.nio.charset.StandardCharsets.UTF_8;
48:
49: /***************************************************************************************************************************************************************
50: *
51: * The Spring MVC implementation of {@link SiteView}.
52: *
53: * @author Fabrizio Giudici
54: *
55: **************************************************************************************************************************************************************/
56: @Configurable @Scope("session") @Slf4j
57:•public class SpringMvcSiteView implements SiteView
58: {
59: @Inject
60: private RequestContext requestContext;
61:
62: @Inject
63: private SpringMvcResponseHolder responseHolder;
64:
65:• private final ThreadLocal<HttpStatus> httpStatus = new ThreadLocal<>();
66:
67: /***********************************************************************************************************************************************************
68: * {@inheritDoc}
69: **********************************************************************************************************************************************************/
70: @Override
71: public void renderSiteNode (@Nonnull final Request request, @Nonnull final SiteNode siteNode)
72: {
73: log.info("renderSiteNode({})", siteNode);
74: httpStatus.set(HttpStatus.OK);
75: final RenderContext renderContext = new DefaultRenderContext(request, requestContext);
76: final var vacBuilder = new ViewAndControllerLayoutBuilder(siteNode, renderContext, this::createErrorView);
77: siteNode.getLayout().accept(vacBuilder);
78: final var renderer = new NodeViewRenderer<>(request, requestContext, vacBuilder, SpringMvcSiteView::attach);
79: siteNode.getLayout().accept(renderer);
80: final var textHolder = renderer.getRootComponent();
81: responseHolder.response().withStatus(httpStatus.get().value())
82: .withBody(textHolder.asBytes(UTF_8))
83: .withContentType(textHolder.getMimeType())
84: .put();
85: }
86:
87: /***********************************************************************************************************************************************************
88: * TODO: sometimes this gets wrapped in a layout (e.g. when it's thrown by a Node Controller), otherwise it just
89: * renders as bare markup. If there is no container, it should be wrapped by a small embedded template.
90: *
91: * Only for codes such as 404 and 500, you could configure a wrapping template from a Node, so that the usual
92: * layout is rendered.
93: **********************************************************************************************************************************************************/
94: @Nonnull
95: private TextHolder createErrorView (@Nonnull final Layout layout, @Nonnull final Throwable t)
96: {
97: log.warn("While processing " + layout, t);
98:• final var status = (t instanceof HttpStatusException)
99: ? HttpStatus.valueOf(((HttpStatusException)t).getHttpStatus())
100: : HttpStatus.INTERNAL_SERVER_ERROR;
101: httpStatus.set(status);
102: return new HtmlHolder("<div><h1>" + status.getReasonPhrase() + "</h1></div>");
103: }
104:
105: /***********************************************************************************************************************************************************
106: *
107: **********************************************************************************************************************************************************/
108: private static void attach (@Nonnull final TextHolder parent, @Nonnull final TextHolder child)
109: {
110: parent.addComponent(child);
111: }
112: }