org.springframework.ldap.NamingException Java Examples

The following examples show how to use org.springframework.ldap.NamingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> T findByDn(Name dn, final Class<T> clazz) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Reading Entry at - %s$1", dn));
    }

    // Make sure the class is OK before doing the lookup
    String[] attributes = odm.manageClass(clazz);

    T result = lookup(dn, attributes, new ContextMapper<T>() {
        @Override
        public T mapFromContext(Object ctx) throws javax.naming.NamingException {
            return odm.mapFromLdapDataEntry((DirContextOperations) ctx, clazz);
        }
    });

    if (result == null) {
        throw new OdmException(String.format("Entry %1$s does not have the required objectclasses ", dn));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Found entry - %s$1", result));
    }

    return result;
}
 
Example #2
Source File: LdapUtils.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
 * Get the value of the Rdn with the requested key in the supplied Name.
 *
 * @param name the Name in which to search for the key.
 * @param key the attribute key to search for.
 * @return the value of the rdn corresponding to the <b>first</b> occurrence of the requested key.
 * @throws NoSuchElementException if no corresponding entry is found.
 * @since 2.0
 */
public static Object getValue(Name name, String key) {
    NamingEnumeration<? extends Attribute> allAttributes = getRdn(name, key).toAttributes().getAll();
    while (allAttributes.hasMoreElements()) {
        Attribute oneAttribute = allAttributes.nextElement();
        if(key.equalsIgnoreCase(oneAttribute.getID())) {
            try {
                return oneAttribute.get();
            } catch (javax.naming.NamingException e) {
                throw convertLdapException(e);
            }
        }
    }

    // This really shouldn't happen
    throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'");
}
 
Example #3
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void search(final Name base, final String filter, final SearchControls controls,
		NameClassPairCallbackHandler handler, DirContextProcessor processor) {

	// Create a SearchExecutor to perform the search.
	SearchExecutor se = new SearchExecutor() {
		public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
			return ctx.search(base, filter, controls);
		}
	};
	if (handler instanceof ContextMapperCallbackHandler) {
		assureReturnObjFlagSet(controls);
	}
	search(se, handler, processor);
}
 
Example #4
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void search(final String base, final String filter, final SearchControls controls,
		NameClassPairCallbackHandler handler) {

	// Create a SearchExecutor to perform the search.
	SearchExecutor se = new SearchExecutor() {
		public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
			return ctx.search(base, filter, controls);
		}
	};
	if (handler instanceof ContextMapperCallbackHandler) {
		assureReturnObjFlagSet(controls);
	}
	search(se, handler);
}
 
Example #5
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public <T> T lookup(final String dn, final String[] attributes, final AttributesMapper<T> mapper) {
	return executeReadOnly(new ContextExecutor<T>() {
		public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
			return mapper.mapFromAttributes(filteredAttributes);
		}
	});
}
 
Example #6
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public <T> T lookup(final String dn, final String[] attributes, final ContextMapper<T> mapper) {
	return executeReadOnly(new ContextExecutor<T>() {
		public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
			LdapName name = LdapUtils.newLdapName(dn);
			DirContextAdapter contextAdapter = new DirContextAdapter(filteredAttributes, name);
			return mapper.mapFromContext(contextAdapter);
		}
	});
}
 
Example #7
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public <T> T lookup(final Name dn, final String[] attributes, final AttributesMapper<T> mapper) {
	return executeReadOnly(new ContextExecutor<T>() {
		public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
			return mapper.mapFromAttributes(filteredAttributes);
		}
	});
}
 
Example #8
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public <T> T lookup(final String dn, final ContextMapper<T> mapper) {
	return executeReadOnly(new ContextExecutor<T>() {
		public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			Object object = ctx.lookup(dn);
			return mapper.mapFromContext(object);
		}
	});
}
 
Example #9
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public <T> T lookup(final Name dn, final ContextMapper<T> mapper) {
	return executeReadOnly(new ContextExecutor<T>() {
		public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			Object object = ctx.lookup(dn);
			return mapper.mapFromContext(object);
		}
	});
}
 
Example #10
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public <T> T lookup(final String dn, final AttributesMapper<T> mapper) {

	return executeReadOnly(new ContextExecutor<T>() {
		public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			Attributes attributes = ctx.getAttributes(dn);
			return mapper.mapFromAttributes(attributes);
		}
	});
}
 
