use of org.apache.directory.api.ldap.model.name.Dn in project ldapchai by ldapchai.
the class ApacheLdapProviderImpl method writeBinaryAttribute.
public void writeBinaryAttribute(final String entryDN, final String attributeName, final byte[][] values, final boolean overwrite) throws ChaiUnavailableException, ChaiOperationException {
activityPreCheck();
getInputValidator().writeBinaryAttribute(entryDN, attributeName, values, overwrite);
try {
final ModifyRequest modifyRequest = new ModifyRequestImpl();
modifyRequest.setName(new Dn(entryDN));
{
final Modification modification = new DefaultModification();
modification.setOperation(overwrite ? ModificationOperation.REPLACE_ATTRIBUTE : ModificationOperation.ADD_ATTRIBUTE);
modification.setAttribute(new DefaultAttribute(attributeName, values));
modifyRequest.addModification(modification);
}
final ModifyResponse response = connection.modify(modifyRequest);
processResponse(response);
} catch (LdapException e) {
throw ChaiOperationException.forErrorMessage(e.getMessage());
}
}
use of org.apache.directory.api.ldap.model.name.Dn in project ldapchai by ldapchai.
the class ApacheLdapProviderImpl method writeStringAttribute.
public void writeStringAttribute(final String entryDN, final String attributeName, final Set<String> values, final boolean overwrite) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
activityPreCheck();
getInputValidator().writeStringAttribute(entryDN, attributeName, values, overwrite);
try {
final ModifyRequest modifyRequest = new ModifyRequestImpl();
modifyRequest.setName(new Dn(entryDN));
{
final Modification modification = new DefaultModification();
modification.setOperation(overwrite ? ModificationOperation.REPLACE_ATTRIBUTE : ModificationOperation.ADD_ATTRIBUTE);
modification.setAttribute(new DefaultAttribute(attributeName, values.toArray(new String[values.size()])));
modifyRequest.addModification(modification);
}
final ModifyResponse response = connection.modify(modifyRequest);
processResponse(response);
} catch (LdapException e) {
throw ChaiOperationException.forErrorMessage(e.getMessage());
}
}
use of org.apache.directory.api.ldap.model.name.Dn in project jackrabbit-oak by apache.
the class LdapIdentityProvider method getDeclaredGroupRefs.
// -----------------------------------------------------------< internal >---
/**
* Collects the declared (direct) groups of an identity
* @param ref reference to the identity
* @return map of identities where the key is the DN of the LDAP entity
*/
Map<String, ExternalIdentityRef> getDeclaredGroupRefs(ExternalIdentityRef ref) throws ExternalIdentityException {
if (!isMyRef(ref)) {
return Collections.emptyMap();
}
String searchFilter = config.getMemberOfSearchFilter(ref.getId());
LdapConnection connection = null;
SearchCursor searchCursor = null;
try {
// Create the SearchRequest object
SearchRequest req = new SearchRequestImpl();
req.setScope(SearchScope.SUBTREE);
String idAttribute = config.getGroupConfig().getIdAttribute();
req.addAttributes(idAttribute == null ? SchemaConstants.NO_ATTRIBUTE : idAttribute);
req.setTimeLimit((int) config.getSearchTimeout());
req.setBase(new Dn(config.getGroupConfig().getBaseDN()));
req.setFilter(searchFilter);
if (log.isDebugEnabled()) {
log.debug("getDeclaredGroupRefs: using SearchRequest {}.", req);
}
Map<String, ExternalIdentityRef> groups = new HashMap<String, ExternalIdentityRef>();
DebugTimer timer = new DebugTimer();
connection = connect();
timer.mark("connect");
searchCursor = connection.search(req);
timer.mark("search");
while (searchCursor.next()) {
Response response = searchCursor.get();
if (response instanceof SearchResultEntry) {
Entry resultEntry = ((SearchResultEntry) response).getEntry();
ExternalIdentityRef groupRef = new ExternalIdentityRef(resultEntry.getDn().toString(), this.getName());
groups.put(groupRef.getId(), groupRef);
}
}
timer.mark("iterate");
if (log.isDebugEnabled()) {
log.debug("getDeclaredGroupRefs: search below {} with {} found {} entries. {}", config.getGroupConfig().getBaseDN(), searchFilter, groups.size(), timer.getString());
}
return groups;
} catch (Exception e) {
log.error("Error during ldap membership search.", e);
throw new ExternalIdentityException("Error during ldap membership search.", e);
} finally {
if (searchCursor != null) {
try {
searchCursor.close();
} catch (IOException e) {
log.warn("Failed to close search cursor.", e);
}
}
disconnect(connection);
}
}
use of org.apache.directory.api.ldap.model.name.Dn in project syncope by apache.
the class ApacheDSStartStopListener method initDirectoryService.
/**
* Initialize the server. It creates the partition, adds the index, and injects the context entries for the created
* partitions.
*
* @param workDir the directory to be used for storing the data
* @param loadDefaultContent if default content should be loaded
* @throws Exception if there were some problems while initializing
*/
private void initDirectoryService(final ServletContext servletContext, final File workDir, final boolean loadDefaultContent) throws Exception {
// Initialize the LDAP service
service = new DefaultDirectoryService();
service.setInstanceLayout(new InstanceLayout(workDir));
CacheService cacheService = new CacheService();
cacheService.initialize(service.getInstanceLayout());
service.setCacheService(cacheService);
// first load the schema
initSchemaPartition();
// then the system partition
// this is a MANDATORY partition
// DO NOT add this via addPartition() method, trunk code complains about duplicate partition
// while initializing
JdbmPartition systemPartition = new JdbmPartition(service.getSchemaManager(), service.getDnFactory());
systemPartition.setId("system");
systemPartition.setPartitionPath(new File(service.getInstanceLayout().getPartitionsDirectory(), systemPartition.getId()).toURI());
systemPartition.setSuffixDn(new Dn(ServerDNConstants.SYSTEM_DN));
systemPartition.setSchemaManager(service.getSchemaManager());
// mandatory to call this method to set the system partition
// Note: this system partition might be removed from trunk
service.setSystemPartition(systemPartition);
// Disable the ChangeLog system
service.getChangeLog().setEnabled(false);
service.setDenormalizeOpAttrsEnabled(true);
// Now we can create as many partitions as we need
Partition ispPartition = addPartition("isp", "o=isp", service.getDnFactory());
// Index some attributes on the apache partition
addIndex(ispPartition, "objectClass", "ou", "uid");
// And start the service
service.startup();
if (loadDefaultContent) {
Resource contentLdif = WebApplicationContextUtils.getWebApplicationContext(servletContext).getResource("classpath:/content.ldif");
LdifInputStreamLoader contentLoader = new LdifInputStreamLoader(service.getAdminSession(), contentLdif.getInputStream());
int numEntries = contentLoader.execute();
LOG.info("Successfully created {} entries", numEntries);
}
}
use of org.apache.directory.api.ldap.model.name.Dn in project syncope by apache.
the class ApacheDSStartStopListener method contextInitialized.
/**
* Startup ApacheDS embedded.
*
* @param sce ServletContext event
*/
@Override
public void contextInitialized(final ServletContextEvent sce) {
File workDir = (File) sce.getServletContext().getAttribute("javax.servlet.context.tempdir");
workDir = new File(workDir, "server-work");
final boolean loadDefaultContent = !workDir.exists();
if (loadDefaultContent && !workDir.mkdirs()) {
throw new RuntimeException("Could not create " + workDir.getAbsolutePath());
}
Entry result;
try {
initDirectoryService(sce.getServletContext(), workDir, loadDefaultContent);
server = new LdapServer();
server.setTransports(new TcpTransport(Integer.parseInt(WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean("testds.port", String.class))));
server.setDirectoryService(service);
server.start();
// store directoryService in context to provide it to servlets etc.
sce.getServletContext().setAttribute(DirectoryService.JNDI_KEY, service);
result = service.getAdminSession().lookup(new Dn("o=isp"));
} catch (Exception e) {
LOG.error("Fatal error in context init", e);
throw new RuntimeException(e);
}
if (result == null) {
throw new RuntimeException("Base DN not found");
} else {
LOG.info("ApacheDS startup completed succesfully");
}
}
Aggregations