Initial search service commit
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / rest / ApiUtils.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.rest;
26
27 import org.openecomp.cl.mdc.MdcContext;
28 import org.openecomp.sa.searchdbabstraction.util.SearchDbConstants;
29 import org.slf4j.MDC;
30
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;
35
36
37 public class ApiUtils {
38
39   public static final String SEARCH_AUTH_POLICY_NAME = "search";
40   public static final String URL_PREFIX = "services/search-data-service/v1/search";
41
42   public enum Action {
43     POST, GET, PUT, DELETE
44   }
45
46   ;
47
48
49   /**
50    * This method uses the contents of the supplied HTTP headers and request
51    * structures to populate the MDC Context used for logging purposes.
52    *
53    * @param httpReq - HTTP request structure.
54    * @param headers - HTTP headers
55    */
56   protected static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
57
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");
62
63       if ((transId == null) || (transId.equals(""))) {
64         transId = UUID.randomUUID().toString();
65       }
66     }
67
68     String fromIp = (httpReq != null) ? httpReq.getRemoteAddr() : "";
69     String fromApp = (headers != null) ? headers.getRequestHeaders().getFirst("X-FromAppId") : "";
70
71     MdcContext.initialize(transId, SearchDbConstants.SDB_SERVICE_NAME, "", fromApp, fromIp);
72   }
73
74
75   protected static void clearMdcContext() {
76     MDC.clear();
77   }
78
79   public static String buildIndexUri(String index) {
80
81     return (URL_PREFIX + "/indexes/") + index;
82   }
83
84   public static String buildDocumentUri(String index, String documentId) {
85
86     return buildIndexUri(index) + "/documents/" + documentId;
87   }
88
89   public static boolean validateIndexUri(String uri) {
90
91     // If the URI starts with a leading '/' character, remove it.
92     uri = uri.startsWith("/") ? uri.substring(1) : uri;
93
94     // Now, tokenize the URI string.
95     String[] tokens = uri.split("/");
96
97     return (tokens.length == 6) && (tokens[4].equals("indexes"));
98
99   }
100
101   public static boolean validateDocumentUri(String uri, boolean requireId) {
102
103     // If the URI starts with a leading '/' character, remove it.
104     uri = uri.startsWith("/") ? uri.substring(1) : uri;
105
106     // Now, tokenize the URI string.
107     String[] tokens = uri.split("/");
108
109     if (requireId) {
110       return (tokens.length == 8) && (tokens[4].equals("indexes")
111           && (tokens[6].equals("documents")));
112     } else {
113       return ((tokens.length == 8) || (tokens.length == 7))
114           && (tokens[4].equals("indexes") && (tokens[6].equals("documents")));
115     }
116   }
117
118   public static String extractIndexFromUri(String uri) {
119
120     // If the URI starts with a leading '/' character, remove it.
121     uri = uri.startsWith("/") ? uri.substring(1) : uri;
122
123     // Now, tokenize the URI string.
124     String[] tokens = uri.split("/");
125
126     int i = 0;
127     for (String token : tokens) {
128       if (token.equals("indexes")) {
129         if (i + 1 < tokens.length) {
130           return tokens[i + 1];
131         }
132       }
133       i++;
134     }
135
136     return null;
137   }
138
139   public static String extractIdFromUri(String uri) {
140
141     // If the URI starts with a leading '/' character, remove it.
142     uri = uri.startsWith("/") ? uri.substring(1) : uri;
143
144     // Now, tokenize the URI string.
145     String[] tokens = uri.split("/");
146
147     int i = 0;
148     for (String token : tokens) {
149       if (token.equals("documents")) {
150         if (i + 1 < tokens.length) {
151           return tokens[i + 1];
152         }
153       }
154       i++;
155     }
156
157     return null;
158   }
159
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
163     // strings.
164     String statusString = "Unknown";
165     Response.Status status = Response.Status.fromStatusCode(httpStatusCode);
166
167     if (status == null) {
168       switch (httpStatusCode) {
169         case 207:
170           statusString = "Multi Status";
171           break;
172         default:
173       }
174     } else {
175       statusString = status.toString();
176     }
177
178     return statusString;
179   }
180 }