Skip to content

Commit

Permalink
FIX cleanup of the previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fgalan committed Aug 9, 2024
1 parent 1d89e17 commit 0712449
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 138 deletions.
26 changes: 1 addition & 25 deletions src/lib/rest/RestService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ std::string payloadParse
ConnectionInfo* ciP,
ParseData* parseDataP,
RestService* service,
//JsonRequest** jsonPP, FIXME PR
JsonDelayedRelease* jsonReleaseP,
std::vector<std::string>& compV
)
Expand Down Expand Up @@ -517,7 +516,6 @@ static std::string restService(ConnectionInfo* ciP, RestService* serviceV)
{
std::vector<std::string> compV;
int components;
//JsonRequest* jsonReqP = NULL; // FIXME PR: remove
ParseData parseData;
JsonDelayedRelease jsonRelease;

Expand Down Expand Up @@ -597,22 +595,14 @@ static std::string restService(ConnectionInfo* ciP, RestService* serviceV)
ciP->parseDataP = &parseData;
metricsMgr.add(ciP->httpHeaders.tenant, spath, METRIC_TRANS_IN_REQ_SIZE, ciP->payloadSize);
LM_T(LmtPayload, ("Parsing payload '%s'", ciP->payload));
// FIXME PR
response = payloadParse(ciP, &parseData, &serviceV[ix], /*&jsonReqP,*/ &jsonRelease, compV);
response = payloadParse(ciP, &parseData, &serviceV[ix], &jsonRelease, compV);
LM_T(LmtParsedPayload, ("payloadParse returns '%s'", response.c_str()));

if (response != "OK")
{
alarmMgr.badInput(clientIp, response);
restReply(ciP, response);

// FIXME PR
/*if (jsonReqP != NULL)
{
jsonReqP->release(&parseData);
}*/


delayedRelease(&jsonRelease);

compV.clear();
Expand Down Expand Up @@ -642,19 +632,11 @@ static std::string restService(ConnectionInfo* ciP, RestService* serviceV)
std::string response = oe.setStatusCodeAndSmartRender(&(ciP->httpStatusCode));

alarmMgr.badInput(clientIp, result);

restReply(ciP, response);

// FIXME PR: remove
//if (jsonReqP != NULL)
//{
// jsonReqP->release(&parseData);
//}

delayedRelease(&jsonRelease);

compV.clear();

return response;
}

Expand All @@ -677,12 +659,6 @@ static std::string restService(ConnectionInfo* ciP, RestService* serviceV)

filterRelease(&parseData, serviceV[ix].request);

// FIXME PR: remove
//if (jsonReqP != NULL)
//{
// jsonReqP->release(&parseData);
//}

delayedRelease(&jsonRelease);

compV.clear();
Expand Down
1 change: 0 additions & 1 deletion src/lib/rest/RestService.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ extern std::string payloadParse
ConnectionInfo* ciP,
ParseData* parseDataP,
RestService* service,
//JsonRequest** jsonPP, FIXME PR
JsonDelayedRelease* jsonReleaseP,
std::vector<std::string>& compV
);
Expand Down
140 changes: 31 additions & 109 deletions src/lib/serviceRoutines/postQueryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,120 +373,42 @@ static bool queryForward
getProviderCount(out.c_str(), totalCount);
}

if (qcrP->providerFormat == PfJson)
bool result;
Entities entities;
OrionError oe;

// Depending NGSIv1 or NGSIv2 in the CPr we use parseEntitiesResponseV1() or parseEntitiesResponseV2()

// Note that parseEntitiesResponse() is thought for client-to-CB interactions, so it takes into account
// ciP->uriParamOptions[OPT_KEY_VALUES]. In this case, we never use keyValues in the CB-to-CPr so we
// set to false and restore its original value later. In this case it seems it is not needed to preserve
// ciP->httpStatusCode as in the similar case above
// FIXME P5: not sure if I like this approach... very "hacking-style". Probably it would be better
// to make JSON parsing logic (internal to parseEntitiesResponse()) independent of ciP and to pass the
// keyValue directly as function parameter.
bool previousKeyValues = ciP->uriParamOptions[OPT_KEY_VALUES];
ciP->uriParamOptions[OPT_KEY_VALUES] = false;
result = qcrP->providerFormat == PfJson ? parseEntitiesResponseV1(ciP, cleanPayload, &entities, &oe) : parseEntitiesResponse(ciP, cleanPayload, &entities, &oe);
ciP->uriParamOptions[OPT_KEY_VALUES] = previousKeyValues;

