<commit_before>/*
 * Copyright (c) 2015 Isode Limited.
 * All rights reserved.
 * See the LICENSE file for more information.
 */

#include <memory>

#include <boost/thread.hpp>

#include <restpp/JSONRESTHandler.h>
#include <restpp/RESTServer.h>

using namespace librestpp;

class StringHandler : public JSONRESTHandler {
	public:
		StringHandler(const std::string& result) : result_(result) {}
		virtual void handleRequest(std::shared_ptr<RESTRequest> request) {
			request->setReplyHeader(RESTRequest::HTTP_OK);
			request->setContentType("text/plain");
			request->addReplyContent(result_);
			request->sendReply();
		}

	private:
		std::string result_;
};

class TextEchoHandler : public JSONRESTHandler {
	public:
		TextEchoHandler() {}
		virtual void handleRequest(std::shared_ptr<RESTRequest> request) {
			request->setReplyHeader(RESTRequest::HTTP_OK);
			request->addReplyContent(request->getBody());
			request->setContentType("text/plain");
			request->sendReply();
		}

};

class JSONEchoHandler : public JSONRESTHandler {
	public:
		JSONEchoHandler() {}
		virtual void handleRequest(std::shared_ptr<RESTRequest> request) {
			std::shared_ptr<JSONObject> json = request->getJSON();

			request->setReplyHeader(RESTRequest::HTTP_OK);
			if (!!json) {
				request->addReplyContent(json);
				request->setContentType("application/json");
			}
			else {
				request->setContentType("text/plain");
				request->addReplyContent("Invalid JSON sent");
			}
			request->sendReply();
		}

};

class JSONChefHandler : public JSONRESTHandler {
	public:
		JSONChefHandler() {}
		virtual void handleRequest(std::shared_ptr<RESTRequest> request) {
			std::shared_ptr<JSONObject> json = request->getJSON();

			request->setReplyHeader(RESTRequest::HTTP_OK);
			if (!!json) {
				request->addReplyContent(std::dynamic_pointer_cast<JSONObject>(chefify(json)));
				request->setContentType("application/json");
			}
			else {
				request->setContentType("text/plain");
				request->addReplyContent("Invalid JSON sent");
			}
			request->sendReply();
		}

	private:
		JSONValue::ref chefify(JSONValue::ref value) {
			std::shared_ptr<JSONObject> jsonObject;
			std::shared_ptr<JSONString> jsonString;
			if ((jsonObject = std::dynamic_pointer_cast<JSONObject>(value))) {
				std::shared_ptr<JSONObject> result = std::make_shared<JSONObject>();
				auto values = jsonObject->getValues();
				for (auto it = values.begin(); it != values.end(); it++) {
					result->set(it->first, chefify(it->second));
				}
				return result;
			}
			else if ((jsonString = std::dynamic_pointer_cast<JSONString>(value))) {<commit_msg>clang-modernize the example too<commit_after>/*
 * Copyright (c) 2015 Isode Limited.
 * All rights reserved.
 * See the LICENSE file for more information.
 */

#include <memory>

#include <boost/thread.hpp>

#include <restpp/JSONRESTHandler.h>
#include <restpp/RESTServer.h>

using namespace librestpp;

class StringHandler : public JSONRESTHandler {
	public:
		StringHandler(std::string  result) : result_(std::move(result)) {}
		virtual void handleRequest(std::shared_ptr<RESTRequest> request) override {
			request->setReplyHeader(RESTRequest::HTTP_OK);
			request->setContentType("text/plain");
			request->addReplyContent(result_);
			request->sendReply();
		}

	private:
		std::string result_;
};

class TextEchoHandler : public JSONRESTHandler {
	public:
		TextEchoHandler() {}
		virtual void handleRequest(std::shared_ptr<RESTRequest> request) override {
			request->setReplyHeader(RESTRequest::HTTP_OK);
			request->addReplyContent(request->getBody());
			request->setContentType("text/plain");
			request->sendReply();
		}

};

class JSONEchoHandler : public JSONRESTHandler {
	public:
		JSONEchoHandler() {}
		virtual void handleRequest(std::shared_ptr<RESTRequest> request) override {
			std::shared_ptr<JSONObject> json = request->getJSON();

			request->setReplyHeader(RESTRequest::HTTP_OK);
			if (!!json) {
				request->addReplyContent(json);
				request->setContentType("application/json");
			}
			else {
				request->setContentType("text/plain");
				request->addReplyContent("Invalid JSON sent");
			}
			request->sendReply();
		}

};

class JSONChefHandler : public JSONRESTHandler {
	public:
		JSONChefHandler() {}
		virtual void handleRequest(std::shared_ptr<RESTRequest> request) override {
			std::shared_ptr<JSONObject> json = request->getJSON();

			request->setReplyHeader(RESTRequest::HTTP_OK);
			if (!!json) {
				request->addReplyContent(std::dynamic_pointer_cast<JSONObject>(chefify(json)));
				request->setContentType("application/json");
			}
			else {
				request->setContentType("text/plain");
				request->addReplyContent("Invalid JSON sent");
			}
			request->sendReply();
		}

	private:
		JSONValue::ref chefify(JSONValue::ref value) {
			std::shared_ptr<JSONObject> jsonObject;
			std::shared_ptr<JSONString> jsonString;
			if ((jsonObject = std::dynamic_pointer_cast<JSONObject>(value))) {
				std::shared_ptr<JSONObject> result = std::make_shared<JSONObject>();
				auto values = jsonObject->getValues();
				for (auto & value : values) {
					result->set(value.first, chefify(value.second));
				}
				return result;
			}
			else if ((jsonString = std::dynamic_pointer_cast<JSONString>(value))) {
{'fraction_non_alphanumeric': 0.14272103658536586, 'fraction_numerical': 0.001524390243902439, 'mean_word_length': 5.132009345794392, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5652692', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1587, 'n_words': 285}