|
| 1 | +// ------------------------------------------------------------------------------ |
| 2 | +// Copyright (c) 2021 Microsoft Corporation |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +package com.microsoft.graph.content; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Objects; |
| 30 | +import java.util.concurrent.ThreadLocalRandom; |
| 31 | +import java.util.regex.Matcher; |
| 32 | +import java.util.regex.Pattern; |
| 33 | + |
| 34 | +import javax.annotation.Nonnull; |
| 35 | +import javax.annotation.Nullable; |
| 36 | + |
| 37 | +import com.google.gson.annotations.Expose; |
| 38 | +import com.google.gson.annotations.SerializedName; |
| 39 | +import com.microsoft.graph.http.HttpMethod; |
| 40 | +import com.microsoft.graph.http.IHttpRequest; |
| 41 | +import com.microsoft.graph.options.HeaderOption; |
| 42 | + |
| 43 | +public class BatchRequestContent { |
| 44 | + @Expose |
| 45 | + @SerializedName("requests") |
| 46 | + public List<BatchRequestStep<?>> requests; |
| 47 | + |
| 48 | + @Nonnull |
| 49 | + public String addBatchRequestStep(@Nonnull final IHttpRequest request) { |
| 50 | + Objects.requireNonNull(request, "request parameter cannot be null"); |
| 51 | + return addBatchRequestStep(request, request.getHttpMethod() == null ? HttpMethod.GET : request.getHttpMethod()); |
| 52 | + } |
| 53 | + @Nonnull |
| 54 | + public String addBatchRequestStep(@Nonnull final IHttpRequest request, @Nonnull final HttpMethod httpMethod) { |
| 55 | + return addBatchRequestStep(request, httpMethod, null); |
| 56 | + } |
| 57 | + @Nonnull |
| 58 | + public <T> String addBatchRequestStep(@Nonnull final IHttpRequest request, @Nonnull final HttpMethod httpMethod, @Nullable final T serializableBody) { |
| 59 | + return addBatchRequestStep(request, httpMethod, serializableBody, null); |
| 60 | + } |
| 61 | + public <T> String addBatchRequestStep(@Nonnull final IHttpRequest request, @Nonnull final HttpMethod httpMethod, @Nullable final T serializableBody, @Nullable final HashSet<String> dependsOnRequestsIds) { |
| 62 | + Objects.requireNonNull(request, "request parameter cannot be null"); |
| 63 | + Objects.requireNonNull(httpMethod, "httpMethod parameter cannot be null"); |
| 64 | + if(dependsOnRequestsIds != null) |
| 65 | + for(final String id : dependsOnRequestsIds) { |
| 66 | + if(getStepById(id) == null) |
| 67 | + throw new IllegalArgumentException("the current request depends on a inexisting request"); |
| 68 | + } |
| 69 | + |
| 70 | + if(requests == null) |
| 71 | + requests = new ArrayList<>(); |
| 72 | + |
| 73 | + final Matcher protocolAndHostReplacementMatcher = protocolAndHostReplacementPattern.matcher(request.getRequestUrl().toString()); |
| 74 | + final BatchRequestStep<T> step = new BatchRequestStep<T>() {{ |
| 75 | + url = protocolAndHostReplacementMatcher.replaceAll(""); |
| 76 | + body = serializableBody; |
| 77 | + method = httpMethod; |
| 78 | + dependsOn = dependsOnRequestsIds; |
| 79 | + id = getNextRequestId(); |
| 80 | + }}; |
| 81 | + |
| 82 | + if(!request.getHeaders().isEmpty()) { |
| 83 | + step.headers = new HashMap<>(); |
| 84 | + for(final HeaderOption headerOption : request.getHeaders()) |
| 85 | + step.headers.putIfAbsent(headerOption.getName(), headerOption.getValue().toString()); |
| 86 | + } |
| 87 | + requests.add(step); |
| 88 | + return step.id; |
| 89 | + } |
| 90 | + public void removeBatchRequestStepWithId(@Nonnull final String ...stepIds) { |
| 91 | + if(requests == null) return; |
| 92 | + |
| 93 | + for(final String stepId : stepIds) { |
| 94 | + Objects.requireNonNull(stepId, "parameter stepIds cannot contain null values"); |
| 95 | + requests.removeIf(x -> x.id == stepId); |
| 96 | + for(final BatchRequestStep<?> step : requests) { |
| 97 | + if(step.dependsOn != null) { |
| 98 | + step.dependsOn.removeIf(x -> x == stepId); |
| 99 | + if(step.dependsOn.isEmpty()) |
| 100 | + step.dependsOn = null; // so we don't send dependsOn: [] over the wire |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + @Nullable |
| 106 | + @SuppressWarnings("unchecked") |
| 107 | + public <T> BatchRequestStep<T> getStepById(@Nonnull final String stepId) { |
| 108 | + Objects.requireNonNull(stepId, "parameter stepId cannot be null"); |
| 109 | + if(requests == null) return null; |
| 110 | + |
| 111 | + for(final BatchRequestStep<?> step : requests) { |
| 112 | + if(stepId.equals(step.id)) |
| 113 | + return (BatchRequestStep<T>)step; |
| 114 | + } |
| 115 | + return null; |
| 116 | + } |
| 117 | + private static final Pattern protocolAndHostReplacementPattern = Pattern.compile("(?i)^http[s]?:\\/\\/graph\\.microsoft\\.com\\/(?>v1\\.0|beta)\\/?"); // (?i) case insensitive |
| 118 | + private String getNextRequestId() { |
| 119 | + String requestId; |
| 120 | + do { |
| 121 | + requestId = Integer.toString(ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE)); |
| 122 | + } while(getStepById(requestId) != null); |
| 123 | + return requestId; |
| 124 | + } |
| 125 | +} |
0 commit comments