Skip to content

Package: DefaultRequest

DefaultRequest

nameinstructionbranchcomplexitylinemethod
DefaultRequest()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getHeader(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMultiValuedHeader(String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMultiValuedParameter(String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getParameter(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPathParams(SiteNode)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
withBaseUrl(String)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
withHeaderMap(Map)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
withParameterMap(Map)
M: 41 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
withPreferredLocales(List)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
withRelativeUri(String)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

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.core.impl.model;
27:
28: import javax.annotation.Nonnull;
29: import javax.annotation.concurrent.Immutable;
30: import java.util.ArrayList;
31: import java.util.HashMap;
32: import java.util.List;
33: import java.util.Locale;
34: import java.util.Map;
35: import java.util.Optional;
36: import it.tidalwave.northernwind.core.model.Request;
37: import it.tidalwave.northernwind.core.model.ResourcePath;
38: import it.tidalwave.northernwind.core.model.SiteNode;
39: import lombok.AllArgsConstructor;
40: import lombok.Getter;
41: import lombok.ToString;
42: import static java.util.Collections.emptyList;
43: import static lombok.AccessLevel.PRIVATE;
44:
45: /***************************************************************************************************************************************************************
46: *
47: * The default implementation of {@link Request}
48: *
49: * @author Fabrizio Giudici
50: *
51: **************************************************************************************************************************************************************/
52: @Immutable @AllArgsConstructor(access = PRIVATE) @Getter @ToString
53: /* package */ class DefaultRequest implements Request
54: {
55: @Nonnull
56: private final String baseUrl;
57:
58: @Nonnull
59: private final String relativeUri;
60:
61: @Nonnull
62: private final String originalRelativeUri;
63:
64: @Nonnull
65: private final Map<String, List<String>> parametersMap;
66:
67: @Nonnull
68: private final Map<String, List<String>> headersMap;
69:
70: @Nonnull
71: private final List<Locale> preferredLocales;
72:
73: /***********************************************************************************************************************************************************
74: *
75: **********************************************************************************************************************************************************/
76: public DefaultRequest()
77: {
78: this("", "", "", new HashMap<>(), new HashMap<>(), new ArrayList<>());
79: }
80:
81: /***********************************************************************************************************************************************************
82: * {@inheritDoc}
83: **********************************************************************************************************************************************************/
84: @Override @Nonnull
85: public Optional<String> getHeader (@Nonnull final String parameterName)
86: {
87: return getMultiValuedHeader(parameterName).stream().findFirst();
88: }
89:
90: /***********************************************************************************************************************************************************
91: * {@inheritDoc}
92: **********************************************************************************************************************************************************/
93: @Override @Nonnull
94: public List<String> getMultiValuedHeader (@Nonnull final String headerName)
95: {
96: return headersMap.getOrDefault(headerName, emptyList());
97: }
98:
99: /***********************************************************************************************************************************************************
100: * {@inheritDoc}
101: **********************************************************************************************************************************************************/
102: @Override @Nonnull
103: public Optional<String> getParameter (@Nonnull final String headerName)
104: {
105: return getMultiValuedParameter(headerName).stream().findFirst();
106: }
107:
108: /***********************************************************************************************************************************************************
109: * {@inheritDoc}
110: **********************************************************************************************************************************************************/
111: @Override @Nonnull
112: public List<String> getMultiValuedParameter (@Nonnull final String parameterName)
113: {
114: return parametersMap.getOrDefault(parameterName, emptyList());
115: }
116:
117: /***********************************************************************************************************************************************************
118: * {@inheritDoc}
119: **********************************************************************************************************************************************************/
120: @Override @Nonnull
121: public DefaultRequest withRelativeUri (@Nonnull final String relativeUri)
122: {
123: return new DefaultRequest(baseUrl,
124: ResourcePath.of(relativeUri).urlDecoded().asString(),
125: relativeUri,
126: parametersMap,
127: headersMap,
128: preferredLocales);
129: }
130:
131: /***********************************************************************************************************************************************************
132: * {@inheritDoc}
133: **********************************************************************************************************************************************************/
134: @Nonnull
135: public DefaultRequest withBaseUrl (@Nonnull final String baseUrl)
136: {
137: return new DefaultRequest(baseUrl, relativeUri, originalRelativeUri, parametersMap, headersMap, preferredLocales);
138: }
139:
140: /***********************************************************************************************************************************************************
141: * {@inheritDoc}
142: **********************************************************************************************************************************************************/
143: @Override @Nonnull
144: public ResourcePath getPathParams (@Nonnull final SiteNode siteNode)
145: {
146: final var siteNodeRelativeUri = siteNode.getRelativeUri().asString();
147:• return (relativeUri.length() <= siteNodeRelativeUri.length())
148: ? ResourcePath.EMPTY
149: : ResourcePath.of(relativeUri.substring(siteNodeRelativeUri.length()));
150: }
151:
152: /***********************************************************************************************************************************************************
153: *
154: **********************************************************************************************************************************************************/
155: @Nonnull
156: public DefaultRequest withParameterMap (@Nonnull final Map<String, String[]> httpParameterMap)
157: {
158: final Map<String, List<String>> parameterMap = new HashMap<>();
159:
160:• for (final var entry : httpParameterMap.entrySet())
161: {
162: parameterMap.put(entry.getKey(), List.of(entry.getValue()));
163: }
164:
165: return new DefaultRequest(baseUrl, relativeUri, originalRelativeUri, parameterMap, headersMap, preferredLocales);
166: }
167:
168: /***********************************************************************************************************************************************************
169: *
170: **********************************************************************************************************************************************************/
171: @Nonnull
172: public DefaultRequest withHeaderMap (@Nonnull final Map<String, List<String>> headersMap)
173: {
174: return new DefaultRequest(baseUrl, relativeUri, originalRelativeUri, parametersMap, headersMap, preferredLocales);
175: }
176:
177: /***********************************************************************************************************************************************************
178: *
179: **********************************************************************************************************************************************************/
180: @Nonnull
181: public DefaultRequest withPreferredLocales (@Nonnull final List<Locale> preferredLocales)
182: {
183: return new DefaultRequest(baseUrl, relativeUri, originalRelativeUri, parametersMap, headersMap, preferredLocales);
184: }
185: }