Format Java code to ONAP standard
[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 // Spring Imports
32
33 public class ApiUtils {
34
35     public static final String SEARCH_AUTH_POLICY_NAME = "search";
36     public static final String URL_PREFIX = "services/search-data-service/v1/search";
37
38     public enum Action {
39         POST,
40         GET,
41         PUT,
42         DELETE
43     };
44
45     /**
46      * This method uses the contents of the supplied HTTP headers and request structures to populate the MDC Context
47      * used for logging purposes.
48      *
49      * @param httpReq - HTTP request structure.
50      * @param headers - HTTP headers
51      */
52     protected static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
53
54         // Auto generate a transaction if we were not provided one.
55         String transId = null;
56         if (headers != null) {
57             transId = headers.getFirst("X-TransactionId");
58
59             if ((transId == null) || (transId.equals(""))) {
60                 transId = UUID.randomUUID().toString();
61             }
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
99         // If the URI starts with a leading '/' character, remove it.
100         uri = uri.startsWith("/") ? uri.substring(1) : uri;
101
102         // Now, tokenize the URI string.
103         String[] tokens = uri.split("/");
104
105         if (requireId) {
106             return (tokens.length == 8) && (tokens[4].equals("indexes") && (tokens[6].equals("documents")));
107         } else {
108             return ((tokens.length == 8) || (tokens.length == 7))
109                     && (tokens[4].equals("indexes") && (tokens[6].equals("documents")));
110         }
111     }
112
113     public static String extractIndexFromUri(String uri) {
114
115         // If the URI starts with a leading '/' character, remove it.
116         uri = uri.startsWith("/") ? uri.substring(1) : uri;
117
118         // Now, tokenize the URI string.
119         String[] tokens = uri.split("/");
120
121         int i = 0;
122         for (String token : tokens) {
123             if (token.equals("indexes")) {
124                 if (i + 1 < tokens.length) {
125                     return tokens[i + 1];
126                 }
127             }
128             i++;
129         }
130
131         return null;
132     }
133
134     public static String extractIdFromUri(String uri) {
135
136         // If the URI starts with a leading '/' character, remove it.
137         uri = uri.startsWith("/") ? uri.substring(1) : uri;
138
139         // Now, tokenize the URI string.
140         String[] tokens = uri.split("/");
141
142         int i = 0;
143         for (String token : tokens) {
144             if (token.equals("documents")) {
145                 if (i + 1 < tokens.length) {
146                     return tokens[i + 1];
147                 }
148             }
149             i++;
150         }
151
152         return null;
153     }
154
155     public static String getHttpStatusString(int httpStatusCode) {
156         // Some of the status codes we use are still in draft state in the standards, and are not
157         // recognized in the javax library. We need to manually translate these to human-readable
158         // strings.
159         String statusString = "Unknown";
160         HttpStatus status = null;
161
162         try {
163             status = HttpStatus.valueOf(httpStatusCode);
164         } catch (IllegalArgumentException e) {
165         }
166
167
168         if (status == null) {
169             switch (httpStatusCode) {
170                 case 207:
171                     statusString = "Multi Status";
172                     break;
173                 default:
174             }
175         } else {
176             statusString = status.getReasonPhrase();
177         }
178
179         return statusString;
180     }
181 }