Search in sources :

Example 1 with NotAllowedException

use of org.structr.rest.exception.NotAllowedException in project structr by structr.

the class CypherQueryResource method doGet.

@Override
public Result doGet(PropertyKey sortKey, boolean sortDescending, int pageSize, int page) throws FrameworkException {
    // Admins only
    if (!securityContext.isSuperUser()) {
        throw new NotAllowedException("Use of the cypher endpoint is restricted to admin users");
    }
    try {
        Object queryObject = securityContext.getRequest().getParameter("query");
        if (queryObject != null) {
            String query = queryObject.toString();
            List<GraphObject> resultList = StructrApp.getInstance(securityContext).command(CypherQueryCommand.class).execute(query, Collections.EMPTY_MAP);
            return new Result(resultList, resultList.size(), true, false);
        }
    } catch (org.structr.api.NotFoundException nfe) {
        throw new NotFoundException("Entity not found for the given query");
    }
    return new Result(Collections.EMPTY_LIST, 0, false, false);
}
Also used : NotAllowedException(org.structr.rest.exception.NotAllowedException) NotFoundException(org.structr.rest.exception.NotFoundException) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) CypherQueryCommand(org.structr.core.graph.CypherQueryCommand) RestMethodResult(org.structr.rest.RestMethodResult) Result(org.structr.core.Result)

Example 2 with NotAllowedException

use of org.structr.rest.exception.NotAllowedException in project structr by structr.

the class MeResource method doGet.

@Override
public Result doGet(PropertyKey sortKey, boolean sortDescending, int pageSize, int page) throws FrameworkException {
    Principal user = securityContext.getUser(true);
    if (user != null) {
        List<GraphObject> resultList = new LinkedList<>();
        resultList.add(user);
        return new Result(resultList, null, isCollectionResource(), isPrimitiveArray());
    } else {
        throw new NotAllowedException("No user");
    }
}
Also used : NotAllowedException(org.structr.rest.exception.NotAllowedException) GraphObject(org.structr.core.GraphObject) Principal(org.structr.core.entity.Principal) LinkedList(java.util.LinkedList) RestMethodResult(org.structr.rest.RestMethodResult) Result(org.structr.core.Result)

Example 3 with NotAllowedException

use of org.structr.rest.exception.NotAllowedException in project structr by structr.

the class MaintenanceResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    if ((securityContext != null) && isSuperUser()) {
        if (this.taskOrCommand != null) {
            try {
                final App app = StructrApp.getInstance(securityContext);
                if (Task.class.isAssignableFrom(taskOrCommand)) {
                    Task task = (Task) taskOrCommand.newInstance();
                    app.processTasks(task);
                } else if (MaintenanceCommand.class.isAssignableFrom(taskOrCommand)) {
                    MaintenanceCommand cmd = (MaintenanceCommand) StructrApp.getInstance(securityContext).command(taskOrCommand);
                    // flush caches if required
                    if (cmd.requiresFlushingOfCaches()) {
                        app.command(FlushCachesCommand.class).execute(Collections.EMPTY_MAP);
                    }
                    // create enclosing transaction if required
                    if (cmd.requiresEnclosingTransaction()) {
                        try (final Tx tx = app.tx()) {
                            cmd.execute(propertySet);
                            tx.success();
                        }
                    } else {
                        cmd.execute(propertySet);
                    }
                    final RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_OK);
                    cmd.getCustomHeaders().forEach((final String headerName, final String headerValue) -> {
                        result.addHeader(headerName, headerValue);
                    });
                    cmd.getCustomHeaders().clear();
                    return result;
                } else {
                    return new RestMethodResult(HttpServletResponse.SC_NOT_FOUND);
                }
                // return 200 OK
                return new RestMethodResult(HttpServletResponse.SC_OK);
            } catch (InstantiationException iex) {
                throw new SystemException(iex.getMessage());
            } catch (IllegalAccessException iaex) {
                throw new SystemException(iaex.getMessage());
            }
        } else {
            if (taskOrCommandName != null) {
                throw new NotFoundException("No such task or command: " + this.taskOrCommandName);
            } else {
                throw new IllegalPathException("Maintenance resource needs parameter");
            }
        }
    } else {
        throw new NotAllowedException("Use of the maintenance endpoint is restricted to admin users");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Task(org.structr.agent.Task) IllegalPathException(org.structr.rest.exception.IllegalPathException) Tx(org.structr.core.graph.Tx) SystemException(org.structr.rest.exception.SystemException) NotAllowedException(org.structr.rest.exception.NotAllowedException) NotFoundException(org.structr.rest.exception.NotFoundException) MaintenanceCommand(org.structr.core.graph.MaintenanceCommand) RestMethodResult(org.structr.rest.RestMethodResult)

Example 4 with NotAllowedException

use of org.structr.rest.exception.NotAllowedException in project structr by structr.

the class CypherQueryResource method doPost.

@Override
public RestMethodResult doPost(final Map<String, Object> propertySet) throws FrameworkException {
    // Admins only
    if (!securityContext.isSuperUser()) {
        throw new NotAllowedException("Use of the cypher endpoint is restricted to admin users");
    }
    try {
        RestMethodResult result = new RestMethodResult(200);
        Object queryObject = propertySet.get("query");
        if (queryObject != null) {
            String query = queryObject.toString();
            List<GraphObject> resultList = StructrApp.getInstance(securityContext).command(CypherQueryCommand.class).execute(query, propertySet);
            for (GraphObject obj : resultList) {
                result.addContent(obj);
            }
        }
        return result;
    } catch (org.structr.api.NotFoundException nfe) {
        throw new NotFoundException("Entity not found for the given query");
    }
}
Also used : NotAllowedException(org.structr.rest.exception.NotAllowedException) NotFoundException(org.structr.rest.exception.NotFoundException) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) RestMethodResult(org.structr.rest.RestMethodResult) CypherQueryCommand(org.structr.core.graph.CypherQueryCommand)

Aggregations

RestMethodResult (org.structr.rest.RestMethodResult)4 NotAllowedException (org.structr.rest.exception.NotAllowedException)4 GraphObject (org.structr.core.GraphObject)3 NotFoundException (org.structr.rest.exception.NotFoundException)3 Result (org.structr.core.Result)2 CypherQueryCommand (org.structr.core.graph.CypherQueryCommand)2 LinkedList (java.util.LinkedList)1 Task (org.structr.agent.Task)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 Principal (org.structr.core.entity.Principal)1 MaintenanceCommand (org.structr.core.graph.MaintenanceCommand)1 Tx (org.structr.core.graph.Tx)1 IllegalPathException (org.structr.rest.exception.IllegalPathException)1 SystemException (org.structr.rest.exception.SystemException)1