|
| 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.resumable; |
| 31 | + |
| 32 | +import com.google.api.core.InternalApi; |
| 33 | +import com.google.auto.value.AutoValue; |
| 34 | +import com.google.common.collect.ImmutableList; |
| 35 | +import com.google.common.collect.ImmutableMap; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | +import org.jspecify.annotations.NullMarked; |
| 40 | +import org.jspecify.annotations.Nullable; |
| 41 | + |
| 42 | +/** Request parameters for initiating a resumable upload session. */ |
| 43 | +@NullMarked |
| 44 | +@InternalApi |
| 45 | +@AutoValue |
| 46 | +public abstract class StartUploadRequest { |
| 47 | + |
| 48 | + /** Returns the URL path to append to the service endpoint. */ |
| 49 | + public abstract String getPath(); |
| 50 | + |
| 51 | + /** Returns the optional initial JSON request payload. */ |
| 52 | + @Nullable |
| 53 | + public abstract String getJsonPayload(); |
| 54 | + |
| 55 | + /** Returns the query parameters for the initiation request. */ |
| 56 | + public abstract Map<String, List<String>> getQueryParams(); |
| 57 | + |
| 58 | + public abstract Builder toBuilder(); |
| 59 | + |
| 60 | + public static Builder newBuilder() { |
| 61 | + return new AutoValue_StartUploadRequest.Builder().setQueryParams(Collections.emptyMap()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Convenience factory for creating a {@link StartUploadRequest} with only a target path. |
| 66 | + * |
| 67 | + * @param path the resource upload path |
| 68 | + * @return a new {@link StartUploadRequest} instance |
| 69 | + */ |
| 70 | + public static StartUploadRequest create(String path) { |
| 71 | + return newBuilder().setPath(path).build(); |
| 72 | + } |
| 73 | + |
| 74 | + @AutoValue.Builder |
| 75 | + public abstract static class Builder { |
| 76 | + public abstract Builder setPath(String path); |
| 77 | + |
| 78 | + public abstract Builder setJsonPayload(@Nullable String jsonPayload); |
| 79 | + |
| 80 | + public abstract Builder setQueryParams(Map<String, List<String>> queryParams); |
| 81 | + |
| 82 | + abstract @Nullable Map<String, List<String>> getQueryParams(); |
| 83 | + |
| 84 | + abstract @Nullable String getPath(); |
| 85 | + |
| 86 | + abstract StartUploadRequest autoBuild(); |
| 87 | + |
| 88 | + public StartUploadRequest build() { |
| 89 | + if (getPath() != null && getPath().startsWith("/")) { |
| 90 | + setPath(getPath().substring(1)); |
| 91 | + } |
| 92 | + |
| 93 | + Map<String, List<String>> params = getQueryParams(); |
| 94 | + if (params != null && !params.isEmpty()) { |
| 95 | + ImmutableMap.Builder<String, List<String>> mapBuilder = ImmutableMap.builder(); |
| 96 | + for (Map.Entry<String, List<String>> entry : params.entrySet()) { |
| 97 | + mapBuilder.put(entry.getKey(), ImmutableList.copyOf(entry.getValue())); |
| 98 | + } |
| 99 | + setQueryParams(mapBuilder.build()); |
| 100 | + } else { |
| 101 | + setQueryParams(Collections.emptyMap()); |
| 102 | + } |
| 103 | + |
| 104 | + return autoBuild(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments