Skip to contentPackage: ResourcePropertiesDelegate
ResourcePropertiesDelegate
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.inject.Inject;
30: import java.text.Normalizer;
31: import java.util.List;
32: import java.util.Optional;
33: import java.util.regex.Pattern;
34: import org.springframework.beans.factory.annotation.Configurable;
35: import it.tidalwave.util.Finder;
36: import it.tidalwave.util.Key;
37: import it.tidalwave.northernwind.core.model.Content;
38: import it.tidalwave.northernwind.core.model.RequestContext;
39: import it.tidalwave.northernwind.core.model.ResourcePath;
40: import it.tidalwave.northernwind.core.model.ResourceProperties;
41: import it.tidalwave.northernwind.core.model.spi.ContentSupport;
42: import lombok.RequiredArgsConstructor;
43: import lombok.ToString;
44: import lombok.experimental.Delegate;
45: import lombok.extern.slf4j.Slf4j;
46:
47: // FIXME: reimplement with an Aspect
48: @RequiredArgsConstructor
49: class ResourcePropertiesDelegate implements ResourceProperties
50: {
51: @Nonnull
52: private final RequestContext requestContext;
53:
54: @Nonnull
55: private final Content content;
56:
57: interface Exclusions
58: {
59: public <T> Optional<T> getProperty (Key<? extends T> key);
60: public <T> Optional<T> getProperty (List<Key<? extends T>> keys);
61: }
62:
63: @Nonnull @Delegate(types=ResourceProperties.class, excludes=Exclusions.class)
64: private final ResourceProperties delegate;
65:
66: @Nonnull
67: @Override
68: public <T> Optional<T> getProperty (@Nonnull final Key<? extends T> key)
69: {
70: try
71: {
72: requestContext.setContent(content);
73: return delegate.getProperty(key);
74: }
75: finally
76: {
77: requestContext.clearContent();
78: }
79: }
80: }
81:
82: /***************************************************************************************************************************************************************
83: *
84: * A piece of content to be composed into a {@code Node}.
85: *
86: * @author Fabrizio Giudici
87: *
88: **************************************************************************************************************************************************************/
89: @Configurable @Slf4j @ToString(callSuper = true, exclude="requestContext")
90: /* package */ class DefaultContent extends ContentSupport
91: {
92: @Inject
93: private RequestContext requestContext;
94:
95: /***********************************************************************************************************************************************************
96: * Creates a new {@code DefaultContent} with the given {@link Content.Builder}.
97: *
98: * @param builder the builder
99: **********************************************************************************************************************************************************/
100: public DefaultContent (@Nonnull final Content.Builder builder)
101: {
102: super(builder);
103: }
104:
105: /***********************************************************************************************************************************************************
106: * {@inheritDoc}
107: **********************************************************************************************************************************************************/
108: @Override @Nonnull
109: public Finder<Content> findChildren()
110: {
111: return new PathFinderSupport<>(this);
112: }
113:
114: /***********************************************************************************************************************************************************
115: * {@inheritDoc}
116: **********************************************************************************************************************************************************/
117: @Override @Nonnull
118: public Optional<ResourcePath> getExposedUri()
119: {
120: final var exposedUri = getProperty(P_EXPOSED_URI);
121: return exposedUri.isPresent() ? exposedUri.map(ResourcePath::of) : getDefaultExposedUri();
122: // return getProperty(P_EXPOSED_URI).map(ResourcePath::of).orElseGet(() -> getDefaultExposedUri()); TODO
123: }
124:
125: /***********************************************************************************************************************************************************
126: * {@inheritDoc}
127: **********************************************************************************************************************************************************/
128: @Nonnull
129: private Optional<ResourcePath> getDefaultExposedUri()
130: {
131: return getResource().getProperty(P_TITLE)
132: .map(DefaultContent::deAccent)
133: .map(t -> t.replaceAll(" ", "-")
134: .replaceAll(",", "")
135: .replaceAll("\\.", "")
136: .replaceAll(";", "")
137: .replaceAll("/", "")
138: .replaceAll("!", "")
139: .replaceAll("\\?", "")
140: .replaceAll(":", "")
141: .replaceAll("[^\\w-]*", ""))
142: .map(String::toLowerCase)
143: .map(ResourcePath::of);
144: }
145:
146: /***********************************************************************************************************************************************************
147: * See http://stackoverflow.com/questions/1008802/converting-symbols-accent-letters-to-english-alphabet
148: **********************************************************************************************************************************************************/
149: @Nonnull
150: public static String deAccent (@Nonnull final String string)
151: {
152: final var nfdNormalizedString = Normalizer.normalize(string, Normalizer.Form.NFD);
153: final var pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
154: return pattern.matcher(nfdNormalizedString).replaceAll("");
155: }
156:
157: @Override @Nonnull
158: public ResourceProperties getProperties()
159: {
160: return new ResourcePropertiesDelegate(requestContext, this, getResource().getProperties());
161: }
162: }