2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sa.rest;
25 import org.openecomp.cl.mdc.MdcContext;
26 import org.openecomp.sa.searchdbabstraction.util.SearchDbConstants;
29 import java.util.UUID;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.core.HttpHeaders;
32 import javax.ws.rs.core.Response;
35 public class ApiUtils {
37 public static final String SEARCH_AUTH_POLICY_NAME = "search";
38 public static final String URL_PREFIX = "services/search-data-service/v1/search";
41 POST, GET, PUT, DELETE
48 * This method uses the contents of the supplied HTTP headers and request
49 * structures to populate the MDC Context used for logging purposes.
51 * @param httpReq - HTTP request structure.
52 * @param headers - HTTP headers
54 protected static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
56 // Auto generate a transaction if we were not provided one.
57 String transId = null;
58 if (headers != null) {
59 transId = headers.getRequestHeaders().getFirst("X-TransactionId");
61 if ((transId == null) || (transId.equals(""))) {
62 transId = UUID.randomUUID().toString();
66 String fromIp = (httpReq != null) ? httpReq.getRemoteAddr() : "";
67 String fromApp = (headers != null) ? headers.getRequestHeaders().getFirst("X-FromAppId") : "";
69 MdcContext.initialize(transId, SearchDbConstants.SDB_SERVICE_NAME, "", fromApp, fromIp);
73 protected static void clearMdcContext() {
77 public static String buildIndexUri(String index) {
79 return (URL_PREFIX + "/indexes/") + index;
82 public static String buildDocumentUri(String index, String documentId) {
84 return buildIndexUri(index) + "/documents/" + documentId;
87 public static boolean validateIndexUri(String uri) {
89 // If the URI starts with a leading '/' character, remove it.
90 uri = uri.startsWith("/") ? uri.substring(1) : uri;
92 // Now, tokenize the URI string.
93 String[] tokens = uri.split("/");
95 return (tokens.length == 6) && (tokens[4].equals("indexes"));
99 public static boolean validateDocumentUri(String uri, boolean requireId) {
101 // If the URI starts with a leading '/' character, remove it.
102 uri = uri.startsWith("/") ? uri.substring(1) : uri;
104 // Now, tokenize the URI string.
105 String[] tokens = uri.split("/");
108 return (tokens.length == 8) && (tokens[4].equals("indexes")
109 && (tokens[6].equals("documents")));
111 return ((tokens.length == 8) || (tokens.length == 7))
112 && (tokens[4].equals("indexes") && (tokens[6].equals("documents")));
116 public static String extractIndexFromUri(String uri) {
118 // If the URI starts with a leading '/' character, remove it.
119 uri = uri.startsWith("/") ? uri.substring(1) : uri;
121 // Now, tokenize the URI string.
122 String[] tokens = uri.split("/");
125 for (String token : tokens) {
126 if (token.equals("indexes")) {
127 if (i + 1 < tokens.length) {
128 return tokens[i + 1];
137 public static String extractIdFromUri(String uri) {
139 // If the URI starts with a leading '/' character, remove it.
140 uri = uri.startsWith("/") ? uri.substring(1) : uri;
142 // Now, tokenize the URI string.
143 String[] tokens = uri.split("/");
146 for (String token : tokens) {
147 if (token.equals("documents")) {
148 if (i + 1 < tokens.length) {
149 return tokens[i + 1];
158 public static String getHttpStatusString(int httpStatusCode) {
159 // Some of the status codes we use are still in draft state in the standards, and are not
160 // recognized in the javax library. We need to manually translate these to human-readable
162 String statusString = "Unknown";
163 Response.Status status = Response.Status.fromStatusCode(httpStatusCode);
165 if (status == null) {
166 switch (httpStatusCode) {
168 statusString = "Multi Status";
173 statusString = status.toString();