Search in sources :

Example 16 with App

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

the class LDAPService method doUpdate.

public void doUpdate() throws IOException, LdapException, CursorException, FrameworkException {
    final LdapConnection connection = new LdapNetworkConnection(host, port, useSsl);
    final App app = StructrApp.getInstance();
    if (connection != null) {
        // make connection persistent
        connection.setTimeOut(0);
        if (connection.connect()) {
            logger.info("Updating user/group information from LDAP server {}:{}..", new Object[] { host, port });
            if (StringUtils.isNotBlank(binddn) && StringUtils.isNotBlank(secret)) {
                connection.bind(binddn, secret);
            } else if (StringUtils.isNotBlank(binddn)) {
                connection.bind(binddn);
            }
            // step 1: fetch / update all users from LDAP server
            final EntryCursor cursor = connection.search(baseDn, filter, SearchScope.valueOf(scope));
            while (cursor.next()) {
                final Entry entry = cursor.get();
                synchronizeUserEntry(connection, entry);
            }
            // step 2: examine local users and refresh / remove
            try (final Tx tx = app.tx()) {
                for (final LDAPUser user : app.nodeQuery(LDAPUser.class).getAsList()) {
                    final String dn = user.getDistinguishedName();
                    if (dn != null) {
                        final Entry userEntry = connection.lookup(dn);
                        if (userEntry != null) {
                            // update user information
                            user.initializeFrom(userEntry);
                        } else {
                            logger.info("User {} doesn't exist in LDAP directory, deleting.", user);
                            app.delete(user);
                        }
                    } else {
                        logger.warn("User {} doesn't have an LDAP distinguished name, ignoring.", user);
                    }
                }
                tx.success();
            }
            cursor.close();
            connection.close();
        } else {
            logger.info("Connection to LDAP server {} failed", host);
        }
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Entry(org.apache.directory.api.ldap.model.entry.Entry) Tx(org.structr.core.graph.Tx) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 17 with App

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

the class LDAPService method synchronizeUserEntry.

// ----- private methods -----
private String synchronizeUserEntry(final LdapConnection connection, final Entry entry) {
    final PropertyKey<String> dnKey = StructrApp.key(LDAPUser.class, "distinguishedName");
    final App app = StructrApp.getInstance();
    final Dn dn = entry.getDn();
    final String dnString = dn.toString();
    try (final Tx tx = app.tx()) {
        LDAPUser user = app.nodeQuery(LDAPUser.class).and(dnKey, dnString).getFirst();
        if (user == null) {
            user = app.create(LDAPUser.class, new NodeAttribute(dnKey, dnString));
            user.initializeFrom(entry);
            final String uuid = user.getUuid();
            if (user.getEntryUuid() == null) {
                try {
                    // try to set "our" UUID in the remote database
                    final Modification addUuid = new DefaultModification(ModificationOperation.ADD_ATTRIBUTE, "entryUUID", normalizeUUID(uuid));
                    connection.modify(dn, addUuid);
                } catch (LdapException ex) {
                    logger.warn("Unable to set entryUUID: {}", ex.getMessage());
                }
            }
        }
        tx.success();
        return user.getUuid();
    } catch (FrameworkException | LdapInvalidAttributeValueException fex) {
        logger.warn("Unable to update LDAP information", fex);
    }
    return null;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NodeAttribute(org.structr.core.graph.NodeAttribute) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Dn(org.apache.directory.api.ldap.model.name.Dn) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 18 with App

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

the class MQTTClient method getTopics.

static String[] getTopics(MQTTClient thisClient) {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        List<MessageSubscriber> subs = thisClient.getSubscribers();
        String[] topics = new String[subs.size()];
        for (int i = 0; i < subs.size(); i++) {
            topics[i] = subs.get(i).getTopic();
        }
        return topics;
    } catch (FrameworkException ex) {
        logger.error("Couldn't retrieve client topics for MQTT subscription.");
        return null;
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) MessageSubscriber(org.structr.messaging.engine.entities.MessageSubscriber) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException)

Example 19 with App

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

the class MQTTClient method connectionStatusCallback.

static void connectionStatusCallback(MQTTClient thisClient, boolean connected) {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        thisClient.setIsConnected(connected);
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("Error in connection status callback for MQTTClient.");
    }
}
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)

Example 20 with App

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

the class MessageClient method sendMessage.

static RestMethodResult sendMessage(MessageClient thisClient, final String topic, final String message) throws FrameworkException {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        List<MessageSubscriber> subscribers = thisClient.getSubscribers();
        if (subscribers != null) {
            subscribers.forEach(sub -> {
                String subTopic = sub.getProperty(StructrApp.key(MessageSubscriber.class, "topic"));
                if (subTopic != null && (subTopic.equals(topic) || subTopic.equals("*"))) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("topic", topic);
                    params.put("message", message);
                    try {
                        sub.invokeMethod("onMessage", params, false);
                    } catch (FrameworkException e) {
                        logger.warn("Could not invoke 'onMessage' method on MessageSubscriber: " + e.getMessage());
                    }
                }
            });
        }
        tx.success();
    }
    return new RestMethodResult(200);
}
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) HashMap(java.util.HashMap) RestMethodResult(org.structr.rest.RestMethodResult)

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