1 package org
.argeo
.cms
.acr
.fs
;
3 import java
.io
.Closeable
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.OutputStream
;
7 import java
.nio
.ByteBuffer
;
8 import java
.nio
.charset
.StandardCharsets
;
9 import java
.nio
.file
.Files
;
10 import java
.nio
.file
.Path
;
11 import java
.nio
.file
.attribute
.FileTime
;
12 import java
.nio
.file
.attribute
.UserDefinedFileAttributeView
;
13 import java
.time
.Instant
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Collections
;
16 import java
.util
.HashMap
;
17 import java
.util
.HashSet
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
21 import java
.util
.Optional
;
24 import javax
.xml
.namespace
.QName
;
26 import org
.argeo
.api
.acr
.Content
;
27 import org
.argeo
.api
.acr
.ContentName
;
28 import org
.argeo
.api
.acr
.ContentResourceException
;
29 import org
.argeo
.api
.acr
.CrName
;
30 import org
.argeo
.api
.acr
.NamespaceUtils
;
31 import org
.argeo
.api
.acr
.spi
.ContentProvider
;
32 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
33 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
34 import org
.argeo
.cms
.acr
.AbstractContent
;
35 import org
.argeo
.cms
.acr
.ContentUtils
;
36 import org
.argeo
.util
.FsUtils
;
38 /** Content persisted as a filesystem {@link Path}. */
39 public class FsContent
extends AbstractContent
implements ProvidedContent
{
40 final static String USER_
= "user:";
42 private static final Map
<QName
, String
> BASIC_KEYS
;
43 private static final Map
<QName
, String
> POSIX_KEYS
;
45 BASIC_KEYS
= new HashMap
<>();
46 BASIC_KEYS
.put(CrName
.creationTime
.qName(), "basic:creationTime");
47 BASIC_KEYS
.put(CrName
.lastModifiedTime
.qName(), "basic:lastModifiedTime");
48 BASIC_KEYS
.put(CrName
.size
.qName(), "basic:size");
49 BASIC_KEYS
.put(CrName
.fileKey
.qName(), "basic:fileKey");
51 POSIX_KEYS
= new HashMap
<>(BASIC_KEYS
);
52 POSIX_KEYS
.put(CrName
.owner
.qName(), "owner:owner");
53 POSIX_KEYS
.put(CrName
.group
.qName(), "posix:group");
54 POSIX_KEYS
.put(CrName
.permissions
.qName(), "posix:permissions");
57 private final FsContentProvider provider
;
58 private final Path path
;
59 private final boolean isRoot
;
60 private final QName name
;
62 protected FsContent(ProvidedSession session
, FsContentProvider contentProvider
, Path path
) {
64 this.provider
= contentProvider
;
66 this.isRoot
= contentProvider
.isMountRoot(path
);
67 // TODO check file names with ':' ?
69 String mountPath
= provider
.getMountPath();
70 if (mountPath
!= null && !mountPath
.equals("/")) {
71 Content mountPoint
= session
.getMountPoint(mountPath
);
72 this.name
= mountPoint
.getName();
74 this.name
= CrName
.root
.qName();
78 // TODO should we support prefixed name for known types?
79 // QName providerName = NamespaceUtils.parsePrefixedName(provider,
80 // path.getFileName().toString());
81 QName providerName
= new QName(path
.getFileName().toString());
82 // TODO remove extension if mounted?
83 this.name
= new ContentName(providerName
, session
);
87 protected FsContent(FsContent context
, Path path
) {
88 this(context
.getSession(), context
.getProvider(), path
);
91 private boolean isPosix() {
92 return path
.getFileSystem().supportedFileAttributeViews().contains("posix");
96 public QName
getName() {
104 @SuppressWarnings("unchecked")
106 public <A
> Optional
<A
> get(QName key
, Class
<A
> clss
) {
109 // We need to add user: when accessing via Files#getAttribute
111 if (POSIX_KEYS
.containsKey(key
)) {
112 value
= Files
.getAttribute(path
, toFsAttributeKey(key
));
114 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
,
115 UserDefinedFileAttributeView
.class);
116 String prefixedName
= NamespaceUtils
.toPrefixedName(provider
, key
);
117 if (!udfav
.list().contains(prefixedName
))
118 return Optional
.empty();
119 ByteBuffer buf
= ByteBuffer
.allocate(udfav
.size(prefixedName
));
120 udfav
.read(prefixedName
, buf
);
125 byte[] arr
= new byte[buf
.remaining()];
130 } catch (IOException e
) {
131 throw new ContentResourceException("Cannot retrieve attribute " + key
+ " for " + path
, e
);
134 if (value
instanceof FileTime
) {
135 if (clss
.isAssignableFrom(FileTime
.class))
137 Instant instant
= ((FileTime
) value
).toInstant();
138 if (Object
.class.isAssignableFrom(clss
)) {// plain object requested
141 // TODO perform trivial file conversion to other formats
143 if (value
instanceof byte[]) {
144 res
= (A
) new String((byte[]) value
, StandardCharsets
.UTF_8
);
149 } catch (ClassCastException e
) {
150 return Optional
.empty();
152 return Optional
.of(res
);
156 protected Iterable
<QName
> keys() {
157 Set
<QName
> result
= new HashSet
<>(isPosix() ? POSIX_KEYS
.keySet() : BASIC_KEYS
.keySet());
158 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
, UserDefinedFileAttributeView
.class);
161 for (String name
: udfav
.list()) {
162 QName providerName
= NamespaceUtils
.parsePrefixedName(provider
, name
);
163 QName sessionName
= new ContentName(providerName
, getSession());
164 result
.add(sessionName
);
166 } catch (IOException e
) {
167 throw new ContentResourceException("Cannot list attributes for " + path
, e
);
174 protected void removeAttr(QName key
) {
175 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
, UserDefinedFileAttributeView
.class);
177 udfav
.delete(NamespaceUtils
.toPrefixedName(provider
, key
));
178 } catch (IOException e
) {
179 throw new ContentResourceException("Cannot delete attribute " + key
+ " for " + path
, e
);
184 public Object
put(QName key
, Object value
) {
185 Object previous
= get(key
);
186 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
, UserDefinedFileAttributeView
.class);
187 ByteBuffer bb
= ByteBuffer
.wrap(value
.toString().getBytes(StandardCharsets
.UTF_8
));
189 udfav
.write(NamespaceUtils
.toPrefixedName(provider
, key
), bb
);
190 } catch (IOException e
) {
191 throw new ContentResourceException("Cannot delete attribute " + key
+ " for " + path
, e
);
196 protected String
toFsAttributeKey(QName key
) {
197 if (POSIX_KEYS
.containsKey(key
))
198 return POSIX_KEYS
.get(key
);
200 return USER_
+ NamespaceUtils
.toPrefixedName(provider
, key
);
207 public Iterator
<Content
> iterator() {
208 if (Files
.isDirectory(path
)) {
210 return Files
.list(path
).map((p
) -> {
211 FsContent fsContent
= new FsContent(this, p
);
212 Optional
<String
> isMount
= fsContent
.get(CrName
.mount
.qName(), String
.class);
213 if (isMount
.orElse("false").equals("true")) {
214 QName
[] classes
= null;
215 ContentProvider contentProvider
= getSession().getRepository()
216 .getMountContentProvider(fsContent
, false, classes
);
217 Content mountedContent
= contentProvider
.get(getSession(), "");
218 return mountedContent
;
220 return (Content
) fsContent
;
223 } catch (IOException e
) {
224 throw new ContentResourceException("Cannot list " + path
, e
);
227 return Collections
.emptyIterator();
232 public Content
add(QName name
, QName
... classes
) {
235 Path newPath
= path
.resolve(NamespaceUtils
.toPrefixedName(provider
, name
));
236 if (ContentName
.contains(classes
, CrName
.collection
.qName()))
237 Files
.createDirectory(newPath
);
239 Files
.createFile(newPath
);
241 // for(ContentClass clss:classes) {
242 // Files.setAttribute(newPath, name, newPath, null)
244 fsContent
= new FsContent(this, newPath
);
245 } catch (IOException e
) {
246 throw new ContentResourceException("Cannot create new content", e
);
249 if (getSession().getRepository().shouldMount(classes
)) {
250 ContentProvider contentProvider
= getSession().getRepository().getMountContentProvider(fsContent
, true,
252 Content mountedContent
= contentProvider
.get(getSession(), "");
253 fsContent
.put(CrName
.mount
.qName(), "true");
254 return mountedContent
;
262 public void remove() {
263 FsUtils
.delete(path
);
267 public Content
getParent() {
269 String mountPath
= provider
.getMountPath();
270 if (mountPath
== null || mountPath
.equals("/"))
272 String
[] parent
= ContentUtils
.getParentPath(mountPath
);
273 return getSession().get(parent
[0]);
275 return new FsContent(this, path
.getParent());
278 @SuppressWarnings("unchecked")
280 public <C
extends Closeable
> C
open(Class
<C
> clss
) throws IOException
, IllegalArgumentException
{
281 if (InputStream
.class.isAssignableFrom(clss
)) {
282 if (Files
.isDirectory(path
))
283 throw new UnsupportedOperationException("Cannot open " + path
+ " as stream, since it is a directory");
284 return (C
) Files
.newInputStream(path
);
285 } else if (OutputStream
.class.isAssignableFrom(clss
)) {
286 if (Files
.isDirectory(path
))
287 throw new UnsupportedOperationException("Cannot open " + path
+ " as stream, since it is a directory");
288 return (C
) Files
.newOutputStream(path
);
290 return super.open(clss
);
297 public ProvidedContent
getMountPoint(String relativePath
) {
298 Path childPath
= path
.resolve(relativePath
);
299 // TODO check that it is a mount
300 return new FsContent(this, childPath
);
308 public List
<QName
> getContentClasses() {
309 List
<QName
> res
= new ArrayList
<>();
310 if (Files
.isDirectory(path
))
311 res
.add(CrName
.collection
.qName());
312 // TODO add other types
321 public FsContentProvider
getProvider() {