Skip to content

Package: DefaultModelFactory

DefaultModelFactory

nameinstructionbranchcomplexitylinemethod
DefaultModelFactory()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
build(Content.Builder)
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%
build(Layout.Builder)
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%
build(Media.Builder)
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%
build(Resource.Builder)
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%
build(ResourceProperties.Builder)
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%
build(Site.Builder)
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%
createRequest()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createRequestFrom(HttpServletRequest)
M: 33 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
createSiteNode(Site, ResourceFile)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getBaseUrl(HttpServletRequest)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toMap(HttpServletRequest)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
toString()
M: 2 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.core.impl.model;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Collections;
31: import java.util.HashMap;
32: import java.util.List;
33: import java.util.Map;
34: import javax.servlet.http.HttpServletRequest;
35: import it.tidalwave.northernwind.core.model.Content;
36: import it.tidalwave.northernwind.core.model.Media;
37: import it.tidalwave.northernwind.core.model.ModelFactory;
38: import it.tidalwave.northernwind.core.model.Request;
39: import it.tidalwave.northernwind.core.model.Resource;
40: import it.tidalwave.northernwind.core.model.ResourceFile;
41: import it.tidalwave.northernwind.core.model.ResourceProperties;
42: import it.tidalwave.northernwind.core.model.Site;
43: import it.tidalwave.northernwind.core.model.SiteNode;
44: import it.tidalwave.northernwind.core.model.spi.ModelFactorySupport;
45: import it.tidalwave.northernwind.frontend.ui.Layout;
46: import it.tidalwave.northernwind.frontend.impl.ui.DefaultLayout;
47: import lombok.ToString;
48:
49: /***********************************************************************************************************************
50: *
51: * The default implementation of {@link ModelFactory}.
52: *
53: * @author Fabrizio Giudici
54: *
55: **********************************************************************************************************************/
56: @ToString
57: public class DefaultModelFactory extends ModelFactorySupport
58: {
59: /*******************************************************************************************************************
60: *
61: * {@inheritDoc}
62: *
63: ******************************************************************************************************************/
64: @Override @Nonnull
65: public Resource build (@Nonnull final Resource.Builder builder)
66: {
67: return new DefaultResource(builder);
68: }
69:
70: /*******************************************************************************************************************
71: *
72: * {@inheritDoc}
73: *
74: ******************************************************************************************************************/
75: @Override @Nonnull
76: public Content build (@Nonnull final Content.Builder builder)
77: {
78: return new DefaultContent(builder);
79: }
80:
81: /*******************************************************************************************************************
82: *
83: * {@inheritDoc}
84: *
85: ******************************************************************************************************************/
86: @Override @Nonnull
87: public Media build (@Nonnull final Media.Builder builder)
88: {
89: return new DefaultMedia(builder);
90: }
91:
92: /*******************************************************************************************************************
93: *
94: * {@inheritDoc}
95: *
96: ******************************************************************************************************************/
97: @Override @Nonnull
98: public SiteNode createSiteNode (@Nonnull final Site site, @Nonnull final ResourceFile folder)
99: {
100: return new DefaultSiteNode(this, (DefaultSite)site, folder);
101: }
102:
103: /*******************************************************************************************************************
104: *
105: * {@inheritDoc}
106: *
107: ******************************************************************************************************************/
108: @Override @Nonnull
109: public Layout build (@Nonnull final Layout.Builder builder)
110: {
111: return new DefaultLayout(builder);
112: }
113:
114: /*******************************************************************************************************************
115: *
116: * {@inheritDoc}
117: *
118: ******************************************************************************************************************/
119: @Override @Nonnull
120: public DefaultRequest createRequest()
121: {
122: return new DefaultRequest();
123: }
124:
125: /*******************************************************************************************************************
126: *
127: * {@inheritDoc}
128: *
129: ******************************************************************************************************************/
130: @Override @Nonnull
131: public Request createRequestFrom (@Nonnull final HttpServletRequest httpServletRequest)
132: {
133: var relativeUri = httpServletRequest.getRequestURI().substring(httpServletRequest.getContextPath().length());
134:• relativeUri = "".equals(relativeUri) ? "/" : relativeUri;
135: return createRequest().withBaseUrl(getBaseUrl(httpServletRequest))
136: .withRelativeUri(relativeUri)
137: .withParameterMap(httpServletRequest.getParameterMap())
138: .withHeaderMap(toMap(httpServletRequest))
139: .withPreferredLocales(Collections.list(httpServletRequest.getLocales()));
140: }
141:
142: /*******************************************************************************************************************
143: *
144: * {@inheritDoc}
145: *
146: ******************************************************************************************************************/
147: @Override @Nonnull
148: public ResourceProperties build (@Nonnull final ResourceProperties.Builder builder)
149: {
150: return new DefaultResourceProperties(builder);
151: // return new DefaultResourceProperties(id, DefaultResourceProperties.PropertyResolver.DEFAULT);
152: }
153:
154: /*******************************************************************************************************************
155: *
156: * {@inheritDoc}
157: *
158: ******************************************************************************************************************/
159: @Override @Nonnull
160: public Site build (@Nonnull final Site.Builder builder)
161: {
162: return new DefaultSite(builder);
163: }
164:
165: /*******************************************************************************************************************
166: *
167: *
168: ******************************************************************************************************************/
169: @Nonnull
170: private static String getBaseUrl (@Nonnull final HttpServletRequest httpServletRequest)
171: {
172: return httpServletRequest.getRequestURL().toString().replaceAll(":.*", "")
173: + "://" + httpServletRequest.getHeader("Host");
174: }
175:
176: /*******************************************************************************************************************
177: *
178: *
179: ******************************************************************************************************************/
180: @Nonnull
181: private static Map<String, List<String>> toMap (@Nonnull final HttpServletRequest httpServletRequest)
182: {
183: final Map<String, List<String>> headerMap = new HashMap<>();
184:
185:• for (final var e = httpServletRequest.getHeaderNames(); e.hasMoreElements(); )
186: {
187: final var headerName = e.nextElement();
188: final var headerValue = httpServletRequest.getHeader(headerName); // FIXME: lacks support for multivalue
189: headerMap.put(headerName, List.of(headerValue));
190: }
191:
192: return headerMap;
193: }
194: }