Updated license
[anna.git] / include / anna / http / wims20 / ServerSide.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_ServerSide_hpp
38 #define anna_http_wims20_ServerSide_hpp
39
40 #include <anna/core/RuntimeException.hpp>
41 #include <anna/core/util/Tokenizer.hpp>
42
43 #include <anna/http/wims20/Abstract.hpp>
44
45 namespace anna {
46
47 namespace http {
48
49 class Request;
50
51 namespace wims20 {
52
53 /**
54    Permite interpretar una URI según las recomendaciones de WIMS 2.0, lo que facilita
55    el desarrollo de aplicaciones integradas en Web 2.0; estas recomendaciones indican
56    cómo debe formarse la petición ServerSide (REpresentational State Transfer) para permitir
57    el desarrollo de cualquier servicio.
58
59    Implementa el interfaz WIMS 2.0 desde el punto de vista del proceso que recibe la petición.
60
61    El formato general de una URI según la recomendación de WIMS 2.0 es:
62
63    <p>
64 http://domain-openapis/path-openapis/serviceID/guid/other_possible_levels?query_parameters
65    </p>
66
67    Dónde los campos tienen siguen la siguiente especificación:
68    \li http://domain-openapis: Identifica el recurso del Open API.
69    \li path-openapis: Recurso opcional que ajusta la ruta hacia los recursos de éste API.
70    \li serviceID: Identificador de recurso.
71    \li guid: Identificador del usuario que solicita la petición.
72    \li other_possible_level: Opcionalmente se pueden indicar tantos niveles jerárquicos como fuera
73    necesario para el servicio.
74    \li query_parameters: Lista de parámetros. Si hay más de un parámetro se separará con '&'.
75 */
76 class ServerSide : public Abstract {
77 public:
78   /**
79    * Contructor indicando el parámetro opcional \em path-openapis. Estos dos parámetros se obtendrá como
80    * parte de la configuración de nuestro sistema.
81    * \param domain: Identifica el recurso del OpenAPI.
82    * \param path: Parámetro opcional que ajusta la ruta hacia los recusos de éste API.
83    */
84   ServerSide(const std::string& domain, const std::string& path) :
85     Abstract("ServerSide", domain, path) {
86     for(int ii = 0; ii < SplitCode::Max; ii ++)
87       a_tokenizer [ii].activateStrip(true);
88   }
89
90   /**
91    * Constructor que no usará el parámetro opcional \em path-openapis. Este parámetro se obtendrá como
92    * parte de la configuración de nuestro sistema.
93    * \param domain: Identifica el recurso del OpenAPI.
94    */
95   explicit ServerSide(const std::string& domain) :
96     Abstract("ServerSide", domain) {
97     for(int ii = 0; ii < SplitCode::Max; ii ++)
98       a_tokenizer [ii].activateStrip(true);
99   }
100
101   /**
102    * Obtiene el valor asociado a parámetro recibido como parámetro, que habrá sido obtenido
103    * de la URI mediante el método #decode.
104    * \param name Nombre del parámetro que se desea obtener.
105    * \param mode Modo de actuar en caso de que no se encuentre el parámetro.
106    * \return El valor asociado al parámetro recibido como parémtro. Puede ser NULL.
107    */
108   const std::string* getValue(const char* name, const Exception::Mode::_v mode = Exception::Mode::Throw) const throw(RuntimeException);
109
110   /**
111    * Obtiene el valor asociado a parámetro recibido como parámetro, que habrá sido obtenido
112    * de la URI mediante el método #decode.
113    * \param name Nombre del parámetro que se desea obtener.
114    * \param mode Modo de actuar en caso de que no se encuentre el parámetro.
115    * \return El valor asociado al parámetro recibido como parémtro. Puede ser NULL.
116    */
117   const char* getCStringValue(const char* name, const Exception::Mode::_v mode = Exception::Mode::Throw) const throw(RuntimeException);
118
119   /**
120    * Obtiene el valor asociado a parámetro recibido como parámetro, que habrá sido obtenido
121    * de la URI mediante el método #decode.
122    * \param name Nombre del parámetro que se desea obtener.
123    * \param mode Modo de actuar en caso de que no se encuentre el parámetro.
124    * \return El valor asociado al parámetro recibido como parémtro. Puede ser NULL.
125    */
126   int getIntegerValue(const char* name, const Exception::Mode::_v mode = Exception::Mode::Throw) const throw(RuntimeException);
127
128   /**
129    * Decodifica la URI de la petición enviada como parámetro y la interpreta según
130    * las recomendaciones de WIMS 2.0
131    *
132    * \param request Petición HTTP que va a tratar este servidor.
133    */
134   void decode(const http::Request& request) throw(RuntimeException);
135
136 private:
137   struct SplitCode { enum _v { HierarchyAndParameter, HierarchyItem, Parameters, ParameterAndArgument, Max }; };
138
139   void decodeHierarchy(const std::string& hierarchy) throw(RuntimeException);
140   void decodeParameters(const std::string& parameters) throw(RuntimeException);
141
142   const Tokenizer& split(const SplitCode::_v splitZone, const std::string&) throw(RuntimeException);
143
144   anna::Tokenizer a_tokenizer [SplitCode::Max];
145 };
146
147 }
148 }
149 }
150
151 #endif