Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / interceptors / pre / HeaderValidation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.schemaservice.interceptors.pre;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
27
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;
34
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;
41
42 @PreMatching
43 @Priority(AAIRequestFilterPriority.HEADER_VALIDATION)
44 public class HeaderValidation extends AAIContainerFilter implements ContainerRequestFilter {
45
46     @Override
47     public void filter(ContainerRequestContext requestContext) throws IOException {
48
49         Optional<Response> oResp;
50
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());
56             return;
57         }
58         String transId = getRequestId(requestContext);
59         oResp = this.validateHeaderValuePresence(transId, "AAI_4010", acceptHeaderValues);
60         if (oResp.isPresent()) {
61             requestContext.abortWith(oResp.get());
62             return;
63         }
64     }
65
66     private Optional<Response> validateHeaderValuePresence(String value, String errorCode,
67         List<MediaType> acceptHeaderValues) {
68         Response response = null;
69         AAIException aaie;
70         if (value == null || value.isEmpty()) {
71             aaie = new AAIException(errorCode);
72             return Optional
73                 .of(Response
74                     .status(aaie.getErrorObject().getHTTPResponseCode()).entity(ErrorLogHelper
75                         .getRESTAPIErrorResponse(acceptHeaderValues, aaie, new ArrayList<>()))
76                     .build());
77         }
78
79         return Optional.ofNullable(response);
80     }
81
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()) {
89                     requestId =
90                         requestContext.getHeaderString(Constants.HttpHeaders.ECOMP_REQUEST_ID);
91                     if (requestId == null || requestId.isEmpty()) {
92                         return requestId;
93                     }
94                 }
95             }
96         }
97         if (requestContext.getHeaders().get(ONAPLogConstants.Headers.REQUEST_ID) != null) {
98             requestContext.getHeaders().get(ONAPLogConstants.Headers.REQUEST_ID).clear();
99         }
100         if (requestContext.getHeaders().get(Constants.HttpHeaders.TRANSACTION_ID) != null) {
101             requestContext.getHeaders().get(Constants.HttpHeaders.TRANSACTION_ID).clear();
102         }
103         if (requestContext.getHeaders().get(Constants.HttpHeaders.HEADER_REQUEST_ID) != null) {
104             requestContext.getHeaders().get(Constants.HttpHeaders.HEADER_REQUEST_ID).clear();
105         }
106         if (requestContext.getHeaders().get(Constants.HttpHeaders.ECOMP_REQUEST_ID) != null) {
107             requestContext.getHeaders().get(Constants.HttpHeaders.ECOMP_REQUEST_ID).clear();
108         }
109         requestContext.getHeaders().add(Constants.HttpHeaders.TRANSACTION_ID, requestId);
110
111         return requestId;
112     }
113
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())) {
119                 return partnerName;
120             }
121         }
122         if (requestContext.getHeaders().get(ONAPLogConstants.Headers.PARTNER_NAME) != null) {
123             requestContext.getHeaders().get(ONAPLogConstants.Headers.PARTNER_NAME).clear();
124         }
125         if (requestContext.getHeaders().get(AAIHeaderProperties.FROM_APP_ID) != null) {
126             requestContext.getHeaders().get(AAIHeaderProperties.FROM_APP_ID).clear();
127         }
128         requestContext.getHeaders().add(AAIHeaderProperties.FROM_APP_ID, partnerName);
129         return partnerName;
130     }
131 }