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   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public abstract class AbstractRequestDsml<E extends Request>
39      extends AbstractDsmlMessageDecorator<E>
40      implements Request
41  {
42      /**
43       * Creates a new instance of AbstractRequestDsml.
44       *
45       * @param ldapMessage
46       *      the message to decorate
47       */
48      public AbstractRequestDsml( LdapApiService codec, E ldapMessage )
49      {
50          super( codec, ldapMessage );
51      }
52  
53  
54      /**
55       * Creates the Request Element and adds RequestID and Controls.
56       *
57       * @param root
58       *      the root element
59       * @return
60       *      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", "" + 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     public int computeLength()
124     {
125         return 0;
126     }
127 
128 
129     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
130     {
131         return null;
132     }
133 
134 
135     /**
136      * {@inheritDoc}
137      */
138     public boolean hasResponse()
139     {
140         return getDecorated().hasResponse();
141     }
142 }