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-2014 ForgeRock AS. 016 */ 017package org.opends.server.extensions; 018 019import org.forgerock.i18n.LocalizableMessage; 020import org.opends.server.admin.std.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 /** {@inheritDoc} */ 058 @Override 059 public void initializeExtendedOperationHandler( 060 CancelExtendedOperationHandlerCfg config) 061 throws ConfigException, InitializationException 062 { 063 super.initializeExtendedOperationHandler(config); 064 } 065 066 /** 067 * Processes the provided extended operation. 068 * 069 * @param operation The extended operation to be processed. 070 */ 071 @Override 072 public void processExtendedOperation(ExtendedOperation operation) 073 { 074 // The value of the request must be a sequence containing an integer element 075 // that holds the message ID of the operation to cancel. If there is no 076 // value or it cannot be decoded, then fail. 077 int idToCancel; 078 ByteString requestValue = operation.getRequestValue(); 079 if (requestValue == null) 080 { 081 operation.setResultCode(ResultCode.PROTOCOL_ERROR); 082 operation.appendErrorMessage(ERR_EXTOP_CANCEL_NO_REQUEST_VALUE.get()); 083 return; 084 } 085 086 try 087 { 088 ASN1Reader reader = ASN1.getReader(requestValue); 089 reader.readStartSequence(); 090 idToCancel = (int)reader.readInteger(); 091 reader.readEndSequence(); 092 } 093 catch (Exception e) 094 { 095 logger.traceException(e); 096 097 operation.setResultCode(ResultCode.PROTOCOL_ERROR); 098 099 LocalizableMessage message = ERR_EXTOP_CANCEL_CANNOT_DECODE_REQUEST_VALUE.get( 100 getExceptionMessage(e)); 101 operation.appendErrorMessage(message); 102 return; 103 } 104 105 106 // Create the cancel request for the target operation. 107 LocalizableMessage cancelReason = 108 INFO_EXTOP_CANCEL_REASON.get(operation.getMessageID()); 109 CancelRequest cancelRequest = new CancelRequest(true, cancelReason); 110 111 112 // Get the client connection and attempt the cancel. 113 ClientConnection clientConnection = operation.getClientConnection(); 114 CancelResult cancelResult = clientConnection.cancelOperation(idToCancel, 115 cancelRequest); 116 117 118 // Update the result of the extended operation and return. 119 ResultCode resultCode = cancelResult.getResultCode(); 120 operation.setResultCode(resultCode == ResultCode.CANCELLED 121 ? ResultCode.SUCCESS : resultCode); 122 operation.appendErrorMessage(cancelResult.getResponseMessage()); 123 } 124 125 /** {@inheritDoc} */ 126 @Override 127 public String getExtendedOperationOID() 128 { 129 return OID_CANCEL_REQUEST; 130 } 131 132 /** {@inheritDoc} */ 133 @Override 134 public String getExtendedOperationName() 135 { 136 return "Cancel"; 137 } 138}