#include "example/soap_book_server/book_service.h" #include #include #include #include "webcc/logger.h" #include "webcc/soap_request.h" #include "webcc/soap_response.h" #include "example/common/book.h" #include "example/common/book_xml.h" // ----------------------------------------------------------------------------- static BookStore g_book_store; static const std::string kResult = "Result"; // ----------------------------------------------------------------------------- bool BookService::Handle(const webcc::SoapRequest& soap_request, webcc::SoapResponse* soap_response) { const std::string& operation = soap_request.operation(); soap_response->set_service_ns({ "ser", "http://www.example.com/book/" }); soap_response->set_operation(operation); if (operation == "CreateBook") { return CreateBook(soap_request, soap_response); } else if (operation == "GetBook") { return GetBook(soap_request, soap_response); } else if (operation == "ListBooks") { return ListBooks(soap_request, soap_response); } else if (operation == "DeleteBook") { return DeleteBook(soap_request, soap_response); } else { LOG_ERRO("Operation '%s' is not supported.", operation.c_str()); return false; } return false; } bool BookService::CreateBook(const webcc::SoapRequest& soap_request, webcc::SoapResponse* soap_response) { // Request SOAP envelope: // // // // // // 1984 // 12.3 // // ]]> // // // // // Response SOAP envelope: // // // // // // // // 1 // // // ]]> // // // // const std::string& title = soap_request.GetParameter("title"); const std::string& book_xml = soap_request.GetParameter("book"); Book book; XmlStringToBook(book_xml, &book); // TODO: Error handling std::string id = g_book_store.AddBook(book); std::string response_xml = NewResultXml(0, "ok", "book", "id", id.c_str()); soap_response->set_simple_result(kResult, std::move(response_xml), true); return true; } bool BookService::GetBook(const webcc::SoapRequest& soap_request, webcc::SoapResponse* soap_response) { // Request SOAP envelope: // // // // 1 // // // // Response SOAP envelope: // // // // // // // // 1 // 1984 // 12.3 // // // ]]> // // // // const std::string& id = soap_request.GetParameter("id"); const Book& book = g_book_store.GetBook(id); soap_response->set_simple_result(kResult, NewResultXml(0, "ok", book), true); return true; } bool BookService::ListBooks(const webcc::SoapRequest& soap_request, webcc::SoapResponse* soap_response) { // Request SOAP envelope: // // // // // // Response SOAP envelope: // // // // // // // // // 1 // 1984 // 12.3 // // ... // // // ]]> // // // // const std::list& books = g_book_store.books(); soap_response->set_simple_result(kResult, NewResultXml(0, "ok", books), true); return true; } bool BookService::DeleteBook(const webcc::SoapRequest& soap_request, webcc::SoapResponse* soap_response) { // Request SOAP envelope: // // // // 1 // // // // Response SOAP envelope: // // // // // // // // ]]> // // // // const std::string& id = soap_request.GetParameter("id"); if (g_book_store.DeleteBook(id)) { soap_response->set_simple_result(kResult, NewResultXml(0, "ok"), true); } else { soap_response->set_simple_result(kResult, NewResultXml(1, "error"), true); } return true; }