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 * https://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 */ 020package org.apache.directory.api.ldap.extras.intermediate.syncrepl_impl; 021 022 023import java.nio.ByteBuffer; 024 025import org.apache.directory.api.asn1.DecoderException; 026import org.apache.directory.api.asn1.ber.Asn1Container; 027import org.apache.directory.api.asn1.ber.Asn1Decoder; 028import org.apache.directory.api.asn1.ber.tlv.BerValue; 029import org.apache.directory.api.asn1.util.Asn1Buffer; 030import org.apache.directory.api.ldap.codec.api.IntermediateOperationFactory; 031import org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValue; 032import org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValueImpl; 033import org.apache.directory.api.ldap.model.message.IntermediateResponse; 034 035 036/** 037 * A {@link IntermediateOperationFactory} which creates {@link SyncInfoValue} instances. 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 * @version $Rev$, $Date$ 041 */ 042public class SyncInfoValueFactory implements IntermediateOperationFactory 043{ 044 /** 045 * Creates a new instance of SyncInfoValueFactory. 046 */ 047 public SyncInfoValueFactory() 048 { 049 } 050 051 052 /** 053 * {@inheritDoc} 054 */ 055 @Override 056 public String getOid() 057 { 058 return SyncInfoValue.OID; 059 } 060 061 062 /** 063 * {@inheritDoc} 064 */ 065 @Override 066 public SyncInfoValue newResponse() 067 { 068 return new SyncInfoValueImpl(); 069 } 070 071 072 /** 073 * {@inheritDoc} 074 */ 075 @Override 076 public SyncInfoValue newResponse( byte[] responseValue ) 077 { 078 SyncInfoValue syncInfoValue = new SyncInfoValueImpl(); 079 080 try 081 { 082 decodeValue( syncInfoValue, responseValue ); 083 } 084 catch ( DecoderException de ) 085 { 086 087 } 088 089 return syncInfoValue; 090 } 091 092 093 @Override 094 public void encodeValue( Asn1Buffer buffer, IntermediateResponse intermediateResponse ) 095 { 096 int start = buffer.getPos(); 097 SyncInfoValue syncInfoValue = ( SyncInfoValue ) intermediateResponse; 098 099 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}