001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.message.extended;
021
022
023 import javax.naming.NamingException;
024 import javax.naming.ldap.ExtendedResponse;
025
026 import org.apache.directory.shared.asn1.codec.DecoderException;
027 import org.apache.directory.shared.asn1.codec.EncoderException;
028 import org.apache.directory.shared.i18n.I18n;
029 import org.apache.directory.shared.ldap.codec.extended.operations.cancel.Cancel;
030 import org.apache.directory.shared.ldap.codec.extended.operations.cancel.CancelDecoder;
031 import org.apache.directory.shared.ldap.message.ExtendedRequestImpl;
032 import org.apache.directory.shared.ldap.message.internal.InternalResultResponse;
033 import org.slf4j.Logger;
034 import org.slf4j.LoggerFactory;
035
036
037 /**
038 * Implement the extended Cancel Request as described in RFC 3909.
039 *
040 * It's grammar is :
041 *
042 * cancelRequestValue ::= SEQUENCE {
043 * cancelID MessageID
044 * -- MessageID is as defined in [RFC2251]
045 * }
046 *
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 * @version $Rev$, $Date$
049 */
050 public class CancelRequest extends ExtendedRequestImpl
051 {
052 /** The serial version UUID */
053 private static final long serialVersionUID = 1L;
054
055 /** A logger for this class */
056 private static final Logger LOG = LoggerFactory.getLogger( CancelRequest.class );
057
058 /** The cancelId of the request to be canceled */
059 private int cancelId;
060
061 /** The requestName for this extended request */
062 public static final String EXTENSION_OID = "1.3.6.1.1.8";
063
064 /**
065 *
066 * Creates a new instance of CancelRequest.
067 *
068 * @param messageId the message id
069 * @param cancelId the message id of the request to cancel
070 */
071 public CancelRequest( int messageId, int cancelId )
072 {
073 super( messageId );
074 setOid( EXTENSION_OID );
075
076 this.cancelId = cancelId;
077 }
078
079
080 /**
081 * Encode the request
082 */
083 private void encodePayload() throws EncoderException
084 {
085 Cancel cancel = new Cancel();
086 cancel.setCancelId( this.cancelId );
087
088 payload = cancel.encode().array();
089 }
090
091 /**
092 * Gets the extended request's <b>requestValue</b> portion of the PDU. The
093 * form of the data is request specific and is determined by the extended
094 * request OID.
095 *
096 * @return byte array of data
097 */
098 public byte[] getPayload()
099 {
100 if ( payload == null )
101 {
102 try
103 {
104 encodePayload();
105 }
106 catch ( EncoderException e )
107 {
108 LOG.error( I18n.err( I18n.ERR_04164 ), e );
109 throw new RuntimeException( e );
110 }
111 }
112
113 return super.getPayload();
114 }
115
116
117 /**
118 * Sets the extended request's <b>requestValue</b> portion of the PDU.
119 *
120 * @param payload byte array of data encapsulating ext. req. parameters
121 */
122 public void setPayload( byte[] payload )
123 {
124 CancelDecoder decoder = new CancelDecoder();
125
126 try
127 {
128 Cancel cancel = ( Cancel ) decoder.decode( payload );
129
130 if ( payload != null )
131 {
132 this.payload = new byte[ payload.length ];
133 System.arraycopy( payload, 0, this.payload, 0, payload.length );
134 } else {
135 this.payload = null;
136 }
137 this.cancelId = cancel.getCancelId();
138 }
139 catch ( DecoderException e )
140 {
141 LOG.error( I18n.err( I18n.ERR_04165 ), e );
142 throw new RuntimeException( e );
143 }
144 }
145
146
147 public ExtendedResponse createExtendedResponse( String id, byte[] berValue, int offset, int length )
148 throws NamingException
149 {
150 return ( ExtendedResponse ) getResultResponse();
151 }
152
153
154 public byte[] getEncodedValue()
155 {
156 return getPayload();
157 }
158
159
160 public InternalResultResponse getResultResponse()
161 {
162 if ( response == null )
163 {
164 response = new CancelResponse( cancelId );
165 }
166
167 return response;
168 }
169 }
170