Search in sources :

Example 86 with SecurityContext

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

the class IncludeFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!(arrayHasMinLengthAndAllElementsNotNull(sources, 1) && sources[0] instanceof String)) {
            return null;
        }
        final PropertyKey<DOMNode> sharedCompKey = StructrApp.key(DOMNode.class, "sharedComponent");
        final SecurityContext securityContext = ctx.getSecurityContext();
        final App app = StructrApp.getInstance(securityContext);
        final RenderContext innerCtx = new RenderContext((RenderContext) ctx);
        final List<DOMNode> nodeList = app.nodeQuery(DOMNode.class).andName((String) sources[0]).getAsList();
        DOMNode node = null;
        /**
         * Nodes can be included via their name property These nodes MUST: 1. be unique in name 2. NOT be in the trash => have an ownerDocument AND a parent (public
         * users are not allowed to see the __ShadowDocument__ ==> this check must either be made in a superuser-context OR the __ShadowDocument could be made public?)
         *
         * These nodes can be: 1. somewhere in the pages tree 2. in the shared components 3. both ==> causes a problem because we now have multiple nodes with the same
         * name (one shared component and multiple linking instances of that component)
         *
         * INFOS:
         *
         * - If a DOMNode has "syncedNodes" it MUST BE a shared component - If a DOMNodes "sharedComponent" is set it MUST BE AN INSTANCE of a shared component => Can
         * we safely ignore these? I THINK SO!
         */
        for (final DOMNode n : nodeList) {
            if (n.inTrash()) {
                continue;
            }
            // IGNORE everything that REFERENCES a shared component!
            if (n.getProperty(sharedCompKey) == null) {
                // the DOMNode is either a shared component OR a named node in the pages tree
                if (node == null) {
                    node = n;
                } else {
                    // TODO: Do we need to remove the nodes from the nodeList which can be ignored? (references to a shared component)
                    return "Ambiguous node name \"" + ((String) sources[0]) + "\" (nodes found: " + StringUtils.join(nodeList, ", ") + ")";
                }
            }
        }
        if (node != null) {
            if (sources.length == 3 && sources[1] instanceof Iterable && sources[2] instanceof String) {
                final Iterable<GraphObject> iterable = FunctionDataSource.map((Iterable) sources[1]);
                final String dataKey = (String) sources[2];
                innerCtx.setListSource(iterable);
                node.renderNodeList(securityContext, innerCtx, 0, dataKey);
            } else {
                node.render(innerCtx, 0);
            }
            if (innerCtx.appLibRendered()) {
                ((RenderContext) ctx).setAppLibRendered(true);
            }
        } else {
            final File file = app.nodeQuery(File.class).andName((String) sources[0]).getFirst();
            if (file != null) {
                final String name = file.getProperty(NodeInterface.name);
                final String contentType = file.getContentType();
                final String charset = StringUtils.substringAfterLast(contentType, "charset=");
                final String extension = StringUtils.substringAfterLast(name, ".");
                if (contentType == null || StringUtils.isBlank(extension)) {
                    logger.warn("No valid file type detected. Please make sure {} has a valid content type set or file extension. Parameters: {}", new Object[] { name, getParametersAsString(sources) });
                    return "No valid file type detected. Please make sure " + name + " has a valid content type set or file extension.";
                }
                if (contentType.startsWith("text/css")) {
                    return "<link href=\"" + file.getPath() + "\" rel=\"stylesheet\">";
                } else if (contentType.contains("/javascript")) {
                    return "<script src=\"" + file.getPath() + "\"></script>";
                } else if (contentType.startsWith("image/svg")) {
                    try (final InputStream is = file.getInputStream()) {
                        final byte[] buffer = new byte[file.getSize().intValue()];
                        IOUtils.read(is, buffer);
                        return StringUtils.toEncodedString(buffer, Charset.forName(charset));
                    } catch (IOException ex) {
                        logger.warn("Exception for parameters: {}", getParametersAsString(sources));
                        logger.error("", ex);
                    }
                    return "<img alt=\"" + name + "\" src=\"" + file.getPath() + "\">";
                } else if (contentType.startsWith("image/")) {
                    return "<img alt=\"" + name + "\" src=\"" + file.getPath() + "\">";
                } else {
                    logger.warn("Don't know how to render content type or extension of {}. Parameters: {}", new Object[] { name, getParametersAsString(sources) });
                    return "Don't know how to render content type or extension of  " + name + ".";
                }
            }
        }
        return StringUtils.join(innerCtx.getBuffer().getQueue(), "");
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) RenderContext(org.structr.web.common.RenderContext) InputStream(java.io.InputStream) IOException(java.io.IOException) GraphObject(org.structr.core.GraphObject) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) DOMNode(org.structr.web.entity.dom.DOMNode) File(org.structr.web.entity.File)

Example 87 with SecurityContext

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

the class SetResponseHeaderFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
    if (sources != null && sources.length == 2) {
        final String name = sources[0].toString();
        final String value = sources[1].toString();
        final SecurityContext securityContext = ctx.getSecurityContext();
        if (securityContext != null) {
            final HttpServletResponse response = securityContext.getResponse();
            if (response != null) {
                response.addHeader(name, value);
            }
        }
        return "";
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : SecurityContext(org.structr.common.SecurityContext) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 88 with SecurityContext

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

the class AnalyzeJavaFunction 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;
        }
        try {
            final SecurityContext securityContext = ctx.getSecurityContext();
            final App app = StructrApp.getInstance(securityContext);
            // Analyze string as Java code
            new JavaParserModule(app).analyzeMethodsInJavaFile((String) sources[0]);
            return "";
        } catch (final ParseProblemException ex) {
            logException(caller, ex, sources);
        }
    } 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) ParseProblemException(com.github.javaparser.ParseProblemException)

Example 89 with SecurityContext

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

the class IndexSourceTreeFunction 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().indexSourceTree(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 90 with SecurityContext

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

the class JavaParserModule method analyzeSourceTree.

/**
 * Analyze the source tree under the given root folder.
 *
 * @param rootFolder
 */
public void analyzeSourceTree(final Folder rootFolder) {
    logger.info("Starting analysis 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);
    analyzeFolder(rootFolder, 0, null);
    logger.info("Done with analysis 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)

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