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   *    https://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.DsmlLiterals;
28  import org.apache.directory.api.dsmlv2.ParserUtils;
29  import org.apache.directory.api.ldap.codec.api.LdapApiService;
30  import org.apache.directory.api.ldap.model.message.Request;
31  import org.dom4j.Element;
32  
33  
34  /**
35   * Abstract class for DSML requests.
36   *
37   * @param <E> The request type
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public abstract class AbstractRequestDsml<E extends Request>
42      extends AbstractDsmlMessageDecorator<E>
43      implements Request
44  {
45      /**
46       * Creates a new instance of AbstractRequestDsml.
47       *
48       * @param codec The LDAP Service to use
49       * @param ldapMessage the message to decorate
50       */
51      public AbstractRequestDsml( LdapApiService codec, E ldapMessage )
52      {
53          super( codec, ldapMessage );
54      }
55  
56  
57      /**
58       * Creates the Request Element and adds RequestID and Controls.
59       *
60       * @param root the root element
61       * @return the Request Element of the given name containing
62       */
63      @Override
64      public Element toDsml( Element root )
65      {
66          Element element = root.addElement( getRequestName() );
67  
68          // Request ID
69          int requestID = getDecorated().getMessageId();
70          if ( requestID > 0 )
71          {
72              element.addAttribute( DsmlLiterals.REQUEST_ID, Integer.toString( requestID ) );
73          }
74  
75          // Controls
76          ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values(), true );
77  
78          return element;
79      }
80  
81  
82      /**
83       * Gets the name of the request according to the type of the decorated element.
84       *
85       * @return the name of the request according to the type of the decorated element.
86       */
87      protected String getRequestName()
88      {
89          switch ( getDecorated().getType() )
90          {
91              case ABANDON_REQUEST:
92                  return DsmlLiterals.ABANDON_REQUEST;
93  
94              case ADD_REQUEST:
95                  return DsmlLiterals.ADD_REQUEST;
96  
97              case BIND_REQUEST:
98                  return DsmlLiterals.AUTH_REQUEST;
99  
100             case COMPARE_REQUEST:
101                 return DsmlLiterals.COMPARE_REQUEST;
102 
103             case DEL_REQUEST:
104                 return DsmlLiterals.DEL_REQUEST;
105 
106             case EXTENDED_REQUEST:
107                 return DsmlLiterals.EXTENDED_REQUEST;
108 
109             case MODIFYDN_REQUEST:
110                 return DsmlLiterals.MOD_DN_REQUEST;
111 
112             case MODIFY_REQUEST:
113                 return DsmlLiterals.MODIFY_REQUEST;
114 
115             case SEARCH_REQUEST:
116                 return DsmlLiterals.SEARCH_REQUEST;
117 
118             default:
119                 return DsmlLiterals.ERROR;
120         }
121     }
122 
123 
124     /**
125      * @return the buffer's length (always 0)
126      */
127     public int computeLength()
128     {
129         return 0;
130     }
131 
132 
133     /**
134      * Encode the request. Always return an empty buffer.
135      *
136      * @param buffer The buffer to allocate
137      * @return The resulting buffer
138      * @throws EncoderException If we had an error while encoding the request
139      */
140     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
141     {
142         return null;
143     }
144 
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public boolean hasResponse()
151     {
152         return getDecorated().hasResponse();
153     }
154 }