Search in sources :

Example 41 with App

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

the class StructrSSHFile method findFile.

public StructrSSHFile findFile(final String path) {
    if ("/".equals(path)) {
        return getRootFolder();
    }
    final App app = StructrApp.getInstance(getRootFolder().getSecurityContext());
    final boolean isAbsolute = path.startsWith("/");
    final String localPath = isAbsolute ? path.substring(1) : path;
    final String[] parts = localPath.split("[/]+");
    final String localPart = parts[0];
    if (".".equals(path)) {
        return this;
    }
    if ("..".equals(path)) {
        return parent;
    }
    if (isAbsolute && parent != null) {
        return getRootFolder().findFile(path);
    } else {
        // look for files/folders without parent
        try (final Tx tx = app.tx()) {
            for (final Folder folder : getFolders()) {
                final String folderName = folder.getName();
                if (localPart.equals(folderName)) {
                    final StructrSSHFile matchingFolder = new StructrSSHFile(this, folderName, folder);
                    if (parts.length > 1) {
                        return matchingFolder.findFile(localPath.substring(folderName.length() + 1));
                    } else {
                        // match found
                        return matchingFolder;
                    }
                }
            }
            for (final File file : getFiles()) {
                final String fileName = file.getName();
                if (localPart.equals(fileName)) {
                    if (parts.length > 1) {
                        throw new IllegalStateException("Found file where folder was expected, aborting.");
                    }
                    return new StructrSSHFile(this, fileName, file);
                }
            }
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("", fex);
        }
    }
    return new StructrSSHFile(this, path, null);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 42 with App

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

the class StructrShellCommand method start.

@Override
public void start(final Environment env) throws IOException {
    env.addSignalListener(this);
    final String userName = env.getEnv().get("USER");
    if (userName != null) {
        final App app = StructrApp.getInstance();
        try (final Tx tx = app.tx()) {
            user = app.nodeQuery(User.class).andName(userName).getFirst();
            if (user != null) {
                // set home directory first
                if (Settings.FilesystemEnabled.getValue()) {
                    currentFolder = user.getHomeDirectory();
                }
            }
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("", fex);
        }
    } else {
        logger.warn("Cannot start Structr shell, no username set!");
        return;
    }
    if (isInteractive()) {
        // abort if no user was found
        if (user == null) {
            logger.warn("Cannot start Structr shell, user not found for name {}!", userName);
            return;
        }
        final String terminalType = env.getEnv().get("TERM");
        if (terminalType != null) {
            switch(terminalType) {
                case "xterm":
                case "vt100":
                case "vt220":
                    term = new XTermTerminalEmulator(in, out, this);
                    break;
                default:
                    logger.warn("Unsupported terminal type {}, aborting.", terminalType);
                    break;
            }
            logger.warn("No terminal type provided, aborting.", terminalType);
        }
        if (term != null) {
            term.start();
            term.print("Welcome to ");
            term.setBold(true);
            term.print("Structr");
            term.print(" 2.0");
            term.setBold(false);
            term.println();
            // display first prompt
            displayPrompt();
        } else {
            callback.onExit(1);
        }
    } else {
        // create terminal emulation
        term = new XTermTerminalEmulator(in, out, this);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException)

Example 43 with App

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

the class StructrShellCommand method getPrompt.

public String getPrompt() {
    final App app = StructrApp.getInstance();
    final StringBuilder buf = new StringBuilder();
    try (final Tx tx = app.tx()) {
        buf.append(user.getName());
        buf.append("@structr:");
        if (currentFolder != null) {
            String folderPart = currentFolder.getPath();
            final Folder homeFolder = user.getHomeDirectory();
            if (homeFolder != null) {
                // replace home directory with ~ if at the beginning of the full path
                final String homeFolderPath = homeFolder.getPath();
                if (folderPart.startsWith(homeFolderPath)) {
                    folderPart = "~" + folderPart.substring(homeFolderPath.length());
                }
            }
            buf.append(folderPart);
        } else {
            buf.append("/");
        }
        buf.append(user.isAdmin() ? "#" : "$");
        buf.append(" ");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    return buf.toString();
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder)

Example 44 with App

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

the class StructrDataFeedsModuleTest method createTestNode.

protected <T extends AbstractNode> T createTestNode(final Class<T> type, final PropertyMap props, final Principal owner) throws FrameworkException {
    final App backendApp = StructrApp.getInstance(SecurityContext.getInstance(owner, AccessMode.Backend));
    try (final Tx tx = backendApp.tx()) {
        final T result = backendApp.create(type, props);
        tx.success();
        return result;
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx)

Example 45 with App

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

the class UpdateFeedAgent method processTask.

@Override
public ReturnValue processTask(final Task<T> task) throws Throwable {
    logger.debug("Processing task {}", task.getClass().getName());
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx(true, true, false)) {
        for (DataFeed feed : task.getWorkObjects()) {
            logger.debug("Updating data feed {} if due", feed.getProperty(DataFeed.name));
            feed.updateIfDue();
        }
        tx.success();
    }
    return ReturnValue.Success;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) DataFeed(org.structr.feed.entity.DataFeed)

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