<< endl;
#endif // DEBUG
headers["Content-Type"] = "text/xml; charset=iso-8859-1";
- status_code = HTTPMessage::OK;
+ build();
}
-Response::Response(const string& body, const string& desc, const Code& code):
+Response::Response(const string& contents, const string& desc, const Code& code):
xml_version("1.0"), xml_code(code), xml_description(desc),
- xml_body(body) {
+ xml_contents(contents) {
#ifdef DEBUG
cerr << __FILE__ << "(" << __LINE__ << ")"
- << ": constructor(body.length = " << body.length()
+ << ": constructor(body.length = " << contents.length()
<< ", desc = " << desc << ", code = " << code << ");" << endl;
#endif // DEBUG
headers["Content-Type"] = "text/xml; charset=iso-8859-1";
- status_code = HTTPMessage::OK;
+ build();
+}
+
+void Response::build(void) {
+ String b;
+ b.from(xml_code);
+ b = string("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n"
+ "<plaqui-response code=\"") + b + "\" version=\""
+ + xml_version + "\" ";
+ if (xml_description.length()) {
+ b += "description=\"" + xml_description + "\" ";
+ }
+ if (xml_contents.length()) {
+ b += ">\n" + xml_contents + "\n</plaqui-response>\n";
+ } else {
+ b += "/>\n";
+ }
+ set_body(b);
+ switch (xml_code) {
+ case OK:
+ status_code = HTTPMessage::OK;
+ break;
+ case INVALID_TARGET:
+ case INVALID_COMMAND:
+ case CONNECTION_NOT_FOUND:
+ case TRANSMISSION_NOT_FOUND:
+ case PLANT_NOT_FOUND:
+ case ELEMENT_NOT_FOUND:
+ case ELEMENT_INPUT_NOT_FOUND:
+ status_code = HTTPMessage::INTERNAL_SERVER_ERROR;
+ break;
+ case ALLREADY_EXISTS:
+ case ARGUMENT_MISSING:
+ status_code = HTTPMessage::CONFLICT;
+ break;
+ case UNKNOWN_ERROR:
+ case ERROR_STARTING_TRANSMISSION:
+ case ERROR_GETING_PLANT_XML:
+ case ERROR_CHANGING_ELEMENT_INPUT:
+ status_code = HTTPMessage::INTERNAL_SERVER_ERROR;
+ break;
+ default:
+ status_code = HTTPMessage::INTERNAL_SERVER_ERROR;
+ break;
+ }
+}
+
+const string& Response::get_version(void) const {
+ return xml_version;
+}
+
+const string& Response::set_version(const string& version_) {
+ xml_version = version_;
+ build();
+ return version_;
+}
+
+const Response::Code& Response::get_code(void) const {
+ return xml_code;
+}
+
+const Response::Code& Response::set_code(const Response::Code& code_) {
+ xml_code = code_;
+ build();
+ return code_;
+}
+
+const string& Response::get_description(void) const {
+ return xml_description;
+}
+
+const string& Response::set_description(const string& description_) {
+ xml_description = description_;
+ build();
+ return description_;
+}
+
+const string& Response::get_contents(void) const {
+ return xml_contents;
+}
+
+const string& Response::set_contents(const string& contents_) {
+ xml_contents = contents_;
+ build();
+ return contents_;
}
istream& operator>>(istream& is, Response& resp)
- /*throw (HTTPResponse::Error, ios::failure, sockerr)*/ {
+ throw (HTTPResponse::Error, ios::failure, sockerr,
+ xmlpp::parse_error) {
#ifdef DEBUG
cerr << __FILE__ << "(" << __LINE__ << ")" << ": operator>>()" << endl;
#endif // DEBUG
is >> static_cast<HTTPResponse&>(resp);
if (resp.get_body().length()) {
- // TODO parseo XML del body para buscar las cosas basicas de la respuesta.
- Response::Parser parser(resp);
- try {
- parser.parse_memory(resp.get_body());
- } catch (const xmlpp::parse_error& e) {
- // TODO - ver gravedad.
-#ifdef DEBUG
- cerr << __FILE__ << "(" << __LINE__ << ")"
- << " : operator>>() error de parseo: " << e.what() << endl;
-#endif // DEBUG
- }
+ Response::Parser(resp).parse_memory(resp.get_body());
}
+ resp.build();
return is;
}
-/// \todo TODO hacer el metodo build como en command.
ostream& operator<<(ostream& os, const Response& resp) {
#ifdef DEBUG
cerr << __FILE__ << "(" << __LINE__ << ")" << ": operator<<()" << endl;
#endif // DEBUG
- String b;
- b.from(resp.xml_code);
- b = string("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n"
- "<plaqui-response code=\"") + b + "\" version=\""
- + resp.xml_version + "\" ";
- if (resp.xml_description.length()) {
- b += "description=\"" + resp.xml_description + "\" ";
- }
- if (resp.xml_body.length()) {
- b += ">\n" + resp.xml_body + "\n</plaqui-response>\n";
- } else {
- b += "/>\n";
- }
- HTTPResponse r(resp);
- r.set_body(b);
- os << static_cast<const HTTPResponse&>(r);
+ os << static_cast<const HTTPResponse&>(resp);
return os;
}