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.intermediate.syncrepl_impl;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.DecoderException;
26  import org.apache.directory.api.asn1.ber.Asn1Container;
27  import org.apache.directory.api.asn1.ber.Asn1Decoder;
28  import org.apache.directory.api.asn1.ber.tlv.BerValue;
29  import org.apache.directory.api.asn1.util.Asn1Buffer;
30  import org.apache.directory.api.ldap.codec.api.IntermediateOperationFactory;
31  import org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValue;
32  import org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValueImpl;
33  import org.apache.directory.api.ldap.model.message.IntermediateResponse;
34  
35  
36  /**
37   * A {@link IntermediateOperationFactory} which creates {@link SyncInfoValue} instances.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev$, $Date$
41   */
42  public class SyncInfoValueFactory implements IntermediateOperationFactory
43  {
44      /**
45       * Creates a new instance of SyncInfoValueFactory.
46       */
47      public SyncInfoValueFactory()
48      {
49      }
50  
51  
52      /**
53       * {@inheritDoc}
54       */
55      @Override
56      public String getOid()
57      {
58          return SyncInfoValue.OID;
59      }
60  
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public SyncInfoValue newResponse()
67      {
68          return new SyncInfoValueImpl();
69      }
70  
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public SyncInfoValue newResponse( byte[] responseValue )
77      {
78          SyncInfoValue syncInfoValue = new SyncInfoValueImpl();
79          
80          try
81          {
82              decodeValue( syncInfoValue, responseValue );
83          }
84          catch ( DecoderException de )
85          {
86              
87          }
88  
89          return syncInfoValue;
90      }
91  
92  
93      @Override
94      public void encodeValue( Asn1Buffer buffer, IntermediateResponse intermediateResponse )
95      {
96          int start = buffer.getPos();
97          SyncInfoValue syncInfoValue = ( SyncInfoValue ) intermediateResponse;
98          
99          switch ( syncInfoValue.getSyncInfoValueType() )
100         {
101             case NEW_COOKIE:
102                 // The cookie
103                 BerValue.encodeOctetString( buffer, 
104                     ( byte ) SyncInfoValueTags.NEW_COOKIE_TAG.getValue(),
105                     syncInfoValue.getCookie() );
106                 break;
107                 
108             case REFRESH_DELETE:
109                 // The refreshDone flag if false
110                 if ( !syncInfoValue.isRefreshDone() )
111                 {
112                     BerValue.encodeBoolean( buffer, false );
113                 }
114                 
115                 // The cookie, if any
116                 if ( syncInfoValue.getCookie() != null )
117                 {
118                     BerValue.encodeOctetString( buffer, syncInfoValue.getCookie() );
119                 }
120                 
121                 // The sequence
122                 BerValue.encodeSequence( buffer, 
123                     ( byte ) SyncInfoValueTags.REFRESH_DELETE_TAG.getValue(), start );
124                 break;
125                 
126             case REFRESH_PRESENT:
127                 // The refreshDone flag if false
128                 if ( !syncInfoValue.isRefreshDone() )
129                 {
130                     BerValue.encodeBoolean( buffer, false );
131                 }
132                 
133                 // The cookie, if any
134                 if ( syncInfoValue.getCookie() != null )
135                 {
136                     BerValue.encodeOctetString( buffer, syncInfoValue.getCookie() );
137                 }
138                 
139                 // The sequence
140                 BerValue.encodeSequence( buffer, 
141                     ( byte ) SyncInfoValueTags.REFRESH_PRESENT_TAG.getValue(), start );
142                 break;
143                 
144             case SYNC_ID_SET:
145                 // The syncUUID set
146                 if ( !syncInfoValue.getSyncUUIDs().isEmpty() )
147                 {
148                     for ( int i = syncInfoValue.getSyncUUIDs().size(); i > 0; i-- )
149                     {
150                         BerValue.encodeOctetString( buffer, syncInfoValue.getSyncUUIDs().get( i - 1 ) );
151                     }
152                 }
153 
154                 // The set
155                 BerValue.encodeSet( buffer, start );
156 
157                 // The refreshDeletes flag if false
158                 if ( syncInfoValue.isRefreshDeletes() )
159                 {
160                     BerValue.encodeBoolean( buffer, true );
161                 }
162                 
163                 // The cookie, if any
164                 if ( syncInfoValue.getCookie() != null )
165                 {
166                     BerValue.encodeOctetString( buffer, syncInfoValue.getCookie() );
167                 }
168                 
169                 // The sequence
170                 BerValue.encodeSequence( buffer, 
171                     ( byte ) SyncInfoValueTags.SYNC_ID_SET_TAG.getValue(), start );
172                 break;
173                 
174             default:
175                 break;
176         }
177     }
178     
179     
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     public void decodeValue( IntermediateResponse intermediateResponse, byte[] responseValue ) throws DecoderException
185     {
186         ByteBuffer buffer = ByteBuffer.wrap( responseValue );
187         SyncInfoValueContainer container = new SyncInfoValueContainer( ( SyncInfoValue ) intermediateResponse );
188         Asn1Decoder.decode( buffer, ( Asn1Container ) container );
189     }
190 }