Search in sources :

Example 21 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class LogResource method doOptions.

@Override
public RestMethodResult doOptions() throws FrameworkException {
    final RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_OK);
    result.addHeader("Allow", "GET,POST,OPTIONS");
    return result;
}
Also used : RestMethodResult(org.structr.rest.RestMethodResult)

Example 22 with RestMethodResult

use of org.structr.rest.RestMethodResult 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 23 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class TypeResource method doPost.

@Override
public RestMethodResult doPost(final Map<String, Object> propertySet) throws FrameworkException {
    // virtual type?
    if (virtualType != null) {
        virtualType.transformInput(securityContext, entityClass, propertySet);
    }
    if (isNode) {
        final RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_CREATED);
        final NodeInterface newNode = createNode(propertySet);
        if (newNode != null) {
            result.addHeader("Location", buildLocationHeader(newNode));
            result.addContent(newNode);
        }
        result.serializeAsPrimitiveArray(true);
        // finally: return 201 Created
        return result;
    } else {
        final App app = StructrApp.getInstance(securityContext);
        final Relation template = getRelationshipTemplate();
        final ErrorBuffer errorBuffer = new ErrorBuffer();
        if (template != null) {
            final NodeInterface sourceNode = identifyStartNode(template, propertySet);
            final NodeInterface targetNode = identifyEndNode(template, propertySet);
            final PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, entityClass, propertySet);
            RelationshipInterface newRelationship = null;
            if (sourceNode == null) {
                errorBuffer.add(new EmptyPropertyToken(entityClass.getSimpleName(), template.getSourceIdProperty()));
            }
            if (targetNode == null) {
                errorBuffer.add(new EmptyPropertyToken(entityClass.getSimpleName(), template.getTargetIdProperty()));
            }
            if (errorBuffer.hasError()) {
                throw new FrameworkException(422, "Source node ID and target node ID of relationsips must be set", errorBuffer);
            }
            template.ensureCardinality(securityContext, sourceNode, targetNode);
            newRelationship = app.create(sourceNode, targetNode, entityClass, properties);
            RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_CREATED);
            if (newRelationship != null) {
                result.addHeader("Location", buildLocationHeader(newRelationship));
                result.addContent(newRelationship);
            }
            result.serializeAsPrimitiveArray(true);
            // finally: return 201 Created
            return result;
        }
        // shouldn't happen
        throw new NotFoundException("Type" + rawType + " does not exist");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken) Relation(org.structr.core.entity.Relation) ErrorBuffer(org.structr.common.error.ErrorBuffer) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) RelationshipInterface(org.structr.core.graph.RelationshipInterface) NotFoundException(org.structr.rest.exception.NotFoundException) RestMethodResult(org.structr.rest.RestMethodResult) NodeInterface(org.structr.core.graph.NodeInterface)

Example 24 with RestMethodResult

use of org.structr.rest.RestMethodResult 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)

Example 25 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class EntityResolverResource method doPost.

@Override
public RestMethodResult doPost(final Map<String, Object> propertySet) throws FrameworkException {
    // TODO: fetch nodes with superuser security context, collect forbidden nodes and return
    // in error response
    RestMethodResult result = new RestMethodResult(200);
    for (Object o : propertySet.values()) {
        if (o instanceof String) {
            String id = (String) o;
            AbstractNode node = (AbstractNode) StructrApp.getInstance().getNodeById(id);
            if (node != null) {
                result.addContent(node);
            }
        }
    }
    return result;
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) RestMethodResult(org.structr.rest.RestMethodResult)

Aggregations

RestMethodResult (org.structr.rest.RestMethodResult)27 App (org.structr.core.app.App)13 StructrApp (org.structr.core.app.StructrApp)13 Tx (org.structr.core.graph.Tx)10 FrameworkException (org.structr.common.error.FrameworkException)9 GraphObject (org.structr.core.GraphObject)7 SecurityContext (org.structr.common.SecurityContext)6 Authenticator (org.structr.core.auth.Authenticator)6 Resource (org.structr.rest.resource.Resource)6 JsonParseException (com.google.gson.JsonParseException)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 RetryException (org.structr.api.RetryException)5 IllegalPathException (org.structr.rest.exception.IllegalPathException)5 PropertyMap (org.structr.core.property.PropertyMap)4 NotFoundException (org.structr.rest.exception.NotFoundException)4 StaticRelationshipResource (org.structr.rest.resource.StaticRelationshipResource)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 Principal (org.structr.core.entity.Principal)3 LinkedHashMap (java.util.LinkedHashMap)2