1//
2// OSPCodeWriter.cpp
3//
4// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "OSPCodeWriter.h"
12#include "Page.h"
13#include "Poco/NumberParser.h"
14
15
16OSPCodeWriter::OSPCodeWriter(const Page& page, const std::string& clazz):
17 CodeWriter(page, clazz)
18{
19}
20
21
22OSPCodeWriter::~OSPCodeWriter()
23{
24}
25
26
27void OSPCodeWriter::writeHeaderIncludes(std::ostream& ostr)
28{
29 CodeWriter::writeHeaderIncludes(ostr);
30 ostr << "#include \"Poco/OSP/Web/WebRequestHandlerFactory.h\"\n";
31 ostr << "#include \"Poco/OSP/BundleContext.h\"\n";
32}
33
34
35void OSPCodeWriter::writeHandlerClass(std::ostream& ostr)
36{
37 std::string base(page().get("page.baseClass", "Poco::Net::HTTPRequestHandler"));
38
39 handlerClass(ostr, base, "Poco::OSP::BundleContext::Ptr");
40}
41
42
43void OSPCodeWriter::writeHandlerMembers(std::ostream& ostr)
44{
45 std::string base(page().get("page.baseClass", ""));
46 if (base.empty())
47 {
48 ostr << "\n";
49 ostr << "protected:\n";
50 ostr << "\tPoco::OSP::BundleContext::Ptr context() const\n";
51 ostr << "\t{\n";
52 ostr << "\t\treturn _pContext;\n";
53 ostr << "\t}\n";
54 ostr << "\n";
55 ostr << "\tPoco::OSP::BundleContext::Ptr c() const\n";
56 ostr << "\t{\n";
57 ostr << "\t\treturn _pContext;\n";
58 ostr << "\t}\n";
59 ostr << "\n";
60 ostr << "private:\n";
61 ostr << "\tPoco::OSP::BundleContext::Ptr _pContext;\n";
62 }
63}
64
65
66void OSPCodeWriter::writeFactoryClass(std::ostream& ostr)
67{
68 ostr << "\n\n";
69 factoryClass(ostr, "Poco::OSP::Web::WebRequestHandlerFactory");
70}
71
72
73void OSPCodeWriter::writeImplIncludes(std::ostream& ostr)
74{
75 CodeWriter::writeImplIncludes(ostr);
76 if (page().has("page.session"))
77 {
78 ostr << "#include \"Poco/OSP/Web/WebSession.h\"\n";
79 ostr << "#include \"Poco/OSP/Web/WebSessionManager.h\"\n";
80 ostr << "#include \"Poco/OSP/ServiceRegistry.h\"\n";
81 }
82}
83
84
85void OSPCodeWriter::writeConstructor(std::ostream& ostr)
86{
87 std::string base(page().get("page.baseClass", ""));
88 ostr << clazz() << "::" << clazz() << "(Poco::OSP::BundleContext::Ptr pContext):\n";
89 if (base.empty())
90 {
91 ostr << "\t_pContext(pContext)\n";
92 }
93 else
94 {
95 ostr << "\t" << base << "(pContext)\n";
96 }
97 ostr << "{\n}\n";
98 ostr << "\n\n";
99}
100
101
102void OSPCodeWriter::writeSession(std::ostream& ostr)
103{
104 if (page().has("page.session"))
105 {
106 std::string session = page().get("page.session");
107 std::string sessionCode;
108 if (session.empty()) return;
109 if (session[0] == '@')
110 sessionCode = "context()->thisBundle()->properties().getString(\"" + session.substr(1) + "\")";
111 else
112 sessionCode = "\"" + session + "\"";
113 std::string sessionTimeoutCode = page().get("page.sessionTimeout", "30");
114 int sessionTimeout;
115 if (!Poco::NumberParser::tryParse(sessionTimeoutCode, sessionTimeout))
116 {
117 sessionTimeoutCode = "context()->thisBundle()->properties().getInt(\"" + sessionTimeoutCode + "\")";
118 }
119 ostr << "\tPoco::OSP::Web::WebSession::Ptr session;\n";
120 ostr << "\t{\n";
121 ostr << "\t\tPoco::OSP::ServiceRef::Ptr pWebSessionManagerRef = context()->registry().findByName(Poco::OSP::Web::WebSessionManager::SERVICE_NAME);\n";
122 ostr << "\t\tif (pWebSessionManagerRef)\n";
123 ostr << "\t\t{\n";
124 ostr << "\t\t\tPoco::OSP::Web::WebSessionManager::Ptr pWebSessionManager = pWebSessionManagerRef->castedInstance<Poco::OSP::Web::WebSessionManager>();\n";
125 if (page().get("page.createSession", "true") != "false")
126 {
127 ostr << "\t\t\tsession = pWebSessionManager->get(" << sessionCode << ", request, " << sessionTimeoutCode << ", context());\n";
128 }
129 else
130 {
131 ostr << "\t\t\tsession = pWebSessionManager->find(" << sessionCode << ", request);\n";
132 }
133 ostr << "\t\t}\n";
134 ostr << "\t}\n";
135 }
136}
137
138
139void OSPCodeWriter::writeFactory(std::ostream& ostr)
140{
141 ostr << "\n\n";
142 factoryImpl(ostr, "context()");
143}
144