05e272f86739730a9da21b49b8d140f5e22822c6
[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  * 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
22 package org.onap.so.adapters.sdnc.sdncrest;
23
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.onap.so.logger.MsoLogger;
29 import org.springframework.web.util.UriUtils;
30 import org.w3c.dom.Element;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33
34 /**
35  * Utility methods used by SDNCAdapterRest.
36  */
37 public final class SDNCAdapterUtils {
38     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, SDNCAdapterUtils.class);
39     /**
40      * Instantiation is not allowed.
41      */
42     private SDNCAdapterUtils() {
43     }
44     
45     /**
46          * Returns a node's child elements in a list.
47          */
48         public static List<Element> childElements(Node node) {
49                 List<Element> elements = new ArrayList<>();
50
51                 NodeList nodeList = node.getChildNodes();
52                 for (int i = 0; i < nodeList.getLength(); i++) {
53                         Node child = nodeList.item(i);
54                         if (child.getNodeType() == Node.ELEMENT_NODE) {
55                                 elements.add((Element) child);
56                         }
57                 }
58
59                 return elements;
60         }
61
62         /**
63          * Encodes a URL path segment according to RFC 3986 Section 2.
64          * @param pathSegment the path segment to encode
65          * @return the encoded path segment
66          */
67         public static String encodeURLPathSegment(String pathSegment) {
68                 try {
69                         return UriUtils.encodePathSegment(pathSegment, "UTF-8");
70                 } catch (UnsupportedEncodingException e) {
71                     LOGGER.debug("Exception:", e);
72                         throw new RuntimeException("UTF-8 encoding is not supported");
73                 }
74         }
75 }