2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License ati
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.rest;
27 import org.openecomp.cl.mdc.MdcContext;
28 import org.openecomp.sa.searchdbabstraction.util.SearchDbConstants;
31 import java.util.UUID;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.Response;
37 public class ApiUtils {
39 public static final String SEARCH_AUTH_POLICY_NAME = "search";
40 public static final String URL_PREFIX = "services/search-data-service/v1/search";
43 POST, GET, PUT, DELETE
50 * This method uses the contents of the supplied HTTP headers and request
51 * structures to populate the MDC Context used for logging purposes.
53 * @param httpReq - HTTP request structure.
54 * @param headers - HTTP headers
56 protected static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
58 // Auto generate a transaction if we were not provided one.
59 String transId = null;
60 if (headers != null) {
61 transId = headers.getRequestHeaders().getFirst("X-TransactionId");
63 if ((transId == null) || (transId.equals(""))) {
64 transId = UUID.randomUUID().toString();
68 String fromIp = (httpReq != null) ? httpReq.getRemoteAddr() : "";
69 String fromApp = (headers != null) ? headers.getRequestHeaders().getFirst("X-FromAppId") : "";
71 MdcContext.initialize(transId, SearchDbConstants.SDB_SERVICE_NAME, "", fromApp, fromIp);
75 protected static void clearMdcContext() {
79 public static String buildIndexUri(String index) {
81 return (URL_PREFIX + "/indexes/") + index;
84 public static String buildDocumentUri(String index, String documentId) {
86 return buildIndexUri(index) + "/documents/" + documentId;
89 public static boolean validateIndexUri(String uri) {
91 // If the URI starts with a leading '/' character, remove it.
92 uri = uri.startsWith("/") ? uri.substring(1) : uri;
94 // Now, tokenize the URI string.
95 String[] tokens = uri.split("/");
97 return (tokens.length == 6) && (tokens[4].equals("indexes"));
101 public static boolean validateDocumentUri(String uri, boolean requireId) {
103 // If the URI starts with a leading '/' character, remove it.
104 uri = uri.startsWith("/") ? uri.substring(1) : uri;
106 // Now, tokenize the URI string.
107 String[] tokens = uri.split("/");
110 return (tokens.length == 8) && (tokens[4].equals("indexes")
111 && (tokens[6].equals("documents")));
113 return ((tokens.length == 8) || (tokens.length == 7))
114 && (tokens[4].equals("indexes") && (tokens[6].equals("documents")));
118 public static String extractIndexFromUri(String uri) {
120 // If the URI starts with a leading '/' character, remove it.
121 uri = uri.startsWith("/") ? uri.substring(1) : uri;
123 // Now, tokenize the URI string.
124 String[] tokens = uri.split("/");
127 for (String token : tokens) {
128 if (token.equals("indexes")) {
129 if (i + 1 < tokens.length) {
130 return tokens[i + 1];
139 public static String extractIdFromUri(String uri) {
141 // If the URI starts with a leading '/' character, remove it.
142 uri = uri.startsWith("/") ? uri.substring(1) : uri;
144 // Now, tokenize the URI string.
145 String[] tokens = uri.split("/");
148 for (String token : tokens) {
149 if (token.equals("documents")) {
150 if (i + 1 < tokens.length) {
151 return tokens[i + 1];
160 public static String getHttpStatusString(int httpStatusCode) {
161 // Some of the status codes we use are still in draft state in the standards, and are not
162 // recognized in the javax library. We need to manually translate these to human-readable
164 String statusString = "Unknown";
165 Response.Status status = Response.Status.fromStatusCode(httpStatusCode);
167 if (status == null) {
168 switch (httpStatusCode) {
170 statusString = "Multi Status";
175 statusString = status.toString();