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