View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.dsmlv2.request;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.EncoderException;
26  import org.apache.directory.api.dsmlv2.AbstractDsmlMessageDecorator;
27  import org.apache.directory.api.dsmlv2.ParserUtils;
28  import org.apache.directory.api.ldap.codec.api.LdapApiService;
29  import org.apache.directory.api.ldap.model.message.Request;
30  import org.dom4j.Element;
31  
32  
33  /**
34   * Abstract class for DSML requests.
35   * 
36   * @param <E> The request type
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public abstract class AbstractRequestDsml<E extends Request>
41      extends AbstractDsmlMessageDecorator<E>
42      implements Request
43  {
44      /**
45       * Creates a new instance of AbstractRequestDsml.
46       *
47       * @param codec The LDAP Service to use
48       * @param ldapMessage the message to decorate
49       */
50      public AbstractRequestDsml( LdapApiService codec, E ldapMessage )
51      {
52          super( codec, ldapMessage );
53      }
54  
55  
56      /**
57       * Creates the Request Element and adds RequestID and Controls.
58       *
59       * @param root the root element
60       * @return the Request Element of the given name containing
61       */
62      public Element toDsml( Element root )
63      {
64          Element element = root.addElement( getRequestName() );
65  
66          // Request ID
67          int requestID = getDecorated().getMessageId();
68          if ( requestID > 0 )
69          {
70              element.addAttribute( "requestID", Integer.toString( requestID ) );
71          }
72  
73          // Controls
74          ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values() );
75  
76          return element;
77      }
78  
79  
80      /**
81       * Gets the name of the request according to the type of the decorated element.
82       *
83       * @return
84       *      the name of the request according to the type of the decorated element.
85       */
86      private String getRequestName()
87      {
88          switch ( getDecorated().getType() )
89          {
90              case ABANDON_REQUEST:
91                  return "abandonRequest";
92  
93              case ADD_REQUEST:
94                  return "addRequest";
95  
96              case BIND_REQUEST:
97                  return "authRequest";
98  
99              case COMPARE_REQUEST:
100                 return "compareRequest";
101 
102             case DEL_REQUEST:
103                 return "delRequest";
104 
105             case EXTENDED_REQUEST:
106                 return "extendedRequest";
107 
108             case MODIFYDN_REQUEST:
109                 return "modDNRequest";
110 
111             case MODIFY_REQUEST:
112                 return "modifyRequest";
113 
114             case SEARCH_REQUEST:
115                 return "searchRequest";
116 
117             default:
118                 return "error";
119         }
120     }
121 
122 
123     /**
124      * @return the buffer's length (always 0)
125      */
126     public int computeLength()
127     {
128         return 0;
129     }
130 
131 
132     /**
133      * Encode the request. Always return an empty buffer.
134      * 
135      * @param buffer The buffer to allocate
136      * @return The resulting buffer
137      * @throws EncoderException If we had an error while encoding the request
138      */
139     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
140     {
141         return null;
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     public boolean hasResponse()
149     {
150         return getDecorated().hasResponse();
151     }
152 }