All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.storage.XGoogApiClientHeaderProvider Maven / Gradle / Ivy

There is a newer version: 2.45.0
Show newest version
/*
 * Copyright 2023 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.storage;

import com.google.api.gax.rpc.HeaderProvider;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.MapDifference;
import com.google.common.collect.MapDifference.ValueDifference;
import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collector;
import java.util.stream.Collectors;

final class XGoogApiClientHeaderProvider implements HeaderProvider {

  /** Separate entries with a space */
  private static final Collector COMBINER = Collectors.joining(" ");

  private final Map headers;

  private XGoogApiClientHeaderProvider(Map headers) {
    this.headers = headers;
  }

  @Override
  public Map getHeaders() {
    return headers;
  }

  static XGoogApiClientHeaderProvider of(
      HeaderProvider baseValue, ImmutableList additionalEntries) {
    if (additionalEntries.isEmpty()) {
      return new XGoogApiClientHeaderProvider(baseValue.getHeaders());
    } else {
      ImmutableMap right =
          ImmutableMap.of("x-goog-api-client", additionalEntries.stream().collect(COMBINER));
      ImmutableMap union = union(baseValue.getHeaders(), right);
      return new XGoogApiClientHeaderProvider(union);
    }
  }

  /**
   * Union two maps, ignoring case of their keys.
   *
   * 

Any key present in both {@code left} and {@code right} will be combined to produce a new * single value. */ @VisibleForTesting static ImmutableMap union(Map left, Map right) { if (left.equals(right)) { return ImmutableMap.copyOf(left); } Map l = lowerKeys(left); Map r = lowerKeys(right); if (l.equals(r)) { return ImmutableMap.copyOf(l); } Map tmp = new HashMap<>(); MapDifference diff = Maps.difference(l, r); tmp.putAll(diff.entriesOnlyOnLeft()); tmp.putAll(diff.entriesOnlyOnRight()); tmp.putAll(diff.entriesInCommon()); for (Entry> e : diff.entriesDiffering().entrySet()) { String k = e.getKey(); ValueDifference v = e.getValue(); tmp.put(k, ImmutableList.of(v.leftValue(), v.rightValue()).stream().collect(COMBINER)); } return ImmutableMap.copyOf(tmp); } private static Map lowerKeys(Map orig) { Map tmp = new HashMap<>(); for (Entry e : orig.entrySet()) { String k = e.getKey(); String v = e.getValue(); tmp.put(k.toLowerCase(Locale.US), v); } return tmp; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy