|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.storage.blob.implementation.util; |
| 5 | + |
| 6 | +import com.azure.core.http.HttpHeaderName; |
| 7 | +import com.azure.core.http.HttpHeaders; |
| 8 | +import com.azure.core.http.HttpMethod; |
| 9 | +import com.azure.core.http.HttpPipelineCallContext; |
| 10 | +import com.azure.core.http.HttpPipelineNextPolicy; |
| 11 | +import com.azure.core.http.HttpResponse; |
| 12 | +import com.azure.core.http.policy.HttpPipelinePolicy; |
| 13 | +import com.azure.core.util.FluxUtil; |
| 14 | +import com.azure.core.util.logging.ClientLogger; |
| 15 | +import com.azure.storage.common.DownloadContentValidationOptions; |
| 16 | +import com.azure.storage.common.implementation.structuredmessage.StructuredMessageDecodingStream; |
| 17 | +import reactor.core.publisher.Flux; |
| 18 | +import reactor.core.publisher.Mono; |
| 19 | + |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.nio.charset.Charset; |
| 22 | + |
| 23 | +/** |
| 24 | + * This is a decoding policy in an {@link com.azure.core.http.HttpPipeline} to decode structured messages in blob |
| 25 | + * download requests. The policy checks for a context value to determine when to apply structured message decoding. |
| 26 | + */ |
| 27 | +public class StructuredMessageDecoderPolicy implements HttpPipelinePolicy { |
| 28 | + private static final ClientLogger LOGGER = new ClientLogger(StructuredMessageDecoderPolicy.class); |
| 29 | + |
| 30 | + /** |
| 31 | + * Context key used to signal that structured message decoding should be applied. |
| 32 | + */ |
| 33 | + public static final String STRUCTURED_MESSAGE_DECODING_CONTEXT_KEY = "azure-storage-structured-message-decoding"; |
| 34 | + |
| 35 | + /** |
| 36 | + * Context key used to pass DownloadContentValidationOptions to the policy. |
| 37 | + */ |
| 38 | + public static final String STRUCTURED_MESSAGE_VALIDATION_OPTIONS_CONTEXT_KEY = |
| 39 | + "azure-storage-structured-message-validation-options"; |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a new instance of {@link StructuredMessageDecoderPolicy}. |
| 43 | + */ |
| 44 | + public StructuredMessageDecoderPolicy() { |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { |
| 49 | + // Check if structured message decoding is enabled for this request |
| 50 | + if (!shouldApplyDecoding(context)) { |
| 51 | + return next.process(); |
| 52 | + } |
| 53 | + |
| 54 | + return next.process().map(httpResponse -> { |
| 55 | + // Only apply decoding to download responses (GET requests with body) |
| 56 | + if (isDownloadResponse(httpResponse)) { |
| 57 | + DownloadContentValidationOptions validationOptions = getValidationOptions(context); |
| 58 | + Long contentLength = getContentLength(httpResponse.getHeaders()); |
| 59 | + |
| 60 | + if (contentLength != null && contentLength > 0 && validationOptions != null) { |
| 61 | + Flux<ByteBuffer> decodedStream = StructuredMessageDecodingStream.wrapStreamIfNeeded( |
| 62 | + httpResponse.getBody(), contentLength, validationOptions); |
| 63 | + |
| 64 | + return new DecodedResponse(httpResponse, decodedStream); |
| 65 | + } |
| 66 | + } |
| 67 | + return httpResponse; |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Checks if structured message decoding should be applied based on context. |
| 73 | + * |
| 74 | + * @param context The pipeline call context. |
| 75 | + * @return true if decoding should be applied, false otherwise. |
| 76 | + */ |
| 77 | + private boolean shouldApplyDecoding(HttpPipelineCallContext context) { |
| 78 | + return context.getData(STRUCTURED_MESSAGE_DECODING_CONTEXT_KEY) |
| 79 | + .map(value -> value instanceof Boolean && (Boolean) value) |
| 80 | + .orElse(false); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets the validation options from context. |
| 85 | + * |
| 86 | + * @param context The pipeline call context. |
| 87 | + * @return The validation options or null if not present. |
| 88 | + */ |
| 89 | + private DownloadContentValidationOptions getValidationOptions(HttpPipelineCallContext context) { |
| 90 | + return context.getData(STRUCTURED_MESSAGE_VALIDATION_OPTIONS_CONTEXT_KEY) |
| 91 | + .filter(value -> value instanceof DownloadContentValidationOptions) |
| 92 | + .map(value -> (DownloadContentValidationOptions) value) |
| 93 | + .orElse(null); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets the content length from response headers. |
| 98 | + * |
| 99 | + * @param headers The response headers. |
| 100 | + * @return The content length or null if not present. |
| 101 | + */ |
| 102 | + private Long getContentLength(HttpHeaders headers) { |
| 103 | + String contentLengthStr = headers.getValue(HttpHeaderName.CONTENT_LENGTH); |
| 104 | + if (contentLengthStr != null) { |
| 105 | + try { |
| 106 | + return Long.parseLong(contentLengthStr); |
| 107 | + } catch (NumberFormatException e) { |
| 108 | + LOGGER.warning("Invalid content length in response headers: " + contentLengthStr); |
| 109 | + } |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Checks if the response is a download response (GET request with body). |
| 116 | + * |
| 117 | + * @param httpResponse The HTTP response. |
| 118 | + * @return true if it's a download response, false otherwise. |
| 119 | + */ |
| 120 | + private boolean isDownloadResponse(HttpResponse httpResponse) { |
| 121 | + return httpResponse.getRequest().getHttpMethod() == HttpMethod.GET && httpResponse.getBody() != null; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * HTTP response wrapper that provides a decoded response body. |
| 126 | + */ |
| 127 | + static class DecodedResponse extends HttpResponse { |
| 128 | + private final Flux<ByteBuffer> decodedBody; |
| 129 | + private final HttpHeaders httpHeaders; |
| 130 | + private final int statusCode; |
| 131 | + |
| 132 | + DecodedResponse(HttpResponse httpResponse, Flux<ByteBuffer> decodedBody) { |
| 133 | + super(httpResponse.getRequest()); |
| 134 | + this.decodedBody = decodedBody; |
| 135 | + this.httpHeaders = httpResponse.getHeaders(); |
| 136 | + this.statusCode = httpResponse.getStatusCode(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int getStatusCode() { |
| 141 | + return statusCode; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String getHeaderValue(String name) { |
| 146 | + return httpHeaders.getValue(name); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public HttpHeaders getHeaders() { |
| 151 | + return httpHeaders; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public Flux<ByteBuffer> getBody() { |
| 156 | + return decodedBody; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public Mono<byte[]> getBodyAsByteArray() { |
| 161 | + return FluxUtil.collectBytesInByteBufferStream(decodedBody); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public Mono<String> getBodyAsString() { |
| 166 | + return FluxUtil.collectBytesInByteBufferStream(decodedBody).map(String::new); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Mono<String> getBodyAsString(Charset charset) { |
| 171 | + return FluxUtil.collectBytesInByteBufferStream(decodedBody).map(b -> new String(b, charset)); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments