de.adorsys.multibanking.ing.http.ResponseHeaders Maven / Gradle / Ivy
package de.adorsys.multibanking.ing.http;
import java.util.*;
public class ResponseHeaders {
private static final String LOCATION = "Location";
private static final String X_REQUEST_ID = "X-Request-ID";
public static final String ASPSP_SCA_APPROACH = "ASPSP-SCA-Approach";
private static final String CONTENT_TYPE = "Content-Type";
private static Map headerNamesLowerCased = new HashMap<>();
static {
headerNamesLowerCased.put(LOCATION.toLowerCase(), LOCATION);
headerNamesLowerCased.put(X_REQUEST_ID.toLowerCase(), X_REQUEST_ID);
headerNamesLowerCased.put(ASPSP_SCA_APPROACH.toLowerCase(), ASPSP_SCA_APPROACH);
headerNamesLowerCased.put(CONTENT_TYPE.toLowerCase(), CONTENT_TYPE);
}
private Map headers;
private ResponseHeaders(Map headers) {
this.headers = headers;
}
public static ResponseHeaders fromMap(Map headersMap) {
if (Objects.isNull(headersMap) || headersMap.isEmpty()) {
return emptyResponseHeaders();
}
Map headers = new HashMap<>();
Set headerNamesLowerCased = ResponseHeaders.headerNamesLowerCased.keySet();
headersMap.forEach((name, value) -> {
String headerNameInLowerCase = name.toLowerCase();
if (isHeaderExistInBGSpecification(headerNamesLowerCased, headerNameInLowerCase)) {
headers.put(ResponseHeaders.headerNamesLowerCased.get(headerNameInLowerCase), value);
}
});
return new ResponseHeaders(headers);
}
public static ResponseHeaders emptyResponseHeaders() {
return new ResponseHeaders(Collections.emptyMap());
}
private static boolean isHeaderExistInBGSpecification(Set headerNames, String headerName) {
return headerNames.contains(headerName);
}
public Map getHeadersMap() {
return new HashMap<>(headers);
}
public String getHeader(String headerName) {
return headers.get(headerName);
}
}