Add missing distributionManagement section to poms
[aai/search-data-service.git] / search-data-service-app / src / main / java / org / onap / aai / sa / rest / ApiUtils.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.sa.rest;
22
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;
29 import org.slf4j.MDC;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpStatus;
32
33 /**
34  * Spring Imports.
35  *
36  */
37 public class ApiUtils {
38
39     public static final String URL_PREFIX = "services/search-data-service/v1/search";
40     public static final String SEARCH_AUTH_POLICY_NAME = "search";
41
42     private static final String URI_SEGMENT_INDEXES = "indexes";
43     private static final String URI_SEGMENT_DOCUMENTS = "documents";
44
45     public enum Action {
46         POST,
47         GET,
48         PUT,
49         DELETE
50     }
51
52     /**
53      * This method uses the contents of the supplied HTTP headers and request structures to populate the MDC Context
54      * used for logging purposes.
55      *
56      * @param httpReq - HTTP request structure.
57      * @param headers - HTTP headers
58      */
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();
66             }
67         }
68
69         String fromIp = (httpReq != null) ? httpReq.getRemoteHost() : "";
70         String fromApp = (headers != null) ? headers.getFirst("X-FromAppId") : "";
71
72         MdcContext.initialize(transId, SearchDbConstants.SDB_SERVICE_NAME, "", fromApp, fromIp);
73     }
74
75     protected static void clearMdcContext() {
76         MDC.clear();
77     }
78
79     public static String buildIndexUri(String index) {
80         return (URL_PREFIX + "/indexes/") + index;
81     }
82
83     public static String buildDocumentUri(String index, String documentId) {
84         return buildIndexUri(index) + "/documents/" + documentId;
85     }
86
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));
91     }
92
93     public static boolean validateDocumentUri(String uri, boolean requireId) {
94         uri = uri.startsWith("/") ? uri.substring(1) : uri;
95         String[] tokens = uri.split("/");
96
97         if (requireId) {
98             return (tokens.length == 8)
99                     && (tokens[4].equals(URI_SEGMENT_INDEXES) && (tokens[6].equals(URI_SEGMENT_DOCUMENTS)));
100         } else {
101             return ((tokens.length == 8) || (tokens.length == 7))
102                     && (tokens[4].equals(URI_SEGMENT_INDEXES) && (tokens[6].equals(URI_SEGMENT_DOCUMENTS)));
103         }
104     }
105
106     public static String extractIndexFromUri(String uri) {
107         uri = uri.startsWith("/") ? uri.substring(1) : uri;
108
109         String[] tokens = uri.split("/");
110
111         int i = 0;
112         for (String token : tokens) {
113             if (token.equals(URI_SEGMENT_INDEXES) && i + 1 < tokens.length) {
114                 return tokens[i + 1];
115             }
116             i++;
117         }
118
119         return null;
120     }
121
122     public static String extractIdFromUri(String uri) {
123         uri = uri.startsWith("/") ? uri.substring(1) : uri;
124
125         String[] tokens = uri.split("/");
126
127         int i = 0;
128         for (String token : tokens) {
129             if (token.equals(URI_SEGMENT_DOCUMENTS) && i + 1 < tokens.length) {
130                 return tokens[i + 1];
131             }
132             i++;
133         }
134
135         return null;
136     }
137
138     public static String getHttpStatusString(int httpStatusCode) {
139         try {
140             return HttpStatus.valueOf(httpStatusCode).getReasonPhrase();
141         } catch (IllegalArgumentException e) {
142             if (207 == httpStatusCode) {
143                 return "Multi-Status";
144             } else {
145                 return "Unknown";
146             }
147         }
148     }
149
150     public static boolean isSuccessStatusCode(int statusCode) {
151         return Family.familyOf(statusCode).equals(Family.SUCCESSFUL);
152     }
153 }