use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class CreateAndAppendDOMNodeCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final Map<String, Object> nodeData = webSocketData.getNodeData();
final String parentId = (String) nodeData.get("parentId");
final String childContent = (String) nodeData.get("childContent");
final String pageId = webSocketData.getPageId();
Boolean inheritVisibilityFlags = (Boolean) nodeData.get("inheritVisibilityFlags");
if (inheritVisibilityFlags == null) {
inheritVisibilityFlags = false;
}
// remove configuration elements from the nodeData so we don't set it on the node
nodeData.remove("parentId");
nodeData.remove("inheritVisibilityFlags");
if (pageId != null) {
// check for parent ID before creating any nodes
if (parentId == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without parentId").build(), true);
return;
}
// check if parent node with given ID exists
final DOMNode parentNode = getDOMNode(parentId);
if (parentNode == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
return;
}
final Document document = getPage(pageId);
if (document != null) {
final String tagName = (String) nodeData.get("tagName");
nodeData.remove("tagName");
try {
DOMNode newNode;
if (tagName != null && "comment".equals(tagName)) {
newNode = (DOMNode) document.createComment("#comment");
} else if (tagName != null && "template".equals(tagName)) {
newNode = (DOMNode) document.createTextNode("#template");
try {
newNode.unlockSystemPropertiesOnce();
newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(NodeInterface.type, Template.class.getSimpleName()));
} catch (FrameworkException fex) {
logger.warn("Unable to set type of node {} to Template: {}", new Object[] { newNode.getUuid(), fex.getMessage() });
}
} else if (tagName != null && !tagName.isEmpty()) {
if ("custom".equals(tagName)) {
try {
// experimental: create DOM element with literal tag
newNode = (DOMElement) StructrApp.getInstance(webSocket.getSecurityContext()).create(DOMElement.class, new NodeAttribute(StructrApp.key(DOMElement.class, "tag"), "custom"), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnDetail"), false), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnIndex"), false));
if (newNode != null && document != null) {
newNode.doAdopt((Page) document);
}
} catch (FrameworkException fex) {
// abort
getWebSocket().send(MessageBuilder.status().code(422).message(fex.getMessage()).build(), true);
return;
}
} else {
newNode = (DOMNode) document.createElement(tagName);
}
} else {
newNode = (DOMNode) document.createTextNode("#text");
}
// Instantiate node again to get correct class
newNode = getDOMNode(newNode.getUuid());
// append new node to parent
if (newNode != null) {
parentNode.appendChild(newNode);
for (Entry entry : nodeData.entrySet()) {
final String key = (String) entry.getKey();
final Object val = entry.getValue();
PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(newNode.getClass(), key);
if (propertyKey != null) {
try {
Object convertedValue = val;
PropertyConverter inputConverter = propertyKey.inputConverter(SecurityContext.getSuperUserInstance());
if (inputConverter != null) {
convertedValue = inputConverter.convert(val);
}
newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(propertyKey, convertedValue));
} catch (FrameworkException fex) {
logger.warn("Unable to set node property {} of node {} to {}: {}", new Object[] { propertyKey, newNode.getUuid(), val, fex.getMessage() });
}
}
}
PropertyMap visibilityFlags = null;
if (inheritVisibilityFlags) {
visibilityFlags = new PropertyMap();
visibilityFlags.put(DOMNode.visibleToAuthenticatedUsers, parentNode.getProperty(DOMNode.visibleToAuthenticatedUsers));
visibilityFlags.put(DOMNode.visibleToPublicUsers, parentNode.getProperty(DOMNode.visibleToPublicUsers));
try {
newNode.setProperties(newNode.getSecurityContext(), visibilityFlags);
} catch (FrameworkException fex) {
logger.warn("Unable to inherit visibility flags for node {} from parent node {}", newNode, parentNode);
}
}
// create a child text node if content is given
if (StringUtils.isNotBlank(childContent)) {
final DOMNode childNode = (DOMNode) document.createTextNode(childContent);
newNode.appendChild(childNode);
if (inheritVisibilityFlags) {
try {
childNode.setProperties(childNode.getSecurityContext(), visibilityFlags);
} catch (FrameworkException fex) {
logger.warn("Unable to inherit visibility flags for node {} from parent node {}", childNode, newNode);
}
}
}
}
} catch (DOMException dex) {
// send DOM exception
getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
}
} else {
getWebSocket().send(MessageBuilder.status().code(404).message("Page not found").build(), true);
}
} else {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot create node without pageId").build(), true);
}
}
use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class StructrSSHFileSystem method provider.
@Override
public FileSystemProvider provider() {
logger.info("x");
return new FileSystemProvider() {
@Override
public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException {
logger.info("x");
OutputStream os = null;
File actualFile = (File) ((StructrSSHFile) path).getActualFile();
try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
if (actualFile == null) {
actualFile = (File) create(path);
}
if (actualFile != null) {
os = ((File) actualFile).getOutputStream();
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
throw new IOException(fex);
}
return os;
}
@Override
public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
// Remote file => file node in Structr
logger.info("x");
InputStream inputStream = null;
try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
final File fileNode = (File) ((StructrSSHFile) path).getActualFile();
inputStream = fileNode.getInputStream();
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
throw new IOException(fex);
}
return inputStream;
}
@Override
public String getScheme() {
logger.info("Method not implemented yet");
return null;
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException {
logger.info("Method not implemented yet");
return null;
}
@Override
public FileSystem getFileSystem(URI uri) {
logger.info("Method not implemented yet");
return null;
}
@Override
public Path getPath(URI uri) {
logger.info("Method not implemented yet");
return null;
}
@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
logger.info("x");
SeekableByteChannel channel = null;
final File fileNode = (File) ((StructrSSHFile) path).getActualFile();
if (fileNode != null) {
try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
final Path filePath = fileNode.getFileOnDisk().toPath();
channel = Files.newByteChannel(filePath);
tx.success();
} catch (FrameworkException fex) {
logger.error("", fex);
throw new IOException(fex);
}
}
return channel;
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
logger.info("x");
return new DirectoryStream() {
boolean closed = false;
@Override
public Iterator iterator() {
if (!closed) {
final App app = StructrApp.getInstance(securityContext);
final List<StructrSSHFile> files = new LinkedList<>();
final StructrSSHFile thisDir = (StructrSSHFile) dir;
try (final Tx tx = app.tx()) {
for (final Folder child : thisDir.getFolders()) {
files.add(new StructrSSHFile(thisDir, child.getName(), child));
}
for (final File child : thisDir.getFiles()) {
files.add(new StructrSSHFile(thisDir, child.getName(), child));
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return files.iterator();
}
return Collections.emptyIterator();
}
@Override
public void close() throws IOException {
closed = true;
}
};
}
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
logger.info("x");
final StructrSSHFile parent = (StructrSSHFile) dir.getParent();
final App app = StructrApp.getInstance(securityContext);
final String name = dir.getFileName().toString();
try (final Tx tx = app.tx()) {
final Folder folder = app.create(Folder.class, new NodeAttribute(AbstractNode.name, name), new NodeAttribute(StructrApp.key(AbstractFile.class, "parent"), parent != null ? parent.getActualFile() : null));
((StructrSSHFile) dir).setActualFile(folder);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
throw new IOException(fex);
}
}
@Override
public void delete(Path path) throws IOException {
logger.info("Method not implemented yet");
}
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
logger.info("Method not implemented yet");
}
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
logger.info("Method not implemented yet");
}
@Override
public boolean isSameFile(Path path, Path path2) throws IOException {
logger.info("x");
return path != null && path.equals(path);
}
@Override
public boolean isHidden(Path path) throws IOException {
logger.info("Method not implemented yet");
return false;
}
@Override
public FileStore getFileStore(Path path) throws IOException {
logger.info("Method not implemented yet");
return null;
}
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException {
logger.info("Checking access", new Object[] { path, modes });
}
@Override
public <V extends FileAttributeView> V getFileAttributeView(final Path path, final Class<V> type, final LinkOption... options) {
logger.info("x");
return (V) new PosixFileAttributeView() {
@Override
public String name() {
return "posix";
}
@Override
public PosixFileAttributes readAttributes() throws IOException {
return new StructrPosixFileAttributes((StructrSSHFile) path);
}
@Override
public void setPermissions(Set<PosixFilePermission> set) throws IOException {
logger.info("Method not implemented yet");
}
@Override
public void setGroup(GroupPrincipal gp) throws IOException {
logger.info("Method not implemented yet");
}
@Override
public void setTimes(FileTime ft, FileTime ft1, FileTime ft2) throws IOException {
logger.info("Method not implemented yet");
}
@Override
public UserPrincipal getOwner() throws IOException {
logger.info("Method not implemented yet");
return null;
}
@Override
public void setOwner(UserPrincipal up) throws IOException {
logger.info("Method not implemented yet");
}
};
}
@Override
public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException {
logger.info("x");
if (path != null) {
if (path instanceof StructrSSHFile) {
final StructrSSHFile sshFile = (StructrSSHFile) path;
if (sshFile.getActualFile() == null) {
throw new NoSuchFileException("SSH file doesn't exist");
}
BasicFileAttributes attrs = new StructrPosixFileAttributes((StructrSSHFile) path);
return (A) attrs;
}
}
throw new IOException("Unable to read attributes: Path is null");
}
@Override
public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException {
return Collections.EMPTY_MAP;
}
@Override
public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException {
logger.info("Method not implemented yet");
;
}
private AbstractFile create(final Path path) throws IOException {
logger.info("x");
final StructrSSHFile parent = (StructrSSHFile) path.getParent();
AbstractFile newFile = null;
final App app = StructrApp.getInstance(securityContext);
try (final Tx tx = app.tx()) {
final String fileName = path.getFileName().toString();
final Folder parentFolder = (Folder) parent.getActualFile();
newFile = app.create(File.class, new NodeAttribute(AbstractNode.name, fileName), new NodeAttribute(StructrApp.key(AbstractFile.class, "parent"), parentFolder));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
throw new IOException(fex);
}
return newFile;
}
};
}
use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class StructrSchemaMethodPath method newFileChannel.
@Override
public FileChannel newFileChannel(final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
// Possible open options are: CREATE, READ, WRITE, TRUNCATE_EXISTING
// The filesystem code will first try to fetch the attributes of a file, then call this
// method with the CREATE and WRITE OPTIONS (and an optional TRUNCATE_EXISTING)
SchemaMethod method = getSchemaMethodNode();
final boolean read = options.contains(StandardOpenOption.READ);
final boolean create = options.contains(StandardOpenOption.CREATE);
final boolean createNew = options.contains(StandardOpenOption.CREATE_NEW);
final boolean write = options.contains(StandardOpenOption.WRITE);
final boolean truncate = options.contains(StandardOpenOption.TRUNCATE_EXISTING);
final boolean append = options.contains(StandardOpenOption.APPEND);
// creation of a new file requested (=> create a new schema method)
if (create || createNew) {
// if CREATE_NEW, file must not exist, otherwise an error should be thrown
if (createNew && method != null) {
throw new java.nio.file.FileAlreadyExistsException(toString());
}
final App app = StructrApp.getInstance(fs.getSecurityContext());
try (final Tx tx = app.tx()) {
// create a new schema method with an empty source string
method = app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.schemaNode, schemaNode), new NodeAttribute<>(SchemaMethod.virtualFileName, name), new NodeAttribute<>(AbstractNode.name, normalizeFileNameForJavaIdentifier(name)), new NodeAttribute<>(SchemaMethod.source, ""));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
return new StructrPropertyValueChannel(fs.getSecurityContext(), method, SchemaMethod.source, truncate, append);
}
use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class Page method createElement.
static Element createElement(final Page page, final String tag, final boolean suppressException) {
final String elementType = StringUtils.capitalize(tag);
final App app = StructrApp.getInstance(page.getSecurityContext());
String c = Content.class.getSimpleName();
// Avoid creating an (invalid) 'Content' DOMElement
if (elementType == null || c.equals(elementType)) {
logger.warn("Blocked attempt to create a DOMElement of type {}", c);
return null;
}
try {
// final Class entityClass = Class.forName("org.structr.web.entity.html." + elementType);
final Class entityClass = Class.forName("org.structr.web.entity.html." + elementType);
if (entityClass != null) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final DOMElement element = (DOMElement) app.create(entityClass, new NodeAttribute(StructrApp.key(entityClass, "tag"), tag), new NodeAttribute(StructrApp.key(entityClass, "hideOnDetail"), false), new NodeAttribute(StructrApp.key(entityClass, "hideOnIndex"), false));
element.doAdopt(page);
return element;
}
} catch (Throwable t) {
if (!suppressException) {
logger.error("Unable to instantiate element of type " + elementType, t);
}
}
return null;
}
use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class User method onCreateAndModify.
// ----- public static methods -----
public static void onCreateAndModify(final User user, final SecurityContext securityContext) throws FrameworkException {
final PropertyKey skipSecurityRels = StructrApp.key(User.class, "skipSecurityRelationships");
if (user.getProperty(skipSecurityRels).equals(Boolean.TRUE) && !user.isAdmin()) {
throw new FrameworkException(422, "", new SemanticErrorToken(user.getClass().getSimpleName(), skipSecurityRels, "can_only_be_set_for_admin_accounts"));
}
if (Settings.FilesystemEnabled.getValue()) {
final PropertyKey<Folder> homeFolderKey = StructrApp.key(Folder.class, "homeFolderOfUser");
final PropertyKey<Folder> parentKey = StructrApp.key(AbstractFile.class, "parent");
// use superuser context here
final SecurityContext storedContext = user.getSecurityContext();
try {
user.setSecurityContext(SecurityContext.getSuperUserInstance());
Folder homeDir = user.getHomeDirectory();
if (homeDir == null) {
// create home directory
final App app = StructrApp.getInstance();
Folder homeFolder = app.nodeQuery(Folder.class).and(Folder.name, "home").and(parentKey, null).getFirst();
if (homeFolder == null) {
homeFolder = app.create(Folder.class, new NodeAttribute(Folder.name, "home"), new NodeAttribute(Folder.owner, null), new NodeAttribute(Folder.visibleToAuthenticatedUsers, true));
}
app.create(Folder.class, new NodeAttribute(Folder.name, user.getUuid()), new NodeAttribute(Folder.owner, user), new NodeAttribute(Folder.visibleToAuthenticatedUsers, true), new NodeAttribute(parentKey, homeFolder), new NodeAttribute(homeFolderKey, user));
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
// restore previous context
user.setSecurityContext(storedContext);
}
}
}
Aggregations