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.asn1.ber.tlv.Value; 029import org.apache.directory.shared.i18n.I18n; 030import org.apache.directory.shared.ldap.codec.api.LdapApiService; 031import org.apache.directory.shared.ldap.codec.api.LdapConstants; 032import org.apache.directory.shared.ldap.codec.api.LdapEncoder; 033import org.apache.directory.shared.ldap.codec.api.MessageDecorator; 034import org.apache.directory.shared.ldap.model.message.Referral; 035import org.apache.directory.shared.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 length of the referral */ 047 private int referralLength; 048 049 /** The search result reference length */ 050 private int searchResultReferenceLength; 051 052 053 /** 054 * Makes a SearchResultReference encodable. 055 * 056 * @param decoratedMessage the decorated SearchResultReference 057 */ 058 public SearchResultReferenceDecorator( LdapApiService codec, SearchResultReference decoratedMessage ) 059 { 060 super( codec, decoratedMessage ); 061 } 062 063 064 /** 065 * @return The encoded Referral's length 066 */ 067 public int getReferralLength() 068 { 069 return referralLength; 070 } 071 072 073 /** 074 * Stores the encoded length for the Referrals 075 * @param referralLength The encoded length 076 */ 077 public void setReferralLength( int referralLength ) 078 { 079 this.referralLength = referralLength; 080 } 081 082 083 /** 084 * @return The encoded SearchResultReference's length 085 */ 086 public int getSearchResultReferenceLength() 087 { 088 return searchResultReferenceLength; 089 } 090 091 092 /** 093 * Stores the encoded length for the SearchResultReference's 094 * @param searchResultReferenceLength The encoded length 095 */ 096 public void setSearchResultReferenceLength( int searchResultReferenceLength ) 097 { 098 this.searchResultReferenceLength = searchResultReferenceLength; 099 } 100 101 102 //------------------------------------------------------------------------- 103 // The SearchResultReference methods 104 //------------------------------------------------------------------------- 105 106 107 /** 108 * {@inheritDoc} 109 */ 110 public Referral getReferral() 111 { 112 return getDecorated().getReferral(); 113 } 114 115 116 /** 117 * {@inheritDoc} 118 */ 119 public void setReferral( Referral referral ) 120 { 121 getDecorated().setReferral( referral ); 122 } 123 124 125 //------------------------------------------------------------------------- 126 // The Decorator methods 127 //------------------------------------------------------------------------- 128 129 130 /** 131 * Compute the SearchResultReference length 132 * 133 * SearchResultReference : 134 * <pre> 135 * 0x73 L1 136 * | 137 * +--> 0x04 L2 reference 138 * +--> 0x04 L3 reference 139 * +--> ... 140 * +--> 0x04 Li reference 141 * +--> ... 142 * +--> 0x04 Ln reference 143 * 144 * L1 = n*Length(0x04) + sum(Length(Li)) + sum(Length(reference[i])) 145 * 146 * Length(SearchResultReference) = Length(0x73 + Length(L1) + L1 147 * </pre> 148 */ 149 public int computeLength() 150 { 151 int searchResultReferenceLength = 0; 152 153 // We may have more than one reference. 154 Referral referral = getReferral(); 155 156 int referralLength = LdapEncoder.computeReferralLength( referral ); 157 158 if ( referralLength != 0 ) 159 { 160 setReferral( referral ); 161 162 searchResultReferenceLength = referralLength; 163 } 164 165 // Store the length of the response 166 setSearchResultReferenceLength( searchResultReferenceLength ); 167 168 return 1 + TLV.getNbBytes( searchResultReferenceLength ) + searchResultReferenceLength; 169 } 170 171 172 /** 173 * Encode the SearchResultReference message to a PDU. 174 * 175 * SearchResultReference : 176 * <pre> 177 * 0x73 LL 178 * 0x04 LL reference 179 * [0x04 LL reference]* 180 * </pre> 181 * @param buffer The buffer where to put the PDU 182 * @param searchResultReferenceDecorator The SearchResultReference decorator 183 * @return The PDU. 184 */ 185 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException 186 { 187 SearchResultReference searchResultReference = getDecorated(); 188 try 189 { 190 // The SearchResultReference Tag 191 buffer.put( LdapConstants.SEARCH_RESULT_REFERENCE_TAG ); 192 buffer.put( TLV.getBytes( getSearchResultReferenceLength() ) ); 193 194 // The referrals, if any 195 Referral referral = searchResultReference.getReferral(); 196 197 if ( referral != null ) 198 { 199 // Each referral 200 for ( byte[] ldapUrlBytes : referral.getLdapUrlsBytes() ) 201 { 202 // Encode the current referral 203 Value.encode( buffer, ldapUrlBytes ); 204 } 205 } 206 } 207 catch ( BufferOverflowException boe ) 208 { 209 throw new EncoderException( I18n.err( I18n.ERR_04005 ) ); 210 } 211 212 return buffer; 213 } 214}