Remove dynamic exceptions
[anna.git] / source / http / wims20 / Abstract.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/core/tracing/Logger.hpp>
10 #include <anna/core/functions.hpp>
11
12 #include <anna/http/wims20/Abstract.hpp>
13
14 using namespace std;
15 using namespace anna;
16
17 http::wims20::Abstract::Abstract(const char* whatis, const std::string& domain, const std::string& path) :
18   a_whatis(whatis),
19   a_domain(domain) {
20   a_path = createString(path);
21   a_otherLevels = NULL;
22   a_parameters = NULL;
23 }
24
25 http::wims20::Abstract::Abstract(const char* whatis, const std::string& domain) :
26   a_whatis(whatis),
27   a_domain(domain) {
28   a_path = NULL;
29   a_otherLevels = NULL;
30   a_parameters = NULL;
31 }
32
33 /* virtual */
34 http::wims20::Abstract::~Abstract() {
35   destroyString(a_path);
36   clear();
37   delete a_otherLevels;
38   delete a_parameters;
39 }
40
41 /* Calcula la parte fija de la URI, se invoca desde el constructor. */
42 const string& http::wims20::Abstract::calculeFixedPart()
43 noexcept(false) {
44   if(a_serviceID.empty() == true) {
45     string msg(asString());
46     msg += " | ServiceID can not be NULL";
47     throw RuntimeException(msg, ANNA_FILE_LOCATION);
48   }
49
50   if(a_guid.empty() == true) {
51     string msg(asString());
52     msg += " | GUID can not be NULL";
53     throw RuntimeException(msg, ANNA_FILE_LOCATION);
54   }
55
56   if(a_fixedPart.empty() == false)
57     return a_fixedPart;
58
59   calculeShortFixedPart();
60   appendWithSlash(a_fixedPart, a_serviceID);
61   appendWithSlash(a_fixedPart, a_guid);
62   return a_fixedPart;
63 }
64
65 const string& http::wims20::Abstract::calculeShortFixedPart()
66 noexcept(false) {
67   string::size_type pos = a_domain.find("http://");
68
69   if(pos == string::npos) {
70     a_fixedPart = "http://";
71     a_fixedPart += a_domain;
72   } else {
73     if(pos > 0) {
74       string msg("Domain '");
75       msg += a_domain;
76       msg += "' is not valid";
77       throw RuntimeException(msg, ANNA_FILE_LOCATION);
78     }
79
80     a_fixedPart = a_domain;
81   }
82
83   if(a_path != NULL)
84     appendWithSlash(a_fixedPart, *a_path);
85
86   return a_fixedPart;
87 }
88
89 void http::wims20::Abstract::other_level_add(const std::string& otherLevel)
90 noexcept(false) {
91   if(a_otherLevels == NULL)
92     a_otherLevels = new other_level_container;
93
94   a_otherLevels->push_back(createString(otherLevel));
95   LOGDEBUG(
96     string msg("http::wims20::Abstract::other_level_add | Level: ");
97     msg += otherLevel;
98     Logger::debug(msg, ANNA_FILE_LOCATION);
99   );
100 }
101
102 /*virtual*/
103 void http::wims20::Abstract::clearOtherLevels()
104 {
105   if(hasOtherLevels()) {
106     for(other_level_iterator ii = other_level_begin(), maxii = other_level_end(); ii != maxii; ii ++)
107       destroyString(otherLevel(ii));
108
109     a_otherLevels->clear();
110   }
111 }
112
113 void http::wims20::Abstract::parameter_set(const std::string& name, const std::string& value)
114 noexcept(false) {
115   bool exists = false;
116
117   if(a_parameters == NULL)
118     a_parameters = new parameter_container;
119
120   for(parameter_iterator ii = parameter_begin(), maxii = parameter_end(); ii != maxii; ii ++) {
121     if(*parameter_name(ii) == name) {
122       *parameter_value(ii) = value;
123       exists = true;
124       break;
125     }
126   }
127
128   if(exists == false) {
129     string* paramName = createString(name);
130     string* paramValue = createString(value);
131     parameter_pkv item(paramName, paramValue);
132     a_parameters->push_back(item);
133   }
134
135   LOGDEBUG(
136     string msg("http::wims20::Abstract::parameter_set | Name: ");
137     msg += name;
138     msg += " | Value: ";
139     msg += value;
140     msg += " | New: ";
141     msg += functions::asString(!exists);
142     Logger::debug(msg, ANNA_FILE_LOCATION);
143   );
144 }
145
146 void http::wims20::Abstract::clearParameters()
147 {
148   if(hasParameters()) {
149     for(parameter_iterator ii = parameter_begin(), maxii = parameter_end(); ii != maxii; ii ++) {
150       destroyString(parameter_name(ii));
151       destroyString(parameter_value(ii));
152     }
153
154     a_parameters->clear();
155   }
156 }
157
158 string http::wims20::Abstract::asString() const
159 {
160   string result("http::wims20::");
161   result += a_whatis;
162   result += " { Domain: ";
163   result += a_domain;
164   result += " | Path: ";
165   result += (a_path == NULL) ? "<null>" : a_path->c_str();
166   result += " | ServiceID: ";
167   result += a_serviceID;
168   result += " | GUID: ";
169   result += a_guid;
170   result += " | OtherLevels:";
171
172   if(hasOtherLevels() == true) {
173     for(const_other_level_iterator ii = other_level_begin(), maxii = other_level_end(); ii != maxii; ii ++) {
174       result += ' ';
175       result += otherLevel(ii);
176     }
177   } else
178     result += " <null>";
179
180   result += " | Parameters:";
181
182   if(hasParameters() == true) {
183     for(const_parameter_iterator ii = parameter_begin(), maxii = parameter_end(); ii != maxii; ii ++) {
184       result += ' ';
185       result += parameter_name(ii);
186       result += '=';
187       result += parameter_value(ii);
188     }
189   } else
190     result += " <null>";
191
192   return result += " }";
193 }
194
195 /*static*/
196 void http::wims20::Abstract::appendWithSlash(std::string& target, const std::string& other)
197 {
198   const char* str = target.c_str();
199
200   if(str [anna_strlen(str) - 1] != '/')
201     target += '/';
202
203   target += other;
204 }