2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.schemaservice.interceptors.pre;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
28 import javax.annotation.Priority;
29 import javax.ws.rs.container.ContainerRequestContext;
30 import javax.ws.rs.container.ContainerRequestFilter;
31 import javax.ws.rs.container.PreMatching;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.logging.ErrorLogHelper;
37 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
38 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
39 import org.onap.logging.filter.base.Constants;
40 import org.onap.logging.ref.slf4j.ONAPLogConstants;
43 @Priority(AAIRequestFilterPriority.HEADER_VALIDATION)
44 public class HeaderValidation extends AAIContainerFilter implements ContainerRequestFilter {
47 public void filter(ContainerRequestContext requestContext) throws IOException {
49 Optional<Response> oResp;
51 List<MediaType> acceptHeaderValues = requestContext.getAcceptableMediaTypes();
52 String fromAppId = getPartnerName(requestContext);
53 oResp = this.validateHeaderValuePresence(fromAppId, "AAI_4009", acceptHeaderValues);
54 if (oResp.isPresent()) {
55 requestContext.abortWith(oResp.get());
58 String transId = getRequestId(requestContext);
59 oResp = this.validateHeaderValuePresence(transId, "AAI_4010", acceptHeaderValues);
60 if (oResp.isPresent()) {
61 requestContext.abortWith(oResp.get());
66 private Optional<Response> validateHeaderValuePresence(String value, String errorCode,
67 List<MediaType> acceptHeaderValues) {
68 Response response = null;
70 if (value == null || value.isEmpty()) {
71 aaie = new AAIException(errorCode);
74 .status(aaie.getErrorObject().getHTTPResponseCode()).entity(ErrorLogHelper
75 .getRESTAPIErrorResponse(acceptHeaderValues, aaie, new ArrayList<>()))
79 return Optional.ofNullable(response);
82 public String getRequestId(ContainerRequestContext requestContext) {
83 String requestId = requestContext.getHeaderString(ONAPLogConstants.Headers.REQUEST_ID);
84 if (requestId == null || requestId.isEmpty()) {
85 requestId = requestContext.getHeaderString(Constants.HttpHeaders.HEADER_REQUEST_ID);
86 if (requestId == null || requestId.isEmpty()) {
87 requestId = requestContext.getHeaderString(Constants.HttpHeaders.TRANSACTION_ID);
88 if (requestId == null || requestId.isEmpty()) {
90 requestContext.getHeaderString(Constants.HttpHeaders.ECOMP_REQUEST_ID);
91 if (requestId == null || requestId.isEmpty()) {
97 if (requestContext.getHeaders().get(ONAPLogConstants.Headers.REQUEST_ID) != null) {
98 requestContext.getHeaders().get(ONAPLogConstants.Headers.REQUEST_ID).clear();
100 if (requestContext.getHeaders().get(Constants.HttpHeaders.TRANSACTION_ID) != null) {
101 requestContext.getHeaders().get(Constants.HttpHeaders.TRANSACTION_ID).clear();
103 if (requestContext.getHeaders().get(Constants.HttpHeaders.HEADER_REQUEST_ID) != null) {
104 requestContext.getHeaders().get(Constants.HttpHeaders.HEADER_REQUEST_ID).clear();
106 if (requestContext.getHeaders().get(Constants.HttpHeaders.ECOMP_REQUEST_ID) != null) {
107 requestContext.getHeaders().get(Constants.HttpHeaders.ECOMP_REQUEST_ID).clear();
109 requestContext.getHeaders().add(Constants.HttpHeaders.TRANSACTION_ID, requestId);
114 public String getPartnerName(ContainerRequestContext requestContext) {
115 String partnerName = requestContext.getHeaderString(ONAPLogConstants.Headers.PARTNER_NAME);
116 if (partnerName == null || (partnerName.isEmpty())) {
117 partnerName = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
118 if (partnerName == null || (partnerName.isEmpty())) {
122 if (requestContext.getHeaders().get(ONAPLogConstants.Headers.PARTNER_NAME) != null) {
123 requestContext.getHeaders().get(ONAPLogConstants.Headers.PARTNER_NAME).clear();
125 if (requestContext.getHeaders().get(AAIHeaderProperties.FROM_APP_ID) != null) {
126 requestContext.getHeaders().get(AAIHeaderProperties.FROM_APP_ID).clear();
128 requestContext.getHeaders().add(AAIHeaderProperties.FROM_APP_ID, partnerName);