import org.argeo.api.acr.Content;
import org.argeo.api.acr.ContentName;
import org.argeo.api.acr.ContentResourceException;
-import org.argeo.api.acr.ContentUtils;
import org.argeo.api.acr.CrName;
import org.argeo.api.acr.NamespaceUtils;
-import org.argeo.api.acr.spi.AbstractContent;
import org.argeo.api.acr.spi.ContentProvider;
import org.argeo.api.acr.spi.ProvidedContent;
import org.argeo.api.acr.spi.ProvidedSession;
+import org.argeo.cms.acr.AbstractContent;
+import org.argeo.cms.acr.ContentUtils;
import org.argeo.util.FsUtils;
+/** Content persisted as a filesystem {@link Path}. */
public class FsContent extends AbstractContent implements ProvidedContent {
final static String USER_ = "user:";
this.session = session;
this.provider = contentProvider;
this.path = path;
- this.isRoot = contentProvider.isRoot(path);
+ this.isRoot = contentProvider.isMountRoot(path);
// TODO check file names with ':' ?
if (isRoot) {
String mountPath = provider.getMountPath();
- if (mountPath != null) {
+ if (mountPath != null && !mountPath.equals("/")) {
Content mountPoint = session.getMountPoint(mountPath);
this.name = mountPoint.getName();
} else {
this.name = CrName.ROOT.get();
}
} else {
- QName providerName = NamespaceUtils.parsePrefixedName(provider, path.getFileName().toString());
+
+ // TODO should we support prefixed name for known types?
+ // QName providerName = NamespaceUtils.parsePrefixedName(provider,
+ // path.getFileName().toString());
+ QName providerName = new QName(path.getFileName().toString());
+ // TODO remove extension if mounted?
this.name = new ContentName(providerName, session);
}
}
* ATTRIBUTES
*/
+ @SuppressWarnings("unchecked")
@Override
public <A> Optional<A> get(QName key, Class<A> clss) {
Object value;
try {
// We need to add user: when accessing via Files#getAttribute
- value = Files.getAttribute(path, toFsAttributeKey(key));
+
+ if (POSIX_KEYS.containsKey(key)) {
+ value = Files.getAttribute(path, toFsAttributeKey(key));
+ } else {
+ UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path,
+ UserDefinedFileAttributeView.class);
+ String prefixedName = NamespaceUtils.toPrefixedName(provider, key);
+ if (!udfav.list().contains(prefixedName))
+ return Optional.empty();
+ ByteBuffer buf = ByteBuffer.allocate(udfav.size(prefixedName));
+ udfav.read(prefixedName, buf);
+ buf.flip();
+ if (buf.hasArray())
+ value = buf.array();
+ else {
+ byte[] arr = new byte[buf.remaining()];
+ buf.get(arr);
+ value = arr;
+ }
+ }
} catch (IOException e) {
throw new ContentResourceException("Cannot retrieve attribute " + key + " for " + path, e);
}
UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
ByteBuffer bb = ByteBuffer.wrap(value.toString().getBytes(StandardCharsets.UTF_8));
try {
- int size = udfav.write(NamespaceUtils.toPrefixedName(provider, key), bb);
+ udfav.write(NamespaceUtils.toPrefixedName(provider, key), bb);
} catch (IOException e) {
throw new ContentResourceException("Cannot delete attribute " + key + " for " + path, e);
}
QName[] classes = null;
ContentProvider contentProvider = session.getRepository().getMountContentProvider(fsContent,
false, classes);
- Content mountedContent = contentProvider.get(session, fsContent.getPath(), "");
+ Content mountedContent = contentProvider.get(session, "");
return mountedContent;
} else {
return (Content) fsContent;
if (session.getRepository().shouldMount(classes)) {
ContentProvider contentProvider = session.getRepository().getMountContentProvider(fsContent, true, classes);
- Content mountedContent = contentProvider.get(session, fsContent.getPath(), "");
+ Content mountedContent = contentProvider.get(session, "");
fsContent.put(CrName.MOUNT.get(), "true");
return mountedContent;
public Content getParent() {
if (isRoot) {
String mountPath = provider.getMountPath();
- if (mountPath == null)
+ if (mountPath == null || mountPath.equals("/"))
return null;
String[] parent = ContentUtils.getParentPath(mountPath);
return session.get(parent[0]);
return new FsContent(this, path.getParent());
}
+ @SuppressWarnings("unchecked")
@Override
public <C extends Closeable> C open(Class<C> clss) throws IOException, IllegalArgumentException {
if (InputStream.class.isAssignableFrom(clss)) {