Enhancements for the aai-common library
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / aailog / logs / ServiceName.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.aailog.logs;
21
22 public class ServiceName {
23     /**
24      * Extract the service name from a URI path
25      * Service name should be the URI path up to two levels down from the version or less
26      * @param path the URI path
27      * @return the service name
28      */
29     public static String extractServiceName (String path) {
30         StringBuilder sBuilder = new StringBuilder();
31         String[] parts = path.split("/");
32         String part = "";
33         for (int i = 0; i < parts.length; i++) {
34             part = parts[i];
35             if (i < 5) {
36                 sBuilder.append(part).append("/");
37             } else {
38                 break;
39             }
40         }
41         if ((sBuilder.length() > 0) && (sBuilder.charAt(sBuilder.length()-1) == '/')) {
42             sBuilder.deleteCharAt(sBuilder.length()-1);
43         }
44         String serviceName = sBuilder.toString();
45         if (serviceName != null && (!serviceName.isEmpty())) {
46             serviceName = serviceName.replaceAll(",", "\\\\,");
47         }
48         return serviceName;
49     }
50 }