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