Skip to content

Commit

Permalink
REMOVE not used items in HttpStatusCode and OrionError::shrinkError()…
Browse files Browse the repository at this point in the history
… and OrionError::smartRender() functions (unit tests broken at this point)
  • Loading branch information
fgalan committed Aug 8, 2024
1 parent 839f795 commit 7fc7db4
Show file tree
Hide file tree
Showing 22 changed files with 46 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/lib/ngsi/StatusCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ std::string StatusCode::toJsonV1(bool comma, bool showKey)
std::string StatusCode::toJson(void)
{
OrionError oe(code, details, reasonPhrase);
return oe.smartRender();
return oe.toJson();
}


Expand Down
30 changes: 12 additions & 18 deletions src/lib/rest/HttpStatusCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,19 @@ std::string httpStatusCodeString(HttpStatusCode code)
{
case SccOk: return "OK";
case SccCreated: return "Created";
case SccBadRequest: return "Bad Request";
case SccBadRequest: return "BadRequest";
case SccForbidden: return "Forbidden";
case SccContextElementNotFound: return "No context element found"; // Standard HTTP for 404: "Not Found"
case SccBadVerb: return "Method Not Allowed";
case SccNotAcceptable: return "Not Acceptable";
case SccConflict: return "Too Many Results";
case SccContentLengthRequired: return "Content Length Required";
case SccRequestEntityTooLarge: return "Request Entity Too Large";
case SccUnsupportedMediaType: return "Unsupported Media Type";
case SccInvalidModification: return "Invalid Modification";
case SccSubscriptionIdNotFound: return "subscriptionId does not correspond to an active subscription"; // FI-WARE
case SccMissingParameter: return "parameter missing in the request"; // FI-WARE
case SccInvalidParameter: return "request parameter is invalid/not allowed"; // FI-WARE
case SccErrorInMetadata: return "Generic error in metadata";
case SccEntityIdReNotAllowed: return "Regular Expression for EntityId is not allowed by receiver";
case SccEntityTypeRequired: return "EntityType required by the receiver";
case SccAttributeListRequired: return "Attribute List required by the receiver";
case SccReceiverInternalError: return "Internal Server Error";
case SccNotImplemented: return "Not Implemented";
case SccContextElementNotFound: return "NotFound";
case SccBadVerb: return "MethodNotAllowed";
case SccNotAcceptable: return "NotAcceptable";
case SccConflict: return "TooManyResults";
case SccContentLengthRequired: return "ContentLengthRequired";
case SccRequestEntityTooLarge: return "RequestEntityTooLarge";
case SccUnsupportedMediaType: return "UnsupportedMediaType";
case SccInvalidModification: return "InvalidModification";
case SccInvalidParameter: return "request parameter is invalid/not allowed"; // FI-WARE // FIXME PR: remove
case SccReceiverInternalError: return "InternalServerError";
case SccNotImplemented: return "NotImplemented";
default: return "Undefined";
}
}
6 changes: 0 additions & 6 deletions src/lib/rest/HttpStatusCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ typedef enum HttpStatusCode
SccRequestEntityTooLarge = 413, // Request Entity Too Large - over 1Mb of payload
SccUnsupportedMediaType = 415, // Unsupported Media Type (only support and application/json and -in some cases- text/plain)
SccInvalidModification = 422, // InvalidModification (unprocessable entity)
SccSubscriptionIdNotFound = 470, // The subscriptionId does not correspond to an active subscription
SccMissingParameter = 471, // A parameter is missing in the request
SccInvalidParameter = 472, // A parameter of the request is invalid/not allowed
SccErrorInMetadata = 473, // Generic error in metadata (e.g. 'expires' older than 'timestamp')
SccEntityIdReNotAllowed = 480, // Regular Expression for EntityId is not allowed by receiver
SccEntityTypeRequired = 481, // The EntityType is required by the receiver
SccAttributeListRequired = 482, // The Attribute List is required by the receiver
SccReceiverInternalError = 500, // An unknown error at the receiver has occurred
SccNotImplemented = 501 // The given operation is not implemented
} HttpStatusCode;
Expand Down
87 changes: 1 addition & 86 deletions src/lib/rest/OrionError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,14 @@ void OrionError::fillOrAppend(HttpStatusCode _code, const std::string& fullDetai



/* ****************************************************************************
*
* OrionError::smartRender -
*/
std::string OrionError::smartRender(void)
{
shrinkError();
return toJson();
}



/* ****************************************************************************
*
* OrionError::setStatusCodeAndSmartRender -
*/
std::string OrionError::setStatusCodeAndSmartRender(HttpStatusCode* scP)
{
*scP = code;
return smartRender();
return toJson();
}


Expand All @@ -169,76 +157,3 @@ std::string OrionError::toJson(void)
return jh.str();
}



