}
ExtendedRequestProtocolOp extendedRequest =
new ExtendedRequestProtocolOp(OID_PASSWORD_MODIFY_REQUEST,
builder.toByteString());
LDAPMessage requestMessage =
new LDAPMessage(nextMessageID.getAndIncrement(), extendedRequest,
controls);
// Send the request to the server and read the response.
try
{
writer.writeMessage(requestMessage);
}
catch (Exception e)
{
Message message = ERR_LDAPPWMOD_CANNOT_SEND_PWMOD_REQUEST.get(
String.valueOf(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
try
{
requestMessage = new LDAPMessage(nextMessageID.getAndIncrement(),
new UnbindRequestProtocolOp());
writer.writeMessage(requestMessage);
}
catch (Exception e2) {}
try
{
reader.close();
writer.close();
} catch (Exception e2) {}
return 1;
}
// Read the response from the server.
LDAPMessage responseMessage = null;
try
{
responseMessage = reader.readMessage();
}
catch (Exception e)
{
Message message = ERR_LDAPPWMOD_CANNOT_READ_PWMOD_RESPONSE.get(
String.valueOf(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
try
{
requestMessage = new LDAPMessage(nextMessageID.getAndIncrement(),
new UnbindRequestProtocolOp());
writer.writeMessage(requestMessage);
}
catch (Exception e2) {}
try
{
reader.close();
writer.close();
} catch (Exception e2) {}
return 1;
}
// Make sure that the response was acceptable.
ExtendedResponseProtocolOp extendedResponse =
responseMessage.getExtendedResponseProtocolOp();
int resultCode = extendedResponse.getResultCode();
if (resultCode != LDAPResultCode.SUCCESS)
{
Message message = ERR_LDAPPWMOD_FAILED.get(resultCode);
err.println(wrapText(message, MAX_LINE_WIDTH));
Message errorMessage = extendedResponse.getErrorMessage();
if ((errorMessage != null) && (errorMessage.length() > 0))
{
message = ERR_LDAPPWMOD_FAILURE_ERROR_MESSAGE.get(errorMessage);
err.println(wrapText(message, MAX_LINE_WIDTH));
}
DN matchedDN = extendedResponse.getMatchedDN();
if (matchedDN != null)
{
message = ERR_LDAPPWMOD_FAILURE_MATCHED_DN.get(matchedDN.toString());
err.println(wrapText(message, MAX_LINE_WIDTH));
}
try
{
requestMessage = new LDAPMessage(nextMessageID.getAndIncrement(),
new UnbindRequestProtocolOp());
writer.writeMessage(requestMessage);
}
catch (Exception e) {}
try
{
reader.close();
writer.close();
} catch (Exception e) {}
return resultCode;
}
else
{
Message message = INFO_LDAPPWMOD_SUCCESSFUL.get();
out.println(wrapText(message, MAX_LINE_WIDTH));
Message additionalInfo = extendedResponse.getErrorMessage();
if ((additionalInfo != null) && (additionalInfo.length() > 0))
{
message = INFO_LDAPPWMOD_ADDITIONAL_INFO.get(additionalInfo);
out.println(wrapText(message, MAX_LINE_WIDTH));
}
}
// See if the response included any controls that we recognize, and if so
// then handle them.
List<Control> responseControls = responseMessage.getControls();
if (responseControls != null)
{
for (Control c : responseControls)
{
if (c.getOID().equals(OID_PASSWORD_POLICY_CONTROL))
{
try
{
PasswordPolicyResponseControl pwPolicyControl =
PasswordPolicyResponseControl.DECODER
.decode(c.isCritical(), ((LDAPControl) c).getValue());
PasswordPolicyWarningType pwPolicyWarningType =
pwPolicyControl.getWarningType();
if (pwPolicyWarningType != null)
{
Message message = INFO_LDAPPWMOD_PWPOLICY_WARNING.get(
pwPolicyWarningType.toString(),
pwPolicyControl.getWarningValue());
out.println(wrapText(message, MAX_LINE_WIDTH));
}
PasswordPolicyErrorType pwPolicyErrorType =
pwPolicyControl.getErrorType();
if (pwPolicyErrorType != null)
{
Message message = INFO_LDAPPWMOD_PWPOLICY_ERROR.get(
pwPolicyErrorType.toString());
out.println(wrapText(message, MAX_LINE_WIDTH));
}
}
catch (Exception e)
{
Message message = ERR_LDAPPWMOD_CANNOT_DECODE_PWPOLICY_CONTROL.get(
String.valueOf(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
}
}
}
}
// See if the response included a generated password.
ByteString responseValue = extendedResponse.getValue();
if (responseValue != null)
{
try
{
ASN1Reader asn1Reader = ASN1.getReader(responseValue);
asn1Reader.readStartSequence();
while(asn1Reader.hasNextElement())
{
if (asn1Reader.peekType() == TYPE_PASSWORD_MODIFY_GENERATED_PASSWORD)
{
Message message = INFO_LDAPPWMOD_GENERATED_PASSWORD.get(
asn1Reader.readOctetStringAsString());
out.println(wrapText(message, MAX_LINE_WIDTH));
}
else
{
Message message = ERR_LDAPPWMOD_UNRECOGNIZED_VALUE_TYPE.get(
asn1Reader.readOctetStringAsString());
err.println(wrapText(message, MAX_LINE_WIDTH));
}
}
asn1Reader.readEndSequence();
}
catch (Exception e)
{
Message message = ERR_LDAPPWMOD_COULD_NOT_DECODE_RESPONSE_VALUE.get(
String.valueOf(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
try
{
requestMessage = new LDAPMessage(nextMessageID.getAndIncrement(),
new UnbindRequestProtocolOp());
writer.writeMessage(requestMessage);
}
catch (Exception e2) {}
try
{
reader.close();
writer.close();
} catch (Exception e2) {}
return 1;
}
}
// Unbind from the server and close the connection.
try
{
requestMessage = new LDAPMessage(nextMessageID.getAndIncrement(),
new UnbindRequestProtocolOp());
writer.writeMessage(requestMessage);
}
catch (Exception e) {}