First commit
[anna.git] / source / http / wims20 / ClientSide.cpp
1 // ANNA - Anna is Not 'N' 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 #include <anna/core/tracing/Logger.hpp>
38
39 #include <anna/http/wims20/ClientSide.hpp>
40
41 #include <anna/http/Request.hpp>
42
43 using namespace std;
44 using namespace anna;
45
46 /*
47  * Los niveles parece que serán bastante estables, así que nos ahorramos calcular toda su ruta
48  * cada vez que tengamos que calcular la URI.
49  */
50 void http::wims20::ClientSide::addOtherLevel(const std::string& otherLevel)
51 throw(RuntimeException) {
52   if(a_strOtherLevels == NULL)
53     a_strOtherLevels = Abstract::createString(otherLevel);
54   else {
55     *a_strOtherLevels += '/';
56     *a_strOtherLevels += otherLevel;
57   }
58
59   Abstract::other_level_add(otherLevel);
60 }
61
62 /*
63    http://domain-openapis/path-openapis/serviceID/guid/other_possible_levels?query_parameters
64
65    Dónde los campos tienen siguen la siguiente especificación:
66    \li http://domain-openapis: Identifica el recurso del Open API.
67    \li path-openapis: Recurso opcional que ajusta la ruta hacia los recursos de éste API.
68    \li serviceID: Identificador de recurso.
69    \li guid: Identificador del usuario que solicita la petición.
70    \li other_possible_level: Opcionalmente se pueden indicar tantos niveles jerárquicos como fuera
71    necesario para el servicio.
72    \li query_parameters: Lista de parámetros. Si hay más de un parámetro se separará con '&'.
73  */
74 void http::wims20::ClientSide::codeOn(http::Request& request)
75 throw(RuntimeException) {
76   a_uri = Abstract::calculeFixedPart();
77
78   if(Abstract::hasOtherLevels())
79     Abstract::appendWithSlash(a_uri, *a_strOtherLevels);
80
81   if(Abstract::hasParameters()) {
82     a_uri += '?';
83     bool first = true;
84
85     for(parameter_iterator ii = parameter_begin(), maxii = parameter_end(); ii != maxii; ii ++) {
86       if(first == false)
87         a_uri += '&';
88
89       a_uri += *Abstract::parameter_name(ii);
90       a_uri += '=';
91       a_uri += *Abstract::parameter_value(ii);
92       first = false;
93     }
94   }
95
96   LOGDEBUG(
97     string msg("http::wims20::ServerSide::codeOn | ");
98     msg += asString();
99     msg += " | Result (URI): ";
100     msg += a_uri;
101     Logger::debug(msg, ANNA_FILE_LOCATION);
102   );
103   request.setURI(a_uri);
104 }