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 * http://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.codec.decorators; 021 022 023import java.nio.BufferOverflowException; 024import java.nio.ByteBuffer; 025 026import org.apache.directory.api.asn1.EncoderException; 027import org.apache.directory.api.asn1.ber.tlv.TLV; 028import org.apache.directory.api.i18n.I18n; 029import org.apache.directory.api.ldap.codec.api.LdapApiService; 030import org.apache.directory.api.ldap.codec.api.LdapConstants; 031import org.apache.directory.api.ldap.model.message.AddResponse; 032 033 034/** 035 * A decorator for the AddResponse message 036 * 037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 038 */ 039public class AddResponseDecorator extends ResponseDecorator<AddResponse> implements AddResponse 040{ 041 /** The encoded addResponse length */ 042 private int addResponseLength; 043 044 045 /** 046 * Makes a AddResponse a MessageDecorator. 047 * 048 * @param decoratedMessage the decorated AddResponse 049 */ 050 public AddResponseDecorator( LdapApiService codec, AddResponse decoratedMessage ) 051 { 052 super( codec, decoratedMessage ); 053 } 054 055 056 /** 057 * @return The decorated AddResponse 058 */ 059 public AddResponse getAddResponse() 060 { 061 return ( AddResponse ) getDecorated(); 062 } 063 064 065 /** 066 * Stores the encoded length for the AddResponse 067 * @param addResponseLength The encoded length 068 */ 069 public void setAddResponseLength( int addResponseLength ) 070 { 071 this.addResponseLength = addResponseLength; 072 } 073 074 075 /** 076 * @return The encoded AddResponse's length 077 */ 078 public int getAddResponseLength() 079 { 080 return addResponseLength; 081 } 082 083 084 //------------------------------------------------------------------------- 085 // The Decorator methods 086 //------------------------------------------------------------------------- 087 /** 088 * Compute the AddResponse length 089 * 090 * AddResponse : 091 * 092 * 0x69 L1 093 * | 094 * +--> LdapResult 095 * 096 * L1 = Length(LdapResult) 097 * 098 * Length(AddResponse) = Length(0x69) + Length(L1) + L1 099 */ 100 public int computeLength() 101 { 102 AddResponse addResponse = getAddResponse(); 103 setLdapResult( new LdapResultDecorator( addResponse.getLdapResult() ) ); 104 int addResponseLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength(); 105 106 setAddResponseLength( addResponseLength ); 107 108 return 1 + TLV.getNbBytes( addResponseLength ) + addResponseLength; 109 } 110 111 112 /** 113 * Encode the AddResponse message to a PDU. 114 * 115 * @param buffer The buffer where to put the PDU 116 * @param addResponseDecorator the AddResponse decorator 117 */ 118 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException 119 { 120 try 121 { 122 // The AddResponse Tag 123 buffer.put( LdapConstants.ADD_RESPONSE_TAG ); 124 buffer.put( TLV.getBytes( getAddResponseLength() ) ); 125 126 // The LdapResult 127 ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer ); 128 129 return buffer; 130 } 131 catch ( BufferOverflowException boe ) 132 { 133 throw new EncoderException( I18n.err( I18n.ERR_04005 ) ); 134 } 135 } 136}