Package: DefaultLibraryRequestProcessor
DefaultLibraryRequestProcessor
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultLibraryRequestProcessor() |
|
|
|
|
|
||||||||||||||||||||
getDuration() |
|
|
|
|
|
||||||||||||||||||||
process(Request) |
|
|
|
|
|
||||||||||||||||||||
setDuration(Duration) |
|
|
|
|
|
||||||||||||||||||||
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 java.time.Duration;
33: import java.io.IOException;
34: import org.springframework.beans.factory.annotation.Configurable;
35: import org.springframework.core.annotation.Order;
36: import it.tidalwave.util.NotFoundException;
37: import it.tidalwave.northernwind.core.model.Request;
38: import it.tidalwave.northernwind.core.model.RequestProcessor;
39: import it.tidalwave.northernwind.core.model.Resource;
40: import it.tidalwave.northernwind.core.model.SiteProvider;
41: import it.tidalwave.northernwind.core.impl.model.FilterSetExpander;
42: import lombok.Getter;
43: import lombok.Setter;
44: import lombok.extern.slf4j.Slf4j;
45: import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
46: import static it.tidalwave.northernwind.core.model.RequestProcessor.Status.*;
47:
48: /***********************************************************************************************************************
49: *
50: * @author Fabrizio Giudici
51: *
52: **********************************************************************************************************************/
53: @Configurable @Order(HIGHEST_PRECEDENCE + 2) @Slf4j
54:•public class DefaultLibraryRequestProcessor implements RequestProcessor
55: {
56: @Inject
57: private Provider<SiteProvider> siteProvider;
58:
59: @Inject
60: private Provider<FilterSetExpander> macroExpander;
61:
62: @Inject
63: private ResponseHolder<?> responseHolder;
64:
65:• @Getter @Setter
66: private Duration duration = Duration.ofDays(7);
67:
68: /*******************************************************************************************************************
69: *
70: * {@inheritDoc}
71: *
72: ******************************************************************************************************************/
73: @Override @Nonnull
74: public Status process (@Nonnull final Request request)
75: {
76: final var relativePath = request.getRelativeUri();
77:
78: try
79: {
80: final var resource = siteProvider.get().getSite().find(Resource.class).withRelativePath(relativePath).result();
81: final var file = resource.getFile();
82: final var mimeType = file.getMimeType();
83:• final Object content = mimeType.startsWith("text/") ? macroExpander.get().filter(file.asText("UTF-8"), mimeType)
84: : file.asBytes();
85: responseHolder.response().forRequest(request)
86: .withBody(content)
87: .withContentType(mimeType)
88: .withLatestModifiedTime(file.getLatestModificationTime())
89: .withExpirationTime(duration)
90: .put();
91: return BREAK;
92: }
93: catch (IOException | NotFoundException e)
94: {
95: log.debug("Requested URI {} doesn't map to a library resource, continuing...", relativePath);
96: }
97:
98: return CONTINUE;
99: }
100: }