Search in sources :

Example 46 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class ListActiveElementsCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    final String id = webSocketData.getId();
    try (final Tx tx = app.tx()) {
        final Page page = app.get(Page.class, id);
        final List<GraphObject> result = new LinkedList<>();
        if (page != null) {
            collectActiveElements(result, page, Collections.EMPTY_SET, null, 0);
            // set full result list
            webSocketData.setResult(result);
            webSocketData.setRawResultCount(result.size());
            // send only over local connection
            getWebSocket().send(webSocketData, true);
        } else {
            getWebSocket().send(MessageBuilder.status().code(404).message("Page with ID " + id + " not found.").build(), true);
        }
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) Page(org.structr.web.entity.dom.Page) GraphObject(org.structr.core.GraphObject)

Example 47 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class ListCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String rawType = (String) nodeData.get("type");
    final String properties = (String) webSocketData.getNodeData().get("properties");
    final boolean rootOnly = Boolean.TRUE.equals((Boolean) nodeData.get("rootOnly"));
    Class type = SchemaHelper.getEntityClassForRawType(rawType);
    if (type == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Type " + rawType + " not found").build(), true);
        return;
    }
    if (properties != null) {
        securityContext.setCustomView(StringUtils.split(properties, ","));
    }
    final String sortOrder = webSocketData.getSortOrder();
    final String sortKey = webSocketData.getSortKey();
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();
    final PropertyKey sortProperty = StructrApp.key(type, sortKey);
    final Query query = StructrApp.getInstance(securityContext).nodeQuery(type).sort(sortProperty).order("desc".equals(sortOrder)).page(page).pageSize(pageSize);
    if (File.class.isAssignableFrom(type)) {
        if (rootOnly) {
            query.and(StructrApp.key(File.class, "hasParent"), false);
        }
        // inverted as isThumbnail is not necessarily present in all objects inheriting from FileBase
        query.not().and(StructrApp.key(Image.class, "isThumbnail"), true);
    }
    // important
    if (Folder.class.isAssignableFrom(type) && rootOnly) {
        query.and(StructrApp.key(Folder.class, "hasParent"), false);
    }
    try {
        // do search
        final Result result = query.getResult();
        // save raw result count
        // filteredResults.size();
        int resultCountBeforePaging = result.getRawResultCount();
        // set full result list
        webSocketData.setResult(result.getResults());
        webSocketData.setRawResultCount(resultCountBeforePaging);
        // send only over local connection
        getWebSocket().send(webSocketData, true);
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) Image(org.structr.web.entity.Image) Folder(org.structr.web.entity.Folder) Result(org.structr.core.Result) SecurityContext(org.structr.common.SecurityContext) File(org.structr.web.entity.File) PropertyKey(org.structr.core.property.PropertyKey)

Example 48 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class ListUnattachedNodesCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        // do search
        List<NodeInterface> filteredResults = getUnattachedNodes(app, securityContext, webSocketData);
        // save raw result count
        int resultCountBeforePaging = filteredResults.size();
        // set full result list
        webSocketData.setResult(PagingHelper.subList(filteredResults, pageSize, page));
        webSocketData.setRawResultCount(resultCountBeforePaging);
        // send only over local connection
        getWebSocket().send(webSocketData, true);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) NodeInterface(org.structr.core.graph.NodeInterface)

Example 49 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class ClonePageCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String nodeId = webSocketData.getId();
    final AbstractNode nodeToClone = getNode(nodeId);
    if (nodeToClone != null) {
        try {
            final Page pageToClone = nodeToClone instanceof Page ? (Page) nodeToClone : null;
            if (pageToClone != null) {
                final Page newPage = (Page) pageToClone.cloneNode(false);
                newPage.setProperties(securityContext, new PropertyMap(Page.name, pageToClone.getProperty(Page.name) + "-" + newPage.getIdString()));
                DOMNode firstChild = (DOMNode) pageToClone.getFirstChild().getNextSibling();
                if (firstChild == null) {
                    firstChild = (DOMNode) pageToClone.getFirstChild();
                }
                if (firstChild != null) {
                    final DOMNode newHtmlNode = DOMNode.cloneAndAppendChildren(securityContext, firstChild);
                    newPage.appendChild(newHtmlNode);
                }
            }
        } catch (FrameworkException fex) {
            logger.warn("Could not create node.", fex);
            getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
        } catch (DOMException dex) {
            logger.warn("Could not create node.", dex);
            getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
        }
    } else {
        logger.warn("Node with uuid {} not found.", webSocketData.getId());
        getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) SecurityContext(org.structr.common.SecurityContext) Page(org.structr.web.entity.dom.Page) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 50 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class CreateCommand method processMessage.

// ~--- methods --------------------------------------------------------
@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    Map<String, Object> nodeData = webSocketData.getNodeData();
    try {
        final PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, nodeData);
        Class type = SchemaHelper.getEntityClassForRawType(properties.get(AbstractNode.type));
        final NodeInterface newNode = app.create(type, properties);
        TransactionCommand.registerNodeCallback(newNode, callback);
        // check for File node and store in WebSocket to receive chunks
        if (newNode instanceof File) {
            getWebSocket().createFileUploadHandler((File) newNode);
        }
    } catch (FrameworkException fex) {
        logger.warn("Could not create node.", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

SecurityContext (org.structr.common.SecurityContext)131 FrameworkException (org.structr.common.error.FrameworkException)76 App (org.structr.core.app.App)56 StructrApp (org.structr.core.app.StructrApp)56 Tx (org.structr.core.graph.Tx)36 GraphObject (org.structr.core.GraphObject)35 PropertyKey (org.structr.core.property.PropertyKey)26 PropertyMap (org.structr.core.property.PropertyMap)26 AbstractNode (org.structr.core.entity.AbstractNode)19 IOException (java.io.IOException)18 Map (java.util.Map)17 File (org.structr.web.entity.File)14 LinkedList (java.util.LinkedList)13 DatabaseService (org.structr.api.DatabaseService)12 DOMNode (org.structr.web.entity.dom.DOMNode)12 Result (org.structr.core.Result)11 PropertyConverter (org.structr.core.converter.PropertyConverter)11 GraphObjectMap (org.structr.core.GraphObjectMap)10 Query (org.structr.core.app.Query)10 Principal (org.structr.core.entity.Principal)10