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
.Collections
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
17 import java
.util
.Iterator
;
19 import java
.util
.Optional
;
22 import javax
.xml
.namespace
.QName
;
24 import org
.argeo
.api
.acr
.Content
;
25 import org
.argeo
.api
.acr
.ContentName
;
26 import org
.argeo
.api
.acr
.ContentResourceException
;
27 import org
.argeo
.api
.acr
.CrName
;
28 import org
.argeo
.api
.acr
.NamespaceUtils
;
29 import org
.argeo
.api
.acr
.spi
.AbstractContent
;
30 import org
.argeo
.api
.acr
.spi
.ContentProvider
;
31 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
32 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
33 import org
.argeo
.cms
.acr
.ContentUtils
;
34 import org
.argeo
.util
.FsUtils
;
36 public class FsContent
extends AbstractContent
implements ProvidedContent
{
37 final static String USER_
= "user:";
39 private static final Map
<QName
, String
> BASIC_KEYS
;
40 private static final Map
<QName
, String
> POSIX_KEYS
;
42 BASIC_KEYS
= new HashMap
<>();
43 BASIC_KEYS
.put(CrName
.CREATION_TIME
.get(), "basic:creationTime");
44 BASIC_KEYS
.put(CrName
.LAST_MODIFIED_TIME
.get(), "basic:lastModifiedTime");
45 BASIC_KEYS
.put(CrName
.SIZE
.get(), "basic:size");
46 BASIC_KEYS
.put(CrName
.FILE_KEY
.get(), "basic:fileKey");
48 POSIX_KEYS
= new HashMap
<>(BASIC_KEYS
);
49 POSIX_KEYS
.put(CrName
.OWNER
.get(), "owner:owner");
50 POSIX_KEYS
.put(CrName
.GROUP
.get(), "posix:group");
51 POSIX_KEYS
.put(CrName
.PERMISSIONS
.get(), "posix:permissions");
54 private final ProvidedSession session
;
55 private final FsContentProvider provider
;
56 private final Path path
;
57 private final boolean isRoot
;
58 private final QName name
;
60 protected FsContent(ProvidedSession session
, FsContentProvider contentProvider
, Path path
) {
61 this.session
= session
;
62 this.provider
= contentProvider
;
64 this.isRoot
= contentProvider
.isRoot(path
);
65 // TODO check file names with ':' ?
67 String mountPath
= provider
.getMountPath();
68 if (mountPath
!= null && !mountPath
.equals("/")) {
69 Content mountPoint
= session
.getMountPoint(mountPath
);
70 this.name
= mountPoint
.getName();
72 this.name
= CrName
.ROOT
.get();
75 QName providerName
= NamespaceUtils
.parsePrefixedName(provider
, path
.getFileName().toString());
76 this.name
= new ContentName(providerName
, session
);
80 protected FsContent(FsContent context
, Path path
) {
81 this(context
.getSession(), context
.getProvider(), path
);
84 private boolean isPosix() {
85 return path
.getFileSystem().supportedFileAttributeViews().contains("posix");
89 public QName
getName() {
98 public <A
> Optional
<A
> get(QName key
, Class
<A
> clss
) {
101 // We need to add user: when accessing via Files#getAttribute
103 if (POSIX_KEYS
.containsKey(key
)) {
104 value
= Files
.getAttribute(path
, toFsAttributeKey(key
));
106 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
,
107 UserDefinedFileAttributeView
.class);
108 String prefixedName
= NamespaceUtils
.toPrefixedName(provider
, key
);
109 if (!udfav
.list().contains(prefixedName
))
110 return Optional
.empty();
111 ByteBuffer buf
= ByteBuffer
.allocate(udfav
.size(prefixedName
));
112 udfav
.read(prefixedName
, buf
);
117 byte[] arr
= new byte[buf
.remaining()];
122 } catch (IOException e
) {
123 throw new ContentResourceException("Cannot retrieve attribute " + key
+ " for " + path
, e
);
126 if (value
instanceof FileTime
) {
127 if (clss
.isAssignableFrom(FileTime
.class))
129 Instant instant
= ((FileTime
) value
).toInstant();
130 if (Object
.class.isAssignableFrom(clss
)) {// plain object requested
133 // TODO perform trivial file conversion to other formats
135 if (value
instanceof byte[]) {
136 res
= (A
) new String((byte[]) value
, StandardCharsets
.UTF_8
);
141 } catch (ClassCastException e
) {
142 return Optional
.empty();
144 return Optional
.of(res
);
148 protected Iterable
<QName
> keys() {
149 Set
<QName
> result
= new HashSet
<>(isPosix() ? POSIX_KEYS
.keySet() : BASIC_KEYS
.keySet());
150 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
, UserDefinedFileAttributeView
.class);
153 for (String name
: udfav
.list()) {
154 QName providerName
= NamespaceUtils
.parsePrefixedName(provider
, name
);
155 QName sessionName
= new ContentName(providerName
, session
);
156 result
.add(sessionName
);
158 } catch (IOException e
) {
159 throw new ContentResourceException("Cannot list attributes for " + path
, e
);
166 protected void removeAttr(QName key
) {
167 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
, UserDefinedFileAttributeView
.class);
169 udfav
.delete(NamespaceUtils
.toPrefixedName(provider
, key
));
170 } catch (IOException e
) {
171 throw new ContentResourceException("Cannot delete attribute " + key
+ " for " + path
, e
);
176 public Object
put(QName key
, Object value
) {
177 Object previous
= get(key
);
178 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
, UserDefinedFileAttributeView
.class);
179 ByteBuffer bb
= ByteBuffer
.wrap(value
.toString().getBytes(StandardCharsets
.UTF_8
));
181 int size
= udfav
.write(NamespaceUtils
.toPrefixedName(provider
, key
), bb
);
182 } catch (IOException e
) {
183 throw new ContentResourceException("Cannot delete attribute " + key
+ " for " + path
, e
);
188 protected String
toFsAttributeKey(QName key
) {
189 if (POSIX_KEYS
.containsKey(key
))
190 return POSIX_KEYS
.get(key
);
192 return USER_
+ NamespaceUtils
.toPrefixedName(provider
, key
);
199 public Iterator
<Content
> iterator() {
200 if (Files
.isDirectory(path
)) {
202 return Files
.list(path
).map((p
) -> {
203 FsContent fsContent
= new FsContent(this, p
);
204 Optional
<String
> isMount
= fsContent
.get(CrName
.MOUNT
.get(), String
.class);
205 if (isMount
.orElse("false").equals("true")) {
206 QName
[] classes
= null;
207 ContentProvider contentProvider
= session
.getRepository().getMountContentProvider(fsContent
,
209 Content mountedContent
= contentProvider
.get(session
, fsContent
.getPath(), "");
210 return mountedContent
;
212 return (Content
) fsContent
;
215 } catch (IOException e
) {
216 throw new ContentResourceException("Cannot list " + path
, e
);
219 return Collections
.emptyIterator();
224 public Content
add(QName name
, QName
... classes
) {
227 Path newPath
= path
.resolve(NamespaceUtils
.toPrefixedName(provider
, name
));
228 if (ContentName
.contains(classes
, CrName
.COLLECTION
.get()))
229 Files
.createDirectory(newPath
);
231 Files
.createFile(newPath
);
233 // for(ContentClass clss:classes) {
234 // Files.setAttribute(newPath, name, newPath, null)
236 fsContent
= new FsContent(this, newPath
);
237 } catch (IOException e
) {
238 throw new ContentResourceException("Cannot create new content", e
);
241 if (session
.getRepository().shouldMount(classes
)) {
242 ContentProvider contentProvider
= session
.getRepository().getMountContentProvider(fsContent
, true, classes
);
243 Content mountedContent
= contentProvider
.get(session
, fsContent
.getPath(), "");
244 fsContent
.put(CrName
.MOUNT
.get(), "true");
245 return mountedContent
;
253 public void remove() {
254 FsUtils
.delete(path
);
258 public Content
getParent() {
260 String mountPath
= provider
.getMountPath();
261 if (mountPath
== null || mountPath
.equals("/"))
263 String
[] parent
= ContentUtils
.getParentPath(mountPath
);
264 return session
.get(parent
[0]);
266 return new FsContent(this, path
.getParent());
270 public <C
extends Closeable
> C
open(Class
<C
> clss
) throws IOException
, IllegalArgumentException
{
271 if (InputStream
.class.isAssignableFrom(clss
)) {
272 if (Files
.isDirectory(path
))
273 throw new UnsupportedOperationException("Cannot open " + path
+ " as stream, since it is a directory");
274 return (C
) Files
.newInputStream(path
);
275 } else if (OutputStream
.class.isAssignableFrom(clss
)) {
276 if (Files
.isDirectory(path
))
277 throw new UnsupportedOperationException("Cannot open " + path
+ " as stream, since it is a directory");
278 return (C
) Files
.newOutputStream(path
);
280 return super.open(clss
);
287 public ProvidedContent
getMountPoint(String relativePath
) {
288 Path childPath
= path
.resolve(relativePath
);
289 // TODO check that it is a mount
290 return new FsContent(this, childPath
);
297 public ProvidedSession
getSession() {
302 public FsContentProvider
getProvider() {