Updated license
[anna.git] / include / anna / http / wims20 / ClientSide.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_http_wims20_ClientSide_hpp
38 #define anna_http_wims20_ClientSide_hpp
39
40 #include <anna/core/functions.hpp>
41
42 #include <anna/http/wims20/Abstract.hpp>
43
44 namespace anna {
45
46 namespace http {
47
48 class Request;
49
50 namespace wims20 {
51
52 /**
53    Permite interpretar una URI según las recomendaciones de WIMS 2.0, lo que facilita
54    el desarrollo de aplicaciones integradas en Web 2.0; estas recomendaciones indican
55    cómo debe formarse la petición ClientSide (REpresentational State Transfer) para permitir
56    el desarrollo de cualquier servicio.
57
58    Implementa el interfaz WIMS 2.0 desde el punto de vista del proceso que genera la petición.
59
60    El formato general de una URI según la recomendación de WIMS 2.0 es:
61
62    <p>
63 http://domain-openapis/path-openapis/serviceID/guid/other_possible_levels?query_parameters
64    </p>
65
66    Dónde los campos tienen siguen la siguiente especificación:
67    \li http://domain-openapis: Identifica el recurso del Open API.
68    \li path-openapis: Recurso opcional que ajusta la ruta hacia los recursos de éste API.
69    \li serviceID: Identificador de recurso.
70    \li guid: Identificador del usuario que solicita la petición.
71    \li other_possible_level: Opcionalmente se pueden indicar tantos niveles jerárquicos como fuera
72    necesario para el servicio.
73    \li query_parameters: Lista de parámetros. Si hay más de un parámetro se separará con '&'.
74 */
75 class ClientSide : public Abstract {
76 public:
77   /**
78    * Contructor indicando el parámetro opcional \em path-openapis. Estos dos parámetros se obtendrá como
79    * parte de la configuración de nuestro sistema.
80    * \param domain: Identifica el recurso del OpenAPI.
81    * \param path: Parámetro opcional que ajusta la ruta hacia los recusos de éste API.
82    */
83   ClientSide(const std::string& domain, const std::string& path) :
84     Abstract("ClientSide", domain, path) {
85     a_strOtherLevels = NULL;
86   }
87
88   /**
89    * Constructor que no usará el parámetro opcional \em path-openapis. Este parámetro se obtendrá como
90    * parte de la configuración de nuestro sistema.
91    * \param domain: Identifica el recurso del OpenAPI.
92    */
93   explicit ClientSide(const std::string& domain) :
94     Abstract("ClientSide", domain) {
95     a_strOtherLevels = NULL;
96   }
97
98   /**
99    * Destructor.
100    */
101   ~ClientSide() { Abstract::destroyString(a_strOtherLevels); }
102
103   /**
104    * Amplía el campo \em other_possible_level con el valor recibido.
105    * \param otherLevel Valor con el que ampliar la ruta.
106    */
107   void addOtherLevel(const std::string& otherLevel) throw(RuntimeException);
108
109   /**
110    * Amplía el campo \em other_possible_level con el valor recibido.
111    * \param otherLevel Valor con el que ampliar la ruta.
112    */
113   void addOtherLevel(const char* otherLevel) throw(RuntimeException) {
114     std::string aux(otherLevel);
115     addOtherLevel(aux);
116   }
117
118   /**
119    * Establece el valor del parámetro indicado como parámetro. Si el parámetro indicado ya
120    * existe en la lista de parámetros registrados su valor se sobreescribe.
121    * \param parameter Nombre del parámetro a establecer.
122    * \param value Valor asociado al parámetro.
123    */
124   void setParameter(const char* parameter, const char* value) throw(RuntimeException) {
125     std::string p(parameter);
126     std::string v(value);
127     Abstract::parameter_set(p, v);
128   }
129
130   /**
131    * Establece el valor del parámetro indicado como parámetro. Si el parámetro indicado ya
132    * existe en la lista de parámetros registrados su valor se sobreescribe.
133    * \param parameter Nombre del parámetro a establecer.
134    * \param value Valor asociado al parámetro.
135    */
136   void setParameter(const char* parameter, const int value) throw(RuntimeException) {
137     std::string p(parameter);
138     Abstract::parameter_set(p, anna::functions::asString(value));
139   }
140
141   /**
142    * Establece el valor del parámetro indicado como parámetro. Si el parámetro indicado ya
143    * existe en la lista de parámetros registrados su valor se sobreescribe.
144    * \param parameter Nombre del parámetro a establecer.
145    * \param value Valor asociado al parámetro.
146    */
147   void setParameter(const std::string& parameter, const std::string& value) throw(RuntimeException) {
148     Abstract::parameter_set(parameter, value);
149   }
150
151   /**
152    * Establece el valor del parámetro indicado como parámetro. Si el parámetro indicado ya
153    * existe en la lista de parámetros registrados su valor se sobreescribe.
154    * \param parameter Nombre del parámetro a establecer.
155    * \param value Valor asociado al parámetro.
156    */
157   void setParameter(const std::string& parameter, const int value) throw(RuntimeException) {
158     Abstract::parameter_set(parameter, anna::functions::asString(value));
159   }
160
161   /**
162    * Limpia el contenido asociado al parámetro \em other_possible_level. Sólo debería
163    * invocarse a este método en caso de que el servicio destino de la petición haya cambiado.
164    */
165   void clearOtherLevels() throw() {
166     Abstract::clearOtherLevels();
167     Abstract::destroyString(a_strOtherLevels);
168     a_strOtherLevels = NULL;
169   }
170
171   /**
172    * Recopila la información contenida en la petición REST de WIMS2.0 y la
173    * codifica sobre la URI de la petición HTTP recibida como parámetro.
174    * \param message Mensaje HTTP sobre el que se establecerá la URI necesaria para
175    * realizar la peticion WIMS 2.0.
176    */
177   void codeOn(http::Request& message) throw(RuntimeException);
178
179 private:
180   std::string* a_strOtherLevels;
181   std::string a_uri;
182 };
183
184 }
185 }
186 }
187
188 #endif