/* ****************************************************************************
*
* OrionError::shrinkError -
*
* This method removes any whitespace in the error field, i.e.
* transforms "Not Found" to "NotFound".
*
* It is used by smartRender method, in order to prepare to render in API v2 case
*
* FIXME P4: The following alternative (more compact) way has been proposed:
*
* #include <algorithm> // std::remove_if
* #include <cctype> // std::isspace
*
* ...
*
* reasonPhrase.erase(std::remove_if(error.begin(), error.end(), std::isspace), error.end());
*
* However, 'std::isspace' doesn't directly work. We have been able to make it work with
* 'static_cast<int(*)(int)>(isspace)'. However, that is obscure so until we can find
* a way of using just 'std::isspace', the current implementation stills.
*
*/
void OrionError::shrinkError(void)
{
char buf[80]; // 80 should be enough to hold any reason phrase

#if 0
strncpy(buf, error.c_str(), sizeof(buf));

// See: http://stackoverflow.com/questions/1726302/removing-spaces-from-a-string-in-c
if (*j != ' ')
{
*i = *j;
++i;
}
++j;

char* i = buf;
char* j = buf;
while (*j != 0)
{
*i = *j++;
if (*i != ' ')
{
i++;
}
}
*i = 0;
#endif

char* fromP = (char*) error.c_str();
char* toP = buf;
unsigned int toLen = 0;

while ((*fromP != 0) && (toLen < sizeof(buf)))
{
// If next char is not whitespace: copy to outgoing
if (*fromP != ' ')
{
*toP = *fromP;
++toP;
++toLen;
}

++fromP;
}
*toP = 0; // End-of string

error = std::string(buf);
}
4 changes: 0 additions & 4 deletions src/lib/rest/OrionError.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ typedef struct OrionError
void fill(const StatusCode& sc);
void fillOrAppend(HttpStatusCode _code, const std::string& fullDetails, const std::string& appendDetail, const std::string& _error);


private:
void shrinkError(void);

} OrionError;

