Move REQUIRE_CLIENT_AUTH code to start script
[aai/babel.git] / src / main / java / org / onap / aai / babel / request / RequestHeaders.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.babel.request;
23
24 import java.util.Optional;
25 import javax.ws.rs.core.HttpHeaders;
26
27 /** Bean to represent the ECOMP request/transaction IDs required for EELF logging. */
28 public class RequestHeaders {
29
30     // ECOMP request ID a.k.a. transaction ID or correlation ID
31     public static final String HEADER_REQUEST_ID = "X-ECOMP-RequestID";
32     public static final String HEADER_SERVICE_INSTANCE_ID = "X-ECOMP-ServiceInstanceID";
33     // This value should match with org.openecomp.restclient.client.Headers.TRANSACTION_ID
34     private static final String HEADER_X_TRANSACTION_ID = "X-TransactionId";
35
36     private String requestId;
37     private String instanceId;
38     private String transactionId;
39
40     public RequestHeaders(HttpHeaders headers) {
41         requestId = headers.getHeaderString(RequestHeaders.HEADER_REQUEST_ID);
42         instanceId = headers.getHeaderString(RequestHeaders.HEADER_SERVICE_INSTANCE_ID);
43         transactionId = headers.getHeaderString(RequestHeaders.HEADER_X_TRANSACTION_ID);
44     }
45
46     public String getRequestId() {
47         return requestId;
48     }
49
50     public String getInstanceId() {
51         return instanceId;
52     }
53
54     public String getTransactionId() {
55         return transactionId;
56     }
57
58     /**
59      * Get the global request ID from the HTTP headers. The value will be taken from the header "X-ECOMP-RequestID" if
60      * this is set, or else the value of "X-TransactionId" (which may be null).
61      *
62      * <p>
63      * If the correlation ID contains the symbol : then this character and any trailing characters are removed. This
64      * allows for an incrementing numeric sequence where there are multiple HTTP requests for a single transaction.
65      *
66      * @return the normalized UUID used for correlating transactions across components, or else null (if no ID is set)
67      */
68     public String getCorrelationId() {
69         // If the request ID is missing, use the transaction ID (if present)
70         String uuid = Optional.ofNullable(getRequestId()).orElse(getTransactionId());
71
72         // Normalize the correlation ID by removing any suffix
73         if (uuid != null && uuid.contains(":")) {
74             uuid = uuid.split(":")[0];
75         }
76
77         return uuid;
78     }
79
80     @Override
81     public String toString() {
82         return "RequestHeaders [requestId=" + requestId + ", instanceId=" + instanceId + ", transactionId="
83                 + transactionId + "]";
84     }
85 }