1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #include <anna/core/tracing/Logger.hpp>
38 #include <anna/core/functions.hpp>
40 #include <anna/http/wims20/Abstract.hpp>
45 http::wims20::Abstract::Abstract(const char* whatis, const std::string& domain, const std::string& path) :
48 a_path = createString(path);
53 http::wims20::Abstract::Abstract(const char* whatis, const std::string& domain) :
62 http::wims20::Abstract::~Abstract() {
63 destroyString(a_path);
69 /* Calcula la parte fija de la URI, se invoca desde el constructor. */
70 const string& http::wims20::Abstract::calculeFixedPart()
71 throw(RuntimeException) {
72 if(a_serviceID.empty() == true) {
73 string msg(asString());
74 msg += " | ServiceID can not be NULL";
75 throw RuntimeException(msg, ANNA_FILE_LOCATION);
78 if(a_guid.empty() == true) {
79 string msg(asString());
80 msg += " | GUID can not be NULL";
81 throw RuntimeException(msg, ANNA_FILE_LOCATION);
84 if(a_fixedPart.empty() == false)
87 calculeShortFixedPart();
88 appendWithSlash(a_fixedPart, a_serviceID);
89 appendWithSlash(a_fixedPart, a_guid);
93 const string& http::wims20::Abstract::calculeShortFixedPart()
94 throw(RuntimeException) {
95 string::size_type pos = a_domain.find("http://");
97 if(pos == string::npos) {
98 a_fixedPart = "http://";
99 a_fixedPart += a_domain;
102 string msg("Domain '");
104 msg += "' is not valid";
105 throw RuntimeException(msg, ANNA_FILE_LOCATION);
108 a_fixedPart = a_domain;
112 appendWithSlash(a_fixedPart, *a_path);
117 void http::wims20::Abstract::other_level_add(const std::string& otherLevel)
118 throw(RuntimeException) {
119 if(a_otherLevels == NULL)
120 a_otherLevels = new other_level_container;
122 a_otherLevels->push_back(createString(otherLevel));
124 string msg("http::wims20::Abstract::other_level_add | Level: ");
126 Logger::debug(msg, ANNA_FILE_LOCATION);
131 void http::wims20::Abstract::clearOtherLevels()
133 if(hasOtherLevels()) {
134 for(other_level_iterator ii = other_level_begin(), maxii = other_level_end(); ii != maxii; ii ++)
135 destroyString(otherLevel(ii));
137 a_otherLevels->clear();
141 void http::wims20::Abstract::parameter_set(const std::string& name, const std::string& value)
142 throw(RuntimeException) {
145 if(a_parameters == NULL)
146 a_parameters = new parameter_container;
148 for(parameter_iterator ii = parameter_begin(), maxii = parameter_end(); ii != maxii; ii ++) {
149 if(*parameter_name(ii) == name) {
150 *parameter_value(ii) = value;
156 if(exists == false) {
157 string* paramName = createString(name);
158 string* paramValue = createString(value);
159 parameter_pkv item(paramName, paramValue);
160 a_parameters->push_back(item);
164 string msg("http::wims20::Abstract::parameter_set | Name: ");
169 msg += functions::asString(!exists);
170 Logger::debug(msg, ANNA_FILE_LOCATION);
174 void http::wims20::Abstract::clearParameters()
176 if(hasParameters()) {
177 for(parameter_iterator ii = parameter_begin(), maxii = parameter_end(); ii != maxii; ii ++) {
178 destroyString(parameter_name(ii));
179 destroyString(parameter_value(ii));
182 a_parameters->clear();
186 string http::wims20::Abstract::asString() const
188 string result("http::wims20::");
190 result += " { Domain: ";
192 result += " | Path: ";
193 result += (a_path == NULL) ? "<null>" : a_path->c_str();
194 result += " | ServiceID: ";
195 result += a_serviceID;
196 result += " | GUID: ";
198 result += " | OtherLevels:";
200 if(hasOtherLevels() == true) {
201 for(const_other_level_iterator ii = other_level_begin(), maxii = other_level_end(); ii != maxii; ii ++) {
203 result += otherLevel(ii);
208 result += " | Parameters:";
210 if(hasParameters() == true) {
211 for(const_parameter_iterator ii = parameter_begin(), maxii = parameter_end(); ii != maxii; ii ++) {
213 result += parameter_name(ii);
215 result += parameter_value(ii);
220 return result += " }";
224 void http::wims20::Abstract::appendWithSlash(std::string& target, const std::string& other)
226 const char* str = target.c_str();
228 if(str [anna_strlen(str) - 1] != '/')