use of org.apache.directory.api.util.exception.NotImplementedException in project directory-ldap-api by apache.
the class LdifReader method parseControl.
/**
* Parse a control. The grammar is :
* <pre>
* <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>
* </pre>
*
* It can be read as :
* <pre>
* "control:" <fill> <ldap-oid> [ " "+ ( "true" |
* "false") ] [ ":" <fill> <SAFE-STRING-e> | "::" <fill> <BASE64-STRING> | ":<"
* <fill> <url> ]
* </pre>
*
* @param line The line containing the control
* @return A control
* @exception LdapLdifException If the control has no OID or if the OID is incorrect,
* of if the criticality is not set when it's mandatory.
*/
private Control parseControl(String line) throws LdapLdifException {
String lowerLine = Strings.toLowerCaseAscii(line).trim();
char[] controlValue = line.trim().toCharArray();
int pos = 0;
int length = controlValue.length;
// Get the <ldap-oid>
if (pos > length) {
// No OID : error !
String msg = I18n.err(I18n.ERR_13409_CONTROL_WITHOUT_OID, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
int initPos = pos;
while (Chars.isCharASCII(controlValue, pos, '.') || Chars.isDigit(controlValue, pos)) {
pos++;
}
if (pos == initPos) {
// Not a valid OID !
String msg = I18n.err(I18n.ERR_13409_CONTROL_WITHOUT_OID, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
// Create and check the OID
String oidString = lowerLine.substring(0, pos);
if (!Oid.isOid(oidString)) {
String message = I18n.err(I18n.ERR_13453_INVALID_OID, oidString, lineNumber);
LOG.error(message);
throw new LdapLdifException(message);
}
LdifControl control = new LdifControl(oidString);
// Skip the <fill>
while (Chars.isCharASCII(controlValue, pos, ' ')) {
pos++;
}
// Check if we have a "true" or a "false"
int criticalPos = lowerLine.indexOf(':');
int criticalLength;
if (criticalPos == -1) {
criticalLength = length - pos;
} else {
criticalLength = criticalPos - pos;
}
if ((criticalLength == 4) && ("true".equalsIgnoreCase(lowerLine.substring(pos, pos + 4)))) {
control.setCritical(true);
} else if ((criticalLength == 5) && ("false".equalsIgnoreCase(lowerLine.substring(pos, pos + 5)))) {
control.setCritical(false);
} else if (criticalLength != 0) {
// If we have a criticality, it should be either "true" or "false",
// nothing else
String msg = I18n.err(I18n.ERR_13410_INVALID_CRITICALITY, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
if (criticalPos > 0) {
// or a file contained value
if (Chars.isCharASCII(controlValue, criticalPos + 1, ':')) {
// Base 64 encoded value
// Skip the <fill>
pos = criticalPos + 2;
while (Chars.isCharASCII(controlValue, pos, ' ')) {
pos++;
}
byte[] value = Base64.decode(line.substring(pos).toCharArray());
control.setValue(value);
} else if (Chars.isCharASCII(controlValue, criticalPos + 1, '<')) {
// File contained value
throw new NotImplementedException(I18n.err(I18n.ERR_13433_SEE_DIRSERVER_1547));
} else {
// Skip the <fill>
pos = criticalPos + 1;
while (Chars.isCharASCII(controlValue, pos, ' ')) {
pos++;
}
// Standard value
byte[] value = new byte[length - pos];
for (int i = 0; i < length - pos; i++) {
value[i] = (byte) controlValue[i + pos];
}
control.setValue(value);
}
}
return control;
}
Aggregations