@@ -221,35 +221,14 @@ void ConfigHandlers::handle_list_configurations(const httplib::Request & req, ht
221221
222222 entity_id = req.matches [1 ];
223223
224- auto entity_validation = ctx_.validate_entity_id (entity_id);
225- if (!entity_validation) {
226- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER, " Invalid entity ID" ,
227- {{" details" , entity_validation.error ()}, {" entity_id" , entity_id}});
228- return ;
229- }
230-
231- // First, verify that the entity actually exists in the cache
232- const auto & cache = ctx_.node ()->get_thread_safe_cache ();
233- auto entity_ref = cache.find_entity (entity_id);
234- if (!entity_ref) {
235- HandlerContext::send_error (res, StatusCode::NotFound_404, ERR_ENTITY_NOT_FOUND, " Entity not found" ,
236- {{" entity_id" , entity_id}});
237- return ;
238- }
239-
240- // Validate entity type matches the route path
241- auto expected_type = extract_entity_type_from_path (req.path );
242- if (expected_type != SovdEntityType::UNKNOWN && entity_ref->type != expected_type) {
243- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER,
244- " Invalid entity type for route: expected " + to_string (expected_type) + " , got " +
245- to_string (entity_ref->type ),
246- {{" entity_id" , entity_id},
247- {" expected_type" , to_string (expected_type)},
248- {" actual_type" , to_string (entity_ref->type )}});
249- return ;
224+ // Validate entity ID and type for this route
225+ auto entity_opt = ctx_.validate_entity_for_route (req, res, entity_id);
226+ if (!entity_opt) {
227+ return ; // Error response already sent
250228 }
251229
252230 // Get aggregated configurations info for this entity
231+ const auto & cache = ctx_.node ()->get_thread_safe_cache ();
253232 auto agg_configs = cache.get_entity_configurations (entity_id);
254233
255234 // If no nodes to query, return empty result
@@ -384,11 +363,10 @@ void ConfigHandlers::handle_get_configuration(const httplib::Request & req, http
384363 entity_id = req.matches [1 ];
385364 param_id = req.matches [2 ];
386365
387- auto entity_validation = ctx_.validate_entity_id (entity_id);
388- if (!entity_validation) {
389- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER, " Invalid entity ID" ,
390- {{" details" , entity_validation.error ()}, {" entity_id" , entity_id}});
391- return ;
366+ // Validate entity ID and type for this route
367+ auto entity_opt = ctx_.validate_entity_for_route (req, res, entity_id);
368+ if (!entity_opt) {
369+ return ; // Error response already sent
392370 }
393371
394372 // Parameter ID may be prefixed with app_id: for aggregated configs
@@ -398,28 +376,8 @@ void ConfigHandlers::handle_get_configuration(const httplib::Request & req, http
398376 return ;
399377 }
400378
401- // Verify entity exists
402- const auto & cache = ctx_.node ()->get_thread_safe_cache ();
403- auto entity_ref = cache.find_entity (entity_id);
404- if (!entity_ref) {
405- HandlerContext::send_error (res, StatusCode::NotFound_404, ERR_ENTITY_NOT_FOUND, " Entity not found" ,
406- {{" entity_id" , entity_id}});
407- return ;
408- }
409-
410- // Validate entity type matches the route path
411- auto expected_type = extract_entity_type_from_path (req.path );
412- if (expected_type != SovdEntityType::UNKNOWN && entity_ref->type != expected_type) {
413- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER,
414- " Invalid entity type for route: expected " + to_string (expected_type) + " , got " +
415- to_string (entity_ref->type ),
416- {{" entity_id" , entity_id},
417- {" expected_type" , to_string (expected_type)},
418- {" actual_type" , to_string (entity_ref->type )}});
419- return ;
420- }
421-
422379 // Get aggregated configurations info
380+ const auto & cache = ctx_.node ()->get_thread_safe_cache ();
423381 auto agg_configs = cache.get_entity_configurations (entity_id);
424382
425383 if (agg_configs.nodes .empty ()) {
@@ -533,11 +491,10 @@ void ConfigHandlers::handle_set_configuration(const httplib::Request & req, http
533491 entity_id = req.matches [1 ];
534492 param_id = req.matches [2 ];
535493
536- auto entity_validation = ctx_.validate_entity_id (entity_id);
537- if (!entity_validation) {
538- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER, " Invalid entity ID" ,
539- {{" details" , entity_validation.error ()}, {" entity_id" , entity_id}});
540- return ;
494+ // Validate entity ID and type for this route
495+ auto entity_opt = ctx_.validate_entity_for_route (req, res, entity_id);
496+ if (!entity_opt) {
497+ return ; // Error response already sent
541498 }
542499
543500 if (param_id.empty () || param_id.length () > MAX_AGGREGATED_PARAM_ID_LENGTH) {
@@ -568,28 +525,8 @@ void ConfigHandlers::handle_set_configuration(const httplib::Request & req, http
568525 return ;
569526 }
570527
571- // Verify entity exists
572- const auto & cache = ctx_.node ()->get_thread_safe_cache ();
573- auto entity_ref = cache.find_entity (entity_id);
574- if (!entity_ref) {
575- HandlerContext::send_error (res, StatusCode::NotFound_404, ERR_ENTITY_NOT_FOUND, " Entity not found" ,
576- {{" entity_id" , entity_id}});
577- return ;
578- }
579-
580- // Validate entity type matches the route path
581- auto expected_type = extract_entity_type_from_path (req.path );
582- if (expected_type != SovdEntityType::UNKNOWN && entity_ref->type != expected_type) {
583- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER,
584- " Invalid entity type for route: expected " + to_string (expected_type) + " , got " +
585- to_string (entity_ref->type ),
586- {{" entity_id" , entity_id},
587- {" expected_type" , to_string (expected_type)},
588- {" actual_type" , to_string (entity_ref->type )}});
589- return ;
590- }
591-
592528 // Get aggregated configurations info
529+ const auto & cache = ctx_.node ()->get_thread_safe_cache ();
593530 auto agg_configs = cache.get_entity_configurations (entity_id);
594531
595532 if (agg_configs.nodes .empty ()) {
@@ -678,35 +615,14 @@ void ConfigHandlers::handle_delete_configuration(const httplib::Request & req, h
678615 entity_id = req.matches [1 ];
679616 param_id = req.matches [2 ];
680617
681- auto entity_validation = ctx_.validate_entity_id (entity_id);
682- if (!entity_validation) {
683- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER, " Invalid entity ID" ,
684- {{" details" , entity_validation.error ()}, {" entity_id" , entity_id}});
685- return ;
686- }
687-
688- // Verify entity exists
689- const auto & cache = ctx_.node ()->get_thread_safe_cache ();
690- auto entity_ref = cache.find_entity (entity_id);
691- if (!entity_ref) {
692- HandlerContext::send_error (res, StatusCode::NotFound_404, ERR_ENTITY_NOT_FOUND, " Entity not found" ,
693- {{" entity_id" , entity_id}});
694- return ;
695- }
696-
697- // Validate entity type matches the route path
698- auto expected_type = extract_entity_type_from_path (req.path );
699- if (expected_type != SovdEntityType::UNKNOWN && entity_ref->type != expected_type) {
700- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER,
701- " Invalid entity type for route: expected " + to_string (expected_type) + " , got " +
702- to_string (entity_ref->type ),
703- {{" entity_id" , entity_id},
704- {" expected_type" , to_string (expected_type)},
705- {" actual_type" , to_string (entity_ref->type )}});
706- return ;
618+ // Validate entity ID and type for this route
619+ auto entity_opt = ctx_.validate_entity_for_route (req, res, entity_id);
620+ if (!entity_opt) {
621+ return ; // Error response already sent
707622 }
708623
709624 // Get aggregated configurations info
625+ const auto & cache = ctx_.node ()->get_thread_safe_cache ();
710626 auto agg_configs = cache.get_entity_configurations (entity_id);
711627
712628 if (agg_configs.nodes .empty ()) {
@@ -779,35 +695,14 @@ void ConfigHandlers::handle_delete_all_configurations(const httplib::Request & r
779695
780696 entity_id = req.matches [1 ];
781697
782- auto entity_validation = ctx_.validate_entity_id (entity_id);
783- if (!entity_validation) {
784- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER, " Invalid entity ID" ,
785- {{" details" , entity_validation.error ()}, {" entity_id" , entity_id}});
786- return ;
787- }
788-
789- // Verify entity exists
790- const auto & cache = ctx_.node ()->get_thread_safe_cache ();
791- auto entity_ref = cache.find_entity (entity_id);
792- if (!entity_ref) {
793- HandlerContext::send_error (res, StatusCode::NotFound_404, ERR_ENTITY_NOT_FOUND, " Entity not found" ,
794- {{" entity_id" , entity_id}});
795- return ;
796- }
797-
798- // Validate entity type matches the route path
799- auto expected_type = extract_entity_type_from_path (req.path );
800- if (expected_type != SovdEntityType::UNKNOWN && entity_ref->type != expected_type) {
801- HandlerContext::send_error (res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER,
802- " Invalid entity type for route: expected " + to_string (expected_type) + " , got " +
803- to_string (entity_ref->type ),
804- {{" entity_id" , entity_id},
805- {" expected_type" , to_string (expected_type)},
806- {" actual_type" , to_string (entity_ref->type )}});
807- return ;
698+ // Validate entity ID and type for this route
699+ auto entity_opt = ctx_.validate_entity_for_route (req, res, entity_id);
700+ if (!entity_opt) {
701+ return ; // Error response already sent
808702 }
809703
810704 // Get aggregated configurations info
705+ const auto & cache = ctx_.node ()->get_thread_safe_cache ();
811706 auto agg_configs = cache.get_entity_configurations (entity_id);
812707
813708 if (agg_configs.nodes .empty ()) {
0 commit comments