@@ -70,14 +70,9 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
7070 return httpResponse ;
7171 }
7272
73- // Confirm the service actually honored our structured-body request before we hand the body to the decoder.
74- validateStructuredMessageHeaders (httpResponse );
75-
76- Long decodedContentLength = getStructuredContentLength (httpResponse .getHeaders ());
77- if (decodedContentLength == null ) {
78- throw LOGGER .logExceptionAsError (new IllegalStateException (
79- "x-ms-structured-content-length header is present but could not be parsed as a long." ));
80- }
73+ // Confirm the service honored our structured-body request and parse the decoded length in one step,
74+ // failing fast with a consistent error if either header is absent or unparseable.
75+ long decodedContentLength = validateAndGetDecodedContentLength (httpResponse );
8176
8277 // Fresh decoder per response so retries each get a clean state machine.
8378 StructuredMessageDecoder decoder = new StructuredMessageDecoder (contentLength );
@@ -98,11 +93,14 @@ private boolean shouldApplyDecoding(HttpPipelineCallContext context) {
9893 }
9994
10095 /**
101- * Verifies the response acknowledges the structured-body request: presence of the
102- * {@code x-ms-structured-body} header and the {@code x-ms-structured-content-length}
103- * header. If either is missing, the service is sending us a normal body and we must not run the decoder over it.
96+ * Verifies the response acknowledges the structured-body request ({@code x-ms-structured-body} present) and
97+ * parses {@code x-ms-structured-content-length} in one step. Throws with a consistent error message if either
98+ * header is absent or the length value is not parseable as a {@code long}, matching the fail-fast behaviour
99+ * used for other validation failures.
100+ *
101+ * @return the decoded payload size declared by the service.
104102 */
105- private void validateStructuredMessageHeaders (HttpResponse httpResponse ) {
103+ private long validateAndGetDecodedContentLength (HttpResponse httpResponse ) {
106104 String structuredBody
107105 = httpResponse .getHeaders ().getValue (Constants .HeaderConstants .STRUCTURED_BODY_TYPE_HEADER_NAME );
108106 String structuredContentLength
@@ -111,6 +109,12 @@ private void validateStructuredMessageHeaders(HttpResponse httpResponse) {
111109 throw LOGGER .logExceptionAsError (
112110 new IllegalStateException ("Structured message was requested but the response did not acknowledge it." ));
113111 }
112+ try {
113+ return Long .parseLong (structuredContentLength );
114+ } catch (NumberFormatException e ) {
115+ throw LOGGER .logExceptionAsError (new IllegalStateException ("x-ms-structured-content-length header value '"
116+ + structuredContentLength + "' could not be parsed as a long." , e ));
117+ }
114118 }
115119
116120 /**
@@ -129,22 +133,6 @@ private static Long getContentLength(HttpHeaders headers) {
129133 return null ;
130134 }
131135
132- /**
133- * Reads {@code x-ms-structured-content-length} as a {@code Long}, returning {@code null} if missing or
134- * unparseable.
135- */
136- private static Long getStructuredContentLength (HttpHeaders headers ) {
137- String value = headers .getValue (Constants .HeaderConstants .STRUCTURED_CONTENT_LENGTH_HEADER_NAME );
138- if (value != null ) {
139- try {
140- return Long .parseLong (value );
141- } catch (NumberFormatException e ) {
142- // Malformed header; fall through to null.
143- }
144- }
145- return null ;
146- }
147-
148136 /**
149137 * @return true for a 2xx response to a GET request, the only response shape that carries a body we
150138 * can decode. 206 (Partial Content) on retried range downloads is included.
0 commit comments