Example #11
Source File: LdapServiceImpl.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private boolean toggleUserInGroup(String realm, Group group, User user, int op) {
    BasicAttribute member = new BasicAttribute("memberUid", user.getUid());
    ModificationItem[] modGroups = new ModificationItem[] {
          new ModificationItem(op, member) };

    Name groupName = buildGroupDN(realm, group.getGroupName());

    try {
        ldapTemplate.modifyAttributes(groupName, modGroups);
    } catch (NamingException e) {
        return false;
    }

    return true;
}
 
Example #12
Source File: TransactionAwareDirContextInvocationHandler.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * Close the supplied context, but only if it is not associated with the
 * current transaction.
 * 
 * @param context
 *            the DirContext to close.
 * @param contextSource
 *            the ContextSource bound to the transaction.
 * @throws NamingException
 */
void doCloseConnection(DirContext context, ContextSource contextSource)
        throws javax.naming.NamingException {
    DirContextHolder transactionContextHolder = (DirContextHolder) TransactionSynchronizationManager
            .getResource(contextSource);
    if (transactionContextHolder == null
            || transactionContextHolder.getCtx() != context) {
        log.debug("Closing context");
        // This is not the transactional context or the transaction is
        // no longer active - we should close it.
        context.close();
    } else {
        log.debug("Leaving transactional context open");
    }
}
 
Example #13
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void list(final Name base, NameClassPairCallbackHandler handler) {
	SearchExecutor searchExecutor = new SearchExecutor() {
		public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
			return ctx.list(base);
		}
	};

	search(searchExecutor, handler);
}
 
Example #14
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void listBindings(final String base, NameClassPairCallbackHandler handler) {
	SearchExecutor searchExecutor = new SearchExecutor() {
		public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
			return ctx.listBindings(base);
		}
	};

	search(searchExecutor, handler);
}
 
Example #15
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private <T> T executeWithContext(ContextExecutor<T> ce, DirContext ctx) {
	try {
		return ce.executeWithContext(ctx);
	}
	catch (javax.naming.NamingException e) {
		throw LdapUtils.convertLdapException(e);
	}
	finally {
		closeContext(ctx);
	}
}
 
Example #16
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public Object lookup(final String dn) {
	return executeReadOnly(new ContextExecutor() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			return ctx.lookup(dn);
		}
	});
}
 
Example #17
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public Object lookup(final Name dn) {
	return executeReadOnly(new ContextExecutor() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			return ctx.lookup(dn);
		}
	});
}
 
Example #18
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public <T> T lookup(final Name dn, final String[] attributes, final ContextMapper<T> mapper) {
	return executeReadOnly(new ContextExecutor<T>() {
		public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
			DirContextAdapter contextAdapter = new DirContextAdapter(filteredAttributes, dn);
			return mapper.mapFromContext(contextAdapter);
		}
	});
}
 
Example #19
Source File: LdapUtil.java    From zstack with Apache License 2.0 5 votes vote down vote up
private String getFullUserDn(LdapTemplate ldapTemplate, String filter) {
    String dn;
    try {
        List<Object> result = ldapTemplate.search("", filter, new AbstractContextMapper<Object>() {
            @Override
            protected Object doMapFromContext(DirContextOperations ctx) {
                return ctx.getNameInNamespace();
            }
        });
        if (result.size() == 1) {
            dn = result.get(0).toString();
        } else if (result.size() > 1) {
            throw new OperationFailureException(err(
                    LdapErrors.UNABLE_TO_GET_SPECIFIED_LDAP_UID, "More than one ldap search result"));
        } else {
            return "";
        }
        logger.info(String.format("getDn success filter:%s, dn:%s", filter, dn));
    } catch (NamingException e) {
        LdapServerVO ldapServerVO = getLdapServer();
        throw new OperationFailureException(err(
                LdapErrors.UNABLE_TO_GET_SPECIFIED_LDAP_UID, "You'd better check the LDAP/AD server[url:%s, baseDN:%s, encryption:%s, username:%s, password:******]" +
                        " configuration and test connection first.getDn error filter:%s",
                ldapServerVO.getUrl(), ldapServerVO.getBase(),
                ldapServerVO.getEncryption(), ldapServerVO.getUsername(), filter));
    }
    return dn;
}
 
Example #20
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void modifyAttributes(final Name dn, final ModificationItem[] mods) {
	executeReadWrite(new ContextExecutor() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			ctx.modifyAttributes(dn, mods);
			return null;
		}
	});
}
 
Example #21
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void modifyAttributes(final String dn, final ModificationItem[] mods) {
	executeReadWrite(new ContextExecutor() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			ctx.modifyAttributes(dn, mods);
			return null;
		}
	});
}
 
