db10c2e4f3110452940cfd6ddc522d14938143e1
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.logging.servlet;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.Optional;
24 import java.util.function.Function;
25
26 /**
27  * Handles any of possible header names to read a value for that header. This is useful for backward compatibility, if
28  * multiple headers may have the same meaning. For instance, when requests come from multiple service, some using an old
29  * header and others using a new header to pass the same information.
30  *
31  * @author evitaliy
32  * @since 25 Mar 2018
33  */
34 public class HttpHeader {
35
36     private static final String NAMES_CANNOT_BE_NULL = "Names cannot be null";
37     private static final String AT_LEAST_ONE_NAME_REQUIRED = "At least one name required";
38
39     private final List<String> headerNames;
40
41     /**
42      * Receives a list of accepted header names as a String array.
43      *
44      * @param headerNames cannot be null or empty
45      */
46     public HttpHeader(String[] headerNames) {
47
48         if (Objects.requireNonNull(headerNames, NAMES_CANNOT_BE_NULL).length < 1) {
49             throw new IllegalArgumentException(AT_LEAST_ONE_NAME_REQUIRED);
50         }
51
52         this.headerNames = Arrays.asList(headerNames);
53     }
54
55     /**
56      * Receives a list of accepted header names as a list of String.
57      *
58      * @param headerNames cannot be null or empty
59      */
60     public HttpHeader(List<String> headerNames) {
61
62         if (Objects.requireNonNull(headerNames, NAMES_CANNOT_BE_NULL).isEmpty()) {
63             throw new IllegalArgumentException(AT_LEAST_ONE_NAME_REQUIRED);
64         }
65
66         this.headerNames = new ArrayList<>(headerNames);
67     }
68
69     /**
70      * Returns the value of any of the possible headers.
71      *
72      * @param reader function for reading an HTTP header.
73      * @return value or empty if not found
74      */
75     public Optional<String> getAny(Function<String, String> reader) {
76
77         for (String k : headerNames) {
78
79             String value = reader.apply(k);
80             if (value != null) {
81                 return Optional.of(value);
82             }
83         }
84
85         return Optional.empty();
86     }
87
88     @Override
89     public boolean equals(Object o) {
90
91         if (this == o) {
92             return true;
93         }
94
95         if (o == null || getClass() != o.getClass()) {
96             return false;
97         }
98
99         HttpHeader that = (HttpHeader) o;
100         return Objects.equals(headerNames, that.headerNames);
101     }
102
103     @Override
104     public int hashCode() {
105         return Objects.hash(headerNames);
106     }
107
108     @Override
109     public String toString() {
110         return "HttpHeader{headerNames=" + headerNames + '}';
111     }
112 }