Search in sources :

Example 56 with SecurityContext

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

the class GetByTypeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String rawType = (String) webSocketData.getNodeData().get("type");
    final String properties = (String) webSocketData.getNodeData().get("properties");
    final boolean includeDeletedAndHidden = (Boolean) webSocketData.getNodeData().get("includeDeletedAndHidden");
    final 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 Query query = StructrApp.getInstance(securityContext).nodeQuery(type).includeDeletedAndHidden(includeDeletedAndHidden);
    if (sortKey != null) {
        final PropertyKey sortProperty = StructrApp.key(type, sortKey);
        if (sortProperty != null) {
            query.sort(sortProperty).order("desc".equals(sortOrder));
        }
    }
    // for image lists, suppress thumbnails
    if (type.equals(Image.class)) {
        query.and(StructrApp.key(Image.class, "isThumbnail"), false);
    }
    try {
        // do search
        Result result = query.getResult();
        // save raw result count
        int resultCountBeforePaging = result.size();
        // set full result list
        webSocketData.setResult(PagingHelper.subList(result.getResults(), pageSize, page));
        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) SecurityContext(org.structr.common.SecurityContext) Image(org.structr.web.entity.Image) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result)

Example 57 with SecurityContext

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

the class QueryCommand 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) nodeData.get("properties");
    final Boolean exact = (Boolean) nodeData.get("exact");
    final Class type = SchemaHelper.getEntityClassForRawType(rawType);
    if (type == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Type " + rawType + " not found").build(), true);
        return;
    }
    final String sortKey = webSocketData.getSortKey();
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();
    final Query query = StructrApp.getInstance(securityContext).nodeQuery(type).page(page).pageSize(pageSize);
    if (sortKey != null) {
        final PropertyKey sortProperty = StructrApp.key(type, sortKey);
        final String sortOrder = webSocketData.getSortOrder();
        query.sort(sortProperty).order("desc".equals(sortOrder));
    }
    if (properties != null) {
        try {
            final Gson gson = new GsonBuilder().create();
            final Map<String, Object> querySource = gson.fromJson(properties, new TypeToken<Map<String, Object>>() {
            }.getType());
            final PropertyMap queryMap = PropertyMap.inputTypeToJavaType(securityContext, type, querySource);
            final boolean inexactQuery = exact != null && exact == false;
            // add properties to query
            for (final Entry<PropertyKey, Object> entry : queryMap.entrySet()) {
                query.and(entry.getKey(), entry.getValue(), !inexactQuery);
            }
        } catch (FrameworkException fex) {
            logger.warn("Exception occured", fex);
            getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
            return;
        }
    }
    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) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Result(org.structr.core.Result) PropertyMap(org.structr.core.property.PropertyMap) TypeToken(com.google.gson.reflect.TypeToken) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Example 58 with SecurityContext

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

the class SaveLocalStorageCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String localStorageString = (String) nodeData.get("localStorageString");
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    if (StringUtils.isNotBlank(localStorageString)) {
        try {
            final User me = (User) securityContext.getUser(false);
            me.setLocalStorage(localStorageString);
            TransactionCommand.registerNodeCallback(me, callback);
        } catch (Throwable t) {
            logger.warn("Error saving localstorage", t);
            // send exception
            getWebSocket().send(MessageBuilder.status().code(422).message(t.toString()).build(), true);
        }
    } else {
        // send exception
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot save localStorage").build(), true);
    }
}
Also used : User(org.structr.web.entity.User) SecurityContext(org.structr.common.SecurityContext)

Example 59 with SecurityContext

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

the class GetCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String nodeId = (String) webSocketData.getNodeData().get("nodeId");
    final String properties = (String) webSocketData.getNodeData().get("properties");
    if (properties != null) {
        securityContext.setCustomView(StringUtils.split(properties, ","));
    }
    final GraphObject graphObject = getGraphObject(webSocketData.getId(), nodeId);
    if (graphObject != null) {
        webSocketData.setResult(Arrays.asList(graphObject));
        // send only over local connection (no broadcast)
        getWebSocket().send(webSocketData, true);
    } else {
    // logger.warn("Node not found for id {}!", webSocketData.getId());
    // Not necessary to send a 404 here
    // getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
}
Also used : SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject)

Example 60 with SecurityContext

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

the class GetLocalStorageCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    try {
        webSocketData.setNodeData("localStorageString", ((User) securityContext.getUser(false)).getLocalStorage());
        // send only over local connection (no broadcast)
        getWebSocket().send(webSocketData, true);
    } catch (Throwable t) {
        logger.warn("Error retrieving localstorage", t);
        // send exception
        getWebSocket().send(MessageBuilder.status().code(422).message(t.toString()).build(), true);
    }
}
Also used : SecurityContext(org.structr.common.SecurityContext)

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