From 12f09774dd61ea1a9c32c1c67c8178d4533a0285 Mon Sep 17 00:00:00 2001 From: Thierry Boileau Date: Sun, 29 Nov 2026 17:45:57 +0100 Subject: [PATCH] ServerResource: extract the retrieval of the validators to lower the cognitive complexity (#1753) Sonar S3776 on doConditionalHandle() (16 for 15 allowed) after the #1753 change. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 --- .../org/restlet/resource/ServerResource.java | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/org.restlet/src/main/java/org/restlet/resource/ServerResource.java b/org.restlet/src/main/java/org/restlet/resource/ServerResource.java index 8b2debb320..2c82d71ce4 100644 --- a/org.restlet/src/main/java/org/restlet/resource/ServerResource.java +++ b/org.restlet/src/main/java/org/restlet/resource/ServerResource.java @@ -281,19 +281,7 @@ protected Representation doConditionalHandle() throws ResourceException { RepresentationInfo resultInfo = null; if (existing) { - Status before = getStatus(); - resultInfo = doGetInfoAndHandleErrors(); - - if (resultInfo != null) { - doConditionalHandle(resultInfo); - } else if (isGetNotAllowed()) { - // The resource supports the method but cannot be read, so no validator is known - // (#1753): the 405 of the missing GET does not concern this request. The - // preconditions are evaluated as for a resource with no known representation. - setStatus(before); - getAllowedMethods().clear(); - doConditionalHandle((RepresentationInfo) null); - } + resultInfo = retrieveValidatorsAndEvaluate(); } else { doConditionalHandle(resultInfo); } @@ -315,6 +303,30 @@ protected Representation doConditionalHandle() throws ResourceException { return result; } + /** + * Retrieves the validators of the current representation of an existing resource and evaluates + * the preconditions against them. + * + * @return The current representation, or null if it could not be retrieved. + */ + private RepresentationInfo retrieveValidatorsAndEvaluate() { + Status before = getStatus(); + RepresentationInfo resultInfo = doGetInfoAndHandleErrors(); + + if (resultInfo != null) { + doConditionalHandle(resultInfo); + } else if (isGetNotAllowed()) { + // The resource supports the method but cannot be read, so no validator is known + // (#1753): the 405 of the missing GET does not concern this request. The + // preconditions are evaluated as for a resource with no known representation. + setStatus(before); + getAllowedMethods().clear(); + doConditionalHandle((RepresentationInfo) null); + } + + return resultInfo; + } + /** * Handles a call by first verifying the optional request conditions. *