Support CDATA for SOAP request and response; Rename Parameter to SoapParameter; Add book server and client example for SOAP.

This commit is contained in:
Adam Gu
2018-07-19 11:19:51 +08:00
parent 23d948cebb
commit 223e59cc7a
48 changed files with 1395 additions and 412 deletions
+11 -4
View File
@@ -1,6 +1,13 @@
set(SRCS book_services.cc book_services.h main.cc)
set(TARGET_NAME rest_book_server)
add_executable(rest_book_server ${SRCS})
set(SRCS
../common/book.cc
../common/book.h
services.cc
services.h
main.cc)
target_link_libraries(rest_book_server webcc jsoncpp ${Boost_LIBRARIES})
target_link_libraries(rest_book_server "${CMAKE_THREAD_LIBS_INIT}")
add_executable(${TARGET_NAME} ${SRCS})
target_link_libraries(${TARGET_NAME} webcc jsoncpp ${Boost_LIBRARIES})
target_link_libraries(${TARGET_NAME} "${CMAKE_THREAD_LIBS_INIT}")
+1 -1
View File
@@ -3,7 +3,7 @@
#include "webcc/logger.h"
#include "webcc/rest_server.h"
#include "example/rest_book_server/book_services.h"
#include "example/rest_book_server/services.h"
// In order to run with VLD, please copy the following files to the example
// output folder from "third_party\win32\bin":
@@ -1,4 +1,4 @@
#include "example/rest_book_server/book_services.h"
#include "example/rest_book_server/services.h"
#include <iostream>
#include <list>
@@ -7,95 +7,21 @@
#include "json/json.h"
#include "webcc/logger.h"
#include "example/common/book.h"
// -----------------------------------------------------------------------------
// In-memory test data.
// There should be some database in a real product.
struct Book {
std::string id;
std::string title;
double price;
bool IsNull() const {
return id.empty();
}
Json::Value ToJson() const {
Json::Value root;
root["id"] = id;
root["title"] = title;
root["price"] = price;
return root;
}
};
std::ostream& operator<<(std::ostream& os, const Book& book) {
os << "{ " << book.id << ", " << book.title << ", " << book.price << " }";
return os;
}
static const Book kNullBook{};
class BookStore {
public:
BookStore() = default;
const std::list<Book>& books() const { return books_; }
const Book& GetBook(const std::string& id) const {
auto it = FindBook(id);
return (it == books_.end() ? kNullBook : *it);
}
bool AddBook(const Book& new_book) {
if (FindBook(new_book.id) == books_.end()) {
books_.push_back(new_book);
return true;
}
return false;
}
bool UpdateBook(const Book& book) {
auto it = FindBook(book.id);
if (it != books_.end()) {
it->title = book.title;
it->price = book.price;
return true;
}
return false;
}
bool DeleteBook(const std::string& id) {
auto it = FindBook(id);
if (it != books_.end()) {
books_.erase(it);
return true;
}
return false;
}
private:
std::list<Book>::const_iterator FindBook(const std::string& id) const {
return std::find_if(books_.begin(), books_.end(),
[&id](const Book& book) { return book.id == id; });
}
std::list<Book>::iterator FindBook(const std::string& id) {
return std::find_if(books_.begin(), books_.end(),
[&id](Book& book) { return book.id == id; });
}
std::list<Book> books_;
};
static BookStore g_book_store;
// -----------------------------------------------------------------------------
static Json::Value BookToJson(const Book& book) {
Json::Value root;
root["id"] = book.id;
root["title"] = book.title;
root["price"] = book.price;
return root;
}
static bool BookFromJson(const std::string& json, Book* book) {
static bool JsonToBook(const std::string& json, Book* book) {
Json::Value root;
Json::CharReaderBuilder builder;
std::stringstream stream(json);
@@ -112,6 +38,8 @@ static bool BookFromJson(const std::string& json, Book* book) {
return true;
}
// -----------------------------------------------------------------------------
// Return all books as a JSON array.
// TODO: Support query parameters.
bool BookListService::Get(const webcc::UrlQuery& /* query */,
@@ -123,7 +51,7 @@ bool BookListService::Get(const webcc::UrlQuery& /* query */,
Json::Value root(Json::arrayValue);
for (const Book& book : g_book_store.books()) {
root.append(book.ToJson());
root.append(BookToJson(book));
}
Json::StreamWriterBuilder builder;
@@ -142,8 +70,9 @@ bool BookListService::Post(const std::string& request_content,
}
Book book;
if (BookFromJson(request_content, &book)) {
return g_book_store.AddBook(book);
if (JsonToBook(request_content, &book)) {
g_book_store.AddBook(book); // TODO: return ID
return true;
}
return false;
@@ -168,7 +97,7 @@ bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
const Book& book = g_book_store.GetBook(book_id);
if (!book.IsNull()) {
Json::StreamWriterBuilder builder;
*response_content = Json::writeString(builder, book.ToJson());
*response_content = Json::writeString(builder, BookToJson(book));
return true;
}
@@ -191,7 +120,7 @@ bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
const std::string& book_id = url_sub_matches[0];
Book book;
if (BookFromJson(request_content, &book)) {
if (JsonToBook(request_content, &book)) {
book.id = book_id;
return g_book_store.UpdateBook(book);
}
@@ -1,5 +1,8 @@
#ifndef BOOK_SERVICES_H_
#define BOOK_SERVICES_H_
#ifndef EXAMPLE_REST_BOOK_SERVER_SERVICES_H_
#define EXAMPLE_REST_BOOK_SERVER_SERVICES_H_
#include <string>
#include <vector>
#include "webcc/rest_service.h"
@@ -13,7 +16,7 @@
// The query parameters could be regular expressions.
class BookListService : public webcc::RestListService {
public:
BookListService(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
explicit BookListService(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
}
protected:
@@ -39,7 +42,8 @@ class BookListService : public webcc::RestListService {
// contains the matched book ID.
class BookDetailService : public webcc::RestDetailService {
public:
BookDetailService(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
explicit BookDetailService(int sleep_seconds)
: sleep_seconds_(sleep_seconds) {
}
protected:
@@ -58,4 +62,4 @@ class BookDetailService : public webcc::RestDetailService {
int sleep_seconds_ = 0;
};
#endif // BOOK_SERVICE_H_
#endif // EXAMPLE_REST_BOOK_SERVER_SERVICES_H_