Merge "[AAI] Fix doc config files"
[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
21 package org.onap.aai.aailog.logs;
22
23 public class ServiceName {
24     /**
25      * Extract the service name from a URI path
26      * Service name should be the URI path up to two levels down from the version or less
27      * 
28      * @param path the URI path
29      * @return the service name
30      */
31     public static String extractServiceName(String path) {
32         StringBuilder sBuilder = new StringBuilder();
33         String[] parts = path.split("/");
34         String part = "";
35         for (int i = 0; i < parts.length; i++) {
36             part = parts[i];
37             if (i < 5) {
38                 sBuilder.append(part).append("/");
39             } else {
40                 break;
41             }
42         }
43         if ((sBuilder.length() > 0) && (sBuilder.charAt(sBuilder.length() - 1) == '/')) {
44             sBuilder.deleteCharAt(sBuilder.length() - 1);
45         }
46         String serviceName = sBuilder.toString();
47         if (serviceName != null && (!serviceName.isEmpty())) {
48             serviceName = serviceName.replaceAll(",", "\\\\,");
49         }
50         return serviceName;
51     }
52 }