#endif
2 changes: 1 addition & 1 deletion src/lib/rest/RestService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ static std::string restService(ConnectionInfo* ciP, RestService* serviceV)
{
alarmMgr.badInput(clientIp, oe.description);
ciP->httpStatusCode = SccBadRequest;
restReply(ciP, oe.smartRender());
restReply(ciP, oe.toJson());
return "URL PATH component error";
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/lib/rest/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static MHD_Result uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char*
{
OrionError error(SccBadRequest, errorString);
ciP->httpStatusCode = error.code;
ciP->answer = error.smartRender();
ciP->answer = error.toJson();
}
// FIXME PR: solve this
/*else if (ciP->apiVersion == ADMIN_API)
Expand All @@ -145,7 +145,7 @@ static MHD_Result uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char*
{
OrionError error(SccBadRequest, std::string("Bad pagination offset: /") + value + "/ [must be a decimal number]");
ciP->httpStatusCode = error.code;
ciP->answer = error.smartRender();
ciP->answer = error.toJson();
return MHD_YES;
}

Expand All @@ -162,7 +162,7 @@ static MHD_Result uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char*
{
OrionError error(SccBadRequest, std::string("Bad pagination limit: /") + value + "/ [must be a positive integer number]");
ciP->httpStatusCode = error.code;
ciP->answer = error.smartRender();
ciP->answer = error.toJson();
return MHD_YES;
}

Expand All @@ -174,7 +174,7 @@ static MHD_Result uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char*
{
OrionError error(SccBadRequest, std::string("Bad pagination limit: /") + value + "/ [max: " + MAX_PAGINATION_LIMIT + "]");
ciP->httpStatusCode = error.code;
ciP->answer = error.smartRender();
ciP->answer = error.toJson();
return MHD_YES;
}
}
Expand All @@ -184,7 +184,7 @@ static MHD_Result uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char*
{
OrionError error(SccBadRequest, std::string("Bad value for /details/: /") + value + "/ [accepted: /on/, /ON/, /off/, /OFF/. Default is /off/]");
ciP->httpStatusCode = error.code;
ciP->answer = error.smartRender();
ciP->answer = error.toJson();
return MHD_YES;
}
}
Expand All @@ -206,7 +206,7 @@ static MHD_Result uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char*
{
OrionError error(SccBadRequest, "Invalid value for URI param /options/");
ciP->httpStatusCode = error.code;
ciP->answer = error.smartRender();
ciP->answer = error.toJson();
}
}
else if (key == URI_PARAM_TYPE)
Expand Down Expand Up @@ -269,7 +269,7 @@ static MHD_Result uriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char*

alarmMgr.badInput(clientIp, details);
ciP->httpStatusCode = error.code;
ciP->answer = error.smartRender();
ciP->answer = error.toJson();
}

return MHD_YES;
Expand Down Expand Up @@ -481,7 +481,7 @@ static void acceptParse(ConnectionInfo* ciP, const char* value)
{
OrionError oe(ciP->httpStatusCode, ciP->acceptHeaderError);

ciP->answer = oe.smartRender();
ciP->answer = oe.toJson();
}
}

Expand Down Expand Up @@ -1019,7 +1019,7 @@ bool urlCheck(ConnectionInfo* ciP, const std::string& url)
{
OrionError error(SccBadRequest, ERROR_DESC_BAD_REQUEST_INVALID_CHAR_URI);
ciP->httpStatusCode = error.code;
ciP->answer = error.smartRender();
ciP->answer = error.toJson();
return false;
}

Expand Down Expand Up @@ -1319,7 +1319,7 @@ static MHD_Result connectionTreat
OrionError oe(SccRequestEntityTooLarge, details);

ciP->httpStatusCode = oe.code;
restReply(ciP, oe.smartRender());
restReply(ciP, oe.toJson());
return MHD_YES;
}

Expand Down Expand Up @@ -1478,7 +1478,7 @@ static MHD_Result connectionTreat

ciP->httpStatusCode = oe.code;
alarmMgr.badInput(clientIp, ciP->acceptHeaderError);
restReply(ciP, oe.smartRender());
restReply(ciP, oe.toJson());
return MHD_YES;
}

Expand All @@ -1502,7 +1502,7 @@ static MHD_Result connectionTreat

ciP->httpStatusCode = oe.code;
alarmMgr.badInput(clientIp, oe.description);
restReply(ciP, oe.smartRender());
restReply(ciP, oe.toJson());
return MHD_YES;
}

Expand All @@ -1528,7 +1528,7 @@ static MHD_Result connectionTreat

ciP->httpStatusCode = oe.code;
alarmMgr.badInput(clientIp, oe.description);
restReply(ciP, oe.smartRender());
restReply(ciP, oe.toJson());
return MHD_YES;
}

Expand All @@ -1543,7 +1543,7 @@ static MHD_Result connectionTreat

ciP->httpStatusCode = oe.code;
alarmMgr.badInput(clientIp, details);
restReply(ciP, oe.smartRender());
restReply(ciP, oe.toJson());
return MHD_YES;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/serviceRoutines/badVerbGetDeleteOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ std::string badVerbGetDeleteOnly

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutines/badVerbGetOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ std::string badVerbGetOnly

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutines/badVerbGetPostOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ std::string badVerbGetPostOnly

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutines/badVerbGetPutDeleteOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ std::string badVerbGetPutDeleteOnly

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutines/badVerbPostOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ std::string badVerbPostOnly

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutines/badVerbPutDeleteOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ std::string badVerbPutDeleteOnly
ciP->httpHeaderValue.push_back("PUT, DELETE");
ciP->httpStatusCode = SccBadVerb;

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutines/badVerbPutOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ std::string badVerbPutOnly

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutinesV2/badVerbAllNotDelete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ std::string badVerbAllNotDelete

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutinesV2/badVerbGetDeletePatchOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ std::string badVerbGetDeletePatchOnly

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutinesV2/badVerbGetPutOnly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ std::string badVerbGetPutOnly

alarmMgr.badInput(clientIp, details);

return oe.smartRender();
return oe.toJson();
}
2 changes: 1 addition & 1 deletion src/lib/serviceRoutinesV2/getEntities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ std::string getEntities
{
OrionError oe;
entities.fill(parseDataP->qcrs.res, &oe);
TIMED_RENDER(answer = oe.smartRender());
TIMED_RENDER(answer = oe.toJson());
ciP->httpStatusCode = oe.code;
}
// 04. Render Entities response
Expand Down
2 changes: 1 addition & 1 deletion src/lib/serviceRoutinesV2/postBatchUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ std::string postBatchUpdate
OrionError oe(SccBadRequest, ERROR_DESC_BAD_REQUEST_EMPTY_ENTITIES_VECTOR);
alarmMgr.badInput(clientIp, ERROR_DESC_BAD_REQUEST_EMPTY_ENTITIES_VECTOR);

TIMED_RENDER(answer = oe.smartRender());
TIMED_RENDER(answer = oe.toJson());
ciP->httpStatusCode = SccBadRequest;

return answer;
Expand Down
Loading

0 comments on commit 7fc7db4

Please sign in to comment.