X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2Ffs%2FFsContent.java;h=55ef6ec46d89e7f4dbc01ef0b6ac2352c8901cf9;hb=904d7789c0e636c3b2e6a55fc6ec9ee333cc2023;hp=bbd77b2217c729ee5880d722a6dbf94804f60ae1;hpb=55d1a78150b6be0004f6bcb28703bcdd6daf55a1;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java b/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java index bbd77b221..55ef6ec46 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/fs/FsContent.java @@ -8,6 +8,7 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.FileTime; import java.nio.file.attribute.UserDefinedFileAttributeView; import java.time.Instant; @@ -20,8 +21,13 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.concurrent.CompletableFuture; import javax.xml.namespace.QName; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; import org.argeo.api.acr.Content; import org.argeo.api.acr.ContentName; @@ -43,35 +49,35 @@ public class FsContent extends AbstractContent implements ProvidedContent { private static final Map POSIX_KEYS; static { BASIC_KEYS = new HashMap<>(); - BASIC_KEYS.put(CrName.CREATION_TIME.get(), "basic:creationTime"); - BASIC_KEYS.put(CrName.LAST_MODIFIED_TIME.get(), "basic:lastModifiedTime"); - BASIC_KEYS.put(CrName.SIZE.get(), "basic:size"); - BASIC_KEYS.put(CrName.FILE_KEY.get(), "basic:fileKey"); + BASIC_KEYS.put(CrName.creationTime.qName(), "basic:creationTime"); + BASIC_KEYS.put(CrName.lastModifiedTime.qName(), "basic:lastModifiedTime"); + BASIC_KEYS.put(CrName.size.qName(), "basic:size"); + BASIC_KEYS.put(CrName.fileKey.qName(), "basic:fileKey"); POSIX_KEYS = new HashMap<>(BASIC_KEYS); - POSIX_KEYS.put(CrName.OWNER.get(), "owner:owner"); - POSIX_KEYS.put(CrName.GROUP.get(), "posix:group"); - POSIX_KEYS.put(CrName.PERMISSIONS.get(), "posix:permissions"); + POSIX_KEYS.put(CrName.owner.qName(), "owner:owner"); + POSIX_KEYS.put(CrName.group.qName(), "posix:group"); + POSIX_KEYS.put(CrName.permissions.qName(), "posix:permissions"); } private final FsContentProvider provider; private final Path path; - private final boolean isRoot; + private final boolean isMountBase; private final QName name; protected FsContent(ProvidedSession session, FsContentProvider contentProvider, Path path) { super(session); this.provider = contentProvider; this.path = path; - this.isRoot = contentProvider.isMountRoot(path); + this.isMountBase = contentProvider.isMountBase(path); // TODO check file names with ':' ? - if (isRoot) { + if (isMountBase) { String mountPath = provider.getMountPath(); if (mountPath != null && !mountPath.equals("/")) { Content mountPoint = session.getMountPoint(mountPath); this.name = mountPoint.getName(); } else { - this.name = CrName.ROOT.get(); + this.name = CrName.root.qName(); } } else { @@ -209,7 +215,7 @@ public class FsContent extends AbstractContent implements ProvidedContent { try { return Files.list(path).map((p) -> { FsContent fsContent = new FsContent(this, p); - Optional isMount = fsContent.get(CrName.MOUNT.get(), String.class); + Optional isMount = fsContent.get(CrName.mount.qName(), String.class); if (isMount.orElse("false").equals("true")) { QName[] classes = null; ContentProvider contentProvider = getSession().getRepository() @@ -233,7 +239,7 @@ public class FsContent extends AbstractContent implements ProvidedContent { FsContent fsContent; try { Path newPath = path.resolve(NamespaceUtils.toPrefixedName(provider, name)); - if (ContentName.contains(classes, CrName.COLLECTION.get())) + if (ContentName.contains(classes, CrName.collection.qName())) Files.createDirectory(newPath); else Files.createFile(newPath); @@ -250,7 +256,7 @@ public class FsContent extends AbstractContent implements ProvidedContent { ContentProvider contentProvider = getSession().getRepository().getMountContentProvider(fsContent, true, classes); Content mountedContent = contentProvider.get(getSession(), ""); - fsContent.put(CrName.MOUNT.get(), "true"); + fsContent.put(CrName.mount.qName(), "true"); return mountedContent; } else { @@ -265,7 +271,7 @@ public class FsContent extends AbstractContent implements ProvidedContent { @Override public Content getParent() { - if (isRoot) { + if (isMountBase) { String mountPath = provider.getMountPath(); if (mountPath == null || mountPath.equals("/")) return null; @@ -308,7 +314,7 @@ public class FsContent extends AbstractContent implements ProvidedContent { public List getContentClasses() { List res = new ArrayList<>(); if (Files.isDirectory(path)) - res.add(CrName.COLLECTION.get()); + res.add(CrName.collection.qName()); // TODO add other types return res; } @@ -322,4 +328,39 @@ public class FsContent extends AbstractContent implements ProvidedContent { return provider; } + /* + * READ / WRITE + */ + @SuppressWarnings("unchecked") + public CompletableFuture write(Class clss) { + if (isContentClass(CrName.collection.qName())) { + throw new IllegalStateException("Cannot directly write to a collection"); + } + if (InputStream.class.isAssignableFrom(clss)) { + CompletableFuture res = new CompletableFuture<>(); + res.thenAccept((in) -> { + try { + Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + throw new RuntimeException("Cannot write to " + path, e); + } + }); + return (CompletableFuture) res; + } else if (Source.class.isAssignableFrom(clss)) { + CompletableFuture res = new CompletableFuture(); + res.thenAccept((source) -> { +// Path targetPath = path.getParent().resolve(path.getFileName()+".xml"); + Path targetPath = path; + try (OutputStream out = Files.newOutputStream(targetPath)) { + StreamResult result = new StreamResult(out); + TransformerFactory.newDefaultInstance().newTransformer().transform(source, result); + } catch (IOException | TransformerException e) { + throw new RuntimeException("Cannot write to " + path, e); + } + }); + return (CompletableFuture) res; + } else { + return super.write(clss); + } + } }