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.BerValue; 028import org.apache.directory.api.asn1.ber.tlv.TLV; 029import org.apache.directory.api.i18n.I18n; 030import org.apache.directory.api.ldap.codec.api.LdapApiService; 031import org.apache.directory.api.ldap.codec.api.LdapCodecConstants; 032import org.apache.directory.api.ldap.codec.api.LdapEncoder; 033import org.apache.directory.api.ldap.codec.api.MessageDecorator; 034import org.apache.directory.api.ldap.model.message.Referral; 035import org.apache.directory.api.ldap.model.message.SearchResultReference; 036 037 038/** 039 * A decorator for the SearchResultReference message 040 * 041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 042 */ 043public class SearchResultReferenceDecorator extends MessageDecorator<SearchResultReference> 044 implements SearchResultReference 045{ 046 /** The search result reference length */ 047 private int searchResultReferenceLength; 048 049 050 /** 051 * Makes a SearchResultReference encodable. 052 * 053 * @param codec The LDAP service instance 054 * @param decoratedMessage the decorated SearchResultReference 055 */ 056 public SearchResultReferenceDecorator( LdapApiService codec, SearchResultReference decoratedMessage ) 057 { 058 super( codec, decoratedMessage ); 059 } 060 061 062 //------------------------------------------------------------------------- 063 // The SearchResultReference methods 064 //------------------------------------------------------------------------- 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Override 070 public Referral getReferral() 071 { 072 return getDecorated().getReferral(); 073 } 074 075 076 /** 077 * {@inheritDoc} 078 */ 079 @Override 080 public void setReferral( Referral referral ) 081 { 082 getDecorated().setReferral( referral ); 083 } 084 085 086 //------------------------------------------------------------------------- 087 // The Decorator methods 088 //------------------------------------------------------------------------- 089 090 /** 091 * Compute the SearchResultReference length 092 * <br> 093 * SearchResultReference : 094 * <pre> 095 * 0x73 L1 096 * | 097 * +--> 0x04 L2 reference 098 * +--> 0x04 L3 reference 099 * +--> ... 100 * +--> 0x04 Li reference 101 * +--> ... 102 * +--> 0x04 Ln reference 103 * 104 * L1 = n*Length(0x04) + sum(Length(Li)) + sum(Length(reference[i])) 105 * 106 * Length(SearchResultReference) = Length(0x73 + Length(L1) + L1 107 * </pre> 108 * 109 * @return The encoded length 110 */ 111 @Override 112 public int computeLength() 113 { 114 searchResultReferenceLength = 0; 115 116 // We may have more than one reference. 117 Referral referral = getReferral(); 118 119 int referralLength = LdapEncoder.computeReferralLength( referral ); 120 121 if ( referralLength != 0 ) 122 { 123 setReferral( referral ); 124 125 searchResultReferenceLength = referralLength; 126 } 127 128 return 1 + TLV.getNbBytes( searchResultReferenceLength ) + searchResultReferenceLength; 129 } 130 131 132 /** 133 * Encode the SearchResultReference message to a PDU. 134 * <br> 135 * SearchResultReference : 136 * <pre> 137 * 0x73 LL 138 * 0x04 LL reference 139 * [0x04 LL reference]* 140 * </pre> 141 * 142 * @param buffer The buffer where to put the PDU 143 * @return The PDU. 144 */ 145 @Override 146 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException 147 { 148 SearchResultReference searchResultReference = getDecorated(); 149 try 150 { 151 // The SearchResultReference Tag 152 buffer.put( LdapCodecConstants.SEARCH_RESULT_REFERENCE_TAG ); 153 buffer.put( TLV.getBytes( searchResultReferenceLength ) ); 154 155 // The referrals, if any 156 Referral referral = searchResultReference.getReferral(); 157 158 if ( referral != null ) 159 { 160 // Each referral 161 for ( byte[] ldapUrlBytes : referral.getLdapUrlsBytes() ) 162 { 163 // Encode the current referral 164 BerValue.encode( buffer, ldapUrlBytes ); 165 } 166 } 167 } 168 catch ( BufferOverflowException boe ) 169 { 170 throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe ); 171 } 172 173 return buffer; 174 } 175}