if (result == false)
{
#if 0
// FIXME PR clean this block. Unifty with else, pobably only the parseEntitiesResponse/parseEntitiesResponseV1 is different
std::string s;

//
// NOTE
// When coming from a convenience operation, such as GET /v1/contextEntities/EID/attributes/attrName,
// the verb/method in ciP is GET. However, the parsing function expects a POST, as if it came from a
// POST /v1/queryContext.
// So, here we change the verb/method for POST.
//
ciP->verb = POST;
ciP->method = "POST";

// Note that jsonTreat() is thought for client-to-CB interactions, thus it modifies ciP->httpStatusCode
// Thus, we need to preserve it before (and recover after) in order a fail in the CB-to-CPr interaction doesn't
// "corrupt" the status code in the client-to-CB interaction.
// FIXME P5: not sure if I like this approach... very "hacking-style". Probably it would be better
// to make JSON parsing logic (internal to jsonTreat()) independent of ciP (in fact, parsing process
// hasn't anything to do with connection).
HttpStatusCode sc = ciP->httpStatusCode;
s = jsonTreat(cleanPayload, ciP, &parseData, RtQueryContextResponse, NULL);
ciP->httpStatusCode = sc;

if (s != "OK")
{
alarmMgr.forwardingError(url, "error parsing reply from prov app: " + s);
parseData.qcr.res.release();
parseData.qcrs.res.release();
return false;
}


//
// 5. Fill in the response from the redirection into the response of this function
//
qcrsP->fill(&parseData.qcrs.res);
#endif

bool r;
Entities entities;
OrionError oe;

// Note that parseEntitiesResponse() is thought for client-to-CB interactions, so it takes into account
// ciP->uriParamOptions[OPT_KEY_VALUES]. In this case, we never use keyValues in the CB-to-CPr so we
// set to false and restore its original value later. In this case it seems it is not needed to preserve
// ciP->httpStatusCode as in the similar case above
// FIXME P5: not sure if I like this approach... very "hacking-style". Probably it would be better
// to make JSON parsing logic (internal to parseEntitiesResponse()) independent of ciP and to pass the
// keyValue directly as function parameter.
bool previousKeyValues = ciP->uriParamOptions[OPT_KEY_VALUES];
ciP->uriParamOptions[OPT_KEY_VALUES] = false;
r = parseEntitiesResponseV1(ciP, cleanPayload, &entities, &oe);
ciP->uriParamOptions[OPT_KEY_VALUES] = previousKeyValues;

if (r == false)
{
alarmMgr.forwardingError(url, "error parsing reply from context provider: " + oe.description);
parseData.qcr.res.release();
parseData.qcrs.res.release();
return false;
}
alarmMgr.forwardingError(url, "error parsing reply from context provider: " + oe.description);
parseData.qcr.res.release();
parseData.qcrs.res.release();
return false;
}

//
// 5. Fill in the response from the redirection into the response of this function
//
if (entities.size() > 0)
{
qcrsP->fill(entities);
}
else
{
qcrsP->errorCode.fill(SccContextElementNotFound);
}
//
// 5. Fill in the response from the redirection into the response of this function
//
if (entities.size() > 0)
{
qcrsP->fill(entities);
}
else
{
bool b;
Entities entities;
OrionError oe;

// Note that parseEntitiesResponse() is thought for client-to-CB interactions, so it takes into account
// ciP->uriParamOptions[OPT_KEY_VALUES]. In this case, we never use keyValues in the CB-to-CPr so we
// set to false and restore its original value later. In this case it seems it is not needed to preserve
// ciP->httpStatusCode as in the similar case above
// FIXME P5: not sure if I like this approach... very "hacking-style". Probably it would be better
// to make JSON parsing logic (internal to parseEntitiesResponse()) independent of ciP and to pass the
// keyValue directly as function parameter.
bool previousKeyValues = ciP->uriParamOptions[OPT_KEY_VALUES];
ciP->uriParamOptions[OPT_KEY_VALUES] = false;
b = parseEntitiesResponse(ciP, cleanPayload, &entities, &oe);
ciP->uriParamOptions[OPT_KEY_VALUES] = previousKeyValues;

if (b == false)
{
alarmMgr.forwardingError(url, "error parsing reply from context provider: " + oe.error + " (" + oe.description + ")");
parseData.qcr.res.release();
parseData.qcrs.res.release();
return false;
}

//
// 5. Fill in the response from the redirection into the response of this function
//
if (entities.size() > 0)
{
qcrsP->fill(entities);
}
else
{
qcrsP->errorCode.fill(SccContextElementNotFound);
}
qcrsP->errorCode.fill(SccContextElementNotFound);
}

//
Expand Down
6 changes: 3 additions & 3 deletions src/lib/serviceRoutines/postUpdateContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ static bool updateForward
//
// 4. Parse the response and fill in a binary UpdateContextResponse
//
bool r;
bool result;
Entities entities;
OrionError oe;

Expand Down Expand Up @@ -296,9 +296,9 @@ static bool updateForward

parseData.upcrs.res.errorCode.fill(SccOk);

r = parseEntitiesResponseV1(ciP, cleanPayload, &entities, &oe);
result = parseEntitiesResponseV1(ciP, cleanPayload, &entities, &oe);

if (!r)
if (!result)
{
alarmMgr.forwardingError(url, "error parsing reply from context provider: " + oe.error + " (" + oe.description + ")");
upcrsP->errorCode.fill(SccContextElementNotFound, "");
Expand Down

0 comments on commit 0712449

Please sign in to comment.