Example #22
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void bind(final String dn, final Object obj, final Attributes attributes) {
	executeReadWrite(new ContextExecutor<Object>() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			ctx.bind(dn, obj, attributes);
			return null;
		}
	});
}
 
Example #23
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private void doUnbind(final String dn) {
	executeReadWrite(new ContextExecutor<Object>() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			ctx.unbind(dn);
			return null;
		}
	});
}
 
Example #24
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void rebind(final Name dn, final Object obj, final Attributes attributes) {
	executeReadWrite(new ContextExecutor() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			ctx.rebind(dn, obj, attributes);
			return null;
		}
	});
}
 
Example #25
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void rebind(final String dn, final Object obj, final Attributes attributes) {
	executeReadWrite(new ContextExecutor() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			ctx.rebind(dn, obj, attributes);
			return null;
		}
	});
}
 
Example #26
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public void rename(final Name oldDn, final Name newDn) {
	executeReadWrite(new ContextExecutor() {
		public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
			ctx.rename(oldDn, newDn);
			return null;
		}
	});
}
 
Example #27
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
       * {@inheritDoc}
       */
public T getObjectFromNameClassPair(NameClassPair nameClassPair) {
	try {
		return mapper.mapFromNameClassPair(nameClassPair);
	}
	catch (javax.naming.NamingException e) {
		throw LdapUtils.convertLdapException(e);
	}
}
 
Example #28
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> T authenticate(LdapQuery query, String password, AuthenticatedLdapEntryContextMapper<T> mapper) {
    SearchControls searchControls = searchControlsForQuery(query, RETURN_OBJ_FLAG);
    ReturningAuthenticatedLdapEntryContext<T> mapperCallback =
            new ReturningAuthenticatedLdapEntryContext<T>(mapper);
    CollectingAuthenticationErrorCallback errorCallback =
            new CollectingAuthenticationErrorCallback();

    AuthenticationStatus authenticationStatus = authenticate(query.base(),
            query.filter().encode(),
            password,
            searchControls,
            mapperCallback,
            errorCallback);

    if(errorCallback.hasError()) {
        Exception error = errorCallback.getError();

        if (error instanceof NamingException) {
            throw (NamingException) error;
        } else {
            throw new UncategorizedLdapException(error);
        }
    } else if(AuthenticationStatus.EMPTYRESULT == authenticationStatus) {
    	throw new EmptyResultDataAccessException(1);
    } else if(!authenticationStatus.isSuccess()) {
        throw new AuthenticationException();
    }

    return mapperCallback.collectedObject;
}
 
Example #29
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public DirContextOperations searchForContext(LdapQuery query) {
    return searchForObject(query, new ContextMapper<DirContextOperations>() {
        @Override
        public DirContextOperations mapFromContext(Object ctx) throws javax.naming.NamingException {
            return (DirContextOperations) ctx;
        }
    });
}
 
Example #30
Source File: LdapUtil.java    From zstack with Apache License 2.0 4 votes vote down vote up
public String decodeGlobalUuid(NameAwareAttribute attribute) throws javax.naming.NamingException {
    String ldapServerUuid = Q.New(LdapServerVO.class)
            .select(LdapServerVO_.uuid)
            .eq(LdapServerVO_.scope, scope)
            .findValue();
    String type = LdapSystemTags.LDAP_SERVER_TYPE.getTokenByResourceUuid(ldapServerUuid, LdapSystemTags.LDAP_SERVER_TYPE_TOKEN);

    if(LdapConstant.WindowsAD.TYPE.equals(type)){
        byte[] GUID = (byte[]) attribute.get();

        String strGUID;
        String byteGUID = "";


        //Convert the GUID into string using the byte format
        for (int c=0;c<GUID.length;c++) {
            byteGUID = byteGUID + "\\" + addLeadingZero((int)GUID[c] & 0xFF);
        }
        strGUID = "{";
        strGUID = strGUID + addLeadingZero((int)GUID[3] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[2] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[1] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[0] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + addLeadingZero((int)GUID[5] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[4] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + addLeadingZero((int)GUID[7] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[6] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + addLeadingZero((int)GUID[8] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[9] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + addLeadingZero((int)GUID[10] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[11] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[12] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[13] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[14] & 0xFF);
        strGUID = strGUID + addLeadingZero((int)GUID[15] & 0xFF);
        strGUID = strGUID + "}";

        return strGUID;
    }

    return attribute.get(0).toString();
}