/** * GetAllValues returns all values for a header or an empty list if the header has no values * @param key the Header key * @return a possibly empty list of values */ public List<String> getAllValues(String key) { return headers.getOrDefault(canonicalKey(key), Collections.emptyList()); }
@Override public void setResponseHeader(String key, String value, String... vs) { Objects.requireNonNull(key, "key"); Objects.requireNonNull(vs, "vs"); Arrays.stream(vs).forEach(v->Objects.requireNonNull(v,"null value in list ")); String cKey = Headers.canonicalKey(key); if (value == null) { additionalResponseHeaders.remove(cKey); return; } additionalResponseHeaders.put(cKey, Collections.singletonList(value)); }
/** * Build a headers object from a map composed of (name, value) entries, we take a copy of the map and * disallow any further modification * * @param headers underlying collection of header entries to copy * @return {@code Headers} built from headers map */ public static Headers fromMultiHeaderMap(Map<String, List<String>> headers) { Map<String, List<String>> hm = new HashMap<>(); headers.forEach((k, vs) -> hm.put(canonicalKey(k), new ArrayList<>(vs))); return new Headers(Collections.unmodifiableMap(new HashMap<>(Objects.requireNonNull(headers)))); }
/** * Sets a map of headers, overwriting any headers in the current headers with the respective values * * @param vals a map of headers * @return a new headers object with thos headers set */ public Headers setHeaders(Map<String, List<String>> vals) { Objects.requireNonNull(vals, "vals"); Map<String, List<String>> nm = new HashMap<>(headers); vals.forEach((k, vs) -> { vs.forEach(v -> Objects.requireNonNull(v, "header list contains null entries")); nm.put(canonicalKey(k), vs); }); return new Headers(nm); }
/** * Build a headers object from a map composed of (name, value) entries, we take a copy of the map and * disallow any further modification * * @param headers underlying collection of header entries to copy * @return {@code Headers} built from headers map */ public static Headers fromMap(Map<String, String> headers) { Objects.requireNonNull(headers, "headersIn"); Map<String, List<String>> h = new HashMap<>(); headers.forEach((k, v) -> h.put(canonicalKey(k), Collections.singletonList(v))); return new Headers(Collections.unmodifiableMap(new HashMap<>(h))); }
/** * Returns the header matching the specified key. This matches headers in a case-insensitive way and substitutes * underscore and hyphen characters such that : "CONTENT_TYPE_HEADER" and "Content-type" are equivalent. If no matching * header is found then {@code Optional.empty} is returned. * <p> * When multiple headers are present then the first value is returned- see { #getAllValues(String key)} to get all values for a header * * @param key match key * @return a header matching key or empty if no header matches. * @throws NullPointerException if {@code key} is null. */ public Optional<String> get(String key) { Objects.requireNonNull(key, "Key cannot be null"); String canonKey = canonicalKey(key); List<String> val = headers.get(canonKey); if (val == null){ return Optional.empty(); } return Optional.of(val.get(0)); }
/** * Creates a new headers object with the specified headers remove - this overwrites any existin values * <p> * This will overwrite an existing header with an exact name match * * @param key new header key * @return a new headers object with the specified header removed */ public Headers removeHeader(String key) { Objects.requireNonNull(key, "key"); String canonKey = canonicalKey(key); if (!headers.containsKey(canonKey)) { return this; } Map<String, List<String>> nm = new HashMap<>(headers); nm.remove(canonKey); return new Headers(Collections.unmodifiableMap(nm)); }
/** * Creates a new headers object with the specified headers set - this overwrites any existin values * <p> * This will overwrite an existing header with an exact name match * * @param key new header key * @param vs header values to set * @return a new headers object with the specified header added */ public Headers setHeader(String key, Collection<String> vs) { Objects.requireNonNull(key, "key"); Objects.requireNonNull(vs, "vs"); if (vs.size() == 0) { throw new IllegalArgumentException("can't set keys to an empty list"); } vs.forEach((v) -> Objects.requireNonNull(v, "vs")); Map<String, List<String>> nm = new HashMap<>(headers); nm.put(canonicalKey(key), Collections.unmodifiableList(new ArrayList<>(vs))); return new Headers(Collections.unmodifiableMap(nm)); }
/** * Creates a new headers object with the specified header set - this overwrites any existin values * <p> * This will overwrite an existing header with an exact name match * * @param key new header key * @param v1 new header value * @param vs more header values to set * @return a new headers object with the specified header added */ public Headers setHeader(String key, String v1, String... vs) { Objects.requireNonNull(key, "key"); Objects.requireNonNull(v1, "v1"); Stream.of(vs).forEach((v) -> Objects.requireNonNull(v, "vs")); Map<String, List<String>> nm = new HashMap<>(headers); List<String> s = new ArrayList<>(); s.add(v1); s.addAll(Arrays.asList(vs)); nm.put(canonicalKey(key), Collections.unmodifiableList(s)); return new Headers(Collections.unmodifiableMap(nm)); }
/** * Creates a new headers object with the specified header added - if a header with the same key existed it the new value is appended * <p> * This will overwrite an existing header with an exact name match * * @param key new header key * @param v1 new header value * @param vs additional header values to set * @return a new headers object with the specified header added */ public Headers addHeader(String key, String v1, String... vs) { Objects.requireNonNull(key, "key"); Objects.requireNonNull(key, "value"); String canonKey = canonicalKey(key); Map<String, List<String>> nm = new HashMap<>(headers); List<String> current = nm.get(canonKey); if (current == null) { List<String> s = new ArrayList<>(); s.add(v1); s.addAll(Arrays.asList(vs)); nm.put(canonKey, Collections.unmodifiableList(s)); } else { List<String> s = new ArrayList<>(current); s.add(v1); s.addAll(Arrays.asList(vs)); nm.put(canonKey, Collections.unmodifiableList(s)); } return new Headers(nm); }
@Override public void setResponseHeader(String key, String value, String... vs) { if (Headers.canonicalKey(key).equals(OutputEvent.CONTENT_TYPE_HEADER)) { invocationContext.setResponseContentType(value); invocationContext.setResponseHeader("Fn-Http-H-" + key, value); } else { invocationContext.setResponseHeader("Fn-Http-H-" + key, value, vs); } }
if (stripInputHeaders.contains(Headers.canonicalKey(h.getName()))) { continue;