diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java')
-rw-r--r-- | openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java | 73 |
1 files changed, 65 insertions, 8 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java index 4dcc197796..db10c2e4f3 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/servlet/HttpHeader.java @@ -16,6 +16,11 @@ package org.openecomp.sdc.logging.servlet; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.function.Function; /** @@ -28,28 +33,80 @@ import java.util.function.Function; */ public class HttpHeader { - private final String[] keys; + private static final String NAMES_CANNOT_BE_NULL = "Names cannot be null"; + private static final String AT_LEAST_ONE_NAME_REQUIRED = "At least one name required"; - public HttpHeader(String... keys) { - this.keys = keys; + private final List<String> headerNames; + + /** + * Receives a list of accepted header names as a String array. + * + * @param headerNames cannot be null or empty + */ + public HttpHeader(String[] headerNames) { + + if (Objects.requireNonNull(headerNames, NAMES_CANNOT_BE_NULL).length < 1) { + throw new IllegalArgumentException(AT_LEAST_ONE_NAME_REQUIRED); + } + + this.headerNames = Arrays.asList(headerNames); + } + + /** + * Receives a list of accepted header names as a list of String. + * + * @param headerNames cannot be null or empty + */ + public HttpHeader(List<String> headerNames) { + + if (Objects.requireNonNull(headerNames, NAMES_CANNOT_BE_NULL).isEmpty()) { + throw new IllegalArgumentException(AT_LEAST_ONE_NAME_REQUIRED); + } + + this.headerNames = new ArrayList<>(headerNames); } /** * Returns the value of any of the possible headers. * * @param reader function for reading an HTTP header. - * @return value or null if not found + * @return value or empty if not found */ - public String getAny(Function<String, String> reader) { + public Optional<String> getAny(Function<String, String> reader) { - for (String k : keys) { + for (String k : headerNames) { String value = reader.apply(k); if (value != null) { - return value; + return Optional.of(value); } } - return null; + return Optional.empty(); + } + + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + HttpHeader that = (HttpHeader) o; + return Objects.equals(headerNames, that.headerNames); + } + + @Override + public int hashCode() { + return Objects.hash(headerNames); + } + + @Override + public String toString() { + return "HttpHeader{headerNames=" + headerNames + '}'; } } |