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 */
020 package org.apache.directory.shared.ldap.codec.search;
021
022
023 import java.nio.BufferOverflowException;
024 import java.nio.ByteBuffer;
025
026 import org.apache.directory.shared.asn1.ber.tlv.TLV;
027 import org.apache.directory.shared.asn1.codec.EncoderException;
028 import org.apache.directory.shared.i18n.I18n;
029 import org.apache.directory.shared.ldap.codec.LdapConstants;
030 import org.apache.directory.shared.ldap.codec.LdapResponseCodec;
031 import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
032
033
034 /**
035 * A SearchResultDone Message. Its syntax is :
036 *
037 * SearchResultDone ::= [APPLICATION 5]
038 *
039 * LDAPResult It's a Response, so it inherites from LdapResponse.
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Dim, 21 fév 2010) $,
043 */
044 public class SearchResultDoneCodec extends LdapResponseCodec
045 {
046 // ~ Constructors
047 // -------------------------------------------------------------------------------
048
049 /**
050 * Creates a new SearchResultDone object.
051 */
052 public SearchResultDoneCodec()
053 {
054 super();
055 }
056
057
058 /**
059 * Get the message type
060 *
061 * @return Returns the type.
062 */
063 public MessageTypeEnum getMessageType()
064 {
065 return MessageTypeEnum.SEARCH_RESULT_DONE;
066 }
067
068
069 /**
070 * {@inheritDoc}
071 */
072 public String getMessageTypeName()
073 {
074 return "SEARCH_RESULT_DONE";
075 }
076
077
078 /**
079 * Compute the SearchResultDone length
080 *
081 * SearchResultDone :
082 * <pre>
083 * 0x65 L1
084 * |
085 * +--> LdapResult
086 *
087 * L1 = Length(LdapResult)
088 * Length(SearchResultDone) = Length(0x65) + Length(L1) + L1
089 * </pre>
090 */
091 protected int computeLengthProtocolOp()
092 {
093 int ldapResponseLength = computeLdapResultLength();
094
095 return 1 + TLV.getNbBytes( ldapResponseLength ) + ldapResponseLength;
096 }
097
098
099 /**
100 * Encode the SearchResultDone message to a PDU.
101 *
102 * @param buffer The buffer where to put the PDU
103 * @return The PDU.
104 */
105 protected void encodeProtocolOp( ByteBuffer buffer ) throws EncoderException
106 {
107 try
108 {
109 // The tag
110 buffer.put( LdapConstants.SEARCH_RESULT_DONE_TAG );
111 buffer.put( TLV.getBytes( getLdapResponseLength() ) );
112 }
113 catch ( BufferOverflowException boe )
114 {
115 throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
116 }
117
118 // The ldapResult
119 super.encode( buffer );
120 }
121
122
123 /**
124 * Get a String representation of a SearchResultDone
125 *
126 * @return A SearchResultDone String
127 */
128 public String toString()
129 {
130
131 StringBuffer sb = new StringBuffer();
132
133 sb.append( " Search Result Done\n" );
134 sb.append( super.toString() );
135
136 return sb.toString();
137 }
138 }