下面列出了怎么用javax.naming.ldap.BasicControl的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@inheritDoc}
*/
@Override
public javax.naming.ldap.Control toJndiControl( Control control ) throws EncoderException
{
// We don't know if it's a request or a response control. Test with request contriols
ControlFactory<?> factory = requestControlFactories.get( control.getOid() );
if ( factory == null )
{
if ( control instanceof OpaqueControl )
{
return new BasicControl( control.getOid(), control.isCritical(), ( ( OpaqueControl ) control ).getEncodedValue() );
}
else
{
return new BasicControl( control.getOid(), control.isCritical(), null );
}
}
else
{
Asn1Buffer asn1Buffer = new Asn1Buffer();
factory.encodeValue( asn1Buffer, control );
return new BasicControl( control.getOid(), control.isCritical(), asn1Buffer.getBytes().array() );
}
}
protected static BasicControl[] convertControls( final ChaiRequestControl[] controls )
{
if ( controls == null )
{
return null;
}
final BasicControl[] newControls = new BasicControl[controls.length];
for ( int i = 0; i < controls.length; i++ )
{
newControls[i] = new BasicControl(
controls[i].getId(),
controls[i].isCritical(),
controls[i].getValue()
);
}
return newControls;
}
@Override
public void beforeLDAPOperation(LdapContext ldapContext, LDAPOperationManager.LdapOperation ldapOperation) throws NamingException {
logger.debug("Applying LDAP_PASSWORD_POLICY_HINTS_OID before update password");
final byte[] controlData = {48, (byte) 132, 0, 0, 0, 3, 2, 1, 1};
// Rather using deprecated OID as it works from MSAD 2008-R2 when the newer works from MSAD 2012
BasicControl control = new BasicControl(LDAP_SERVER_POLICY_HINTS_DEPRECATED_OID, true, controlData);
BasicControl[] controls = new BasicControl[] { control };
ldapContext.setRequestControls(controls);
}
public final void writeStringAttributes(
final String entryDN,
final Map<String, String> attributeValueProps,
final boolean overwrite,
final BasicControl[] controls
)
throws ChaiUnavailableException, ChaiOperationException
{
activityPreCheck();
getInputValidator().writeStringAttributes( entryDN, attributeValueProps, overwrite );
// Determine the modification type, if replace, only replace on the first attribute, the rest just get added.
final int modType = overwrite ? DirContext.REPLACE_ATTRIBUTE : DirContext.ADD_ATTRIBUTE;
// Create the ModificationItem
final List<ModificationItem> modificationItems = new ArrayList<>();
for ( final Map.Entry<String, String> entry : attributeValueProps.entrySet() )
{
// Create a BasicAttribute for the object.
final BasicAttribute attributeToReplace = new BasicAttribute( entry.getKey(), entry.getValue() );
// Populate the ModificationItem object with the flag & the attribute to replace.
modificationItems.add( new ModificationItem( modType, attributeToReplace ) );
}
// convert to array
final ModificationItem[] modificationItemArray = modificationItems.toArray( new ModificationItem[modificationItems.size()] );
// get ldap connection
final LdapContext ldapConnection = getLdapConnection();
// Modify the Attributes.
try
{
ldapConnection.modifyAttributes( addJndiEscape( entryDN ), modificationItemArray );
}
catch ( NamingException e )
{
convertNamingException( e );
}
}
/**
* Parse a control. The grammar is : <control> ::= "control:" <fill>
* <ldap-oid> <critical-e> <value-spec-e> <sep> <critical-e> ::= <spaces>
* <boolean> | e <boolean> ::= "true" | "false" <value-spec-e> ::=
* <value-spec> | e <value-spec> ::= ":" <fill> <SAFE-STRING-e> | "::"
* <fill> <BASE64-STRING> | ":<" <fill> <url>
* <p/>
* It can be read as : "control:" <fill> <ldap-oid> [ " "+ ( "true" |
* "false") ] [ ":" <fill> <SAFE-STRING-e> | "::" <fill> <BASE64-STRING> | ":<"
* <fill> <url> ]
*
* @param line The line containing the control
* @return A control
*/
private Control parseControl(String line) {
String lowerLine = line.toLowerCase().trim();
char[] controlValue = line.trim().toCharArray();
int pos = 0;
int length = controlValue.length;
// Get the <ldap-oid>
if (pos > length) {
// No OID : error !
throw new LdifParseException("Bad control, no oid", line);
}
int initPos = pos;
while (Utils.isCharASCII(controlValue, pos, '.') || Utils.isDigit(controlValue, pos)) {
pos++;
}
if (pos == initPos) {
// Not a valid OID !
throw new LdifParseException("Bad control, no oid", line);
}
String oid = lowerLine.substring(0, pos);
boolean criticality=false;
byte[] controlBytes = null;
// Get the criticality, if any
// Skip the <fill>
while (Utils.isCharASCII(controlValue, pos, ' ')) {
pos++;
}
// Check if we have a "true" or a "false"
int criticalPos = lowerLine.indexOf(':');
int criticalLength = 0;
if (criticalPos == -1) {
criticalLength = length - pos;
} else {
criticalLength = criticalPos - pos;
}
if ((criticalLength == 4) && ("true".equalsIgnoreCase(lowerLine.substring(pos, pos + 4)))) {
criticality=true;
} else if ((criticalLength == 5) && ("false".equalsIgnoreCase(lowerLine.substring(pos, pos + 5)))) {
criticality=false;
} else if (criticalLength != 0) {
// If we have a criticality, it should be either "true" or "false",
// nothing else
throw new LdifParseException("Bad control criticality", line);
}
if (criticalPos > 0) {
// We have a value. It can be a normal value, a base64 encoded value
// or a file contained value
if (Utils.isCharASCII(controlValue, criticalPos + 1, ':')) {
// Base 64 encoded value
controlBytes = Utils.base64Decode(line.substring(criticalPos + 2).toCharArray());
} else if (Utils.isCharASCII(controlValue, criticalPos + 1, '<')) {
// File contained value
} else {
// Standard value
byte[] value = new byte[length - criticalPos - 1];
for (int i = 0; i < length - criticalPos - 1; i++) {
value[i] = (byte) controlValue[i + criticalPos + 1];
}
controlBytes=value;
}
}
return new BasicControl(oid, criticality, controlBytes);
}