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   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public abstract class AbstractResultResponseRequestDsml<E extends ResultResponseRequest, F extends ResultResponse>
41      extends AbstractRequestDsml<E>
42      implements ResultResponseRequest, AbandonableRequest
43  {
44      /**
45       * Creates a new instance of AbstractRequestDsml.
46       *
47       * @param ldapMessage
48       *      the message to decorate
49       */
50      public AbstractResultResponseRequestDsml( 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
60       *      the root element
61       * @return
62       *      the Request Element of the given name containing
63       */
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( "requestID", "" + requestID );
73          }
74  
75          // Controls
76          ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values() );
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
86       *      the name of the request according to the type of the decorated element.
87       */
88      private String getRequestName()
89      {
90          switch ( getDecorated().getType() )
91          {
92              case ABANDON_REQUEST:
93                  return "abandonRequest";
94  
95              case ADD_REQUEST:
96                  return "addRequest";
97  
98              case BIND_REQUEST:
99                  return "authRequest";
100 
101             case COMPARE_REQUEST:
102                 return "compareRequest";
103 
104             case DEL_REQUEST:
105                 return "delRequest";
106 
107             case EXTENDED_REQUEST:
108                 return "extendedRequest";
109 
110             case MODIFYDN_REQUEST:
111                 return "modDNRequest";
112 
113             case MODIFY_REQUEST:
114                 return "modifyRequest";
115 
116             case SEARCH_REQUEST:
117                 return "searchRequest";
118 
119             default:
120                 return "error";
121         }
122     }
123 
124 
125     public int computeLength()
126     {
127         return 0;
128     }
129 
130 
131     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
132     {
133         return null;
134     }
135 
136 
137     /**
138      * {@inheritDoc}
139      */
140     public ResultResponse getResultResponse()
141     {
142         return getDecorated().getResultResponse();
143     }
144 
145 
146     /**
147      * {@inheritDoc}
148      */
149     public void abandon()
150     {
151         ( ( AbandonableRequest ) getDecorated() ).abandon();
152     }
153 
154 
155     /**
156      * {@inheritDoc}
157      */
158     public boolean isAbandoned()
159     {
160         return ( ( AbandonableRequest ) getDecorated() ).isAbandoned();
161     }
162 
163 
164     /**
165      * {@inheritDoc}
166      */
167     public AbandonableRequest addAbandonListener( AbandonListener listener )
168     {
169         ( ( AbandonableRequest ) getDecorated() ).addAbandonListener( listener );
170 
171         return this;
172     }
173 }