下面列出了javax.naming.directory.InvalidAttributeValueException#org.apache.directory.api.ldap.model.exception.LdapException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* {@inheritDoc}
*/
public EntryFilteringCursor search( SearchOperationContext searchContext ) throws LdapException
{
Dn base = searchContext.getDn();
// TODO since we're handling the *, and + in the EntryFilteringCursor
// we may not need this code: we need see if this is actually the
// case and remove this code.
if ( base.size() == 0 )
{
return searchFromRoot( searchContext );
}
// Not sure we need this code...
base.apply( schemaManager );
// Normal case : do a search on the specific partition
Partition backend = getPartition( base );
return backend.search( searchContext );
}
@Test
public void testSimpleRdnValueNullSchemaManager() throws LdapException
{
String errors = null;
Rdn rdn = new Rdn( (SchemaManager)null, "Cn= TEST " );
if ( !"Cn= TEST ".equals( rdn.getName() ) )
{
errors += "\nRdn.getName fails '" + rdn.getName() + "'";
}
if ( !"Cn=TEST".equals( rdn.getNormName() ) )
{
errors = "\nRdn.getNormName fails '" + rdn.getNormName() + "'";
}
if ( !"TEST".equals( rdn.getValue( "cn" ) ) )
{
errors += "\nRdn.getNormValue( 'cn' ) fails '" + ( String ) rdn.getValue( "cn" ) + "'";
}
assertEquals( null, errors );
}
/**
* {@inheritDoc}
*/
@Override
public void unregisterSchemaElements( String schemaName ) throws LdapException
{
if ( schemaName == null )
{
return;
}
// Loop on all the SchemaObjects stored and remove those associated
// with the give schemaName
for ( LdapComparator<?> comparator : this )
{
if ( schemaName.equalsIgnoreCase( comparator.getSchemaName() ) )
{
String oid = comparator.getOid();
SchemaObject removed = unregister( oid );
if ( LOG.isDebugEnabled() )
{
LOG.debug( I18n.msg( I18n.MSG_13702_REMOVED_FROM_REGISTRY, removed, oid ) );
}
}
}
}
/**
* Test for inequality when only the IDs are different.
*/
@Test
public void testNotEqualDiffId() throws LdapException
{
AddRequestImpl req0 = new AddRequestImpl();
req0.setMessageId( 7 );
req0.setEntryDn( new Dn( "cn=admin,dc=example,dc=com" ) );
req0.setEntry( getEntry() );
AddRequestImpl req1 = new AddRequestImpl();
req1.setMessageId( 5 );
req1.setEntryDn( new Dn( "cn=admin,dc=example,dc=com" ) );
req1.setEntry( getEntry() );
assertFalse( req0.equals( req1 ) );
}
/**
* Normalize a simple Dn with a # on first position
*/
@Test
public void testNormalizeDnSharpFirst() throws LdapException, LdapException
{
Dn dn = new Dn( "ou = \\#this is a sharp" );
assertTrue( Dn.isValid( "ou = \\#this is a sharp" ) );
assertEquals( "ou=\\#this is a sharp", dn.getEscaped() );
assertEquals( "ou = \\#this is a sharp", dn.getName() );
// Check the normalization now
Dn ndn = new Dn( schemaManager, dn );
assertEquals( "ou = \\#this is a sharp", ndn.getName() );
assertEquals( "ou=\\#this is a sharp", ndn.getEscaped() );
}
/**
* Test method for removeAttributes( String... )
*/
@Test
public void testRemoveAttributesStringArray() throws LdapException
{
Entry entry = new DefaultEntry( exampleDn );
Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
entry.put( attrOC, attrCN, attrSN, attrPWD );
entry.removeAttributes( "CN", "SN" );
assertFalse( entry.containsAttribute( "cn", "sn" ) );
assertTrue( entry.containsAttribute( "objectclass", "userpassword" ) );
entry.removeAttributes( "badId" );
entry.removeAttributes( ( String ) null );
}
/**
* {@inheritDoc}
*/
@Override
public DitStructureRule lookup( int ruleId ) throws LdapException
{
DitStructureRule ditStructureRule = byRuleId.get( ruleId );
if ( ditStructureRule == null )
{
String msg = I18n.err( I18n.ERR_13731_DIT_STRUCTURE_RULE_DOES_NOT_EXIST, ruleId );
if ( LOG.isDebugEnabled() )
{
LOG.debug( msg );
}
throw new LdapException( msg );
}
if ( LOG.isDebugEnabled() )
{
LOG.debug( I18n.msg( I18n.MSG_13724_FOUND_WITH_RULE_ID, ditStructureRule, ruleId ) );
}
return ditStructureRule;
}
/**
* This method is used to initialize the OBJECT_CLASS_AT attributeType.
*
* We want to do it only once, so it's a synchronized method. Note that
* the alternative would be to call the lookup() every time, but this won't
* be very efficient, as it will get the AT from a map, which is also
* synchronized, so here, we have a very minimal cost.
*
* We can't do it once as a static part in the body of this class, because
* the access to the registries is mandatory to get back the AttributeType.
*/
private void initObjectClassAT()
{
if ( schemaManager == null )
{
return;
}
try
{
synchronized ( MUTEX )
{
if ( objectClassAttributeType == null )
{
objectClassAttributeType = schemaManager
.lookupAttributeTypeRegistry( SchemaConstants.OBJECT_CLASS_AT );
}
}
}
catch ( LdapException ne )
{
// do nothing...
}
}
/**
* Tests to see the same exact copy returns true.
*/
@Test
public void testNotEqualsDiffResult() throws LdapException
{
AbstractResultResponse msg0 = new AbstractResultResponse( 5, MessageTypeEnum.BIND_REQUEST )
{
};
AbstractResultResponse msg1 = new AbstractResultResponse( 5, MessageTypeEnum.BIND_REQUEST )
{
};
LdapResult r0 = msg0.getLdapResult();
LdapResult r1 = msg1.getLdapResult();
r0.setDiagnosticMessage( "blah blah blah" );
r1.setDiagnosticMessage( "blah blah blah" );
r0.setMatchedDn( new Dn( "dc=example,dc=com" ) );
r1.setMatchedDn( new Dn( "dc=apache,dc=org" ) );
r0.setResultCode( ResultCodeEnum.TIME_LIMIT_EXCEEDED );
r1.setResultCode( ResultCodeEnum.TIME_LIMIT_EXCEEDED );
Referral refs0 = new ReferralImpl();
refs0.addLdapUrl( "ldap://someserver.com" );
refs0.addLdapUrl( "ldap://anotherserver.org" );
Referral refs1 = new ReferralImpl();
refs1.addLdapUrl( "ldap://someserver.com" );
refs1.addLdapUrl( "ldap://anotherserver.org" );
assertFalse( msg0.equals( msg1 ) );
assertFalse( msg1.equals( msg0 ) );
}
/**
* test a composite Dn with multiple NC and separators : a=b+c=d, e=f + g=h +
* i=j
*/
@Test
public void testLdapDNCompositeMultivaluedAttribute() throws LdapException
{
Dn dn = new Dn( "a=b+c=d, e=f + g=h + i=j" );
assertEquals( "a=b+c=d,e=f+g=h+i=j", dn.getEscaped() );
assertEquals( "a=b+c=d, e=f + g=h + i=j", dn.getName() );
}
/**
* {@inheritDoc}
*/
@Override
public EntryCursor search( Dn baseDn, String filter, SearchScope scope, String... attributes )
throws LdapException
{
return connection.search( baseDn, filter, scope, attributes );
}
/**
* {@inheritDoc}
*/
public Entry getRootDse( GetRootDseOperationContext getRootDseContext ) throws LdapException
{
if ( IS_DEBUG )
{
OPERATION_LOG.debug( ">> GetRootDseOperation : {}", getRootDseContext );
}
long opStart = 0L;
if ( IS_TIME )
{
opStart = System.nanoTime();
}
ensureStarted();
Interceptor head = directoryService.getInterceptor( getRootDseContext.getNextInterceptor() );
Entry root = head.getRootDse( getRootDseContext );
if ( IS_DEBUG )
{
OPERATION_LOG.debug( "<< getRootDseOperation successful" );
}
if ( IS_TIME )
{
OPERATION_TIME.debug( "GetRootDSE operation took " + ( System.nanoTime() - opStart ) + " ns" );
}
return root;
}
/**
* Convert a ModificationItem to an instance of a ServerModification object
*
* @param modificationImpl the modification instance to convert
* @param attributeType the associated attributeType
* @return a instance of a ServerModification object
*/
private static Modification toServerModification( ModificationItem modificationImpl, AttributeType attributeType )
throws LdapException
{
ModificationOperation operation;
switch ( modificationImpl.getModificationOp() )
{
case DirContext.REMOVE_ATTRIBUTE:
operation = ModificationOperation.REMOVE_ATTRIBUTE;
break;
case DirContext.REPLACE_ATTRIBUTE:
operation = ModificationOperation.REPLACE_ATTRIBUTE;
break;
case DirContext.ADD_ATTRIBUTE:
default:
operation = ModificationOperation.ADD_ATTRIBUTE;
break;
}
Modification modification = new DefaultModification(
operation,
ServerEntryUtils.toServerAttribute( modificationImpl.getAttribute(), attributeType ) );
return modification;
}
/**
* Test for DIRSHARED-3.
* Tests that equals() is invertable for single-valued RDNs.
*
* @throws LdapException
*/
@Test
public void testCompareInvertableNC2NC() throws LdapException
{
Rdn rdn1 = new Rdn( " a = b " );
Rdn rdn2 = new Rdn( " a = c " );
assertFalse( rdn1.equals( rdn2 ) );
assertFalse( rdn2.equals( rdn1 ) );
}
/**
* {@inheritDoc}
*/
public Entry delete( DeleteOperationContext deleteContext ) throws LdapException
{
Partition partition = getPartition( deleteContext.getDn() );
Entry deletedEntry = partition.delete( deleteContext );
return deletedEntry;
}
/**
* {@inheritDoc}
*/
@Override
public ExtendedResponse extended( String oid, byte[] value ) throws LdapException
{
try
{
return extended( Oid.fromString( oid ), value );
}
catch ( DecoderException e )
{
String msg = I18n.err( I18n.ERR_04153_OID_DECODING_FAILURE, oid );
LOG.error( msg );
throw new LdapException( msg, e );
}
}
/**
* test a simple AttributeTypeAndValue : a = b
*/
@Test
public void testAvaSimpleNorm() throws LdapException
{
Ava atav = new Ava( schemaManager, " CommonName ", " This is a TEST " );
assertEquals( " CommonName =\\ This is a TEST\\ ", atav.toString() );
assertEquals( " CommonName =\\ This is a TEST\\ ", atav.getEscaped() );
assertEquals( " CommonName =\\ This is a TEST\\ ", atav.getName() );
assertEquals( atav, new Ava( schemaManager, "cn", "this is a test" ) );
}
/**
* {@inheritDoc}
*/
@Override
public AddResponse add( Dn dn, final Attribute... attributes )
{
return add( dn,
new RequestBuilder<AddRequest>()
{
@Override
public void buildRequest( AddRequest request ) throws LdapException
{
request.getEntry().add( attributes );
}
} );
}
/**
* {@inheritDoc}
*/
public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
{
if ( Strings.isEmpty( value ) )
{
return value;
}
return value;
}
/**
* {@inheritDoc}
*/
@Override
public boolean remove( AttributeType attributeType, byte[]... values ) throws LdapException
{
if ( attributeType == null )
{
return false;
}
try
{
Attribute attribute = attributes.get( attributeType.getOid() );
if ( attribute == null )
{
// Can't remove values from a not existing attribute !
return false;
}
int nbOldValues = attribute.size();
// Remove the values
attribute.remove( values );
if ( attribute.size() == 0 )
{
// No mare values, remove the attribute
attributes.remove( attributeType.getOid() );
return true;
}
return nbOldValues != attribute.size();
}
catch ( IllegalArgumentException iae )
{
LOG.error( I18n.err( I18n.ERR_13205_CANNOT_REMOVE_VAL_MISSING_ATTR, attributeType ) );
return false;
}
}
/**
* Test method for getDN()
*/
@Test
public void testGetDn() throws LdapException
{
Entry entry = new DefaultEntry( exampleDn );
assertEquals( exampleDn, entry.getDn() );
Dn testDn = new Dn( "cn=test" );
entry.setDn( testDn );
assertEquals( testDn, entry.getDn() );
}
/**
* Test method for hashcode()
*/
@Test
public void testHashCode() throws LdapException, LdapException
{
Entry entry1 = new DefaultEntry( exampleDn );
Entry entry2 = new DefaultEntry( exampleDn );
assertEquals( entry1.hashCode(), entry2.hashCode() );
entry2.setDn( new Dn( "ou=system,dc=com" ) );
assertNotSame( entry1.hashCode(), entry2.hashCode() );
entry2.setDn( exampleDn );
assertEquals( entry1.hashCode(), entry2.hashCode() );
Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
entry1.add( attrOC, attrCN, attrSN, attrPWD );
entry2.add( attrOC, attrCN, attrSN, attrPWD );
assertEquals( entry1.hashCode(), entry2.hashCode() );
Entry entry3 = new DefaultEntry( exampleDn );
entry3.add( attrOC, attrSN, attrCN, attrPWD );
assertEquals( entry1.hashCode(), entry3.hashCode() );
}
/**
* Test for DIRSERVER-191
*/
@Test
public void testGetPrefixName() throws LdapException, InvalidNameException
{
LdapName jName = new LdapName( "cn=four,cn=three,cn=two,cn=one" );
Dn aName = new Dn( "cn=four,cn=three,cn=two,cn=one" );
assertEquals( jName.getPrefix( 0 ).toString(), aName.getAncestorOf( "cn=four,cn=three,cn=two,cn=one" )
.toString() );
assertEquals( jName.getPrefix( 1 ).toString(), aName.getAncestorOf( "cn=four,cn=three,cn=two" ).toString() );
assertEquals( jName.getPrefix( 2 ).toString(), aName.getAncestorOf( "cn=four,cn=three" ).toString() );
assertEquals( jName.getPrefix( 3 ).toString(), aName.getAncestorOf( "cn=four" ).toString() );
assertEquals( jName.getPrefix( 4 ).toString(), aName.getAncestorOf( "" ).toString() );
}
@Test
public void testVsldapExtras() throws LdapException
{
assertThrows( TooComplexDnException.class, () ->
{
FastDnParser
.parse( "cn=Billy Bakers, OID.2.5.4.11=Corporate Tax, ou=Fin-Accounting, ou=Americas, ou=Search, o=IMC, c=US" );
} );
}
/**
* Test the instanceOf method
*/
@Test
public void testInstanceOf() throws LdapException
{
AttributeType attribute = EntryUtils.getIA5StringAttributeType();
Value ssv = Value.createValue( attribute );
assertTrue( ssv.isInstanceOf( attribute ) );
attribute = EntryUtils.getBytesAttributeType();
assertFalse( ssv.isInstanceOf( attribute ) );
}
/**
* @param user
* @throws UpdateException
*/
void resetUserPassword( User user ) throws UpdateException
{
LdapConnection ld = null;
String userDn = getDn( user.getUserId(), user.getContextId() );
try
{
List<Modification> mods = new ArrayList<Modification>();
mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, SchemaConstants
.USER_PASSWORD_AT, user.getPassword() ) );
mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, OPENLDAP_PW_RESET, "TRUE" ) );
ld = getAdminConnection();
modify( ld, userDn, mods, user );
}
catch ( LdapException e )
{
String warning = "resetUserPassword userId [" + user.getUserId() + "] caught LDAPException=" + e
.getMessage();
throw new UpdateException( GlobalErrIds.USER_PW_RESET_FAILED, warning, e );
}
finally
{
closeAdminConnection( ld );
}
}
/**
* {@inheritDoc}
*/
public boolean remove( AttributeType attributeType, byte[]... values ) throws LdapException
{
if ( attributeType == null )
{
return false;
}
try
{
Attribute attribute = attributes.get( attributeType.getOid() );
if ( attribute == null )
{
// Can't remove values from a not existing attribute !
return false;
}
int nbOldValues = attribute.size();
// Remove the values
attribute.remove( values );
if ( attribute.size() == 0 )
{
// No mare values, remove the attribute
attributes.remove( attributeType.getOid() );
return true;
}
return nbOldValues != attribute.size();
}
catch ( IllegalArgumentException iae )
{
LOG.error( I18n.err( I18n.ERR_04465, attributeType ) );
return false;
}
}
/**
* {@inheritDoc}
*/
@Override
public void beforeFirst() throws LdapException, CursorException
{
checkNotClosed();
this.index = -1;
}
/**
* Test for DIRSERVER-184
* @throws LdapException
*/
@Test
public void testDIRSERVER_184_7()
{
try
{
new Dn( "dn=a trailing pound #" );
}
catch ( LdapException ine )
{
assertTrue( true );
}
}
private static void bindAdmin(LdapConnection conn, LdapOptions options) throws LdapException {
if (!Strings.isEmpty(options.adminDn)) {
conn.bind(options.adminDn, options.adminPasswd);
} else {
conn.bind();
}
}