2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sa.rest;
23 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
24 import org.onap.aai.cl.mdc.MdcContext;
27 import java.util.UUID;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.HttpHeaders;
30 import javax.ws.rs.core.Response;
33 public class ApiUtils {
35 public static final String SEARCH_AUTH_POLICY_NAME = "search";
36 public static final String URL_PREFIX = "services/search-data-service/v1/search";
39 POST, GET, PUT, DELETE
46 * This method uses the contents of the supplied HTTP headers and request
47 * structures to populate the MDC Context used for logging purposes.
49 * @param httpReq - HTTP request structure.
50 * @param headers - HTTP headers
52 protected static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
54 // Auto generate a transaction if we were not provided one.
55 String transId = null;
56 if (headers != null) {
57 transId = headers.getRequestHeaders().getFirst("X-TransactionId");
59 if ((transId == null) || (transId.equals(""))) {
60 transId = UUID.randomUUID().toString();
64 String fromIp = (httpReq != null) ? httpReq.getRemoteAddr() : "";
65 String fromApp = (headers != null) ? headers.getRequestHeaders().getFirst("X-FromAppId") : "";
67 MdcContext.initialize(transId, SearchDbConstants.SDB_SERVICE_NAME, "", fromApp, fromIp);
71 protected static void clearMdcContext() {
75 public static String buildIndexUri(String index) {
77 return (URL_PREFIX + "/indexes/") + index;
80 public static String buildDocumentUri(String index, String documentId) {
82 return buildIndexUri(index) + "/documents/" + documentId;
85 public static boolean validateIndexUri(String uri) {
87 // If the URI starts with a leading '/' character, remove it.
88 uri = uri.startsWith("/") ? uri.substring(1) : uri;
90 // Now, tokenize the URI string.
91 String[] tokens = uri.split("/");
93 return (tokens.length == 6) && (tokens[4].equals("indexes"));
97 public static boolean validateDocumentUri(String uri, boolean requireId) {
99 // If the URI starts with a leading '/' character, remove it.
100 uri = uri.startsWith("/") ? uri.substring(1) : uri;
102 // Now, tokenize the URI string.
103 String[] tokens = uri.split("/");
106 return (tokens.length == 8) && (tokens[4].equals("indexes")
107 && (tokens[6].equals("documents")));
109 return ((tokens.length == 8) || (tokens.length == 7))
110 && (tokens[4].equals("indexes") && (tokens[6].equals("documents")));
114 public static String extractIndexFromUri(String uri) {
116 // If the URI starts with a leading '/' character, remove it.
117 uri = uri.startsWith("/") ? uri.substring(1) : uri;
119 // Now, tokenize the URI string.
120 String[] tokens = uri.split("/");
123 for (String token : tokens) {
124 if (token.equals("indexes")) {
125 if (i + 1 < tokens.length) {
126 return tokens[i + 1];
135 public static String extractIdFromUri(String uri) {
137 // If the URI starts with a leading '/' character, remove it.
138 uri = uri.startsWith("/") ? uri.substring(1) : uri;
140 // Now, tokenize the URI string.
141 String[] tokens = uri.split("/");
144 for (String token : tokens) {
145 if (token.equals("documents")) {
146 if (i + 1 < tokens.length) {
147 return tokens[i + 1];
156 public static String getHttpStatusString(int httpStatusCode) {
157 // Some of the status codes we use are still in draft state in the standards, and are not
158 // recognized in the javax library. We need to manually translate these to human-readable
160 String statusString = "Unknown";
161 Response.Status status = Response.Status.fromStatusCode(httpStatusCode);
163 if (status == null) {
164 switch (httpStatusCode) {
166 statusString = "Multi Status";
171 statusString = status.toString();