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);
}
}
}
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;
}
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;
}
}
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.");
}
}
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);
}
Aggregations