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

com.arangodb.shaded.vertx.ext.web.client.impl.cache.Vary Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright 2021 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */
package com.arangodb.shaded.vertx.ext.web.client.impl.cache;

import com.arangodb.shaded.vertx.core.MultiMap;
import com.arangodb.shaded.vertx.core.http.HttpHeaders;
import com.arangodb.shaded.vertx.core.http.RequestOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Craig Day
 * @author Jordi Llach
 */
public class Vary {

  private final MultiMap requestHeaders;
  private final MultiMap responseHeaders;
  private final Set variations;

  public Vary(MultiMap requestHeaders, MultiMap responseHeaders) {
    this.requestHeaders = requestHeaders;
    this.responseHeaders = responseHeaders;
    this.variations = parseHeaders(responseHeaders);
  }

  public boolean matchesRequest(RequestOptions request) {
    return variations.stream().allMatch(variation -> variationMatches(variation, request));
  }

  @Override
  public String toString() {
    List parts = new ArrayList<>(variations.size());

    for (CharSequence variation : variations) {
      parts.addAll(normalizeValues(requestHeaders.getAll(variation)));
    }

    return parts.stream().sorted().collect(Collectors.joining(","));
  }

  private boolean variationMatches(CharSequence variation, RequestOptions request) {
    if (HttpHeaders.USER_AGENT.equals(variation)) {
      return isUserAgentMatch(request);
    } else if (HttpHeaders.ACCEPT_ENCODING.equals(variation)) {
      return true;
    } else {
      return isExactMatch(variation, request);
    }
  }

  private boolean isUserAgentMatch(RequestOptions request) {
    UserAgent original = UserAgent.parse(requestHeaders);
    UserAgent current = UserAgent.parse(request.getHeaders());

    return original.equals(current);
  }

  private boolean isExactMatch(CharSequence variation, RequestOptions request) {
    Set a = normalizeValues(request.getHeaders().getAll(variation));
    Set b = normalizeValues(requestHeaders.getAll(variation));

    return a.equals(b);
  }

  private Set normalizeValues(List values) {
    return values
      .stream()
      .flatMap(v -> Arrays.stream(v.split(",")))
      .map(v -> v.trim().toLowerCase())
      .collect(Collectors.toSet());
  }

  private Set parseHeaders(MultiMap headers) {
    List varyHeaders = headers.getAll(HttpHeaders.VARY);
    Set parsed = new HashSet<>(varyHeaders.size());

    varyHeaders.forEach(names -> {
      for (String name : names.split(",")) {
        parsed.add(HttpHeaders.createOptimized(name.trim().toLowerCase()));
      }
    });

    return parsed;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy