Fix the esr-server setup issue.
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / externalservice / aai / AaiCommon.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.aai.esr.externalservice.aai;
17
18 import java.io.IOException;
19 import java.io.StringReader;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
23
24 import org.glassfish.jersey.client.ClientResponse;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Node;
27 import org.w3c.dom.NodeList;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
30
31 public class AaiCommon {
32
33   private static String AAI_AUTHENTICATION_USER = "AAI";
34   private static String AAI_AUTHENTICATION_PAASWORD = "AAI";
35   private static String RESOURCE_VERSION_PARAM = "resource-version";
36   
37   public String getAuthenticationCredentials() {
38     String usernameAndPassword = AAI_AUTHENTICATION_USER + ":"
39         + AAI_AUTHENTICATION_PAASWORD;
40     return "Basic " + java.util.Base64.getEncoder().encodeToString("AAI:AAI".getBytes());
41   }
42   
43   public String getResourceVersion(ClientResponse response)
44       throws ParserConfigurationException, SAXException, IOException {
45     String respData = response.readEntity(String.class);
46
47     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
48     DocumentBuilder builder = factory.newDocumentBuilder();
49     InputSource is = new InputSource(new StringReader(respData));
50     Document doc = builder.parse(is);
51
52     NodeList nodeList = doc.getDocumentElement().getChildNodes();
53     for (int i = 0; i < nodeList.getLength(); i++) {
54       Node currentNode = nodeList.item(i);
55       if (currentNode.getNodeName().equals(RESOURCE_VERSION_PARAM)) {
56         return currentNode.getTextContent();
57       }
58     }
59
60     return null;
61   }
62 }