Skip to content

Commit 348bb0e

Browse files
authored
Don't throw when setting value and config is empty (#560)
IB-7845 Signed-off-by: Raul Metsma <[email protected]>
1 parent a5868e6 commit 348bb0e

File tree

1 file changed

+58
-72
lines changed

1 file changed

+58
-72
lines changed

src/XmlConf.cpp

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <fstream>
2828

2929
using namespace std;
30+
using namespace digidoc;
3031
using namespace digidoc::util;
3132
using namespace xercesc;
3233
using namespace xml_schema;
@@ -38,7 +39,7 @@ template <class A>
3839
class XmlConfParam: public unique_ptr<A>
3940
{
4041
public:
41-
XmlConfParam(string _name, A def = {}): name(std::move(_name)), _def(std::move(def)) {}
42+
XmlConfParam(string &&_name, A &&def = {}): name(std::move(_name)), defaultValue(std::move(def)) {}
4243

4344
bool setValue(const Configuration::ParamType &p, bool global)
4445
{
@@ -59,13 +60,13 @@ class XmlConfParam: public unique_ptr<A>
5960

6061
XmlConfParam &operator=(const A &other)
6162
{
62-
unique_ptr<A>::reset(_def != other ? new A(other) : nullptr);
63+
unique_ptr<A>::reset(defaultValue != other ? new A(other) : nullptr);
6364
return *this;
6465
}
6566

6667
operator A() const
6768
{
68-
return value(_def);
69+
return value(defaultValue);
6970
}
7071

7172
A value(const A &def) const
@@ -74,7 +75,7 @@ class XmlConfParam: public unique_ptr<A>
7475
}
7576

7677
const string name;
77-
A _def;
78+
const A defaultValue;
7879
bool locked = false;
7980
};
8081

@@ -86,10 +87,7 @@ class XmlConf::Private
8687
void init(const string &path, bool global);
8788
unique_ptr<Configuration> read(const string &path) const;
8889
template <class A>
89-
void setUserConf(XmlConfParam<A> &param, const A &defined, const A &value);
90-
static string to_string(bool val) { return val ? "true" : "false"; }
91-
static string to_string(const string &val) { return val; }
92-
90+
void setUserConf(XmlConfParam<A> &param, A &&defined, const A &value);
9391

9492
XmlConfParam<int> logLevel{"log.level", 3};
9593
XmlConfParam<string> logFile{"log.file"};
@@ -112,11 +110,11 @@ class XmlConf::Private
112110
std::set<std::string> ocspTMProfiles;
113111

114112
string SCHEMA_LOC;
115-
string USER_CONF_LOC;
113+
static const string USER_CONF_LOC;
116114
};
117115
}
118116

119-
using namespace digidoc;
117+
const string XmlConf::Private::USER_CONF_LOC = File::path(File::digidocppPath(), "digidocpp.conf");
120118

121119
XmlConf::Private::Private(const string &path, string schema)
122120
: SCHEMA_LOC(std::move(schema))
@@ -128,13 +126,12 @@ XmlConf::Private::Private(const string &path, string schema)
128126
catch (const XMLException &e) {
129127
try {
130128
string result = xsd::cxx::xml::transcode<char>(e.getMessage());
131-
THROW("Error during initialisation of Xerces: %s", result.c_str());
129+
THROW("Error during initialization of Xerces: %s", result.c_str());
132130
} catch(const xsd::cxx::xml::invalid_utf16_string & /* ex */) {
133-
THROW("Error during initialisation of Xerces");
131+
THROW("Error during initialization of Xerces");
134132
}
135133
}
136134

