8cafbda325d3ee2c27d7053a0e9a587544e0a012
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / onap / so / adapters / sdnc / sdncrest / SDNCAdapterUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.adapters.sdnc.sdncrest;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.web.util.UriUtils;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
35
36 /**
37  * Utility methods used by SDNCAdapterRest.
38  */
39 public final class SDNCAdapterUtils {
40     private static final Logger logger = LoggerFactory.getLogger(SDNCAdapterUtils.class);
41     /**
42      * Instantiation is not allowed.
43      */
44     private SDNCAdapterUtils() {
45     }
46     
47     /**
48          * Returns a node's child elements in a list.
49          */
50         public static List<Element> childElements(Node node) {
51                 List<Element> elements = new ArrayList<>();
52
53                 NodeList nodeList = node.getChildNodes();
54                 for (int i = 0; i < nodeList.getLength(); i++) {
55                         Node child = nodeList.item(i);
56                         if (child.getNodeType() == Node.ELEMENT_NODE) {
57                                 elements.add((Element) child);
58                         }
59                 }
60
61                 return elements;
62         }
63
64         /**
65          * Encodes a URL path segment according to RFC 3986 Section 2.
66          * @param pathSegment the path segment to encode
67          * @return the encoded path segment
68          */
69         public static String encodeURLPathSegment(String pathSegment) {         
70                         return UriUtils.encodePathSegment(pathSegment, "UTF-8");
71         }
72 }