public static void setUserMetadataHeaders(Map<String, String> userMetadataMap,
      RestResponseChannel restResponseChannel, Set<String> keysToNotPrefix) {
    Objects.requireNonNull(userMetadataMap, "user metadata must be supplied");
    Objects.requireNonNull(keysToNotPrefix, "keys to not prefix must be supplied");
    for (Map.Entry<String, String> entry : userMetadataMap.entrySet()) {
      String headerName = entry.getKey();
      String headerValue = entry.getValue();
      // Ensure um prefix is prepended to header name
      if (!headerName.startsWith(Headers.USER_META_DATA_HEADER_PREFIX)) {
        headerName = Headers.USER_META_DATA_HEADER_PREFIX + headerName;
      }
      String headerNameWithoutPrefix = headerName.substring(Headers.USER_META_DATA_HEADER_PREFIX.length());
      if (keysToNotPrefix.contains(headerNameWithoutPrefix)) {
        headerName = headerNameWithoutPrefix;
      }
      if (StandardCharsets.US_ASCII.newEncoder().canEncode(headerValue)) {
        try {
          restResponseChannel.setHeader(headerName, headerValue);
        } catch (IllegalArgumentException iae) {
          // if responseChannel fails to set header due to special characters like "\n\r", we encode it and add to a special header
          encodeMetadataValueAndSetHeader(headerName, headerValue, restResponseChannel);
        }
      } else {
        // if the header value contains non-ascii character, we explicitly encode the value and set in response header
        encodeMetadataValueAndSetHeader(headerName, headerValue, restResponseChannel);
      }
    }
  }
Sets the user metadata in the headers of the response.
@param userMetadataMap map of user metadata that needs to be sent.
@param restResponseChannel the {@link RestResponseChannel} that is used for sending the response.
@param keysToNotPrefix a set of user metadata keys to not add user metadata prefix in the response header
@throws RestServiceException if there are any problems setting the header.
ambry-api/src/main/java/com/github/ambry/rest/RestUtils.java