Updated license
[anna.git] / include / anna / ldap / Response.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_ldap_Response_hpp
38 #define anna_ldap_Response_hpp
39
40 #include <anna/core/util/Recycler.hpp>
41 #include <anna/core/Allocator.hpp>
42
43 #include <anna/ldap/defines.hpp>
44 #include <anna/ldap/ClassCode.hpp>
45 #include <anna/ldap/ResultCode.hpp>
46
47 namespace anna {
48
49 namespace ldap {
50
51 class Session;
52 class Attribute;
53 class Timer;
54 class Request;
55
56 /**
57    Modela las respuesta que podemos recibir de un servidor LDAP.
58
59    La respuesta correspondiente a una peticion se genera automaticamente al invocar a ldap::Session::send.
60    El nucleo de ANNA.ldap notifica el resultado de la operacion solicitada mediante la invocacion
61    al metodo-menejador ldap::Session::eventResponse de nuestra sesion.
62 */
63 class Response {
64 public:
65   typedef Recycler<Attribute> attribute_container;
66   typedef attribute_container::iterator attribute_iterator;
67   typedef attribute_container::const_iterator const_attribute_iterator;
68
69   typedef Recycler<std::string> referral_container;
70   typedef referral_container::const_iterator const_referral_iterator;
71
72   /**
73      Devuelve el tipo de la clase de esta respuesta.
74      \return El tipo de la clase de esta respuesta.
75   */
76   ClassCode::_v getClassCode() const throw() { return a_classCode; }
77
78   /**
79      Devuelve la identificacion del mensaje LDAP.
80      Esta identificacion sera generada automaticamente al enviar la peticion.
81      \return Identificacion del mensaje asociado a esta peticion/respuesta.
82      \see ldap::Session::send
83   */
84   IdMessage getIdMessage() const throw() { return a_idMessage; }
85
86   /**
87      Devuelve la sesion que genera esta respuesta.
88      \return La instancia de la sesion que genera esta respuesta.
89   */
90   const Session* getSession() const throw() { return a_session; }
91
92   /**
93      Devuelve el resultado de la peticion LDAP solicitada.
94      \return El resultado de la peticion LDAP solicitada.
95   */
96   const ResultCode& getResultCode() const throw() { return a_resultCode; }
97
98   /**
99      Devuelve el nombre del DN asociado a esta peticion.
100      \return El nombre del DN asociado a esta peticion.
101   */
102   const std::string& getName() const throw() { return a_name; }
103
104   /**
105      Devuelve la peticion que origino la creacion de esta respuesta.
106      \return La peticion que origino la creacion de esta respuesta.
107      \see ldap::Session::send
108   */
109   const Request* getRequest()const  throw() { return a_request; }
110
111   /**
112      Devuelve la sesion que origino la creacion de esta respuesta.
113      \return La sesion que origino la creacion de esta respuesta.
114   */
115   Session* getSession() throw() { return a_session; }
116
117   /**
118    * Devuelve el nº de atributos que contiene la respuesta.
119    * \return el nº de atributos que contiene la respuesta.
120    */
121   int attribute_size() const throw() { return a_attributes.size(); }
122
123   /**
124      Devuelve el iterador al comienzo de la lista de atributos asociados a esta respuesta.
125      \return El iterador al comienzo de la lista de atributos asociados a esta respuesta.
126   */
127   const_attribute_iterator attribute_begin() const throw() { return a_attributes.begin(); }
128
129   /**
130      Devuelve el iterador al final de la lista de atributos asociados a esta respuesta.
131      \return El iterador al final de la lista de atributos asociados a esta respuesta.
132   */
133   const_attribute_iterator attribute_end() const throw() { return a_attributes.end(); }
134
135   /**
136    * Devuelve el nº de referencias que contiene la respuesta.
137    * \return el nº de referencias que contiene la respuesta.
138    */
139   int referral_size() const throw() { return a_referrals.size(); }
140
141   /**
142      Devuelve el iterador al comienzo de la lista de referencias asociados a esta respuesta.
143      \return El iterador al comienzo de la lista de referencias asociados a esta respuesta.
144   */
145   const_referral_iterator referral_begin() const throw() { return a_referrals.begin(); }
146
147   /**
148      Devuelve el iterador al final de la lista de referencias asociados a esta respuesta.
149      \return El iterador al final de la lista de referencias asociados a esta respuesta.
150   */
151   const_referral_iterator referral_end() const throw() { return a_referrals.end(); }
152
153   /**
154      Devuelve el atributo asociado al iterador recibido como parametro.
155      \param ii Instancia del iterador usado para recorrer los datos. Estara comprendido
156      entre [#attribute_begin, #attribute_end).
157      \return El atributo referenciado por el iterador.
158   */
159   static const Attribute* attribute(const_attribute_iterator ii) throw() { return attribute_container::data(ii); }
160
161   /**
162      Devuelve la referencia asociada al iterador recibido como parametro.
163      \param ii Instancia del iterador usado para recorrer los datos. Estara comprendido
164      entre [#referral_begin, #referral_end).
165      \return La referencia apuntada por el iterador.
166   */
167   static const std::string& referral(const_referral_iterator ii) throw() { return *referral_container::data(ii); }
168
169   /**
170      Devuelve una cadena con la informacion relevante sobre esta instancia.
171      \return Una cadena con la informacion relevante sobre esta instancia.
172   */
173   std::string asString() const throw();
174
175 private:
176   ClassCode::_v a_classCode;
177   IdMessage a_idMessage;
178   Session* a_session;
179   ResultCode a_resultCode;
180   std::string a_name;
181   attribute_container a_attributes;
182   referral_container a_referrals;
183   Timer* a_timer;
184   const Request* a_request;
185
186   typedef Recycler<Response> response_pool;
187   static response_pool st_responses;
188
189   Response();
190
191   static Response* instance(const ClassCode::_v, const IdMessage) throw(RuntimeException);
192   static void release(Response* response) throw();
193
194   void clear() throw();
195
196   void setName(const std::string& name) throw() { a_name = name; }
197   void setSession(Session* session) throw() { a_session = session; }
198   void setRequest(const Request* request) throw() { a_request = request; }
199   void activateTimer() throw(RuntimeException);
200   void cancelTimer() throw();
201   void setResultCode(const ResultCode& resultCode) throw() { a_resultCode = resultCode; }
202
203   Attribute* createAttribute(const std::string& name) throw(RuntimeException);
204
205   void createReferral(const char* value) throw(RuntimeException) {
206     std::string* newReferral = a_referrals.create();
207     *newReferral = value;
208   }
209
210
211   attribute_iterator attribute_begin() throw() { return a_attributes.begin(); }
212   attribute_iterator attribute_end() throw() { return a_attributes.end(); }
213   static Attribute* attribute(attribute_iterator ii) throw() { return attribute_container::data(ii); }
214
215   friend class Session;
216   friend class Allocator<Response>;
217 };
218
219 }
220 }
221
222 #endif
223