]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/gcr/fs/FsContent.java
SLF4J implementation.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / gcr / fs / FsContent.java
1 package org.argeo.cms.gcr.fs;
2
3 import java.io.IOException;
4 import java.nio.charset.StandardCharsets;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.attribute.FileTime;
8 import java.nio.file.attribute.UserDefinedFileAttributeView;
9 import java.time.Instant;
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15
16 import org.argeo.api.gcr.Content;
17 import org.argeo.api.gcr.ContentResourceException;
18 import org.argeo.api.gcr.ContentSystemProvider;
19 import org.argeo.api.gcr.spi.AbstractContent;
20
21 public class FsContent extends AbstractContent implements Content {
22 private static final Set<String> BASIC_KEYS = new HashSet<>(
23 Arrays.asList("basic:creationTime", "basic:lastModifiedTime", "basic:size", "basic:fileKey"));
24 private static final Set<String> POSIX_KEYS;
25 static {
26 POSIX_KEYS = new HashSet<>(BASIC_KEYS);
27 POSIX_KEYS.add("owner:owner");
28 POSIX_KEYS.add("posix:group");
29 POSIX_KEYS.add("posix:permissions");
30 }
31
32 private FsContentSession contentSession;
33 private final Path path;
34
35 public FsContent(FsContentSession contentSession, Path path) {
36 super();
37 this.contentSession = contentSession;
38 this.path = path;
39 }
40
41 private boolean isPosix() {
42 return path.getFileSystem().supportedFileAttributeViews().contains("posix");
43 }
44
45 @Override
46 public Iterator<Content> iterator() {
47 if (Files.isDirectory(path)) {
48 try {
49 return Files.list(path).map((p) -> (Content) new FsContent(contentSession, p)).iterator();
50 } catch (IOException e) {
51 throw new ContentResourceException("Cannot list " + path, e);
52 }
53 } else {
54 return Collections.emptyIterator();
55 }
56 }
57
58 @Override
59 public String getName() {
60 return path.getFileName().toString();
61 }
62
63 @Override
64 public <A> A get(String key, Class<A> clss) {
65 Object value;
66 try {
67 value = Files.getAttribute(path, key);
68 } catch (IOException e) {
69 throw new ContentResourceException("Cannot retrieve attribute " + key + " for " + path, e);
70 }
71 if (value instanceof FileTime) {
72 if (clss.isAssignableFrom(FileTime.class))
73 return (A) value;
74 Instant instant = ((FileTime) value).toInstant();
75 if (Object.class.isAssignableFrom(clss)) {// plain object requested
76 return (A) instant;
77 }
78 // TODO perform trivial file conversion to other formats
79 }
80 if (value instanceof byte[]) {
81 return (A) new String((byte[]) value, StandardCharsets.UTF_8);
82 }
83 return (A) value;
84 }
85
86
87 @Override
88 protected Iterable<String> keys() {
89 Set<String> result = new HashSet<>(isPosix() ? POSIX_KEYS : BASIC_KEYS);
90 UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
91 if (udfav != null) {
92 try {
93 for (String name : udfav.list()) {
94 result.add("user:" + name);
95 }
96 } catch (IOException e) {
97 throw new ContentResourceException("Cannot liast attributes for " + path, e);
98 }
99 }
100 return result;
101 }
102
103 }