Fix simple Sonar Lint issues
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / rest / ApiUtils.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 package org.onap.aai.sa.rest;
22
23 import java.util.UUID;
24 import javax.servlet.http.HttpServletRequest;
25 import org.onap.aai.cl.mdc.MdcContext;
26 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
27 import org.slf4j.MDC;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.HttpStatus;
30
31 /**
32  * Spring Imports.
33  *
34  */
35 public class ApiUtils {
36
37     public static final String SEARCH_AUTH_POLICY_NAME = "search";
38     public static final String URL_PREFIX = "services/search-data-service/v1/search";
39
40     public enum Action {
41         POST,
42         GET,
43         PUT,
44         DELETE
45     }
46
47     /**
48      * This method uses the contents of the supplied HTTP headers and request structures to populate the MDC Context
49      * used for logging purposes.
50      *
51      * @param httpReq - HTTP request structure.
52      * @param headers - HTTP headers
53      */
54     protected static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
55         // Auto generate a transaction if we were not provided one.
56         String transId = null;
57         if (headers != null) {
58             transId = headers.getFirst("X-TransactionId");
59
60             if ((transId == null) || (transId.equals(""))) {
61                 transId = UUID.randomUUID().toString();
62             }
63         }
64
65         String fromIp = (httpReq != null) ? httpReq.getRemoteHost() : "";
66         String fromApp = (headers != null) ? headers.getFirst("X-FromAppId") : "";
67
68         MdcContext.initialize(transId, SearchDbConstants.SDB_SERVICE_NAME, "", fromApp, fromIp);
69     }
70
71     protected static void clearMdcContext() {
72         MDC.clear();
73     }
74
75     public static String buildIndexUri(String index) {
76
77         return (URL_PREFIX + "/indexes/") + index;
78     }
79
80     public static String buildDocumentUri(String index, String documentId) {
81
82         return buildIndexUri(index) + "/documents/" + documentId;
83     }
84
85     public static boolean validateIndexUri(String uri) {
86
87         // If the URI starts with a leading '/' character, remove it.
88         uri = uri.startsWith("/") ? uri.substring(1) : uri;
89
90         // Now, tokenize the URI string.
91         String[] tokens = uri.split("/");
92
93         return (tokens.length == 6) && (tokens[4].equals("indexes"));
94
95     }
96
97     public static boolean validateDocumentUri(String uri, boolean requireId) {
98         uri = uri.startsWith("/") ? uri.substring(1) : uri;
99
100         // Now, tokenize the URI string.
101         String[] tokens = uri.split("/");
102
103         if (requireId) {
104             return (tokens.length == 8) && (tokens[4].equals("indexes") && (tokens[6].equals("documents")));
105         } else {
106             return ((tokens.length == 8) || (tokens.length == 7))
107                     && (tokens[4].equals("indexes") && (tokens[6].equals("documents")));
108         }
109     }
110
111     public static String extractIndexFromUri(String uri) {
112         uri = uri.startsWith("/") ? uri.substring(1) : uri;
113
114         String[] tokens = uri.split("/");
115
116         int i = 0;
117         for (String token : tokens) {
118             if (token.equals("indexes") && i + 1 < tokens.length) {
119                 return tokens[i + 1];
120             }
121             i++;
122         }
123
124         return null;
125     }
126
127     public static String extractIdFromUri(String uri) {
128         uri = uri.startsWith("/") ? uri.substring(1) : uri;
129
130         String[] tokens = uri.split("/");
131
132         int i = 0;
133         for (String token : tokens) {
134             if (token.equals("documents") && i + 1 < tokens.length) {
135                 return tokens[i + 1];
136             }
137             i++;
138         }
139
140         return null;
141     }
142
143     public static String getHttpStatusString(int httpStatusCode) {
144         try {
145             return HttpStatus.valueOf(httpStatusCode).getReasonPhrase();
146         } catch (IllegalArgumentException e) {
147             if (httpStatusCode == 207) {
148                 return "Multi-Status";
149             } else {
150                 return "Unknown";
151             }
152         }
153     }
154 }