|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.httpjson; |
| 31 | + |
| 32 | +import com.google.api.gax.resumable.ResumableUploadSession; |
| 33 | +import com.google.api.gax.resumable.ResumableUploadStatus; |
| 34 | +import com.google.api.gax.rpc.ApiExceptionFactory; |
| 35 | +import com.google.api.gax.rpc.StatusCode; |
| 36 | +import com.google.common.base.Strings; |
| 37 | +import org.jspecify.annotations.NullMarked; |
| 38 | + |
| 39 | +/** Shared header constants and parsing utilities for HTTP/JSON resumable upload wire calls. */ |
| 40 | +@NullMarked |
| 41 | +final class ResumableUploadHeaders { |
| 42 | + |
| 43 | + static final String UPLOAD_PROTOCOL_HEADER = "X-Goog-Upload-Protocol"; |
| 44 | + static final String UPLOAD_COMMAND_HEADER = "X-Goog-Upload-Command"; |
| 45 | + static final String UPLOAD_URL_HEADER = "X-Goog-Upload-URL"; |
| 46 | + static final String UPLOAD_GRANULARITY_HEADER = "X-Goog-Upload-Chunk-Granularity"; |
| 47 | + static final String UPLOAD_OFFSET_HEADER = "X-Goog-Upload-Offset"; |
| 48 | + static final String UPLOAD_STATUS_HEADER = "X-Goog-Upload-Status"; |
| 49 | + static final String UPLOAD_SIZE_RECEIVED_HEADER = "X-Goog-Upload-Size-Received"; |
| 50 | + |
| 51 | + private ResumableUploadHeaders() {} |
| 52 | + |
| 53 | + static ResumableUploadStatus parseUploadStatus(HttpJsonMetadata responseHeaders) { |
| 54 | + return ResumableUploadStatus.fromHeader( |
| 55 | + HttpHeadersUtils.getSingleHeader(responseHeaders.getHeaders(), UPLOAD_STATUS_HEADER)); |
| 56 | + } |
| 57 | + |
| 58 | + static int parseChunkGranularity(HttpJsonMetadata responseHeaders) { |
| 59 | + String granularityStr = |
| 60 | + HttpHeadersUtils.getSingleHeader(responseHeaders.getHeaders(), UPLOAD_GRANULARITY_HEADER); |
| 61 | + if (Strings.isNullOrEmpty(granularityStr)) { |
| 62 | + return ResumableUploadSession.DEFAULT_CHUNK_GRANULARITY; |
| 63 | + } |
| 64 | + try { |
| 65 | + int parsed = Integer.parseInt(granularityStr); |
| 66 | + if (parsed <= 0) { |
| 67 | + throw ApiExceptionFactory.createException( |
| 68 | + "Response contained non-positive " |
| 69 | + + UPLOAD_GRANULARITY_HEADER |
| 70 | + + " header: " |
| 71 | + + granularityStr, |
| 72 | + /* cause= */ null, |
| 73 | + HttpJsonStatusCode.of(StatusCode.Code.INTERNAL), |
| 74 | + /* retryable= */ false); |
| 75 | + } |
| 76 | + return parsed; |
| 77 | + } catch (NumberFormatException e) { |
| 78 | + throw ApiExceptionFactory.createException( |
| 79 | + "Response contained invalid " + UPLOAD_GRANULARITY_HEADER + " header: " + granularityStr, |
| 80 | + e, |
| 81 | + HttpJsonStatusCode.of(StatusCode.Code.INTERNAL), |
| 82 | + /* retryable= */ false); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments