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      public CancelRequestDecorator( LdapApiService codec, CancelRequest decoratedMessage )
57      {
58          super( codec, decoratedMessage );
59          cancelRequest = decoratedMessage;
60      }
61  
62  
63      /**
64       * {@inheritDoc}
65       */
66      public int getCancelId()
67      {
68          return cancelRequest.getCancelId();
69      }
70  
71  
72      /**
73       * {@inheritDoc}
74       */
75      public void setCancelId( int cancelId )
76      {
77          if ( cancelId == cancelRequest.getCancelId() )
78          {
79              return;
80          }
81  
82          this.requestValue = null;
83          cancelRequest.setCancelId( cancelId );
84      }
85  
86  
87      /**
88       * {@inheritDoc}
89       */
90      public byte[] getRequestValue()
91      {
92          if ( requestValue == null )
93          {
94              try
95              {
96                  requestValue = encodeInternal().array();
97              }
98              catch ( EncoderException e )
99              {
100                 LOG.error( I18n.err( I18n.ERR_04164 ), e );
101                 throw new RuntimeException( e );
102             }
103         }
104 
105         return requestValue;
106     }
107 
108 
109     /**
110      * Sets the extended request's <b>requestValue</b> portion of the PDU.
111      *
112      * @param payload byte array of data encapsulating ext. req. parameters
113      */
114     @Override
115     public void setRequestValue( byte[] requestValue )
116     {
117         CancelDecoder decoder = new CancelDecoder();
118 
119         try
120         {
121             CancelRequest cancel = decoder.decode( requestValue );
122 
123             if ( requestValue != null )
124             {
125                 this.requestValue = new byte[requestValue.length];
126                 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
127             }
128             else
129             {
130                 this.requestValue = null;
131             }
132 
133             cancelRequest.setCancelId( cancel.getCancelId() );
134         }
135         catch ( DecoderException e )
136         {
137             LOG.error( I18n.err( I18n.ERR_04165 ), e );
138             throw new RuntimeException( e );
139         }
140     }
141 
142 
143     /**
144      * Compute the Cancel length 
145      * 
146      * 0x30 L1 
147      *   | 
148      *   +--> 0x02 0x0(1-4) [0..2^31-1] 
149      */
150     /* no qualifier */ int computeLengthInternal()
151     {
152         // The messageId length
153         cancelSequenceLength = 1 + 1 + BerValue.getNbBytes( cancelRequest.getCancelId() );
154 
155         // Add the sequence and the length
156         return 1 + 1 + cancelSequenceLength;
157     }
158 
159 
160     /**
161      * Encodes the cancel extended operation.
162      * 
163      * @return A ByteBuffer that contains the encoded PDU
164      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
165      */
166     /* no qualifier */ ByteBuffer encodeInternal() throws EncoderException
167     {
168         // Allocate the bytes buffer.
169         ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
170 
171         // The sequence
172         bb.put( UniversalTag.SEQUENCE.getValue() );
173         bb.put( TLV.getBytes( cancelSequenceLength ) );
174 
175         // The messageId
176         BerValue.encode( bb, cancelRequest.getCancelId() );
177 
178         return bb;
179     }
180 }