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 com.google.common.base.Strings;
24 import java.util.UUID;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.core.Response.Status.Family;
27 import org.onap.aai.cl.mdc.MdcContext;
28 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpStatus;
37 public class ApiUtils {
39 public static final String URL_PREFIX = "services/search-data-service/v1/search";
40 public static final String SEARCH_AUTH_POLICY_NAME = "search";
42 private static final String URI_SEGMENT_INDEXES = "indexes";
43 private static final String URI_SEGMENT_DOCUMENTS = "documents";
53 * This method uses the contents of the supplied HTTP headers and request structures to populate the MDC Context
54 * used for logging purposes.
56 * @param httpReq - HTTP request structure.
57 * @param headers - HTTP headers
59 protected static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
60 // Auto generate a transaction if we were not provided one.
61 String transId = null;
62 if (headers != null) {
63 transId = headers.getFirst("X-TransactionId");
64 if (Strings.isNullOrEmpty(transId)) {
65 transId = UUID.randomUUID().toString();
69 String fromIp = (httpReq != null) ? httpReq.getRemoteHost() : "";
70 String fromApp = (headers != null) ? headers.getFirst("X-FromAppId") : "";
72 MdcContext.initialize(transId, SearchDbConstants.SDB_SERVICE_NAME, "", fromApp, fromIp);
75 protected static void clearMdcContext() {
79 public static String buildIndexUri(String index) {
80 return (URL_PREFIX + "/indexes/") + index;
83 public static String buildDocumentUri(String index, String documentId) {
84 return buildIndexUri(index) + "/documents/" + documentId;
87 public static boolean validateIndexUri(String uri) {
88 uri = uri.startsWith("/") ? uri.substring(1) : uri;
89 String[] tokens = uri.split("/");
90 return (tokens.length == 6) && (tokens[4].equals(URI_SEGMENT_INDEXES));
93 public static boolean validateDocumentUri(String uri, boolean requireId) {
94 uri = uri.startsWith("/") ? uri.substring(1) : uri;
95 String[] tokens = uri.split("/");
98 return (tokens.length == 8)
99 && (tokens[4].equals(URI_SEGMENT_INDEXES) && (tokens[6].equals(URI_SEGMENT_DOCUMENTS)));
101 return ((tokens.length == 8) || (tokens.length == 7))
102 && (tokens[4].equals(URI_SEGMENT_INDEXES) && (tokens[6].equals(URI_SEGMENT_DOCUMENTS)));
106 public static String extractIndexFromUri(String uri) {
107 uri = uri.startsWith("/") ? uri.substring(1) : uri;
109 String[] tokens = uri.split("/");
112 for (String token : tokens) {
113 if (token.equals(URI_SEGMENT_INDEXES) && i + 1 < tokens.length) {
114 return tokens[i + 1];
122 public static String extractIdFromUri(String uri) {
123 uri = uri.startsWith("/") ? uri.substring(1) : uri;
125 String[] tokens = uri.split("/");
128 for (String token : tokens) {
129 if (token.equals(URI_SEGMENT_DOCUMENTS) && i + 1 < tokens.length) {
130 return tokens[i + 1];
138 public static String getHttpStatusString(int httpStatusCode) {
140 return HttpStatus.valueOf(httpStatusCode).getReasonPhrase();
141 } catch (IllegalArgumentException e) {
142 if (207 == httpStatusCode) {
143 return "Multi-Status";
150 public static boolean isSuccessStatusCode(int statusCode) {
151 return Family.familyOf(statusCode).equals(Family.SUCCESSFUL);