001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.protocols.ldap;
028    import org.opends.messages.Message;
029    
030    
031    
032    import org.opends.server.protocols.asn1.ASN1Element;
033    import org.opends.server.types.LDAPException;
034    
035    import static org.opends.messages.ProtocolMessages.*;
036    import static org.opends.server.protocols.ldap.LDAPConstants.*;
037    import static org.opends.server.protocols.ldap.LDAPResultCode.*;
038    
039    
040    
041    /**
042     * This class defines the structures and methods for an LDAP protocol op, which
043     * is the core of an LDAP message.
044     */
045    public abstract class ProtocolOp
046    {
047      /**
048       * Retrieves the BER type for this protocol op.
049       *
050       * @return  The BER type for this protocol op.
051       */
052      public abstract byte getType();
053    
054    
055    
056      /**
057       * Retrieves the name for this protocol op type.
058       *
059       * @return  The name for this protocol op type.
060       */
061      public abstract String getProtocolOpName();
062    
063    
064    
065      /**
066       * Encodes this protocol op to an ASN.1 element suitable for including in an
067       * LDAP message.
068       *
069       * @return  The ASN.1 element containing the encoded protocol op.
070       */
071      public abstract ASN1Element encode();
072    
073    
074    
075      /**
076       * Decodes the provided ASN.1 element as an LDAP protocol op.
077       *
078       * @param  element  The ASN.1 element containing the encoded LDAP protocol op.
079       *
080       * @return  The LDAP protocol op decoded from the provided ASN.1 element.
081       *
082       * @throws  LDAPException  If a problem occurs while trying to decode the
083       *                         provided ASN.1 element as an LDAP protocol op.
084       */
085      public static ProtocolOp decode(ASN1Element element)
086             throws LDAPException
087      {
088        if (element == null)
089        {
090          Message message = ERR_LDAP_PROTOCOL_OP_DECODE_NULL.get();
091          throw new LDAPException(PROTOCOL_ERROR, message);
092        }
093    
094        switch (element.getType())
095        {
096          case OP_TYPE_UNBIND_REQUEST:                                       // 0x42
097            return UnbindRequestProtocolOp.decodeUnbindRequest(element);
098          case 0x43:                                                         // 0x43
099          case 0x44:                                                         // 0x44
100          case 0x45:                                                         // 0x45
101          case 0x46:                                                         // 0x46
102          case 0x47:                                                         // 0x47
103          case 0x48:                                                         // 0x48
104          case 0x49:                                                         // 0x49
105            Message message =
106                ERR_LDAP_PROTOCOL_OP_DECODE_INVALID_TYPE.get(element.getType());
107            throw new LDAPException(PROTOCOL_ERROR, message);
108          case OP_TYPE_DELETE_REQUEST:                                       // 0x4A
109            return DeleteRequestProtocolOp.decodeDeleteRequest(element);
110          case 0x4B:                                                         // 0x4B
111          case 0x4C:                                                         // 0x4C
112          case 0x4D:                                                         // 0x4D
113          case 0x4E:                                                         // 0x4E
114          case 0x4F:                                                         // 0x4F
115            message =
116                ERR_LDAP_PROTOCOL_OP_DECODE_INVALID_TYPE.get(element.getType());
117            throw new LDAPException(PROTOCOL_ERROR, message);
118          case OP_TYPE_ABANDON_REQUEST:                                      // 0x50
119            return AbandonRequestProtocolOp.decodeAbandonRequest(element);
120          case 0x51:                                                         // 0x51
121          case 0x52:                                                         // 0x52
122          case 0x53:                                                         // 0x53
123          case 0x54:                                                         // 0x54
124          case 0x55:                                                         // 0x55
125          case 0x56:                                                         // 0x56
126          case 0x57:                                                         // 0x57
127          case 0x58:                                                         // 0x58
128          case 0x59:                                                         // 0x59
129          case 0x5A:                                                         // 0x5A
130          case 0x5B:                                                         // 0x5B
131          case 0x5C:                                                         // 0x5C
132          case 0x5D:                                                         // 0x5D
133          case 0x5E:                                                         // 0x5E
134          case 0x5F:                                                         // 0x5F
135            message =
136                ERR_LDAP_PROTOCOL_OP_DECODE_INVALID_TYPE.get(element.getType());
137            throw new LDAPException(PROTOCOL_ERROR, message);
138          case OP_TYPE_BIND_REQUEST:                                         // 0x60
139            return BindRequestProtocolOp.decodeBindRequest(element);
140          case OP_TYPE_BIND_RESPONSE:                                        // 0x61
141            return BindResponseProtocolOp.decodeBindResponse(element);
142          case 0x62:                                                         // 0x62
143            message =
144                ERR_LDAP_PROTOCOL_OP_DECODE_INVALID_TYPE.get(element.getType());
145            throw new LDAPException(PROTOCOL_ERROR, message);
146          case OP_TYPE_SEARCH_REQUEST:                                       // 0x63
147            return SearchRequestProtocolOp.decodeSearchRequest(element);
148          case OP_TYPE_SEARCH_RESULT_ENTRY:                                  // 0x64
149            return SearchResultEntryProtocolOp.decodeSearchEntry(element);
150          case OP_TYPE_SEARCH_RESULT_DONE:                                   // 0x65
151            return SearchResultDoneProtocolOp.decodeSearchDone(element);
152          case OP_TYPE_MODIFY_REQUEST:                                       // 0x66
153            return ModifyRequestProtocolOp.decodeModifyRequest(element);
154          case OP_TYPE_MODIFY_RESPONSE:                                      // 0x67
155            return ModifyResponseProtocolOp.decodeModifyResponse(element);
156          case OP_TYPE_ADD_REQUEST:                                          // 0x68
157            return AddRequestProtocolOp.decodeAddRequest(element);
158          case OP_TYPE_ADD_RESPONSE:                                         // 0x69
159            return AddResponseProtocolOp.decodeAddResponse(element);
160          case 0x6A:                                                         // 0x6A
161            message =
162                ERR_LDAP_PROTOCOL_OP_DECODE_INVALID_TYPE.get(element.getType());
163            throw new LDAPException(PROTOCOL_ERROR, message);
164          case OP_TYPE_DELETE_RESPONSE:                                      // 0x6B
165            return DeleteResponseProtocolOp.decodeDeleteResponse(element);
166          case OP_TYPE_MODIFY_DN_REQUEST:                                    // 0x6C
167            return ModifyDNRequestProtocolOp.decodeModifyDNRequest(element);
168          case OP_TYPE_MODIFY_DN_RESPONSE:                                   // 0x6D
169            return ModifyDNResponseProtocolOp.decodeModifyDNResponse(element);
170          case OP_TYPE_COMPARE_REQUEST:                                      // 0x6E
171            return CompareRequestProtocolOp.decodeCompareRequest(element);
172          case OP_TYPE_COMPARE_RESPONSE:                                     // 0x6F
173            return CompareResponseProtocolOp.decodeCompareResponse(element);
174          case 0x70:                                                         // 0x70
175          case 0x71:                                                         // 0x71
176          case 0x72:                                                         // 0x72
177            message =
178                ERR_LDAP_PROTOCOL_OP_DECODE_INVALID_TYPE.get(element.getType());
179            throw new LDAPException(PROTOCOL_ERROR, message);
180          case OP_TYPE_SEARCH_RESULT_REFERENCE:                              // 0x73
181            return SearchResultReferenceProtocolOp.decodeSearchReference(element);
182          case 0x74:                                                         // 0x74
183          case 0x75:                                                         // 0x75
184          case 0x76:                                                         // 0x76
185            message =
186                ERR_LDAP_PROTOCOL_OP_DECODE_INVALID_TYPE.get(element.getType());
187            throw new LDAPException(PROTOCOL_ERROR, message);
188          case OP_TYPE_EXTENDED_REQUEST:                                     // 0x77
189            return ExtendedRequestProtocolOp.decodeExtendedRequest(element);
190          case OP_TYPE_EXTENDED_RESPONSE:                                    // 0x78
191            return ExtendedResponseProtocolOp.decodeExtendedResponse(element);
192          case OP_TYPE_INTERMEDIATE_RESPONSE:                                // 0x79
193            return
194                 IntermediateResponseProtocolOp.decodeIntermediateResponse(element);
195          default:
196            message =
197                ERR_LDAP_PROTOCOL_OP_DECODE_INVALID_TYPE.get(element.getType());
198            throw new LDAPException(PROTOCOL_ERROR, message);
199        }
200      }
201    
202    
203    
204      /**
205       * Retrieves a string representation of this LDAP protocol op.
206       *
207       * @return  A string representation of this LDAP protocol op.
208       */
209      public String toString()
210      {
211        StringBuilder buffer = new StringBuilder();
212        toString(buffer);
213        return buffer.toString();
214      }
215    
216    
217    
218      /**
219       * Appends a string representation of this LDAP protocol op to the provided
220       * buffer.
221       *
222       * @param  buffer  The buffer to which the string should be appended.
223       */
224      public abstract void toString(StringBuilder buffer);
225    
226    
227    
228      /**
229       * Appends a multi-line string representation of this LDAP protocol op to the
230       * provided buffer.
231       *
232       * @param  buffer  The buffer to which the information should be appended.
233       * @param  indent  The number of spaces from the margin that the lines should
234       *                 be indented.
235       */
236      public abstract void toString(StringBuilder buffer, int indent);
237    }
238