View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.extras.extended.ads_impl.cancel;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.Asn1Object;
26  import org.apache.directory.api.asn1.DecoderException;
27  import org.apache.directory.api.asn1.EncoderException;
28  import org.apache.directory.api.asn1.ber.tlv.BerValue;
29  import org.apache.directory.api.asn1.ber.tlv.TLV;
30  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
31  import org.apache.directory.api.i18n.I18n;
32  import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator;
33  import org.apache.directory.api.ldap.codec.api.LdapApiService;
34  import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequest;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * A Decorator for CancelRequests.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class CancelRequestDecorator extends ExtendedRequestDecorator<CancelRequest> implements
45      CancelRequest, Asn1Object
46  {
47      private static final Logger LOG = LoggerFactory.getLogger( CancelRequestDecorator.class );
48  
49      /** The Id of the the message to cancel */
50      private CancelRequest cancelRequest;
51  
52      /** Length of the sequence */
53      private int cancelSequenceLength;
54  
55  
56      /**
57       * Creates a new instance of CancelRequestDecorator.
58       * 
59       * @param codec The LDAP Service to use
60       * @param decoratedMessage The canceled request
61       */
62      public CancelRequestDecorator( LdapApiService codec, CancelRequest decoratedMessage )
63      {
64          super( codec, decoratedMessage );
65          cancelRequest = decoratedMessage;
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public int getCancelId()
74      {
75          return cancelRequest.getCancelId();
76      }
77  
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public void setCancelId( int cancelId )
84      {
85          if ( cancelId == cancelRequest.getCancelId() )
86          {
87              return;
88          }
89  
90          this.requestValue = null;
91          cancelRequest.setCancelId( cancelId );
92      }
93  
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public byte[] getRequestValue()
100     {
101         if ( requestValue == null )
102         {
103             try
104             {
105                 requestValue = encodeInternal().array();
106             }
107             catch ( EncoderException e )
108             {
109                 LOG.error( I18n.err( I18n.ERR_04164 ), e );
110                 throw new RuntimeException( e );
111             }
112         }
113 
114         return requestValue;
115     }
116 
117 
118     /**
119      * Sets the extended request's <b>requestValue</b> portion of the PDU.
120      *
121      * @param requestValue byte array of data encapsulating ext. req. parameters
122      */
123     @Override
124     public void setRequestValue( byte[] requestValue )
125     {
126         CancelDecoder decoder = new CancelDecoder();
127 
128         try
129         {
130             if ( requestValue != null )
131             {
132                 CancelRequest cancel = decoder.decode( requestValue );
133                 cancelRequest.setCancelId( cancel.getCancelId() );
134 
135                 this.requestValue = new byte[requestValue.length];
136                 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
137             }
138             else
139             {
140                 this.requestValue = null;
141                 cancelRequest.setCancelId( 0 );
142             }
143 
144         }
145         catch ( DecoderException e )
146         {
147             LOG.error( I18n.err( I18n.ERR_04165 ), e );
148             throw new RuntimeException( e );
149         }
150     }
151 
152 
153     /**
154      * Compute the Cancel length 
155      * <pre>
156      * 0x30 L1 
157      *   | 
158      *   +--&gt; 0x02 0x0(1-4) [0..2^31-1]
159      * </pre> 
160      */
161     /* no qualifier */int computeLengthInternal()
162     {
163         // The messageId length
164         cancelSequenceLength = 1 + 1 + BerValue.getNbBytes( cancelRequest.getCancelId() );
165 
166         // Add the sequence and the length
167         return 1 + 1 + cancelSequenceLength;
168     }
169 
170 
171     /**
172      * Encodes the cancel extended operation.
173      * 
174      * @return A ByteBuffer that contains the encoded PDU
175      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
176      */
177     /* no qualifier */ByteBuffer encodeInternal() throws EncoderException
178     {
179         // Allocate the bytes buffer.
180         ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
181 
182         // The sequence
183         bb.put( UniversalTag.SEQUENCE.getValue() );
184         bb.put( TLV.getBytes( cancelSequenceLength ) );
185 
186         // The messageId
187         BerValue.encode( bb, cancelRequest.getCancelId() );
188 
189         return bb;
190     }
191 }