137-
USER_CONF_LOC = File::path(File::digidocppPath(), "digidocpp.conf");
138135
if(path.empty())
139136
{
140137
init(File::path(File::confPath(), "digidocpp.conf"), true);
@@ -151,46 +148,35 @@ XmlConf::Private::Private(const string &path, string schema)
151148
void XmlConf::Private::init(const string& path, bool global)
152149
{
153150
DEBUG("XmlConfPrivate::init(%s, %u)", path.c_str(), global);
154-
try
155-
{
156-
unique_ptr<Configuration> conf = read(path);
157-
for(const Configuration::ParamType &p: conf->param())
158-
{
159-
if(logLevel.setValue(p, global) ||
160-
logFile.setValue(p, global) ||
161-
digestUri.setValue(p, global) ||
162-
signatureDigestUri.setValue(p, global) ||
163-
PKCS11Driver.setValue(p, global) ||
164-
proxyForceSSL.setValue(p, global) ||
165-
proxyTunnelSSL.setValue(p, global) ||
166-
proxyHost.setValue(p, global) ||
167-
proxyPort.setValue(p, global) ||
168-
proxyUser.setValue(p, global) ||
169-
proxyPass.setValue(p, global) ||
170-
TSUrl.setValue(p, global) ||
171-
TSLAutoUpdate.setValue(p, global) ||
172-
TSLCache.setValue(p, global) ||
173-
TSLOnlineDigest.setValue(p, global) ||
174-
TSLTimeOut.setValue(p, global) ||
175-
verifyServiceUri.setValue(p, global))
176-
continue;
177-
if(p.name() == "ocsp.tm.profile" && global)
178-
ocspTMProfiles.emplace(p);
179-
else
180-
WARN("Unknown configuration parameter %s", p.name().c_str());
181-
}
182-
183-
for(const Configuration::OcspType &o: conf->ocsp())
184-
ocsp[o.issuer()] = o;
185-
}
186-
catch(const Exception &e)
187-
{
188-
WARN("Failed to parse configuration: %s %d %s", path.c_str(), global, e.msg().c_str());
189-
}
190-
catch(const xml_schema::Exception &e)
151+
unique_ptr<Configuration> conf = read(path);
152+
for(const Configuration::ParamType &p: conf->param())
191153
{
192-
WARN("Failed to parse configuration: %s %d %s", path.c_str(), global, e.what());
154+
if(logLevel.setValue(p, global) ||
155+
logFile.setValue(p, global) ||
156+
digestUri.setValue(p, global) ||
157+
signatureDigestUri.setValue(p, global) ||
158+
PKCS11Driver.setValue(p, global) ||
159+
proxyForceSSL.setValue(p, global) ||
160+
proxyTunnelSSL.setValue(p, global) ||
161+
proxyHost.setValue(p, global) ||
162+
proxyPort.setValue(p, global) ||
163+
proxyUser.setValue(p, global) ||
164+
proxyPass.setValue(p, global) ||
165+
TSUrl.setValue(p, global) ||
166+
TSLAutoUpdate.setValue(p, global) ||
167+
TSLCache.setValue(p, global) ||
168+
TSLOnlineDigest.setValue(p, global) ||
169+
TSLTimeOut.setValue(p, global) ||
170+
verifyServiceUri.setValue(p, global))
171+
continue;
172+
if(p.name() == "ocsp.tm.profile" && global)
173+
ocspTMProfiles.emplace(p);
174+
else
175+
WARN("Unknown configuration parameter %s", p.name().c_str());
193176
}
177+
178+
for(const Configuration::OcspType &o: conf->ocsp())
179+
ocsp[o.issuer()] = o;
194180
}
195181

196182
/**
@@ -202,7 +188,7 @@ unique_ptr<Configuration> XmlConf::Private::read(const string &path) const
202188
{
203189
try
204190
{
205-
if(File::fileExists(path))
191+
if(File::fileExists(path) && File::fileSize(path) > 0)
206192
{
207193
Properties props;
208194
props.no_namespace_schema_location(SCHEMA_LOC);
@@ -211,22 +197,22 @@ unique_ptr<Configuration> XmlConf::Private::read(const string &path) const
211197
}
212198
catch(const xml_schema::Exception& e)
213199
{
214-
THROW("Failed to parse configuration: %s (%s) - %s",
200+
WARN("Failed to parse configuration: %s (%s) - %s",
215201
path.c_str(), SCHEMA_LOC.c_str(), e.what());
216202
}
217203
catch(const xsd::cxx::xml::properties<char>::argument & /* e */)
218204
{
219-
THROW("Failed to parse configuration: %s (%s)",
205+
WARN("Failed to parse configuration: %s (%s)",
220206
path.c_str(), SCHEMA_LOC.c_str());
221207
}
222208
catch(const xsd::cxx::xml::invalid_utf8_string & /* e */)
223209
{
224-
THROW("Failed to parse configuration: %s (%s)",
210+
WARN("Failed to parse configuration: %s (%s)",
225211
path.c_str(), SCHEMA_LOC.c_str());
226212
}
227213
catch(const xsd::cxx::xml::invalid_utf16_string & /* e */)
228214
{
229-
THROW("Failed to parse configuration: %s (%s)",
215+
WARN("Failed to parse configuration: %s (%s)",
230216
path.c_str(), SCHEMA_LOC.c_str());
231217
}
232218
return make_unique<Configuration>();
@@ -241,38 +227,38 @@ unique_ptr<Configuration> XmlConf::Private::read(const string &path) const
241227
* @throws Exception exception is thrown if reading, writing or creating of a user configuration file fails.
242228
*/
243229
template<class A>
244-
void XmlConf::Private::setUserConf(XmlConfParam<A> &param, const A &defined, const A &value)
230+
void XmlConf::Private::setUserConf(XmlConfParam<A> &param, A &&defined, const A &value)
245231
{
246232
if(param.locked)
247233
return;
248234
param = value;
249235
unique_ptr<Configuration> conf = read(USER_CONF_LOC);
250-
try
236+
Configuration::ParamSequence &paramSeq = conf->param();
237+
for(auto it = paramSeq.begin(); it != paramSeq.end(); ++it)
251238
{
252-
Configuration::ParamSequence &paramSeq = conf->param();
253-
for(Configuration::ParamSequence::iterator it = paramSeq.begin(); it != paramSeq.end(); ++it)
239+
if(param.name == it->name())
254240
{
255-
if(param.name == it->name())
256-
{
257-
paramSeq.erase(it);
258-
break;
259-
}
241+
paramSeq.erase(it);
242+
break;
260243
}
261-
if(defined != value) //if it's a new parameter
262-
paramSeq.push_back(make_unique<Param>(to_string(value), param.name));
263244
}
264-
catch (const xml_schema::Exception& e)
245+
if(defined != value) //if it's a new parameter
265246
{
266-
THROW("(in set %s) Failed to parse configuration: %s", param.name.c_str(), e.what());
247+
if constexpr(is_same<A,bool>::value)
248+
paramSeq.push_back(make_unique<Param>(value ? "true" : "false", param.name));
249+
else if constexpr(is_integral<A>::value)
250+
paramSeq.push_back(make_unique<Param>(to_string(value), param.name));
251+
else
252+
paramSeq.push_back(make_unique<Param>(std::move(value), param.name));
267253
}
268254

269255
File::createDirectory(File::directory(USER_CONF_LOC));
270256
ofstream ofs(File::encodeName(USER_CONF_LOC));
271257
if (ofs.fail())
272258
THROW("Failed to open configuration: %s", USER_CONF_LOC.c_str());
273-
NamespaceInfomap map;
274-
map[{}].name = {};
275-
map[{}].schema = SCHEMA_LOC;
259+
NamespaceInfomap map{{
260+
{{}, {{}, SCHEMA_LOC}}
261+
}};
276262
configuration(ofs, *conf, map, "UTF-8", Flags::dont_initialize);
277263
}
278264

0 commit comments

Comments
 (0)