Search in sources :

Example 86 with App

use of org.structr.core.app.App in project structr by structr.

the class SchemaJsonResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    if (propertySet != null && propertySet.containsKey("schema")) {
        final App app = StructrApp.getInstance(securityContext);
        String schemaJson = (String) propertySet.get("schema");
        try {
            StructrSchema.replaceDatabaseSchema(app, StructrSchema.createFromSource(schemaJson));
        } catch (URISyntaxException | InvalidSchemaException ex) {
            logger.error("Error while importing JsonSchema: " + ex.getMessage());
        }
        return new RestMethodResult(200, "Schema import started");
    }
    return new RestMethodResult(400, "Invalid request body. Specify schema json string as 'schema' in request body.");
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) URISyntaxException(java.net.URISyntaxException) RestMethodResult(org.structr.rest.RestMethodResult)

Example 87 with App

use of org.structr.core.app.App in project structr by structr.

the class SchemaMethodResource method doPost.

@Override
public RestMethodResult doPost(final Map<String, Object> propertySet) throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    RestMethodResult result = null;
    if (source != null) {
        try (final Tx tx = app.tx()) {
            result = SchemaMethodResource.invoke(securityContext, null, source, propertySet, methodResource.getUriPart());
            tx.success();
        }
    }
    if (result == null) {
        throw new IllegalPathException("Type and method name do not match the given path.");
    }
    return result;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) IllegalPathException(org.structr.rest.exception.IllegalPathException) Tx(org.structr.core.graph.Tx) RestMethodResult(org.structr.rest.RestMethodResult)

Example 88 with App

use of org.structr.core.app.App in project structr by structr.

the class GlobalSchemaMethodResource method checkAndConfigure.

@Override
public boolean checkAndConfigure(final String part, final SecurityContext securityContext, final HttpServletRequest request) {
    this.securityContext = securityContext;
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        final List<SchemaMethod> methods = app.nodeQuery(SchemaMethod.class).andName(part).and(SchemaMethod.schemaNode, null).getAsList();
        tx.success();
        if (!methods.isEmpty()) {
            this.methodName = part;
            return true;
        }
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    return false;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException)

Example 89 with App

use of org.structr.core.app.App in project structr by structr.

the class LogResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    final HttpServletRequest request = securityContext.getRequest();
    if (request != null) {
        // initialize?!
        if ("true".equals(request.getParameter("initialize"))) {
            final String filesPath = Settings.FilesPath.getValue();
            try (final Context context = new Context(1000)) {
                collectFilesAndStore(context, new File(filesPath + SUBJECTS).toPath(), 0);
            } catch (FrameworkException fex) {
                logger.warn("", fex);
            }
            return new RestMethodResult(200);
        }
        final String subjectId = (String) propertySet.get(subjectProperty.jsonName());
        final String objectId = (String) propertySet.get(objectProperty.jsonName());
        final String action = (String) propertySet.get(actionProperty.jsonName());
        final String message = (String) propertySet.get(messageProperty.jsonName());
        if (subjectId != null && objectId != null && action != null) {
            final App app = StructrApp.getInstance(securityContext);
            LogEvent event = null;
            try (final Tx tx = app.tx()) {
                final PropertyMap properties = new PropertyMap();
                properties.put(LogEvent.timestampProperty, new Date());
                properties.put(LogEvent.actionProperty, action);
                properties.put(LogEvent.subjectProperty, subjectId);
                properties.put(LogEvent.objectProperty, objectId);
                properties.put(LogEvent.messageProperty, message);
                properties.put(LogEvent.visibleToPublicUsers, true);
                properties.put(LogEvent.visibleToAuthenticatedUsers, true);
                event = app.create(LogEvent.class, properties);
                tx.success();
            }
            final RestMethodResult result = new RestMethodResult(201);
            result.addContent(event);
            return result;
        } else {
            final ErrorBuffer errorBuffer = new ErrorBuffer();
            if (StringUtils.isEmpty(subjectId)) {
                errorBuffer.add(new EmptyPropertyToken("LogFile", subjectProperty));
            }
            if (StringUtils.isEmpty(objectId)) {
                errorBuffer.add(new EmptyPropertyToken("LogFile", objectProperty));
            }
            if (StringUtils.isEmpty(action)) {
                errorBuffer.add(new EmptyPropertyToken("LogFile", actionProperty));
            }
            throw new FrameworkException(422, "Log entry must consist of at least subjectId, objectId and action", errorBuffer);
        }
    }
    // no request object, this is fatal
    throw new FrameworkException(500, "No request object present, aborting.");
}
Also used : SecurityContext(org.structr.common.SecurityContext) App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken) FrameworkException(org.structr.common.error.FrameworkException) Tx(org.structr.core.graph.Tx) LogEvent(org.structr.rest.logging.entity.LogEvent) Date(java.util.Date) HttpServletRequest(javax.servlet.http.HttpServletRequest) PropertyMap(org.structr.core.property.PropertyMap) ErrorBuffer(org.structr.common.error.ErrorBuffer) File(java.io.File) RestMethodResult(org.structr.rest.RestMethodResult)

Example 90 with App

use of org.structr.core.app.App in project structr by structr.

the class SnapshotCommand method addSnapshot.

private void addSnapshot(final String fileName) throws FrameworkException {
    final App app = StructrApp.getInstance();
    // isolate write output
    try (final Tx tx = app.tx()) {
        if (fileName != null) {
            final File snapshotFile = locateFile(fileName, false);
            try (final Reader reader = new FileReader(snapshotFile)) {
                final JsonSchema schema = StructrSchema.createFromSource(reader);
                StructrSchema.extendDatabaseSchema(app, schema);
            } catch (InvalidSchemaException iex) {
                throw new FrameworkException(422, iex.getMessage());
            }
        } else {
            throw new FrameworkException(422, "Please supply schema snapshot name to add to database.");
        }
        tx.success();
    } catch (IOException | URISyntaxException ioex) {
        logger.warn("", ioex);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) JsonSchema(org.structr.schema.json.JsonSchema) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File)

Aggregations

App (org.structr.core.app.App)296 StructrApp (org.structr.core.app.StructrApp)294 Tx (org.structr.core.graph.Tx)201 FrameworkException (org.structr.common.error.FrameworkException)176 LinkedList (java.util.LinkedList)60 SecurityContext (org.structr.common.SecurityContext)56 PropertyMap (org.structr.core.property.PropertyMap)41 Folder (org.structr.web.entity.Folder)38 GraphObject (org.structr.core.GraphObject)35 Principal (org.structr.core.entity.Principal)31 IOException (java.io.IOException)30 AbstractFile (org.structr.web.entity.AbstractFile)27 AbstractNode (org.structr.core.entity.AbstractNode)26 Test (org.junit.Test)24 NodeAttribute (org.structr.core.graph.NodeAttribute)24 File (org.structr.web.entity.File)23 NodeInterface (org.structr.core.graph.NodeInterface)22 SchemaNode (org.structr.core.entity.SchemaNode)19 PropertyKey (org.structr.core.property.PropertyKey)17 Map (java.util.Map)16