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.ParserUtils;
27  import org.apache.directory.api.ldap.codec.api.LdapApiService;
28  import org.apache.directory.api.ldap.model.message.AbandonListener;
29  import org.apache.directory.api.ldap.model.message.AbandonableRequest;
30  import org.apache.directory.api.ldap.model.message.ResultResponse;
31  import org.apache.directory.api.ldap.model.message.ResultResponseRequest;
32  import org.dom4j.Element;
33  
34  
35  /**
36   * Abstract class for DSML requests.
37   *
38   * @param <E> The response request result type
39   * @param <F> The response result type
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public abstract class AbstractResultResponseRequestDsml<E extends ResultResponseRequest, F extends ResultResponse>
44      extends AbstractRequestDsml<E>
45      implements ResultResponseRequest, AbandonableRequest
46  {
47      /**
48       * Creates a new instance of AbstractRequestDsml.
49       *
50       * @param codec The LDAP Service to use
51       * @param ldapMessage the message to decorate
52       */
53      public AbstractResultResponseRequestDsml( LdapApiService codec, E ldapMessage )
54      {
55          super( codec, ldapMessage );
56      }
57  
58  
59      /**
60       * Creates the Request Element and adds RequestID and Controls.
61       *
62       * @param root
63       *      the root element
64       * @return
65       *      the Request Element of the given name containing
66       */
67      public Element toDsml( Element root )
68      {
69          Element element = root.addElement( getRequestName() );
70  
71          // Request ID
72          int requestID = getDecorated().getMessageId();
73          if ( requestID > 0 )
74          {
75              element.addAttribute( "requestID", Integer.toString( requestID ) );
76          }
77  
78          // Controls
79          ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values() );
80  
81          return element;
82      }
83  
84  
85      /**
86       * Gets the name of the request according to the type of the decorated element.
87       *
88       * @return
89       *      the name of the request according to the type of the decorated element.
90       */
91      private String getRequestName()
92      {
93          switch ( getDecorated().getType() )
94          {
95              case ABANDON_REQUEST:
96                  return "abandonRequest";
97  
98              case ADD_REQUEST:
99                  return "addRequest";
100 
101             case BIND_REQUEST:
102                 return "authRequest";
103 
104             case COMPARE_REQUEST:
105                 return "compareRequest";
106 
107             case DEL_REQUEST:
108                 return "delRequest";
109 
110             case EXTENDED_REQUEST:
111                 return "extendedRequest";
112 
113             case MODIFYDN_REQUEST:
114                 return "modDNRequest";
115 
116             case MODIFY_REQUEST:
117                 return "modifyRequest";
118 
119             case SEARCH_REQUEST:
120                 return "searchRequest";
121 
122             default:
123                 return "error";
124         }
125     }
126 
127 
128     public int computeLength()
129     {
130         return 0;
131     }
132 
133 
134     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
135     {
136         return null;
137     }
138 
139 
140     /**
141      * {@inheritDoc}
142      */
143     public ResultResponse getResultResponse()
144     {
145         return getDecorated().getResultResponse();
146     }
147 
148 
149     /**
150      * {@inheritDoc}
151      */
152     public void abandon()
153     {
154         ( ( AbandonableRequest ) getDecorated() ).abandon();
155     }
156 
157 
158     /**
159      * {@inheritDoc}
160      */
161     public boolean isAbandoned()
162     {
163         return ( ( AbandonableRequest ) getDecorated() ).isAbandoned();
164     }
165 
166 
167     /**
168      * {@inheritDoc}
169      */
170     public AbandonableRequest addAbandonListener( AbandonListener listener )
171     {
172         ( ( AbandonableRequest ) getDecorated() ).addAbandonListener( listener );
173 
174         return this;
175     }
176 }