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

com.yahoo.elide.utils.Headers Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020, Yahoo Inc.
 * Licensed under the Apache License, Version 2.0
 * See LICENSE file in project root for terms.
 */

package com.yahoo.elide.utils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * Utility class which modifies request headers.
 */
public class Headers {
    private Headers() {
    }

    private static final Set AUTHORIZATION_HEADER_NAMES;

    static {
        AUTHORIZATION_HEADER_NAMES = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        AUTHORIZATION_HEADER_NAMES.addAll(Set.of("authorization", "proxy-authorization"));
    }

    /**
     * Remove Authorization and Proxy Authorization headers from request headers.
     * @param headers the headers to process
     * @return the processed headers or original headers
     */
     public static Map> removeAuthorizationHeaders(Map> headers) {
         boolean contains = false;
         for (String headerName : headers.keySet()) {
             if (AUTHORIZATION_HEADER_NAMES.contains(headerName)) {
                 contains = true;
                 break;
             }
         }
         if (!contains) {
             return headers;
         }

         // HTTP header names should be case insensitive, but maybe not all libraries consider this
         Map> requestHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

         headers.entrySet().stream().forEach(entry -> {
             if (!AUTHORIZATION_HEADER_NAMES.contains(entry.getKey())) {
                 requestHeaders.put(entry.getKey(), entry.getValue());
             }
         });
         return requestHeaders;
     }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy