001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2012-2016 ForgeRock AS.
016 */
017package org.opends.server.extensions;
018
019import org.forgerock.i18n.LocalizableMessage;
020import org.forgerock.opendj.server.config.server.CancelExtendedOperationHandlerCfg;
021import org.opends.server.api.ClientConnection;
022import org.opends.server.api.ExtendedOperationHandler;
023import org.forgerock.opendj.config.server.ConfigException;
024import org.opends.server.core.ExtendedOperation;
025import org.forgerock.i18n.slf4j.LocalizedLogger;
026import org.forgerock.opendj.io.ASN1;
027import org.forgerock.opendj.io.ASN1Reader;
028import org.opends.server.types.*;
029import org.forgerock.opendj.ldap.ResultCode;
030import org.forgerock.opendj.ldap.ByteString;
031import static org.opends.messages.ExtensionMessages.*;
032import static org.opends.server.util.ServerConstants.*;
033import static org.opends.server.util.StaticUtils.*;
034
035/**
036 * This class implements the LDAP cancel extended operation defined in RFC 3909.
037 * It is similar to the LDAP abandon operation, with the exception that it
038 * requires a response for both the operation that is cancelled and the cancel
039 * request (whereas an abandon request never has a response, and if it is
040 * successful the abandoned operation won't get one either).
041 */
042public class CancelExtendedOperation
043       extends ExtendedOperationHandler<CancelExtendedOperationHandlerCfg>
044{
045  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
046
047  /**
048   * Create an instance of this cancel extended operation.  All initialization
049   * should be performed in the <CODE>initializeExtendedOperationHandler</CODE>
050   * method.
051   */
052  public CancelExtendedOperation()
053  {
054    super();
055  }
056
057  @Override
058  public void initializeExtendedOperationHandler(
059                   CancelExtendedOperationHandlerCfg config)
060         throws ConfigException, InitializationException
061  {
062    super.initializeExtendedOperationHandler(config);
063  }
064
065  /**
066   * Processes the provided extended operation.
067   *
068   * @param  operation  The extended operation to be processed.
069   */
070  @Override
071  public void processExtendedOperation(ExtendedOperation operation)
072  {
073    // The value of the request must be a sequence containing an integer element
074    // that holds the message ID of the operation to cancel.  If there is no
075    // value or it cannot be decoded, then fail.
076    int idToCancel;
077    ByteString requestValue = operation.getRequestValue();
078    if (requestValue == null)
079    {
080      operation.setResultCode(ResultCode.PROTOCOL_ERROR);
081      operation.appendErrorMessage(ERR_EXTOP_CANCEL_NO_REQUEST_VALUE.get());
082      return;
083    }
084
085    try
086    {
087      ASN1Reader reader = ASN1.getReader(requestValue);
088      reader.readStartSequence();
089      idToCancel = (int)reader.readInteger();
090      reader.readEndSequence();
091    }
092    catch (Exception e)
093    {
094      logger.traceException(e);
095
096      operation.setResultCode(ResultCode.PROTOCOL_ERROR);
097
098      LocalizableMessage message = ERR_EXTOP_CANCEL_CANNOT_DECODE_REQUEST_VALUE.get(
099              getExceptionMessage(e));
100      operation.appendErrorMessage(message);
101      return;
102    }
103
104    // Create the cancel request for the target operation.
105    LocalizableMessage cancelReason =
106        INFO_EXTOP_CANCEL_REASON.get(operation.getMessageID());
107    CancelRequest cancelRequest = new CancelRequest(true, cancelReason);
108
109    // Get the client connection and attempt the cancel.
110    ClientConnection clientConnection = operation.getClientConnection();
111    CancelResult cancelResult = clientConnection.cancelOperation(idToCancel,
112                                                                 cancelRequest);
113
114    // Update the result of the extended operation and return.
115    ResultCode resultCode = cancelResult.getResultCode();
116    operation.setResultCode(resultCode == ResultCode.CANCELLED
117                                ? ResultCode.SUCCESS : resultCode);
118    operation.appendErrorMessage(cancelResult.getResponseMessage());
119  }
120
121  @Override
122  public String getExtendedOperationOID()
123  {
124    return OID_CANCEL_REQUEST;
125  }
126
127  @Override
128  public String getExtendedOperationName()
129  {
130    return "Cancel";
131  }
132}