Search in sources :

Example 91 with SecurityContext

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

the class JavaParserModule method indexSourceTree.

/**
 * Create an index containing all compilation units of Java files from
 * the source tree under the given root folder.
 *
 * @param rootFolder
 */
public void indexSourceTree(final Folder rootFolder) {
    logger.info("Starting indexing of source tree " + rootFolder.getPath());
    final SecurityContext securityContext = rootFolder.getSecurityContext();
    app = StructrApp.getInstance(securityContext);
    structrTypeSolver.parseRoot(rootFolder);
    final CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new ReflectionTypeSolver());
    typeSolver.add(structrTypeSolver);
    facade = JavaParserFacade.get(typeSolver);
    logger.info("Done with indexing of source tree " + rootFolder.getPath());
}
Also used : SecurityContext(org.structr.common.SecurityContext) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)

Example 92 with SecurityContext

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

the class JavaParserModule method parseSourceTree.

/**
 * Parse the source tree under the given root folder.
 *
 * @param rootFolder
 */
public void parseSourceTree(final Folder rootFolder) {
    logger.info("Starting parsing of source tree " + rootFolder.getPath());
    final SecurityContext securityContext = rootFolder.getSecurityContext();
    app = StructrApp.getInstance(securityContext);
    final CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new ReflectionTypeSolver());
    typeSolver.add(structrTypeSolver);
    facade = JavaParserFacade.get(typeSolver);
    parseFolder(rootFolder, 0, null);
    logger.info("Done with parsing of source tree " + rootFolder.getPath());
}
Also used : SecurityContext(org.structr.common.SecurityContext) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)

Example 93 with SecurityContext

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

the class ParseSourceTreeFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!(arrayHasLengthAndAllElementsNotNull(sources, 1) && sources[0] instanceof String)) {
            return null;
        }
        final SecurityContext securityContext = ctx.getSecurityContext();
        final App app = StructrApp.getInstance(securityContext);
        new JavaParserModule().parseSourceTree(app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), (String) sources[0]).getFirst());
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return "";
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) SecurityContext(org.structr.common.SecurityContext) Folder(org.structr.web.entity.Folder)

Example 94 with SecurityContext

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

the class VideoFile method getMetadata.

static RestMethodResult getMetadata(final VideoFile thisVideo) throws FrameworkException {
    final SecurityContext securityContext = thisVideo.getSecurityContext();
    final Map<String, String> metadata = AVConv.newInstance(securityContext, thisVideo).getMetadata();
    final RestMethodResult result = new RestMethodResult(200);
    final GraphObjectMap map = new GraphObjectMap();
    for (final Entry<String, String> entry : metadata.entrySet()) {
        map.setProperty(new StringProperty(entry.getKey()), entry.getValue());
    }
    result.addContent(map);
    return result;
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) StringProperty(org.structr.core.property.StringProperty) RestMethodResult(org.structr.rest.RestMethodResult)

Example 95 with SecurityContext

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

the class GraphQLServlet method handleGraphQLRequest.

// ----- private methods -----
private void handleGraphQLRequest(final HttpServletRequest request, final HttpServletResponse response, final String query) throws IOException, FrameworkException {
    final SecurityContext securityContext;
    final Authenticator authenticator;
    try {
        // isolate request authentication in a transaction
        try (final Tx tx = StructrApp.getInstance().tx()) {
            authenticator = config.getAuthenticator();
            securityContext = authenticator.initializeAndExamineRequest(request, response);
            tx.success();
        }
        final App app = StructrApp.getInstance(securityContext);
        if (securityContext != null) {
            // isolate write output
            try (final Tx tx = app.tx()) {
                final Document doc = GraphQLRequest.parse(new Parser(), query);
                if (doc != null) {
                    final List<ValidationError> errors = new Validator().validateDocument(SchemaService.getGraphQLSchema(), doc);
                    if (errors.isEmpty()) {
                        // no validation errors in query, do request
                        final GraphQLWriter graphQLWriter = new GraphQLWriter(true);
                        // no trailing semicolon so we dont trip MimeTypes.getContentTypeWithoutCharset
                        response.setContentType("application/json; charset=utf-8");
                        final Writer writer = response.getWriter();
                        graphQLWriter.stream(securityContext, writer, new GraphQLRequest(securityContext, doc, query));
                        // useful newline
                        writer.append("\n");
                    } else {
                        final Map<String, Object> map = new LinkedHashMap<>();
                        final Writer writer = response.getWriter();
                        final Gson gson = getGson();
                        map.put("errors", errors);
                        gson.toJson(map, writer);
                        // useful newline
                        writer.append("\n");
                        // send 422 status
                        response.setStatus(422);
                    }
                }
                tx.success();
            }
        }
    } catch (FrameworkException frameworkException) {
        // set status & write JSON output
        response.setStatus(frameworkException.getStatus());
        getGson().toJson(frameworkException, response.getWriter());
        response.getWriter().println();
    } catch (IllegalStateException | IllegalArgumentException iex) {
        final Map<String, Object> map = new LinkedHashMap<>();
        map.put("code", 422);
        map.put("message", iex.getMessage());
        // set status & write JSON output
        response.setStatus(422);
        getGson().toJson(map, response.getWriter());
        response.getWriter().println();
    } catch (UnsupportedOperationException uoe) {
        logger.warn("POST not supported");
        int code = HttpServletResponse.SC_BAD_REQUEST;
        response.setStatus(code);
        response.getWriter().append(RestMethodResult.jsonError(code, "POST not supported: " + uoe.getMessage()));
    } catch (Throwable t) {
        logger.warn("Exception in POST", t);
        int code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        response.setStatus(code);
        response.getWriter().append(RestMethodResult.jsonError(code, "JsonSyntaxException in POST: " + t.getMessage()));
    } finally {
        try {
            // response.getWriter().flush();
            response.getWriter().close();
        } catch (Throwable t) {
            logger.warn("Unable to flush and close response: {}", t.getMessage());
        }
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Gson(com.google.gson.Gson) Document(graphql.language.Document) LinkedHashMap(java.util.LinkedHashMap) GraphQLRequest(org.structr.core.graphql.GraphQLRequest) GraphQLWriter(org.structr.rest.serialization.GraphQLWriter) ValidationError(graphql.validation.ValidationError) Authenticator(org.structr.core.auth.Authenticator) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Parser(graphql.parser.Parser) SecurityContext(org.structr.common.SecurityContext) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Validator(graphql.validation.Validator) Writer(java.io.Writer) GraphQLWriter(org.structr.rest.serialization.GraphQLWriter)

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