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   *     https://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.DecoderException;
26  import org.apache.directory.api.asn1.ber.Asn1Decoder;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.util.Asn1Buffer;
29  import org.apache.directory.api.ldap.codec.api.AbstractExtendedOperationFactory;
30  import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
31  import org.apache.directory.api.ldap.codec.api.LdapApiService;
32  import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequest;
33  import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequestImpl;
34  import org.apache.directory.api.ldap.extras.extended.cancel.CancelResponse;
35  import org.apache.directory.api.ldap.extras.extended.cancel.CancelResponseImpl;
36  import org.apache.directory.api.ldap.model.message.ExtendedRequest;
37  
38  
39  /**
40   * An {@link ExtendedOperationFactory} for creating cancel extended request response 
41   * pairs.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class CancelFactory extends AbstractExtendedOperationFactory
46  {
47      /**
48       * Creates a new instance of CancelFactory.
49       *
50       * @param codec The codec for this factory.
51       */
52      public CancelFactory( LdapApiService codec )
53      {
54          super( codec, CancelRequest.EXTENSION_OID );
55      }
56  
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public CancelRequest newRequest()
63      {
64          return new CancelRequestImpl();
65      }
66  
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public CancelRequest newRequest( byte[] encodedValue ) throws DecoderException
73      {
74          CancelRequest cancelRequest = new CancelRequestImpl();
75          decodeValue( cancelRequest, encodedValue );
76  
77          return cancelRequest;
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public CancelResponse newResponse()
86      {
87          return new CancelResponseImpl();
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void decodeValue( ExtendedRequest extendedRequest, byte[] requestValue ) throws DecoderException
96      {
97          ByteBuffer bb = ByteBuffer.wrap( requestValue );
98          CancelRequestContainer container = new CancelRequestContainer();
99          container.setCancelRequest( ( CancelRequest ) extendedRequest ); 
100         Asn1Decoder.decode( bb, container );
101     }
102 
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public void encodeValue( Asn1Buffer buffer, ExtendedRequest extendedRequest )
109     {
110         int start  = buffer.getPos();
111         CancelRequest cancelRequest = ( CancelRequest ) extendedRequest;
112         
113         // the ID
114         BerValue.encodeInteger( buffer, cancelRequest.getCancelId() );
115         
116         // The sequence
117         BerValue.encodeSequence( buffer, start );
118         
119     }
120 }