X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/ffc9f02ea0544d4b1aa86e29afe12a2b4bace953..ea3d0f30b8e44952b1b9ac3e2f5b2a08cd65ca48:/Server/src/response.cpp diff --git a/Server/src/response.cpp b/Server/src/response.cpp index b39aaea..3a3f4bf 100644 --- a/Server/src/response.cpp +++ b/Server/src/response.cpp @@ -53,64 +53,124 @@ Response::Response(const Code& code, const string& desc): << 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("\n" + "\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(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("\n" - "\n"; - } else { - b += "/>\n"; - } - HTTPResponse r(resp); - r.set_body(b); - os << static_cast(r); + os << static_cast(resp); return os; }