* @throws TaskClientException if there is no task with the requested id
*/
public synchronized void cancelTask(String id)
throws TaskClientException, IOException, ASN1Exception, LDAPException
{
LDAPReader reader = connection.getLDAPReader();
LDAPWriter writer = connection.getLDAPWriter();
TaskEntry entry = getTaskEntry(id);
TaskState state = entry.getTaskState();
if (state != null) {
if (!TaskState.isDone(state)) {
ByteString dn = ByteString.valueOf(entry.getDN().toString());
ArrayList<RawModification> mods = new ArrayList<RawModification>();
ArrayList<ByteString> values = new ArrayList<ByteString>();
String newState;
if (TaskState.isPending(state)) {
newState = TaskState.CANCELED_BEFORE_STARTING.name();
} else {
newState = TaskState.STOPPED_BY_ADMINISTRATOR.name();
}
values.add(ByteString.valueOf(newState));
LDAPAttribute attr = new LDAPAttribute(ATTR_TASK_STATE, values);
mods.add(new LDAPModification(ModificationType.REPLACE, attr));
ModifyRequestProtocolOp modRequest =
new ModifyRequestProtocolOp(dn, mods);
LDAPMessage requestMessage =
new LDAPMessage(nextMessageID.getAndIncrement(), modRequest, null);
writer.writeMessage(requestMessage);
LDAPMessage responseMessage = reader.readMessage();
if (responseMessage == null) {
Message message = ERR_TASK_CLIENT_UNEXPECTED_CONNECTION_CLOSURE.get();
throw new LDAPException(UNAVAILABLE.getIntValue(), message);
}
if (responseMessage.getProtocolOpType() !=
LDAPConstants.OP_TYPE_MODIFY_RESPONSE)
{
throw new LDAPException(
LDAPResultCode.CLIENT_SIDE_LOCAL_ERROR,
ERR_TASK_CLIENT_INVALID_RESPONSE_TYPE.get(
responseMessage.getProtocolOpName()));
}
ModifyResponseProtocolOp modResponse =
responseMessage.getModifyResponseProtocolOp();
Message errorMessage = modResponse.getErrorMessage();
if (errorMessage != null) {
throw new LDAPException(
LDAPResultCode.CLIENT_SIDE_LOCAL_ERROR,
errorMessage);
}
} else if (TaskState.isRecurring(state)) {
ByteString dn = ByteString.valueOf(entry.getDN().toString());
DeleteRequestProtocolOp deleteRequest =
new DeleteRequestProtocolOp(dn);
LDAPMessage requestMessage = new LDAPMessage(
nextMessageID.getAndIncrement(), deleteRequest, null);
writer.writeMessage(requestMessage);
LDAPMessage responseMessage = reader.readMessage();
if (responseMessage == null) {
Message message = ERR_TASK_CLIENT_UNEXPECTED_CONNECTION_CLOSURE.get();
throw new LDAPException(UNAVAILABLE.getIntValue(), message);
}