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