Skip to contentPackage: ResourceFileNetBeansPlatform$Exclusions
ResourceFileNetBeansPlatform$Exclusions
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.frontend.filesystem.impl;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import javax.inject.Provider;
32: import java.time.Instant;
33: import java.time.ZoneId;
34: import java.time.ZonedDateTime;
35: import java.util.ArrayList;
36: import java.util.Collection;
37: import java.util.Collections;
38: import java.util.Date;
39: import java.util.List;
40: import java.io.File;
41: import java.io.IOException;
42: import org.springframework.beans.factory.annotation.Configurable;
43: import org.openide.filesystems.FileObject;
44: import org.openide.filesystems.FileUtil;
45: import it.tidalwave.util.As;
46: import it.tidalwave.northernwind.core.model.MimeTypeResolver;
47: import it.tidalwave.northernwind.core.model.ResourceFile;
48: import it.tidalwave.northernwind.core.model.ResourceFileSystem;
49: import it.tidalwave.northernwind.core.model.ResourcePath;
50: import it.tidalwave.northernwind.core.model.spi.ResourceFileFinderSupport;
51: import lombok.Getter;
52: import lombok.ToString;
53: import lombok.experimental.Delegate;
54: import lombok.extern.slf4j.Slf4j;
55:
56: /***********************************************************************************************************************
57: *
58: * @author Fabrizio Giudici
59: *
60: **********************************************************************************************************************/
61: @Configurable @Slf4j @ToString(of = "delegate")
62: public class ResourceFileNetBeansPlatform implements ResourceFile
63: {
64: interface Exclusions
65: {
66: public String getName();
67: public ResourcePath getPath();
68: public ResourceFile getParent();
69: public ResourceFile getFileObject (String fileName);
70: public Collection<ResourceFile> getChildren();
71: public Collection<ResourceFile> getChildren (boolean b);
72: public String getMIMEType();
73: public File toFile();
74: public ResourceFile createFolder (String name);
75: public void copyTo (ResourceFile targetFolder);
76: public ResourceFileSystem getFileSystem();
77: public Date lastModified();
78: }
79:
80: @Inject
81: private Provider<MimeTypeResolver> mimeTypeResolver;
82:
83: @Getter @Nonnull
84: private final ResourceFileSystemNetBeansPlatform fileSystem;
85:
86: @Getter @Delegate(excludes = Exclusions.class) @Nonnull
87: private final FileObject delegate;
88:
89: @Delegate
90: private final As asSupport = As.forObject(this);
91:
92: public ResourceFileNetBeansPlatform (@Nonnull final ResourceFileSystemNetBeansPlatform fileSystem,
93: @Nonnull final FileObject delegate)
94: {
95: this.fileSystem = fileSystem;
96: this.delegate = delegate;
97: }
98:
99: @Override @Nonnull
100: public ResourcePath getPath()
101: {
102: return ResourcePath.of(delegate.getPath());
103: }
104:
105: @Override @Nonnull
106: public String getName()
107: {
108: return delegate.getNameExt();
109: }
110:
111: @Override
112: public ResourceFile getParent()
113: {
114: return fileSystem.createResourceFile(delegate.getParent());
115: }
116:
117: @Override @Nonnull
118: public ResourceFile createFolder (@Nonnull final String name)
119: throws IOException
120: {
121: return fileSystem.createResourceFile(delegate.createFolder(name));
122: }
123:
124: @Override @Nonnull
125: public Finder findChildren()
126: {
127: return ResourceFileFinderSupport.withComputeResults(getClass().getSimpleName(), f ->
128: {
129: final var name = f.getName();
130: final var recursive = f.isRecursive();
131:
132: final List<ResourceFile> result = new ArrayList<>();
133:
134: if (name != null)
135: {
136: final var child = delegate.getFileObject(name);
137:
138: if (child != null)
139: {
140: result.add(fileSystem.createResourceFile(child));
141: }
142: }
143: else
144: {
145: for (final FileObject child : Collections.list(delegate.getChildren(recursive)))
146: {
147: result.add(fileSystem.createResourceFile(child));
148: }
149: }
150:
151: return result;
152: });
153: }
154:
155: @Override @Nonnull
156: public String getMimeType()
157: {
158: return mimeTypeResolver.get().getMimeType(delegate.getNameExt());
159: }
160:
161: @Override @Nonnull
162: public ZonedDateTime getLatestModificationTime()
163: {
164: // See NW-154
165: final var file = toFile();
166:
167: final var millis = (file != null) ? file.lastModified() : delegate.lastModified().getTime();
168: return Instant.ofEpochMilli(millis).atZone(ZoneId.of("GMT"));
169: }
170:
171: @Nonnull
172: @Override
173: public File toFile()
174: {
175: return FileUtil.toFile(delegate);
176: }
177:
178: @Override
179: public void copyTo (@Nonnull final ResourceFile targetFolder)
180: throws IOException
181: {
182: FileUtil.copyFile(delegate, ((ResourceFileNetBeansPlatform)targetFolder).delegate, delegate.getName());
183: }
184:
185: // TODO: equals and hashcode
186: }