Package: DefaultContentRequestProcessor
DefaultContentRequestProcessor
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultContentRequestProcessor() |
|
|
|
|
|
||||||||||||||||||||
enforceTrailingSlash(String, Site) |
|
|
|
|
|
||||||||||||||||||||
process(Request) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
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.model.spi;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import javax.inject.Provider;
32: import org.springframework.beans.factory.annotation.Configurable;
33: import org.springframework.context.annotation.Scope;
34: import org.springframework.core.annotation.Order;
35: import it.tidalwave.util.NotFoundException;
36: import it.tidalwave.northernwind.core.model.HttpStatusException;
37: import it.tidalwave.northernwind.core.model.Request;
38: import it.tidalwave.northernwind.core.model.RequestContext;
39: import it.tidalwave.northernwind.core.model.RequestProcessor;
40: import it.tidalwave.northernwind.core.model.Site;
41: import it.tidalwave.northernwind.core.model.SiteProvider;
42: import it.tidalwave.northernwind.frontend.ui.SiteView;
43: import static org.springframework.core.Ordered.LOWEST_PRECEDENCE;
44: import static it.tidalwave.northernwind.core.model.RequestProcessor.Status.*;
45: import static it.tidalwave.northernwind.core.model.SiteNode._SiteNode_;
46:
47: /***********************************************************************************************************************
48: *
49: * @author Fabrizio Giudici
50: *
51: **********************************************************************************************************************/
52: @Configurable @Scope("session") @Order(LOWEST_PRECEDENCE)
53:•public class DefaultContentRequestProcessor implements RequestProcessor
54: {
55: @Inject
56: private Provider<SiteProvider> siteProvider;
57:
58: @Inject
59: private SiteView siteView;
60:
61: @Inject
62: private RequestHolder requestHolder;
63:
64: @Inject
65: private RequestContext requestContext;
66:
67: /*******************************************************************************************************************
68: *
69: * {@inheritDoc}
70: *
71: ******************************************************************************************************************/
72: @Override @Nonnull
73: public Status process (@Nonnull final Request request)
74: throws NotFoundException, HttpStatusException
75: {
76: try
77: {
78: final var relativeUri = request.getRelativeUri();
79: final var site = siteProvider.get().getSite();
80: final var node = site.find(_SiteNode_).withRelativeUri(relativeUri).result();
81: requestContext.setNode(node);
82: siteView.renderSiteNode(request, node);
83: //
84: // Check *after* finding the SiteNode, since a "not found" must have been already handled here.
85: //
86: enforceTrailingSlash(relativeUri, site);
87: return BREAK;
88: }
89: finally
90: {
91: requestContext.clearNode();
92: }
93: }
94:
95: /*******************************************************************************************************************
96: *
97: * If relativeUri doesn't end with a trailing slash, send a redirect to the proper Uri.
98: * FIXME: could be dropped and replaced with a configurable redirect?
99: *
100: ******************************************************************************************************************/
101: private void enforceTrailingSlash (@Nonnull final String relativeUri, @Nonnull final Site site)
102: throws HttpStatusException
103: {
104: final var originalRelativeUri = requestHolder.get().getOriginalRelativeUri();
105:
106:• if (!relativeUri.contains(".") && !originalRelativeUri.endsWith("/"))
107: {
108: throw HttpStatusException.temporaryRedirect(site, relativeUri); // TODO: temporary or permanent?
109: }
110: }
111: }