summaryrefslogtreecommitdiff
path: root/icespider/core/irouteHandler.cpp
blob: a0d8fcd2a3999967c2dc470cf5e1cbe918c969b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "irouteHandler.h"
#include "core.h"
#include <plugins.impl.h>

INSTANTIATEPLUGINOF(IceSpider::IRouteHandler);

namespace IceSpider {
	IRouteHandler::IRouteHandler(HttpMethod m, const std::string & p) :
		Path(p),
		method(m)
	{
		auto globalSerializers = AdHoc::PluginManager::getDefault()->getAll<Slicer::StreamSerializerFactory>();
		for (const auto & gs : globalSerializers) {
			auto slash = gs->name.find('/');
			routeSerializers.insert({ { gs->name.substr(0, slash), gs->name.substr(slash + 1) }, gs->implementation() });
		}
	}

	IRouteHandler::~IRouteHandler()
	{
	}

	Ice::ObjectPrx
	IRouteHandler::getProxy(IHttpRequest * request, const char * type) const
	{
		return request->core->getProxy(type);
	}

	Slicer::SerializerPtr
	IRouteHandler::getSerializer(const char * grp, const char * type, std::ostream & strm) const
	{
		for (const auto & rs : routeSerializers) {
			if ((!grp || rs.first.first == grp) && (!type || rs.first.second == type)) {
				return rs.second->create(strm);
			}
		}
		return nullptr;
	}

	Slicer::SerializerPtr
	IRouteHandler::defaultSerializer(std::ostream & strm) const
	{
		return Slicer::StreamSerializerFactory::createNew(
			"application/json", strm);
	}

	void
	IRouteHandler::addRouteSerializer(const ContentType & ct, StreamSerializerFactoryPtr ssfp)
	{
		routeSerializers.insert({ ct, ssfp });
	}

	void
	IRouteHandler::removeRouteSerializer(const ContentType & ct)
	{
		auto i = routeSerializers.find(ct);
		if (i != routeSerializers.end()) {
			delete i->second;
			routeSerializers.erase(i);
		}
	}
}