loggingagent.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "httpclient.hpp"
  14. #include "ws_loggingservice_esp.ipp"
  15. #include "LoggingErrors.hpp"
  16. #include "loggingcommon.hpp"
  17. #include "loggingagentbase.hpp"
  18. #include "loggingagent.hpp"
  19. static const int DefaultMaxTriesGTS = -1;
  20. static const char* const PropESPServer = "ESPServer";
  21. static const char* const PropServerUrl = "@url";
  22. static const char* const PropServerUserID = "@user";
  23. static const char* const PropServerPassword = "@password";
  24. static const char* const PropServerWaitingSeconds = "MaxServerWaitingSeconds";
  25. static const char* const PropMaxTransIDLength = "MaxTransIDLength";
  26. static const char* const PropMaxTransIDSequenceNumber = "MaxTransIDSequenceNumber";
  27. static const char* const PropMaxTransSeedTimeoutMinutes = "MaxTransSeedTimeoutMinutes";
  28. static const char* const MaxTriesGTS = "MaxTriesGTS";
  29. static const char* const appESPServerLoggingAgent = "ESPServerLoggingAgent";
  30. bool CESPServerLoggingAgent::init(const char * name, const char * type, IPropertyTree * cfg, const char * process)
  31. {
  32. if (!cfg)
  33. return false;
  34. IPropertyTree* espServer = cfg->queryBranch(PropESPServer);
  35. if(!espServer)
  36. throw MakeStringException(-1,"Unable to find ESPServer settings for log agent %s:%s", name, type);
  37. const char* url = espServer->queryProp(PropServerUrl);
  38. if (url && *url)
  39. serverUrl.set(url);
  40. const char* userID = espServer->queryProp(PropServerUserID);
  41. const char* password = espServer->queryProp(PropServerPassword);
  42. if (userID && *userID && password && *password)
  43. {
  44. serverUserID.set(userID);
  45. decrypt(serverPassword, password);
  46. }
  47. maxServerWaitingSeconds = cfg->getPropInt(PropServerWaitingSeconds);
  48. maxGTSRetries = cfg->getPropInt(MaxTriesGTS, DefaultMaxTriesGTS);
  49. BoolHash uniqueGroupNames;
  50. StringBuffer sourceName, groupName, dbName, localTransactionSeed;
  51. Owned<IPropertyTreeIterator> iter = cfg->getElements("LogSourceMap/LogSource");
  52. ForEach(*iter)
  53. {
  54. ensureInputString(iter->query().queryProp("@name"), false, sourceName, -1, "LogSource @name required");
  55. ensureInputString(iter->query().queryProp("@maptologgroup"), true, groupName, -1, "LogSource @maptologgroup required");
  56. ensureInputString(iter->query().queryProp("@maptodb"), true, dbName, -1, "LogSource @maptodb required");
  57. Owned<CLogSource> logSource = new CLogSource(sourceName.str(), groupName.str(), dbName.str());
  58. logSources.setValue(sourceName.str(), logSource);
  59. bool* found = uniqueGroupNames.getValue(groupName.str());
  60. if (!found || !*found)
  61. {
  62. uniqueGroupNames.setValue(groupName.str(), true);
  63. unsigned maxSeq = 0;
  64. unsigned maxLength = 0;
  65. unsigned seedExpiredSeconds = 0;
  66. VStringBuffer xpath("LogGroup/[@name='%s']", groupName.str());
  67. IPropertyTree* logGroup = cfg->queryBranch(xpath.str());
  68. if (logGroup)
  69. {
  70. maxLength = logGroup->getPropInt(PropMaxTransIDLength, 0);
  71. maxSeq = logGroup->getPropInt(PropMaxTransIDSequenceNumber, 0),
  72. seedExpiredSeconds = 60 * logGroup->getPropInt(PropMaxTransSeedTimeoutMinutes, 0);
  73. }
  74. StringBuffer transactionSeed, statusMessage;
  75. getTransactionSeed(groupName.str(), transactionSeed, statusMessage);
  76. if (transactionSeed.length() > 0)
  77. {
  78. Owned<CTransIDBuilder> entry = new CTransIDBuilder(transactionSeed.str(), false,
  79. maxLength, maxSeq, seedExpiredSeconds);
  80. transIDMap.setValue(groupName.str(), entry);
  81. if (iter->query().getPropBool("@default", false))
  82. defaultGroup.set(groupName.str());
  83. }
  84. else
  85. PROGLOG("Failed to get TransactionSeed for <%s>", groupName.str());
  86. }
  87. }
  88. createLocalTransactionSeed(localTransactionSeed);
  89. Owned<CTransIDBuilder> localTransactionEntry = new CTransIDBuilder(localTransactionSeed.str(), true,
  90. cfg->getPropInt(PropMaxTransIDLength, 0), cfg->getPropInt(PropMaxTransIDSequenceNumber, 0),
  91. 60 * cfg->getPropInt(PropMaxTransSeedTimeoutMinutes, 0));
  92. transIDMap.setValue(appESPServerLoggingAgent, localTransactionEntry);
  93. readAllLogFilters(cfg);
  94. return true;
  95. }
  96. void CESPServerLoggingAgent::readAllLogFilters(IPropertyTree* cfg)
  97. {
  98. bool groupFilterRead = false;
  99. VStringBuffer xpath("Filters/Filter[@type='%s']", espLogContentGroupNames[ESPLCGBackEndResp]);
  100. IPropertyTree* filter = cfg->queryBranch(xpath.str());
  101. if (filter && filter->hasProp("@value"))
  102. {
  103. logBackEndResp = filter->getPropBool("@value");
  104. groupFilterRead = true;
  105. }
  106. for (unsigned i = 0; i < ESPLCGBackEndResp; i++)
  107. {
  108. if (readLogFilters(cfg, i))
  109. groupFilterRead = true;
  110. }
  111. if (!groupFilterRead)
  112. {
  113. groupFilters.clear();
  114. readLogFilters(cfg, ESPLCGAll);
  115. }
  116. }
  117. bool CESPServerLoggingAgent::readLogFilters(IPropertyTree* cfg, unsigned groupID)
  118. {
  119. Owned<CESPLogContentGroupFilters> espLogContentGroupFilters = new CESPLogContentGroupFilters((ESPLogContentGroup) groupID);
  120. StringBuffer xpath;
  121. if (groupID != ESPLCGAll)
  122. xpath.appendf("Filters/Filter[@type='%s']", espLogContentGroupNames[groupID]);
  123. else
  124. xpath.append("Filters/Filter");
  125. Owned<IPropertyTreeIterator> filters = cfg->getElements(xpath.str());
  126. ForEach(*filters)
  127. {
  128. IPropertyTree &filter = filters->query();
  129. StringBuffer value = filter.queryProp("@value");
  130. if (!value.length())
  131. continue;
  132. //clean "//"
  133. unsigned idx = value.length()-1;
  134. while (idx)
  135. {
  136. if ((value.charAt(idx-1) == '/') && (value.charAt(idx) == '/'))
  137. value.remove(idx, 1);
  138. idx--;
  139. }
  140. //clean "/*" at the end
  141. while ((value.length() > 1) && (value.charAt(value.length()-2) == '/') && (value.charAt(value.length()-1) == '*'))
  142. value.setLength(value.length() - 2);
  143. if (value.length() && !streq(value.str(), "*") && !streq(value.str(), "/") && !streq(value.str(), "*/"))
  144. {
  145. espLogContentGroupFilters->addFilter(value.str());
  146. }
  147. else
  148. {
  149. espLogContentGroupFilters->clearFilters();
  150. break;
  151. }
  152. }
  153. bool hasFilter = espLogContentGroupFilters->getFilterCount() > 0;
  154. if (hasFilter)
  155. groupFilters.append(*espLogContentGroupFilters.getClear());
  156. return hasFilter;
  157. }
  158. void CESPServerLoggingAgent::createLocalTransactionSeed(StringBuffer& transactionSeed)
  159. {
  160. unsigned ip = queryHostIP().iphash();
  161. unsigned mstick6char = ((unsigned)usTick() & 0xFFFFFF);
  162. unsigned processId2char = ((unsigned)GetCurrentProcessId()) & 0xF;
  163. unsigned threadId1char = ((unsigned) (memsize_t) GetCurrentThreadId()) & 0xF;
  164. transactionSeed.setf("%02X%06X%X%X", ip, mstick6char, processId2char, threadId1char);
  165. }
  166. bool CESPServerLoggingAgent::getTransactionSeed(IEspGetTransactionSeedRequest& req, IEspGetTransactionSeedResponse& resp)
  167. {
  168. StringBuffer statusMessage, transactionSeed;
  169. int statusCode = getTransactionSeed(req.getApplication(), transactionSeed, statusMessage);
  170. resp.setStatusCode(statusCode);
  171. resp.setSeedId(transactionSeed.str());
  172. if (statusMessage.length())
  173. resp.setStatusMessage(statusMessage.str());
  174. return (statusCode != -2);
  175. }
  176. int CESPServerLoggingAgent::getTransactionSeed(const char* appName, StringBuffer& transactionSeed, StringBuffer& statusMessage)
  177. {
  178. StringBuffer soapreq(
  179. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  180. "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
  181. " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  182. " <soap:Body>");
  183. if (!appName || !*appName)
  184. soapreq.append("<GetTransactionSeedRequest/>");
  185. else
  186. soapreq.append("<GetTransactionSeedRequest><Application>").append(appName).append("</Application></GetTransactionSeedRequest>");
  187. soapreq.append("</soap:Body></soap:Envelope>");
  188. unsigned retry = 1;
  189. int statusCode = 0;
  190. while (1)
  191. {
  192. try
  193. {
  194. if (!getTransactionSeed(soapreq, statusCode, statusMessage, transactionSeed))
  195. throw MakeStringException(EspLoggingErrors::GetTransactionSeedFailed,"Failed to get TransactionSeed");
  196. break;
  197. }
  198. catch (IException* e)
  199. {
  200. StringBuffer errorStr;
  201. statusMessage.set("Failed to get TransactionSeed: error code ").append(e->errorCode()).append(", error message ").append(e->errorMessage(errorStr));
  202. ERRLOG("%s -- try %d", statusMessage.str(), retry);
  203. e->Release();
  204. if (retry >= maxGTSRetries)
  205. {
  206. statusCode = -2;
  207. break;
  208. }
  209. Sleep(retry*3000);
  210. retry++;
  211. }
  212. }
  213. return statusCode;
  214. }
  215. bool CESPServerLoggingAgent::getTransactionSeed(StringBuffer& soapreq, int& statusCode, StringBuffer& statusMessage, StringBuffer& seedID)
  216. {
  217. StringBuffer status, response;
  218. if (!sendHTTPRequest(soapreq, response, status) || !response.length() || !status.length())
  219. throw MakeStringException(EspLoggingErrors::GetTransactionSeedFailed, "Failed to send Transaction Seed request to %s", serverUrl.str());
  220. if (!strieq(status, "200 OK"))
  221. throw MakeStringException(EspLoggingErrors::GetTransactionSeedFailed, "%s", status.str());
  222. Owned<IPropertyTree> pTree = createPTreeFromXMLString(response.str());
  223. if (!pTree)
  224. throw MakeStringException(EspLoggingErrors::GetTransactionSeedFailed, "Failed to read response from %s", serverUrl.str());
  225. //statusCode: 0 = success, -1 = failed after retries
  226. statusCode = pTree->getPropInt("soap:Body/GetTransactionSeedResponse/StatusCode");
  227. statusMessage.set(pTree->queryProp("soap:Body/GetTransactionSeedResponse/StatusMessage"));
  228. seedID.set(pTree->queryProp("soap:Body/GetTransactionSeedResponse/SeedId"));
  229. if (statusCode || !seedID.length())
  230. throw MakeStringException(EspLoggingErrors::GetTransactionSeedFailed, "Failed to get Transaction Seed from %s", serverUrl.str());
  231. return true;
  232. }
  233. void CESPServerLoggingAgent::resetTransSeed(CTransIDBuilder *builder, const char* groupName)
  234. {
  235. StringBuffer transactionSeed, statusMessage;
  236. if (builder->isLocalSeed())
  237. createLocalTransactionSeed(transactionSeed);
  238. else
  239. {
  240. int statusCode = getTransactionSeed(groupName, transactionSeed, statusMessage);
  241. if (!transactionSeed.length() || (statusCode != 0))
  242. {
  243. StringBuffer msg = "Failed to get Transaction Seed for ";
  244. msg.append(groupName).append(". statusCode: ").append(statusCode);
  245. if (!statusMessage.isEmpty())
  246. msg.append(", ").append(statusMessage.str());
  247. throw MakeStringException(EspLoggingErrors::GetTransactionSeedFailed, "%s", msg.str());
  248. }
  249. }
  250. builder->resetTransSeed(transactionSeed.str());
  251. };
  252. void CESPServerLoggingAgent::getTransactionID(StringAttrMapping* transFields, StringBuffer& transactionID)
  253. {
  254. const char* groupName = nullptr;
  255. CTransIDBuilder* transIDBuilder = nullptr;
  256. StringAttr* source = nullptr;
  257. if (transFields)
  258. source = transFields->getValue(sTransactionMethod);
  259. if (source)
  260. {
  261. CLogSource* logSource = logSources.getValue(source->get());
  262. if (logSource)
  263. {
  264. groupName = logSource->getGroupName();
  265. transIDBuilder = transIDMap.getValue(groupName);
  266. }
  267. }
  268. if (!transIDBuilder && (defaultGroup.length() != 0))
  269. {
  270. groupName = defaultGroup.str();
  271. transIDBuilder = transIDMap.getValue(groupName);
  272. }
  273. if (!transIDBuilder)
  274. {
  275. groupName = appESPServerLoggingAgent;
  276. transIDBuilder = transIDMap.getValue(appESPServerLoggingAgent);
  277. }
  278. if (!transIDBuilder) //This should not happen.
  279. throw MakeStringException(EspLoggingErrors::GetTransactionSeedFailed, "Failed to getTransactionID");
  280. if (!transIDBuilder->checkMaxSequenceNumber() || !transIDBuilder->checkTimeout())
  281. resetTransSeed(transIDBuilder, groupName);
  282. transIDBuilder->getTransID(transFields, transactionID);
  283. if (!transIDBuilder->checkMaxLength(transactionID.length()))
  284. {
  285. resetTransSeed(transIDBuilder, groupName);
  286. transIDBuilder->getTransID(transFields, transactionID);
  287. }
  288. return;
  289. }
  290. bool CESPServerLoggingAgent::updateLog(IEspUpdateLogRequestWrap& req, IEspUpdateLogResponse& resp)
  291. {
  292. try
  293. {
  294. StringBuffer soapreq(
  295. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  296. "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
  297. " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  298. " <soap:Body>"
  299. );
  300. soapreq.append(req.getUpdateLogRequest());
  301. soapreq.append("</soap:Body></soap:Envelope>");
  302. StringBuffer status, respStr;
  303. if (sendHTTPRequest(soapreq, respStr, status) && status.length() && strieq(status, "200 OK"))
  304. resp.setStatusCode(0);
  305. else if (status.length() && !strieq(status, "200 OK"))
  306. throw MakeStringException(EspLoggingErrors::UpdateLogFailed, "%s", status.str());
  307. else if (respStr.length())
  308. throw MakeStringException(EspLoggingErrors::UpdateLogFailed, "%s", respStr.str());
  309. else
  310. throw MakeStringException(EspLoggingErrors::UpdateLogFailed, "Failed to send update log request to %s", serverUrl.str());
  311. }
  312. catch (IException* e)
  313. {//retry will be in update log queue.
  314. StringBuffer errorStr, errorMessage;
  315. errorMessage.append("Failed to update log: error code ").append(e->errorCode()).append(", error message ").append(e->errorMessage(errorStr));
  316. ERRLOG("%s", errorMessage.str());
  317. resp.setStatusCode(-1);
  318. resp.setStatusMessage(errorMessage.str());
  319. e->Release();
  320. }
  321. return true;
  322. }
  323. void CESPServerLoggingAgent::addLogContentBranch(StringArray& branchNames, IPropertyTree* contentToLogBranch, IPropertyTree* updateLogRequestTree)
  324. {
  325. IPropertyTree* pTree = updateLogRequestTree;
  326. unsigned numOfBranchNames = branchNames.length();
  327. if (numOfBranchNames > 0)
  328. {
  329. unsigned i = 0;
  330. while (i < numOfBranchNames)
  331. {
  332. const char* branchName = branchNames.item(i);
  333. if (branchName && *branchName)
  334. pTree = ensurePTree(pTree, branchName);
  335. i++;
  336. }
  337. }
  338. pTree->addPropTree(contentToLogBranch->queryName(), LINK(contentToLogBranch));
  339. }
  340. void CESPServerLoggingAgent::filterAndAddLogContentBranch(StringArray& branchNamesInFilter, unsigned idx,
  341. StringArray& branchNamesInLogContent, IPropertyTree* originalLogContentBranch, IPropertyTree* updateLogRequestTree, bool& logContentEmpty)
  342. {
  343. Owned<IPropertyTreeIterator> contentItr = originalLogContentBranch->getElements(branchNamesInFilter.item(idx));
  344. ForEach(*contentItr)
  345. {
  346. IPropertyTree& contentToLogBranch = contentItr->query();
  347. if (idx == branchNamesInFilter.length() - 1)
  348. {
  349. addLogContentBranch(branchNamesInLogContent, &contentToLogBranch, updateLogRequestTree);
  350. logContentEmpty = false;
  351. }
  352. else
  353. {
  354. branchNamesInLogContent.append(contentToLogBranch.queryName());
  355. filterAndAddLogContentBranch(branchNamesInFilter, idx+1, branchNamesInLogContent, &contentToLogBranch,
  356. updateLogRequestTree, logContentEmpty);
  357. branchNamesInLogContent.remove(branchNamesInLogContent.length() - 1);
  358. }
  359. }
  360. }
  361. void CESPServerLoggingAgent::filterLogContentTree(StringArray& filters, IPropertyTree* originalContentTree, IPropertyTree* newLogContentTree, bool& logContentEmpty)
  362. {
  363. ForEachItemIn(i, filters)
  364. {
  365. const char* logContentFilter = filters.item(i);
  366. if(!logContentFilter || !*logContentFilter)
  367. continue;
  368. StringArray branchNamesInFilter, branchNamesInLogContent;
  369. branchNamesInFilter.appendListUniq(logContentFilter, "/");
  370. filterAndAddLogContentBranch(branchNamesInFilter, 0, branchNamesInLogContent, originalContentTree, newLogContentTree, logContentEmpty);
  371. }
  372. }
  373. void CESPServerLoggingAgent::filterLogContent(IEspUpdateLogRequestWrap* req)
  374. {
  375. const char* logContent = req->getUpdateLogRequest();
  376. Owned<IPropertyTree> updateLogRequestTree = createPTree("UpdateLogRequest");
  377. StringBuffer source;
  378. if (groupFilters.length() < 1)
  379. {//No filter
  380. if (logContent && *logContent)
  381. {
  382. Owned<IPropertyTree> pTree = createPTreeFromXMLString(logContent);
  383. source = pTree->queryProp("Source");
  384. updateLogRequestTree->addPropTree(pTree->queryName(), LINK(pTree));
  385. }
  386. else
  387. {
  388. Owned<IPropertyTree> espContext = req->getESPContext();
  389. Owned<IPropertyTree> userContext = req->getUserContext();
  390. Owned<IPropertyTree> userRequest = req->getUserRequest();
  391. const char* userResp = req->getUserResponse();
  392. const char* logDatasets = req->getLogDatasets();
  393. const char* backEndResp = req->getBackEndResponse();
  394. if (!espContext && !userContext && !userRequest && (!userResp || !*userResp) && (!backEndResp || !*backEndResp))
  395. throw MakeStringException(EspLoggingErrors::UpdateLogFailed, "Failed to read log content");
  396. source = userContext->queryProp("Source");
  397. StringBuffer espContextXML, userContextXML, userRequestXML;
  398. IPropertyTree* logContentTree = ensurePTree(updateLogRequestTree, "LogContent");
  399. if (espContext)
  400. {
  401. logContentTree->addPropTree(espContext->queryName(), LINK(espContext));
  402. }
  403. if (userContext)
  404. {
  405. IPropertyTree* pTree = ensurePTree(logContentTree, espLogContentGroupNames[ESPLCGUserContext]);
  406. pTree->addPropTree(userContext->queryName(), LINK(userContext));
  407. }
  408. if (userRequest)
  409. {
  410. IPropertyTree* pTree = ensurePTree(logContentTree, espLogContentGroupNames[ESPLCGUserReq]);
  411. pTree->addPropTree(userRequest->queryName(), LINK(userRequest));
  412. }
  413. if (userResp && *userResp)
  414. {
  415. IPropertyTree* pTree = ensurePTree(logContentTree, espLogContentGroupNames[ESPLCGUserResp]);
  416. Owned<IPropertyTree> userRespTree = createPTreeFromXMLString(userResp);
  417. pTree->addPropTree(userRespTree->queryName(), LINK(userRespTree));
  418. }
  419. if (logDatasets && *logDatasets)
  420. {
  421. IPropertyTree* pTree = ensurePTree(logContentTree, espLogContentGroupNames[ESPLCGLogDatasets]);
  422. Owned<IPropertyTree> logDatasetTree = createPTreeFromXMLString(logDatasets);
  423. pTree->addPropTree(logDatasetTree->queryName(), LINK(logDatasetTree));
  424. }
  425. if (backEndResp && *backEndResp)
  426. logContentTree->addProp(espLogContentGroupNames[ESPLCGBackEndResp], backEndResp);
  427. }
  428. }
  429. else
  430. {
  431. bool logContentEmpty = true;
  432. IPropertyTree* logContentTree = ensurePTree(updateLogRequestTree, "LogContent");
  433. if (logContent && *logContent)
  434. {
  435. Owned<IPropertyTree> originalContentTree = createPTreeFromXMLString(logContent);
  436. source = originalContentTree->queryProp("Source");
  437. filterLogContentTree(groupFilters.item(0).getFilters(), originalContentTree, logContentTree, logContentEmpty);
  438. }
  439. else
  440. {
  441. for (unsigned group = 0; group < ESPLCGBackEndResp; group++)
  442. {
  443. Owned<IPropertyTree> originalContentTree;
  444. if (group == ESPLCGESPContext)
  445. originalContentTree.setown(req->getESPContext());
  446. else if (group == ESPLCGUserContext)
  447. {
  448. originalContentTree.setown(req->getUserContext());
  449. source = originalContentTree->queryProp("Source");
  450. }
  451. else if (group == ESPLCGUserReq)
  452. originalContentTree.setown(req->getUserRequest());
  453. else if (group == ESPLCGLogDatasets)
  454. {
  455. const char* logDatasets = req->getLogDatasets();
  456. if (logDatasets && *logDatasets)
  457. originalContentTree.setown(createPTreeFromXMLString(logDatasets));
  458. }
  459. else //group = ESPLCGUserResp
  460. {
  461. const char* resp = req->getUserResponse();
  462. if (!resp || !*resp)
  463. continue;
  464. originalContentTree.setown(createPTreeFromXMLString(resp));
  465. }
  466. if (!originalContentTree)
  467. continue;
  468. IPropertyTree* newContentTree = ensurePTree(logContentTree, espLogContentGroupNames[group]);
  469. bool hasFilters = false;
  470. ForEachItemIn(i, groupFilters)
  471. {
  472. CESPLogContentGroupFilters& filtersGroup = groupFilters.item(i);
  473. if (filtersGroup.getGroup() == group)
  474. {
  475. if (group != ESPLCGESPContext)//For non ESPLCGESPContext, we want to keep the root of original tree.
  476. newContentTree = ensurePTree(newContentTree, originalContentTree->queryName());
  477. filterLogContentTree(filtersGroup.getFilters(), originalContentTree, newContentTree, logContentEmpty);
  478. hasFilters = true;
  479. break;
  480. }
  481. }
  482. if (!hasFilters)
  483. {
  484. newContentTree->addPropTree(originalContentTree->queryName(), LINK(originalContentTree));
  485. logContentEmpty = false;
  486. }
  487. }
  488. if (logBackEndResp)
  489. {
  490. const char* resp = req->getBackEndResponse();
  491. if (resp && *resp)
  492. {
  493. logContentTree->addProp(espLogContentGroupNames[ESPLCGBackEndResp], resp);
  494. logContentEmpty = false;
  495. }
  496. }
  497. }
  498. if (logContentEmpty)
  499. throw MakeStringException(EspLoggingErrors::UpdateLogFailed, "Failed to read log content");
  500. }
  501. if (!source.isEmpty())
  502. updateLogRequestTree->addProp("LogContent/Source", source.str());
  503. const char* option = req->getOption();
  504. if (option && *option)
  505. updateLogRequestTree->addProp("Option", option);
  506. StringBuffer updateLogRequestXML;
  507. toXML(updateLogRequestTree, updateLogRequestXML);
  508. ESPLOG(LogMax, "filtered content and option: <%s>", updateLogRequestXML.str());
  509. req->clearOriginalContent();
  510. req->setUpdateLogRequest(updateLogRequestXML.str());
  511. }
  512. bool CESPServerLoggingAgent::sendHTTPRequest(StringBuffer& req, StringBuffer &resp, StringBuffer &status)
  513. {
  514. Owned<IHttpClientContext> httpctx = getHttpClientContext();
  515. Owned <IHttpClient> httpclient = httpctx->createHttpClient(NULL, serverUrl.str());
  516. if (serverUserID.length() && serverPassword.length())
  517. {
  518. httpclient->setUserID(serverUserID.str());
  519. httpclient->setPassword(serverPassword.str());
  520. }
  521. httpclient->setTimeOut(maxServerWaitingSeconds);
  522. return !httpclient->sendRequest("POST", "text/xml", req, resp, status);
  523. }
  524. extern "C"
  525. {
  526. ESPSERVERLOGGINGAGENT_API IEspLogAgent* newLoggingAgent()
  527. {
  528. return new CESPServerLoggingAgent();
  529. }
  530. }